View | Details | Raw Unified | Return to bug 30719
Collapse All | Expand All

(-)a/Koha/Illbatch.pm (-2 / +61 lines)
Lines 53-59 Return the patron object associated with this batch Link Here
53
53
54
sub patron {
54
sub patron {
55
    my ($self) = @_;
55
    my ($self) = @_;
56
    return Koha::Patron->_new_from_dbic( scalar $self->_result->borrowernumber );
56
    my $patron = return Koha::Patrons->find( { borrowernumber => $self->borrowernumber } );
57
    return unless $patron;
57
}
58
}
58
59
59
=head3 branch
60
=head3 branch
Lines 66-72 Return the branch object associated with this batch Link Here
66
67
67
sub branch {
68
sub branch {
68
    my ($self) = @_;
69
    my ($self) = @_;
69
    return Koha::Library->_new_from_dbic( scalar $self->_result->branchcode );
70
    my $library = return Koha::Libraries->find( { branchcode => $self->branchcode } );
71
    return unless $library;
70
}
72
}
71
73
72
=head3 requests_count
74
=head3 requests_count
Lines 82-87 sub requests_count { Link Here
82
    return Koha::Illrequests->search( { batch_id => $self->id } )->count;
84
    return Koha::Illrequests->search( { batch_id => $self->id } )->count;
83
}
85
}
84
86
87
=head3 requests
88
89
Return the requests for this batch
90
91
=cut
92
93
sub requests {
94
    my ($self) = @_;
95
    my $requests = $self->_result->illrequests;
96
    return Koha::Illrequests->_new_from_dbic($requests);
97
}
98
85
=head3 create_and_log
99
=head3 create_and_log
86
100
87
    $batch->create_and_log;
101
    $batch->create_and_log;
