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

(-)a/Koha/REST/V1/Items.pm (-1 / +65 lines)
Lines 58-64 sub list { Link Here
58
    };
58
    };
59
}
59
}
60
60
61
62
=head3 get
61
=head3 get
63
62
64
Controller function that handles retrieving a single Koha::Item
63
Controller function that handles retrieving a single Koha::Item
Lines 83-88 sub get { Link Here
83
    };
82
    };
84
}
83
}
85
84
85
=head3 delete
86
87
Controller function that handles deleting a single Koha::Item
88
89
=cut
90
91
sub delete {
92
    my $c = shift->openapi->valid_input or return;
93
94
    return try {
95
        my $item = Koha::Items->find($c->validation->param('item_id'));
96
        unless ( $item ) {
97
            return $c->render(
98
                status => 404,
99
                openapi => { error => 'Item not found'}
100
            );
101
        }
102
103
        my $safe_to_delete = $item->safe_to_delete;
104
105
        if ( !$safe_to_delete ) {
106
107
            # Pick the first error, if any
108
            my ( $error ) = grep { $_->type eq 'error' } @{ $safe_to_delete->messages };
109
110
            unless ( $error ) {
111
                Koha::Exception->throw('Koha::Item->safe_to_delete returned false but carried no error message');
112
            }
113
114
            my $errors = {
115
                book_on_loan       => { code => 'checked_out',        description => 'The item is checked out' },
116
                book_reserved      => { code => 'found_hold',         description => 'Waiting or in-transit hold for the item' },
117
                last_item_for_hold => { code => 'last_item_for_hold', description => 'The item is the last one on a record on which a biblio-level hold is placed' },
118
                linked_analytics   => { code => 'linked_analytics',   description => 'The item has linked analytic records' },
119
                not_same_branch    => { code => 'not_same_branch',    description => 'The item is blocked by independent branches' },
120
            };
121
122
            if ( any { $error->message eq $_ } keys %{$errors} ) {
123
124
                my $code = $error->message;
125
126
                return $c->render(
127
                    status  => 409,
128
                    openapi => {
129
                        error      => $errors->{ $code }->{description},
130
                        error_code => $errors->{ $code }->{code},
131
                    }
132
                );
133
            } else {
134
                Koha::Exception->throw( 'Koha::Patron->safe_to_delete carried an unexpected message: ' . $error->message );
135
            }
136
        }
137
138
        $item->safe_delete;
139
140
        return $c->render(
141
            status  => 204,
142
            openapi => q{}
143
        );
144
    }
145
    catch {
146
        $c->unhandled_exception($_);
147
    };
148
}
149
86
=head3 pickup_locations
150
=head3 pickup_locations
87
151
88
Method that returns the possible pickup_locations for a given item
152
Method that returns the possible pickup_locations for a given item
(-)a/api/v1/swagger/paths/items.yaml (+52 lines)
Lines 93-98 Link Here
93
    x-koha-authorization:
93
    x-koha-authorization:
94
      permissions:
94
      permissions:
95
        catalogue: "1"
95
        catalogue: "1"
96
  delete:
97
    x-mojo-to: Items#delete
98
    operationId: deleteItem
99
    tags:
100
      - items
101
    summary: Delete item
102
    parameters:
103
      - $ref: "../swagger.yaml#/parameters/item_id_pp"
104
    consumes:
105
      - application/json
106
    produces:
107
      - application/json
108
    responses:
109
      "204":
110
        description: Deleted item
111
      "400":
112
        description: Missing or wrong parameters
113
        schema:
114
          $ref: "../swagger.yaml#/definitions/error"
115
      "403":
116
        description: Access forbidden
117
        schema:
118
          $ref: "../swagger.yaml#/definitions/error"
119
      "404":
120
        description: Item not found
121
        schema:
122
          $ref: "../swagger.yaml#/definitions/error"
123
      "409":
124
        description: |
125
          Conflict. Possible `error_code` attribute values:
126
127
            * book_on_loan: The item is checked out
128
            * book_reserved: Waiting or in-transit hold for the item
129
            * last_item_for_hold: The item is the last one on a record on which a biblio-level hold is placed
130
            * linked_analytics: The item has linked analytic records
131
            * not_same_branch: The item is blocked by independent branches
132
        schema:
