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

(-)a/Koha/Illbatch.pm (+95 lines)
Line 0 Link Here
1
package Koha::Illbatch;
2
3
# Copyright PTFS Europe 2022
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Koha::Database;
22
use base qw(Koha::Object);
23
24
=head1 NAME
25
26
Koha::Illbatch - Koha Illbatch Object class
27
28
=head2 Class methods
29
30
=head3 patron
31
32
    my $patron = Koha::Illbatch->patron;
33
34
Return the patron object associated with this batch
35
36
=cut
37
38
sub patron {
39
    my ( $self ) = @_;
40
    return Koha::Patron->_new_from_dbic(
41
        scalar $self->_result->borrowernumber
42
    );
43
}
44
45
=head3 branch
46
47
    my $branch = Koha::Illbatch->branch;
48
49
Return the branch object associated with this batch
50
51
=cut
52
53
sub branch {
54
    my ( $self ) = @_;
55
    return Koha::Library->_new_from_dbic(
56
        scalar $self->_result->branchcode
57
    );
58
}
59
60
=head3 requests_count
61
62
    my $requests_count = Koha::Illbatch->requests_count;
63
64
Return the number of requests associated with this batch
65
66
=cut
67
68
sub requests_count {
69
    my ( $self ) = @_;
70
    return Koha::Illrequests->search({
71
        batch_id => $self->id
72
    })->count;
73
}
74
75
=head2 Internal methods
76
77
=head3 _type
78
79
    my $type = Koha::Illbatch->_type;
80
81
Return this object's type
82
83
=cut
84
85
sub _type {
86
    return 'Illbatch';
87
}
88
89
=head1 AUTHOR
90
91
Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
92
93
=cut
94
95
1;
(-)a/Koha/Illbatches.pm (+61 lines)
Line 0 Link Here
1
package Koha::Illbatches;
2
3
# Copyright PTFS Europe 2022
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Koha::Database;
22
use Koha::Illbatch;
23
use base qw(Koha::Objects);
24
25
=head1 NAME
26
27
Koha::Illbatches - Koha Illbatches Object class
28
29
=head2 Internal methods
30
31
=head3 _type
32
33
    my $type = Koha::Illbatches->_type;
34
35
Return this object's type
36
37
=cut
38
39
sub _type {
40
    return 'Illbatch';
41
}
42
43
=head3 object_class
44
45
    my $class = Koha::Illbatches->object_class;
46
47
Return this object's class name
48
49
=cut
50
51
sub object_class {
52
    return 'Koha::Illbatch';
53
}
54
55
=head1 AUTHOR
56
57
Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
58
59
=cut
60
61
1;
(-)a/Koha/Illrequest.pm (+18 lines)
Lines 32-37 use Koha::Illrequestattributes; Link Here
32
use Koha::AuthorisedValue;
32
use Koha::AuthorisedValue;
33
use Koha::Illrequest::Logger;
33
use Koha::Illrequest::Logger;
34
use Koha::Patron;
34
use Koha::Patron;
35
use Koha::Illbatches;
35
use Koha::AuthorisedValues;
36
use Koha::AuthorisedValues;
36
use Koha::Biblios;
37
use Koha::Biblios;
37
use Koha::Items;
38
use Koha::Items;
Lines 143-148 sub push_processor { Link Here
143
    push @{$self->{processors}}, $processor;
144
    push @{$self->{processors}}, $processor;
144
}
145
}
145
146
147
=head3 batch
148
149
    my $batch = $request->batch;
150
151
Returns the batch associated with a request
152
153
=cut
154
155
sub batch {
156
    my ( $self ) = @_;
157
158
    return Koha::Illbatches->find($self->_result->batch_id);
159
#    return Koha::Illbatch->_new_from_dbic(
160
#        scalar $self->_result->batch_id
161
#    );
162
}
163
146
=head3 statusalias
164
=head3 statusalias
147
165
148
    my $statusalias = $request->statusalias;
166
    my $statusalias = $request->statusalias;