Lines 175-180 sub delete_and_log { Link Here
175
189
176
=head2 Internal methods
190
=head2 Internal methods
177
191
192
193
=head3 strings_map
194
195
=cut
196
197
sub strings_map {
198
    my ( $self, $params ) = @_;
199
200
    my $strings = {};
201
202
    if ( defined $self->statuscode ) {
203
        my $ill_batch_status = Koha::IllbatchStatuses->search( { code => $self->statuscode } );
204
        my $ill_batch_status_str =
205
            $ill_batch_status->count
206
                ? $ill_batch_status->next->name
207
        : $self->statuscode;
208
209
        $strings->{status} = {
210
            name => $ill_batch_status_str,
211
        };
212
    }
213
214
    if ( defined $self->branchcode ) {
215
        my $ill_batch_branch = Koha::Libraries->find( $self->branchcode );
216
        my $ill_batch_branchname_str = $ill_batch_branch ? $ill_batch_branch->branchname : $self->branchcode;
217
218
        $strings->{branchname} = $ill_batch_branchname_str;
219
    }
220
221
    return $strings;
222
}
223
224
=head3 to_api_mapping
225
226
=cut
227
228
sub to_api_mapping {
229
    return {
230
        id             => 'batch_id',
231
        branchcode     => 'library_id',
232
        borrowernumber => 'patron_id',
233
        status         => 'batch_status',
234
    };
235
}
236
178
=head3 _type
237
=head3 _type
179
238
180
    my $type = Koha::Illbatch->_type;
239
    my $type = Koha::Illbatch->_type;
(-)a/Koha/REST/V1/Illbatches.pm (-55 / +7 lines)
Lines 38-100 Return a list of available ILL batches Link Here
38
=cut
38
=cut
39
39
40
sub list {
40
sub list {
41
    my $c = shift->openapi->valid_input;
41
    my $c = shift->openapi->valid_input or return;
42
43
    #FIXME: This should be $c->objects-search
44
    my @batches = Koha::Illbatches->search()->as_list;
45
42
46
    #FIXME: Below should be coming from $c->objects accessors
43
    return try {
47
    # Get all patrons associated with all our batches
44
        my $illbatches = $c->objects->search( Koha::Illbatches->new );
48
    # in one go
45
        return $c->render( status => 200, openapi => $illbatches );
49
    my $patrons = {};
46
    } catch {
50
    foreach my $batch (@batches) {
47
        $c->unhandled_exception($_);
51
        my $patron_id = $batch->borrowernumber;
48
    };
52
        $patrons->{$patron_id} = 1;
53
    }
54
    my @patron_ids     = keys %{$patrons};
55
    my $patron_results = Koha::Patrons->search( { borrowernumber => { -in => \@patron_ids } } );
56
57
    # Get all branches associated with all our batches
58
    # in one go
59
    my $branches = {};
60
    foreach my $batch (@batches) {
61
        my $branch_id = $batch->branchcode;
62
        $branches->{$branch_id} = 1;
63
    }
64
    my @branchcodes    = keys %{$branches};
65
    my $branch_results = Koha::Libraries->search( { branchcode => { -in => \@branchcodes } } );
66
67
    # Get all batch statuses associated with all our batches
68
    # in one go
69
    my $statuses = {};
70
    foreach my $batch (@batches) {
71
        my $code = $batch->statuscode;
72
        $statuses->{$code} = 1;
73
    }
74
    my @statuscodes    = keys %{$statuses};
75
    my $status_results = Koha::IllbatchStatuses->search( { code => { -in => \@statuscodes } } );
76
77
    # Populate the response
78
    my @to_return = ();
79
    foreach my $it_batch (@batches) {
80
        my $patron = $patron_results->find( { borrowernumber => $it_batch->borrowernumber } );
81
        my $branch = $branch_results->find( { branchcode     => $it_batch->branchcode } );
82
        my $status = $status_results->find( { code           => $it_batch->statuscode } );
83
        push @to_return, {
84
            batch_id       => $it_batch->id,
85
            backend        => $it_batch->backend,
86
            library_id     => $it_batch->branchcode,
87
            name           => $it_batch->name,
88
            statuscode     => $it_batch->statuscode,
89
            patron_id      => $it_batch->borrowernumber,
90
            patron         => $patron,
91
            branch         => $branch,
92
            status         => $status,
93
            requests_count => $it_batch->requests_count
94
        };
95
    }
96
49
97
    return $c->render( status => 200, openapi => \@to_return );
98
}
50
}
99
51
100
=head3 get
52
=head3 get
(-)a/api/v1/swagger/definitions/ill_batch.yaml (+5 lines)
Lines 40-45 properties: Link Here
40
  requests_count:
40
  requests_count:
41
    type: string
41
    type: string
42
    description: The number of requests in this batch
42
    description: The number of requests in this batch
43
  _strings:
44
    type:
45
      - object
46
      - "null"
47
    description: Expanded coded fields (x-koha-embed)
43
additionalProperties: false
48
additionalProperties: false
44
required:
49
required:
45
  - name
50
  - name
(-)a/api/v1/swagger/paths/ill_batches.yaml (-1 / +21 lines)
Lines 6-12 Link Here
6
    tags:
6
    tags:
7
      - ill_batches
7
      - ill_batches
8
    summary: List ILL batches
8
    summary: List ILL batches
9
    parameters: []
9
    parameters:
10
      - $ref: "../swagger.yaml#/parameters/page"
11
      - $ref: "../swagger.yaml#/parameters/per_page"
12
      - $ref: "../swagger.yaml#/parameters/match"
13
      - $ref: "../swagger.yaml#/parameters/order_by"
14
      - $ref: "../swagger.yaml#/parameters/q_param"
15
      - $ref: "../swagger.yaml#/parameters/q_body"
16
      - $ref: "../swagger.yaml#/parameters/request_id_header"
17
      - name: x-koha-embed
18
        in: header
19
        required: false
20
        description: Embed list sent as a request header
21
        type: array
22
        items:
23
          type: string
24
          enum:
25
            - +strings
26
            - requests+count
27
            - patron
28
            - branch
29
        collectionFormat: csv
10
    produces:
30
    produces:
11
      - application/json
31
      - application/json
12
    responses:
32
    responses:
(-)a/koha-tmpl/intranet-tmpl/prog/js/ill-batch-table.js (-6 / +10 lines)
Lines 27-37 Link Here
27
        table = initTable();
27
        table = initTable();
28
28
29
        // Do the initial data population
29
        // Do the initial data population
30
        window.doBatchApiRequest()
30
        window.doBatchApiRequest('', {
31
            .then(function (response) {
31
                headers: {
32
                    'x-koha-embed': '+strings,requests+count,patron'
33
                }
34
            })
35
            .then(function(response) {
32
                return response.json();
36
                return response.json();
33
            })
37
            })
34
            .then(function (data) {
38
            .then(function(data) {
35
                batchesProxy.data = data;
39
                batchesProxy.data = data;
36
            });
40
            });
37
41
Lines 87-94 Link Here
87
    }