133
              $ref: "../swagger.yaml#/definitions/error"
134
      "500":
135
        description: |
136
          Internal server error. Possible `error_code` attribute values:
137
138
          * `internal_server_error`
139
        schema:
140
          $ref: "../swagger.yaml#/definitions/error"
141
      "503":
142
        description: Under maintenance
143
        schema:
144
          $ref: "../swagger.yaml#/definitions/error"
145
    x-koha-authorization:
146
      permissions:
147
        editcatalogue: edit_catalogue
96
"/items/{item_id}/bundled_items":
148
"/items/{item_id}/bundled_items":
97
  post:
149
  post:
98
    x-mojo-to: Items#add_to_bundle
150
    x-mojo-to: Items#add_to_bundle
(-)a/t/db_dependent/api/v1/items.t (-2 / +78 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 3;
22
use Test::More tests => 4;
23
use Test::MockModule;
23
use Test::Mojo;
24
use Test::Mojo;
24
use Test::Warn;
25
use Test::Warn;
25
26
Lines 187-192 subtest 'get() tests' => sub { Link Here
187
    $schema->storage->txn_rollback;
188
    $schema->storage->txn_rollback;
188
};
189
};
189
190
191
subtest 'delete() tests' => sub {
192
193
    plan tests => 23;
194
195
    $schema->storage->txn_begin;
196
197
    my $fail = 0;
198
    my $expected_error;
199
200
    # we want to control all the safe_to_delete use cases
201
    my $item_class = Test::MockModule->new('Koha::Item');
202
    $item_class->mock( 'safe_to_delete', sub {
203
        if ( $fail ) {
204
            return Koha::Result::Boolean->new(0)->add_message({ message => $expected_error });
205
        }
206
        else {
207
            return Koha::Result::Boolean->new(1);
208
        }
209
    });
210
211
    my $librarian = $builder->build_object(
212
        {
213
            class => 'Koha::Patrons',
214
            value => { flags => 2**9 }    # catalogue flag = 2
215
        }
216
    );
217
    my $password = 'thePassword123';
218
    $librarian->set_password( { password => $password, skip_validation => 1 } );
219
    my $userid = $librarian->userid;
220
221
    my $item = $builder->build_sample_item;
222
223
    my $errors = {
224
        book_on_loan       => { code => 'checked_out',        description => 'The item is checked out' },
225
        book_reserved      => { code => 'found_hold',         description => 'Waiting or in-transit hold for the item' },
226
        last_item_for_hold => { code => 'last_item_for_hold', description => 'The item is the last one on a record on which a biblio-level hold is placed' },
227
        linked_analytics   => { code => 'linked_analytics',   description => 'The item has linked analytic records' },
228
        not_same_branch    => { code => 'not_same_branch',    description => 'The item is blocked by independent branches' },
229
    };
230
231
    $fail = 1;
232
233
    foreach my $error_code ( keys %{$errors} ) {
234
235
        $expected_error = $error_code;
236
237
        $t->delete_ok( "//$userid:$password@/api/v1/items/" . $item->id )
238
          ->status_is(409)
239
          ->json_is(
240
            { error      => $errors->{$error_code}->{description},
241
              error_code => $errors->{$error_code}->{code},
242
            }
243
        );
244
    }
245
246
    $expected_error = 'unknown_error';
247
    $t->delete_ok( "//$userid:$password@/api/v1/items/" . $item->id )
248
      ->status_is(500, 'unhandled error case generated default unhandled exception message')
249
      ->json_is(
250
        { error      => 'Something went wrong, check Koha logs for details.',
251
          error_code => 'internal_server_error',
252
        }
253
    );
254
255
    $fail = 0;
256
257
    $t->delete_ok("//$userid:$password@/api/v1/items/" . $item->id)
258
      ->status_is(204, 'SWAGGER3.2.4')
259
      ->content_is('', 'SWAGGER3.3.4');
260
261
    $t->delete_ok("//$userid:$password@/api/v1/items/" . $item->id)
262
      ->status_is(404);
263
264
    $schema->storage->txn_rollback;
265
};
266
190
subtest 'pickup_locations() tests' => sub {
267
subtest 'pickup_locations() tests' => sub {
191
268
192
    plan tests => 16;
269
    plan tests => 16;
193
- 

Return to bug 31797