(-)a/Koha/REST/V1/Illbatches.pm (+219 lines)
Line 0 Link Here
1
package Koha::REST::V1::Illbatches;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Illbatches;
23
use Koha::Illrequests;
24
25
=head1 NAME
26
27
Koha::REST::V1::Illbatches
28
29
=head2 Operations
30
31
=head3 list
32
33
Return a list of available ILL batches
34
35
=cut
36
37
sub list {
38
    my $c = shift->openapi->valid_input;
39
40
    my @batches = Koha::Illbatches->search()->as_list;
41
42
    # Get all patrons associated with all our batches
43
    # in one go
44
    my $patrons = {};
45
    foreach my $batch(@batches) {
46
        my $patron_id = $batch->borrowernumber;
47
        $patrons->{$patron_id} = 1
48
    };
49
    my @patron_ids = keys %{$patrons};
50
    my $patron_results = Koha::Patrons->search({
51
        borrowernumber => { -in => \@patron_ids }
52
    });
53
54
    # Get all branches associated with all our batches
55
    # in one go
56
    my $branches = {};
57
    foreach my $batch(@batches) {
58
        my $branch_id = $batch->branchcode;
59
        $branches->{$branch_id} = 1
60
    };
61
    my @branchcodes = keys %{$branches};
62
    my $branch_results = Koha::Libraries->search({
63
        branchcode => { -in => \@branchcodes }
64
    });
65
66
    # Populate the response
67
    my @to_return = ();
68
    foreach my $it_batch(@batches) {
69
        my $patron = $patron_results->find({ borrowernumber => $it_batch->borrowernumber});
70
        my $branch = $branch_results->find({ branchcode => $it_batch->branchcode });
71
        push @to_return, {
72
            %{$it_batch->unblessed},
73
            patron   => $patron,
74
            branch   => $branch,
75
            requests_count => $it_batch->requests_count
76
        };
77
    }
78
79
    return $c->render( status => 200, openapi => \@to_return );
80
}
81
82
=head3 get
83
84
Get one batch
85
86
=cut
87
88
sub get {
89
    my $c = shift->openapi->valid_input;
90
91
    my $batchid = $c->validation->param('illbatch_id');
92
93
    my $batch = Koha::Illbatches->find($batchid);
94
95
    if (not defined $batch) {
96
        return $c->render(
97
            status => 404,
98
            openapi => { error => "ILL batch not found" }
99
        );
100
    }
101
102
    return $c->render(
103
        status => 200,
104
        openapi => {
105
            %{$batch->unblessed},
106
            patron => $batch->patron->unblessed,
107
            branch => $batch->branch->unblessed,
108
            requests_count => $batch->requests_count
109
        }
110
    );
111
}
112
113
=head3 add
114
115
Add a new batch
116
117
=cut
118
119
sub add {
120
    my $c = shift->openapi->valid_input or return;
121
122
    my $body = $c->validation->param('body');
123
124
    # We receive cardnumber, so we need to look up the corresponding
125
    # borrowernumber
126
    my $patron = Koha::Patrons->find({ cardnumber => $body->{cardnumber} });
127
    delete $body->{cardnumber};
128
    $body->{borrowernumber} = $patron->borrowernumber;
129
130
    return try {
131
        my $batch = Koha::Illbatch->new( $body );
132
        $batch->store;
133
        $c->res->headers->location( $c->req->url->to_string . '/' . $batch->id );
134
135
        my $ret = {
136
            %{$batch->unblessed},
137
            patron => $batch->patron->unblessed,
138
            branch => $batch->branch->unblessed,
139
            requests_count   => 0
140
        };
141
142
        return $c->render(
143
            status  => 201,
144
            openapi => $ret
145
        );
146
    }
147
    catch {
148
        $c->unhandled_exception($_);
149
    };
150
}
151
152
=head3 update
153
154
Update a batch
155
156
=cut
157
158
sub update {
159
    my $c = shift->openapi->valid_input or return;
160
161
    my $batch = Koha::Illbatches->find( $c->validation->param('illbatch_id') );
162
163
    if ( not defined $batch ) {
164
        return $c->render(
165
            status  => 404,
166
            openapi => { error => "ILL batch not found" }
167
        );
168
    }
169
170
    my $params = $c->req->json;
171
    delete $params->{cardnumber};
172
173
    return try {
174
        $batch->set( $params );
175
        $batch->store();
176
177
        my $ret = {
178
            %{$batch->unblessed},
179
            patron => $batch->patron->unblessed,
180
            branch => $batch->branch->unblessed,
181
            requests_count => $batch->requests_count
182
        };
183
184
        return $c->render(
185
            status  => 200,
186
            openapi => $ret
187
        );
188
    }
189
    catch {
190
        $c->unhandled_exception($_);
191
    };
192
}
193
194
=head3 delete
195
196
Delete a batch
197
198
=cut
199
200
sub delete {
201
202
    my $c = shift->openapi->valid_input or return;
203
204
    my $batch = Koha::Illbatches->find( $c->validation->param( 'illbatch_id' ) );
205
206
    if ( not defined $batch ) {
207
        return $c->render( status => 404, openapi => { error => "ILL batch not found" } );
208
    }
209
210
    return try {
211
        $batch->delete;
212
        return $c->render( status => 204, openapi => '');
213
    }
214
    catch {
215
        $c->unhandled_exception($_);
216
    };
217
}
218
219
1;
(-)a/Koha/REST/V1/Illrequests.pm (-1 / +4 lines)
Lines 61-73 sub list { Link Here
61
    my $hidden_statuses = [ split /\|/, $hidden_statuses_string ];
61
    my $hidden_statuses = [ split /\|/, $hidden_statuses_string ];
62
62
63
    # Get all requests
63
    # Get all requests
64
    # If necessary, only get those from a specified patron
64
    # If necessary, restrict the resultset
65
    my @requests = Koha::Illrequests->search({
65
    my @requests = Koha::Illrequests->search({
66
        $hidden_statuses
66
        $hidden_statuses
67
        ? ( status => { 'not in' => $hidden_statuses } )
67
        ? ( status => { 'not in' => $hidden_statuses } )
68
        : (),
68
        : (),
69
        $args->{borrowernumber}
69
        $args->{borrowernumber}
70
        ? ( borrowernumber => $args->{borrowernumber} )
70
        ? ( borrowernumber => $args->{borrowernumber} )
71
        : (),
72
        $args->{batch_id}
73
        ? ( batch_id => $args->{batch_id} )
71
        : ()
74
        : ()
72
    })->as_list;
75
    })->as_list;
73
76
(-)a/api/v1/swagger/definitions/illbatch.yaml (+39 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  id:
5
    type: string
6
    description: Internal ILL batch identifier
7
  name:
8
    type: string
9
    description: Name of the ILL batch
10
  backend:
11
    type: string
12
    description: Backend name
13
  cardnumber:
14
    type: string
15
    description: Card number of the patron of the ILL batch
16
  borrowernumber:
17
    type: string
18
    description: Borrower number of the patron of the ILL batch
19
  branchcode:
20
    type: string
21
    description: Branch code of the branch of the ILL batch
22
  patron:
23
    type:
24
      - object
25
      - "null"
26
    description: The patron associated with the batch
27
  branch:
28
    type:
29
      - object
30
      - "null"
31
    description: The branch associated with the batch
32
  requests_count:
33
    type: string
34
    description: The number of requests in this batch
35
additionalProperties: false
36
required:
37
  - name
38
  - backend
39
  - branchcode
(-)a/api/v1/swagger/definitions/illbatches.yaml (+5 lines)
Line 0 Link Here
1
---
2
type: array
3
items:
4
  $ref: "illbatch.yaml"
5
additionalProperties: false
(-)a/api/v1/swagger/paths/illbatches.yaml (+232 lines)
Line 0 Link Here
1
---
2
/illbatches:
3
  get:
4
    x-mojo-to: Illbatches#list
5
    operationId: listIllbatches
6
    tags:
7
      - illbatches
8
    summary: List ILL batches
9
    parameters: []
10
    produces:
11
      - application/json
12
    responses:
13
      "200":
14
        description: A list of ILL batches
15
        schema:
16
          $ref: "../swagger.yaml#/definitions/illbatches"
17
      "401":
18
        description: Authentication required
19
        schema:
20
          $ref: "../swagger.yaml#/definitions/error"
21
      "403":
22
        description: Access forbidden
23
        schema:
24
          $ref: "../swagger.yaml#/definitions/error"
25
      "404":
26
        description: ILL batches not found
27
        schema:
28
          $ref: "../swagger.yaml#/definitions/error"
29
      "500":
30
        description: |
31
          Internal server error. Possible `error_code` attribute values:
32
33
          * `internal_server_error`
34
        schema:
35
          $ref: "../swagger.yaml#/definitions/error"
36
      "503":
37
        description: Under maintenance
38
        schema:
39
          $ref: "../swagger.yaml#/definitions/error"
40
    x-koha-authorization:
41
      permissions:
42
        ill: "1"
43
  post:
44
    x-mojo-to: Illbatches#add
45
    operationId: addIllbatch
46
    tags:
47
      - illbatches
48
    summary: Add ILL batch
49
    parameters:
50
      - name: body
51
        in: body
52
        description: A JSON object containing informations about the new batch
53
        required: true
54
        schema:
55
          $ref: "../swagger.yaml#/definitions/illbatch"
56
    produces:
57
      - application/json
58
    responses:
59
      "201":
60
        description: Batch added
61
        schema:
62
          $ref: "../swagger.yaml#/definitions/illbatch"
63
      "400":
64
        description: Bad request
65
        schema:
66
          $ref: "../swagger.yaml#/definitions/error"
67
      "401":
68
        description: Authentication required
69
        schema:
70
          $ref: "../swagger.yaml#/definitions/error"
71
      "403":
72
        description: Access forbidden
73
        schema:
74
          $ref: "../swagger.yaml#/definitions/error"
75
      "409":
76
        description: Conflict in creating resource
77
        schema:
78
          $ref: "../swagger.yaml#/definitions/error"
79
      "500":
80
        description: |
81
          Internal server error. Possible `error_code` attribute values:
82
83
          * `internal_server_error`
84
        schema:
85
          $ref: "../swagger.yaml#/definitions/error"
86
      "503":
87
        description: Under maintenance
88
        schema:
89
          $ref: "../swagger.yaml#/definitions/error"
90
    x-koha-authorization:
91
      permissions:
92
        ill: "1"
93
"/illbatches/{illbatch_id}":
94
  get:
95
    x-mojo-to: Illbatches#get
96
    operationId: getIllbatches
97
    tags:
98
      - illbatches
99
    summary: Get ILL batch
100
    parameters:
101
      - name: illbatch_id
102
        in: path
103
        description: ILL batch id/name/contents
104
        required: true
105
        type: string
106
    produces:
107
      - application/json
108
    responses:
109
      "200":
110
        description: An ILL batch
111
        schema:
112
          $ref: "../swagger.yaml#/definitions/illbatch"
113
      "401":
114
        description: Authentication required
115
        schema:
116
          $ref: "../swagger.yaml#/definitions/error"
117
      "403":
118
        description: Access forbidden
119
        schema:
120
          $ref: "../swagger.yaml#/definitions/error"
121
      "404":
122
        description: ILL batch not found
123
        schema:
124
          $ref: "../swagger.yaml#/definitions/error"
125
      "500":
126
        description: |
127
          Internal server error. Possible `error_code` attribute values:
128
129
          * `internal_server_error`
130
        schema:
131
          $ref: "../swagger.yaml#/definitions/error"
132
      "503":
133
        description: Under maintenance
134
        schema:
135
          $ref: "../swagger.yaml#/definitions/error"
136
    x-koha-authorization:
137
      permissions:
138
        ill: "1"
139
  put:
140
    x-mojo-to: Illbatches#update
141
    operationId: updateIllBatch
142
    tags:
143
      - illbatches
144
    summary: Update batch
145
    parameters:
146
      - $ref: "../swagger.yaml#/parameters/illbatch_id_pp"
147
      - name: body
148
        in: body
149
        description: A JSON object containing information on the batch
150
        required: true
151
        schema:
152
          $ref: "../swagger.yaml#/definitions/illbatch"
153
    consumes:
154
      - application/json
155
    produces:
156
      - application/json
157
    responses:
158
      "200":
159
        description: An ILL batch
160
        schema:
161
          $ref: "../swagger.yaml#/definitions/illbatch"
162
      "400":
163
        description: Bad request
164
        schema:
165
          $ref: "../swagger.yaml#/definitions/error"
166
      "401":
167
        description: Authentication required
168
        schema:
169
          $ref: "../swagger.yaml#/definitions/error"
170
      "403":
171
        description: Access forbidden
172
        schema:
173
          $ref: "../swagger.yaml#/definitions/error"
174
      "404":
175
        description: ILL batch not found
176
        schema:
177
          $ref: "../swagger.yaml#/definitions/error"
178
      "500":
179
        description: |
180
          Internal server error. Possible `error_code` attribute values:
181
182
          * `internal_server_error`
183
        schema:
184
          $ref: "../swagger.yaml#/definitions/error"
185
      "503":
186
        description: Under maintenance
187
        schema:
188
          $ref: "../swagger.yaml#/definitions/error"
189
    x-koha-authorization:
190
      permissions:
191
        ill: "1"
192
  delete:
193
    x-mojo-to: Illbatches#delete
194
    operationId: deleteBatch
195
    tags:
196
      - illbatches
197
    summary: Delete ILL batch
198
    parameters:
199
      - $ref: "../swagger.yaml#/parameters/illbatch_id_pp"
200
    produces:
201
      - application/json
202
    responses:
203
      "204":
204
        description: ILL batch deleted
205
        schema:
206
          type: string
207
      "401":
208
        description: Authentication required
209
        schema:
210
          $ref: "../swagger.yaml#/definitions/error"
211
      "403":
212
        description: Access forbidden
213
        schema:
214
          $ref: "../swagger.yaml#/definitions/error"
215
      "404":
216
        description: ILL batch not found
217
        schema:
218
          $ref: "../swagger.yaml#/definitions/error"
219
      "500":
220
        description: |
221
          Internal server error. Possible `error_code` attribute values:
222
223
          * `internal_server_error`
224
        schema:
225
          $ref: "../swagger.yaml#/definitions/error"
226
      "503":
227
        description: Under maintenance
228
        schema:
229
          $ref: "../swagger.yaml#/definitions/error"
230
    x-koha-authorization:
231
      permissions:
232
        ill: "1"
(-)a/api/v1/swagger/paths/illrequests.yaml (+5 lines)
Lines 38-43 Link Here
38
        description: Internal biblio identifier
38
        description: Internal biblio identifier
39
        required: false
39
        required: false
40
        type: integer
40
        type: integer
41
      - name: batch_id
42
        in: query
43
        description: Internal identifier of the batch the request belongs to
44
        required: false
45
        type: integer
41
      - name: borrowernumber
46
      - name: borrowernumber
42
        in: query
47
        in: query
43
        description: Internal patron identifier
48
        description: Internal patron identifier
(-)a/api/v1/swagger/swagger.yaml (+17 lines)
Lines 48-53 definitions: Link Here
48
    $ref: ./definitions/ill_backend.yaml
48
    $ref: ./definitions/ill_backend.yaml
49
  ill_backends:
49
  ill_backends:
50
    $ref: ./definitions/ill_backends.yaml
50
    $ref: ./definitions/ill_backends.yaml
51
  illbatch:
52
    $ref: ./definitions/illbatch.yaml
53
  illbatches:
54
    $ref: ./definitions/illbatches.yaml
51
  import_batch_profile:
55
  import_batch_profile:
52
    $ref: ./definitions/import_batch_profile.yaml
56
    $ref: ./definitions/import_batch_profile.yaml
53
  import_batch_profiles:
57
  import_batch_profiles:
Lines 225-230 paths: Link Here
225
    $ref: ./paths/ill_backends.yaml#/~1ill_backends
229
    $ref: ./paths/ill_backends.yaml#/~1ill_backends
226
  "/ill_backends/{ill_backend_id}":
230
  "/ill_backends/{ill_backend_id}":
227
    $ref: "./paths/ill_backends.yaml#/~1ill_backends~1{ill_backend_id}"
231
    $ref: "./paths/ill_backends.yaml#/~1ill_backends~1{ill_backend_id}"
232
  /illbatches:
233
    $ref: ./paths/illbatches.yaml#/~1illbatches
234
  "/illbatches/{illbatch_id}":
235
    $ref: "./paths/illbatches.yaml#/~1illbatches~1{illbatch_id}"
228
  /illrequests:
236
  /illrequests:
229
    $ref: ./paths/illrequests.yaml#/~1illrequests
237
    $ref: ./paths/illrequests.yaml#/~1illrequests
230
  "/import_batches/{import_batch_id}/records/{import_record_id}/matches/chosen":
238
  "/import_batches/{import_batch_id}/records/{import_record_id}/matches/chosen":
Lines 420-425 parameters: Link Here
420
    name: hold_id
428
    name: hold_id
421
    required: true
429
    required: true
422
    type: integer
430
    type: integer
431
  illbatch_id_pp:
432
    description: Internal ILL batch identifier
433
    in: path
434
    name: illbatch_id
435
    required: true
436
    type: integer
423
  import_batch_profile_id_pp:
437
  import_batch_profile_id_pp:
424
    description: Internal profile identifier
438
    description: Internal profile identifier
425
    in: path
439
    in: path
Lines 713-718 tags: Link Here
713
  - description: "Manage ILL module backends\n"
727
  - description: "Manage ILL module backends\n"
714
    name: illbackends
728
    name: illbackends
715
    x-displayName: ILL backends
729
    x-displayName: ILL backends
730
  - description: "Manage ILL module batches\n"
731
    name: illbatches
732
    x-displayName: ILL batches
716
  - description: "Manage ILL requests\n"
733
  - description: "Manage ILL requests\n"
717
    name: illrequests
734
    name: illrequests
718
    x-displayName: ILL requests
735
    x-displayName: ILL requests
(-)a/installer/data/mysql/atomicupdate/bug_30719_add_ill_batches.pl (+38 lines)
Line 0 Link Here
1
use Modern::Perl;
2
3
return {
4
    bug_number => "30719",
5
    description => "Add ILL batches",
6
    up => sub {
7
        my ($args) = @_;
8
        my ($dbh, $out) = @$args{qw(dbh out)};
9
        $dbh->do(q{
10
            CREATE TABLE `illbatches` (
11
                `id` int(11) NOT NULL auto_increment, -- Batch ID
12
                `name` varchar(100) NOT NULL,         -- Unique name of batch
13
                `backend` varchar(20) NOT NULL,       -- Name of batch backend
14
                `borrowernumber` int(11),             -- Patron associated with batch
15
                `branchcode` varchar(50),             -- Branch associated with batch
16
                PRIMARY KEY (`id`),
17
                UNIQUE KEY `u_illbatches__name` (`name`)
18
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
19
        });
20
        $dbh->do(q{
21
            ALTER TABLE `illrequests`
22
                ADD COLUMN `batch_id` int(11) AFTER backend -- Optional ID of batch that this request belongs to
23
        });
24
        $dbh->do(q{
25
            ALTER TABLE `illrequests`
26
                ADD CONSTRAINT `illrequests_ibfk` FOREIGN KEY (`batch_id`) REFERENCES `illbatches` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
27
        });
28
        $dbh->do(q{
29
            ALTER TABLE `illbatches`
30
                ADD CONSTRAINT `illbatches_bnfk` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE CASCADE
31
        });
32
        $dbh->do(q{
33
            ALTER TABLE `illbatches`
34
                ADD CONSTRAINT `illbatches_bcfk` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE SET NULL ON UPDATE CASCADE
35
        });
36
        say $out "Bug 30719: Add ILL batches completed"
37
    },
38
};
(-)a/installer/data/mysql/kohastructure.sql (-1 / +19 lines)
Lines 3227-3232 CREATE TABLE `illrequestattributes` ( Link Here
3227
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
3227
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
3228
/*!40101 SET character_set_client = @saved_cs_client */;
3228
/*!40101 SET character_set_client = @saved_cs_client */;
3229
3229
3230
--
3231
-- Table structure for table `illbatches`
3232
--
3233
DROP TABLE IF EXISTS `illbatches`;
3234
CREATE TABLE `illbatches` (
3235
    `id` int(11) NOT NULL auto_increment, -- Batch ID
3236
    `name` varchar(100) NOT NULL,         -- Unique name of batch
3237
    `backend` varchar(20) NOT NULL,       -- Name of batch backend
3238
    `borrowernumber` int(11),             -- Patron associated with batch
3239
    `branchcode` varchar(50),             -- Branch associated with batch
3240
    PRIMARY KEY (`id`),
3241
    UNIQUE KEY `u_illbatches__name` (`name`),
3242
    CONSTRAINT `illbatches_bnfk` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE CASCADE,
3243
    CONSTRAINT `illbatches_bcfk` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE SET NULL ON UPDATE CASCADE
3244
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
3245
3230
--
3246
--
3231
-- Table structure for table `illrequests`
3247
-- Table structure for table `illrequests`
3232
--
3248
--
Lines 3254-3266 CREATE TABLE `illrequests` ( Link Here
3254
  `notesstaff` mediumtext DEFAULT NULL COMMENT 'Staff notes attached to request',
3270
  `notesstaff` mediumtext DEFAULT NULL COMMENT 'Staff notes attached to request',
3255
  `orderid` varchar(50) DEFAULT NULL COMMENT 'Backend id attached to request',
3271
  `orderid` varchar(50) DEFAULT NULL COMMENT 'Backend id attached to request',
3256
  `backend` varchar(20) DEFAULT NULL COMMENT 'The backend used to create request',
3272
  `backend` varchar(20) DEFAULT NULL COMMENT 'The backend used to create request',
3273
  `batch_id` int(11) COMMENT 'Optional ID of batch that this request belongs to',
3257
  PRIMARY KEY (`illrequest_id`),
3274
  PRIMARY KEY (`illrequest_id`),
3258
  KEY `illrequests_bnfk` (`borrowernumber`),
3275
  KEY `illrequests_bnfk` (`borrowernumber`),
3259
  KEY `illrequests_bcfk_2` (`branchcode`),
3276
  KEY `illrequests_bcfk_2` (`branchcode`),
3260
  KEY `illrequests_safk` (`status_alias`),
3277
  KEY `illrequests_safk` (`status_alias`),
3261
  CONSTRAINT `illrequests_bcfk_2` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE,
3278
  CONSTRAINT `illrequests_bcfk_2` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE,
3262
  CONSTRAINT `illrequests_bnfk` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE,
3279
  CONSTRAINT `illrequests_bnfk` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE,
3263
  CONSTRAINT `illrequests_safk` FOREIGN KEY (`status_alias`) REFERENCES `authorised_values` (`authorised_value`) ON DELETE SET NULL ON UPDATE CASCADE
3280
  CONSTRAINT `illrequests_safk` FOREIGN KEY (`status_alias`) REFERENCES `authorised_values` (`authorised_value`) ON DELETE SET NULL ON UPDATE CASCADE,
3281
  CONSTRAINT `illrequests_ibfk` FOREIGN KEY (`batch_id`) REFERENCES `illbatches` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
3264
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
3282
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
3265
/*!40101 SET character_set_client = @saved_cs_client */;
3283
/*!40101 SET character_set_client = @saved_cs_client */;
3266
3284
(-)a/t/db_dependent/Illbatches.t (+96 lines)
Line 0 Link Here
1
#s!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use File::Basename qw/basename/;
21
use Koha::Database;
22
use Koha::Illbatch;
23
use Koha::Illbatches;
24
use Koha::Illrequests;
25
use Koha::Patrons;
26
use t::lib::Mocks;
27
use t::lib::TestBuilder;
28
use Test::MockObject;
29
use Test::MockModule;
30
31
use Test::More tests => 8;
32
33
my $schema = Koha::Database->new->schema;
34
my $builder = t::lib::TestBuilder->new;
35
use_ok('Koha::Illbatch');
36
use_ok('Koha::Illbatches');
37
38
$schema->storage->txn_begin;
39
40
Koha::Illrequests->search->delete;
41
42
# Create a patron
43
my $patron = $builder->build({ source => 'Borrower' });
44
45
# Create a librarian
46
my $librarian = $builder->build({
47
    source => 'Borrower',
48
    value => {
49
        firstname => "Grogu"
50
    }
51
});
52
53
# Create a branch
54
my $branch = $builder->build({
55
    source => 'Branch'
56
});
57
58
# Create a batch
59
my $illbatch = $builder->build({
60
    source => 'Illbatch',
61
    value => {
62
        name  => "My test batch",
63
        backend  => "Mock",
64
        borrowernumber => $librarian->{borrowernumber},
65
        branchcode => $branch->{branchcode}
66
    }
67
});
68
my $batch_obj = Koha::Illbatches->find($illbatch->{id});
69
isa_ok( $batch_obj, 'Koha::Illbatch' );
70
71
# Create an ILL request in the batch
72
my $illrq = $builder->build({
73
    source => 'Illrequest',
74
    value => {
75
        borrowernumber => $patron->{borrowernumber},
76
        batch_id       => $illbatch->{id}
77
    }
78
});
79
my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
80
81
# Check requests_count
82
my $requests_count = $batch_obj->requests_count;
83
is( $requests_count, 1, 'requests_count returns correctly' );
84
85
# Check patron
86
my $batch_patron = $batch_obj->patron;
87
isa_ok( $batch_patron, 'Koha::Patron' );
88
is( $batch_patron->firstname, "Grogu", "patron returns correctly" );
89
90
# Check branch
91
my $batch_branch = $batch_obj->branch;
92
isa_ok( $batch_branch, 'Koha::Library' );
93
is( $batch_branch->branchcode, $branch->{branchcode}, "branch returns correctly" );
94
95
$illrq_obj->delete;
96
$schema->storage->txn_rollback;
(-)a/t/db_dependent/api/v1/illbatches.t (-1 / +417 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 5;
21
use Test::Mojo;
22
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
25
26
use Koha::Illbatch;
27
use Koha::Illbatches;
28
use Koha::Illrequests;
29
use Koha::Database;
30
31
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
34
my $t = Test::Mojo->new('Koha::REST::V1');
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37
subtest 'list() tests' => sub {
38
39
    plan tests => 19;
40
41
    $schema->storage->txn_begin;
42
43
    Koha::Illbatches->search->delete;
44
45
    my $librarian = $builder->build_object(
46
        {
47
            class => 'Koha::Patrons',
48
            value => {
49
                flags => 2 ** 22 # 22 => ill
50
            }
51
        }
52
    );
53
54
    my $branch = $builder->build_object(
55
        {
56
            class => 'Koha::Libraries'
57
        }
58
    );
59
60
    my $password = 'sheev_is_da_boss!';
61
    $librarian->set_password( { password => $password, skip_validation => 1 } );
62
    my $userid = $librarian->userid;
63
64
    ## Authorized user tests
65
    # No batches, so empty array should be returned
66
    $t->get_ok("//$userid:$password@/api/v1/illbatches")
67
      ->status_is(200)
68
      ->json_is( [] );
69
70
    my $batch = $builder->build_object({
71
        class => 'Koha::Illbatches',
72
        value => {
73
            name           => "PapaPalpatine",
74
            backend        => "Mock",
75
            borrowernumber => $librarian->borrowernumber,
76
            branchcode => $branch->branchcode
77
        }
78
    });
79
80
    my $illrq = $builder->build({
81
        source => 'Illrequest',
82
        value => {
83
            borrowernumber => $librarian->borrowernumber,
84
            batch_id       => $batch->id
85
        }
86
    });
87
88
    # One batch created, should get returned
89
    $t->get_ok("//$userid:$password@/api/v1/illbatches")
90
      ->status_is(200)
91
      ->json_has( '/0/id', 'Batch ID' )
92
      ->json_has( '/0/name', 'Batch name' )
93
      ->json_has( '/0/backend', 'Backend name' )
94
      ->json_has( '/0/borrowernumber', 'Borrowernumber' )
95
      ->json_has( '/0/branchcode', 'Branchcode' )
96
      ->json_has( '/0/patron', 'patron embedded' )
97
      ->json_has( '/0/branch', 'branch embedded' )
98
      ->json_has( '/0/requests_count', 'request count' );
99
100
    # Try to create a second batch with the same name, this should fail
101
    my $another_batch = $builder->build_object({ class => 'Koha::Illbatches', value => {
102
        name => $batch->name
103
    } });
104
    # Create a second batch with a different name
105
    my $batch_with_another_name = $builder->build_object({ class => 'Koha::Illbatches' });
106
107
    # Two batches created, they should both be returned
108
    $t->get_ok("//$userid:$password@/api/v1/illbatches")
109
      ->status_is(200)
110
      ->json_has('/0', 'has first batch')
111
      ->json_has('/1', 'has second batch');
112
113
    my $patron = $builder->build_object(
114
        {
115
            class => 'Koha::Patrons',
116
            value => {
117
                cardnumber => 999,
118
                flags => 0
119
            }
120
        }
121
    );
122
123
    $patron->set_password( { password => $password, skip_validation => 1 } );
124
    my $unauth_userid = $patron->userid;
125
126
    # Unauthorized access
127
    $t->get_ok("//$unauth_userid:$password@/api/v1/illbatches")
128
      ->status_is(403);
129
130
    $schema->storage->txn_rollback;
131
};
132
133
subtest 'get() tests' => sub {
134
135
    plan tests => 15;
136
137
    $schema->storage->txn_begin;
138
139
    my $librarian = $builder->build_object(
140
        {
141
            class => 'Koha::Patrons',
142
            value => { flags => 2**22 }    # 22 => ill
143
        }
144
    );
145
    my $password = 'Rebelz4DaWin';
146
    $librarian->set_password( { password => $password, skip_validation => 1 } );
147
    my $userid = $librarian->userid;
148
149
    my $patron = $builder->build_object(
150
        {
151
            class => 'Koha::Patrons',
152
            value => { flags => 0 }
153
        }
154
    );
155
156
    my $branch = $builder->build_object(
157
        {
158
            class => 'Koha::Libraries'
159
        }
160
    );
161
162
    my $batch = $builder->build_object({
163
        class => 'Koha::Illbatches',
164
        value => {
165
            name           => "LeiaOrgana",
166
            backend        => "Mock",
167
            borrowernumber => $librarian->borrowernumber,
168
            branchcode     => $branch->branchcode
169
        }
170
    });
171
172
173
    $patron->set_password( { password => $password, skip_validation => 1 } );
174
    my $unauth_userid = $patron->userid;
175
176
    $t->get_ok( "//$userid:$password@/api/v1/illbatches/" . $batch->id )
177
      ->status_is(200)
178
      ->json_has( '/id', 'Batch ID' )
179
      ->json_has( '/name', 'Batch name' )
180
      ->json_has( '/backend', 'Backend name' )
181
      ->json_has( '/borrowernumber', 'Borrowernumber' )
182
      ->json_has( '/branchcode', 'Branchcode' )
183
      ->json_has( '/patron', 'patron embedded' )
184
      ->json_has( '/branch', 'branch embedded' )
185
      ->json_has( '/requests_count', 'request count' );
186
187
    $t->get_ok( "//$unauth_userid:$password@/api/v1/illbatches/" . $batch->id )
188
      ->status_is(403);
189
190
    my $batch_to_delete = $builder->build_object({ class => 'Koha::Illbatches' });
191
    my $non_existent_id = $batch_to_delete->id;
192
    $batch_to_delete->delete;
193
194
    $t->get_ok( "//$userid:$password@/api/v1/illbatches/$non_existent_id" )
195
      ->status_is(404)
196
      ->json_is( '/error' => 'ILL batch not found' );
197
198
    $schema->storage->txn_rollback;
199
};
200
201
subtest 'add() tests' => sub {
202
203
    plan tests =>16;
204
205
    $schema->storage->txn_begin;
206
207
    my $librarian = $builder->build_object(
208
        {
209
            class => 'Koha::Patrons',
210
            value => { flags => 2**22 }    # 22 => ill
211
        }
212
    );
213
    my $password = 'v4d3rRox';
214
    $librarian->set_password( { password => $password, skip_validation => 1 } );
215
    my $userid = $librarian->userid;
216
217
    my $patron = $builder->build_object(
218
        {
219
            class => 'Koha::Patrons',
220
            value => { flags => 0 }
221
        }
222
    );
223
224
    $patron->set_password( { password => $password, skip_validation => 1 } );
225
    my $unauth_userid = $patron->userid;
226
227
    my $branch = $builder->build_object(
228
        {
229
            class => 'Koha::Libraries'
230
        }
231
    );
232
233
    my $batch_metadata = {
234
        name           => "Anakin's requests",
235
        backend        => "Mock",
236
        borrowernumber => $librarian->borrowernumber,
237
        branchcode     => $branch->branchcode
238
    };
239
240
    # Unauthorized attempt to write
241
    $t->post_ok("//$unauth_userid:$password@/api/v1/illbatches" => json => $batch_metadata)
242
      ->status_is(403);
243
244
    # Authorized attempt to write invalid data
245
    my $batch_with_invalid_field = {
246
        %{$batch_metadata},
247
        doh => 1
248
    };
249
250
    $t->post_ok( "//$userid:$password@/api/v1/illbatches" => json => $batch_with_invalid_field )
251
      ->status_is(400)
252
      ->json_is(
253
        "/errors" => [
254
            {
255
                message => "Properties not allowed: doh.",
256
                path    => "/body"
257
            }
258
        ]
259
      );
260
261
    # Authorized attempt to write
262
    my $batch_id =
263
      $t->post_ok( "//$userid:$password@/api/v1/illbatches" => json => $batch_metadata )
264
        ->status_is( 201 )
265
        ->json_is( '/name'           => $batch_metadata->{name} )
266
        ->json_is( '/backend'        => $batch_metadata->{backend} )
267
        ->json_is( '/borrowernumber' => $batch_metadata->{borrowernumber} )
268
        ->json_is( '/branchcode'     => $batch_metadata->{branchcode} )
269
        ->json_has( '/patron' )
270
        ->json_has( '/branch' );
271
272
    # Authorized attempt to create with null id
273
    $batch_metadata->{id} = undef;
274
    $t->post_ok( "//$userid:$password@/api/v1/illbatches" => json => $batch_metadata )
275
      ->status_is(400)
276
      ->json_has('/errors');
277
278
    $schema->storage->txn_rollback;
279
};
280
281
subtest 'update() tests' => sub {
282
283
    plan tests => 15;
284
285
    $schema->storage->txn_begin;
286
287
    my $librarian = $builder->build_object(
288
        {
289
            class => 'Koha::Patrons',
290
            value => { flags => 2**22 }    # 22 => ill
291
        }
292
    );
293
    my $password = 'aw3s0m3y0d41z';
294
    $librarian->set_password( { password => $password, skip_validation => 1 } );
295
    my $userid = $librarian->userid;
296
297
    my $patron = $builder->build_object(
298
        {
299
            class => 'Koha::Patrons',
300
            value => { flags => 0 }
301
        }
302
    );
303
304
    $patron->set_password( { password => $password, skip_validation => 1 } );
305
    my $unauth_userid = $patron->userid;
306
307
    my $branch = $builder->build_object(
308
        {
309
            class => 'Koha::Libraries'
310
        }
311
    );
312
313
    my $batch_id = $builder->build_object({ class => 'Koha::Illbatches' } )->id;
314
315
    # Unauthorized attempt to update
316
    $t->put_ok( "//$unauth_userid:$password@/api/v1/illbatches/$batch_id" => json => { name => 'These are not the droids you are looking for' } )
317
      ->status_is(403);
318
319
    # Attempt partial update on a PUT
320
    my $batch_with_missing_field = {
321
        backend => "Mock",
322
        borrowernumber => $librarian->borrowernumber,
323
        branchcode => $branch->branchcode
324
    };
325
326
    $t->put_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_missing_field )
327
      ->status_is(400)
328
      ->json_is( "/errors" =>
329
          [ { message => "Missing property.", path => "/body/name" } ]
330
      );
331
332
    # Full object update on PUT
333
    my $batch_with_updated_field = {
334
        name           => "Master Ploo Koon",
335
        backend        => "Mock",
336
        borrowernumber => $librarian->borrowernumber,
337
        branchcode => $branch->branchcode
338
    };
339
340
    $t->put_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_updated_field )
341
      ->status_is(200)
342
      ->json_is( '/name' => 'Master Ploo Koon' );
343
344
    # Authorized attempt to write invalid data
345
    my $batch_with_invalid_field = {
346
        doh  => 1,
347
        name => "Master Mace Windu",
348
        backend => "Mock"
349
    };
350
351
    $t->put_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_invalid_field )
352
      ->status_is(400)
353
      ->json_is(
354
        "/errors" => [
355
            {
356
                message => "Properties not allowed: doh.",
357
                path    => "/body"
358
            }
359
        ]
360
    );
361
362
    my $batch_to_delete = $builder->build_object({ class => 'Koha::Cities' });
363
    my $non_existent_id = $batch_to_delete->id;
364
    $batch_to_delete->delete;
365
366
    $t->put_ok( "//$userid:$password@/api/v1/illbatches/$non_existent_id" => json => $batch_with_updated_field )
367
      ->status_is(404);
368
369
    # Wrong method (POST)
370
    $batch_with_updated_field->{id} = 2;
371
372
    $t->post_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_updated_field )
373
      ->status_is(404);
374
375
    $schema->storage->txn_rollback;
376
};
377
378
subtest 'delete() tests' => sub {
379
380
    plan tests => 6;
381
382
    $schema->storage->txn_begin;
383
384
    my $librarian = $builder->build_object(
385
        {
386
            class => 'Koha::Patrons',
387
            value => { flags => 2**22 }    # 22 => ill
388
        }
389
    );
390
    my $password = 's1th43v3r!';
391
    $librarian->set_password( { password => $password, skip_validation => 1 } );
392
    my $userid = $librarian->userid;
393
394
    my $patron = $builder->build_object(
395
        {
396
            class => 'Koha::Patrons',
397
            value => { flags => 0 }
398
        }
399
    );
400
401
    $patron->set_password( { password => $password, skip_validation => 1 } );
402
    my $unauth_userid = $patron->userid;
403
404
    my $batch_id = $builder->build_object({ class => 'Koha::Illbatches' })->id;
405
406
    # Unauthorized attempt to delete
407
    $t->delete_ok( "//$unauth_userid:$password@/api/v1/illbatches/$batch_id" )
408
      ->status_is(403);
409
410
    $t->delete_ok("//$userid:$password@/api/v1/illbatches/$batch_id")
411
      ->status_is(204);
412
413
    $t->delete_ok("//$userid:$password@/api/v1/illbatches/$batch_id")
414
      ->status_is(404);
415
416
    $schema->storage->txn_rollback;
417
};

Return to bug 30719