91
    }
88
92
89
    // A render function for branch name
93
    // A render function for branch name
90
    var createBranch = function (data) {
94
    var createBranch = function (x, y, data) {
91
        return data.branchname;
95
        return data._strings.branchname;
92
    };
96
    };
93
97
94
    // A render function for batch name
98
    // A render function for batch name
Lines 102-108 Link Here
102
106
103
    // A render function for batch status
107
    // A render function for batch status
104
    var createStatus = function (x, y, data) {
108
    var createStatus = function (x, y, data) {
105
        return data.status.name;
109
        return data._strings.status.name;
106
    };
110
    };
107
111
108
    // A render function for our patron link
112
    // A render function for our patron link
(-)a/t/db_dependent/api/v1/ill_batches.t (-7 / +7 lines)
Lines 85-95 subtest 'list() tests' => sub { Link Here
85
    );
85
    );
86
86
87
    # One batch created, should get returned
87
    # One batch created, should get returned
88
    $t->get_ok("//$userid:$password@/api/v1/ill/batches")->status_is(200)->json_has( '/0/batch_id', 'Batch ID' )
88
    $t->get_ok(
89
        ->json_has( '/0/name',           'Batch name' )->json_has( '/0/backend', 'Backend name' )
89
        "//$userid:$password@/api/v1/ill/batches" => { 'x-koha-embed' => '+strings,requests+count,patron,branch' } )
90
        ->json_has( '/0/patron_id',      'Borrowernumber' )->json_has( '/0/library_id', 'Branchcode' )
90
        ->status_is(200)->json_has( '/0/batch_id', 'Batch ID' )->json_has( '/0/name', 'Batch name' )
91
        ->json_has( '/0/patron',         'patron embedded' )->json_has( '/0/branch', 'branch embedded' )
91
        ->json_has( '/0/backend',    'Backend name' )->json_has( '/0/patron_id', 'Borrowernumber' )
92
        ->json_has( '/0/requests_count', 'request count' );
92
        ->json_has( '/0/library_id', 'Branchcode' )->json_has( '/0/patron', 'patron embedded' )
93
        ->json_has( '/0/branch',     'branch embedded' )->json_has( '/0/requests_count', 'request count' );
93
94
94
    # Try to create a second batch with the same name, this should fail
95
    # Try to create a second batch with the same name, this should fail
95
    my $another_batch = $builder->build_object( { class => 'Koha::Illbatches', value => { name => $batch->name } } );
96
    my $another_batch = $builder->build_object( { class => 'Koha::Illbatches', value => { name => $batch->name } } );
Lines 327-333 subtest 'update() tests' => sub { Link Here
327
        ]
328
        ]
328
        );
329
        );
329
330
330
    my $batch_to_delete = $builder->build_object( { class => 'Koha::Cities' } );
331
    my $batch_to_delete = $builder->build_object( { class => 'Koha::Illbatches' } );
331
    my $non_existent_id = $batch_to_delete->id;
332
    my $non_existent_id = $batch_to_delete->id;
332
    $batch_to_delete->delete;
333
    $batch_to_delete->delete;
333
334
334
- 

Return to bug 30719