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

(-)a/Koha/REST/V1/Biblios.pm (+85 lines)
Lines 152-157 sub delete { Link Here
152
    };
152
    };
153
}
153
}
154
154
155
=head3 populate_empty_callnumbers
156
157
Controller function that handles deleting a biblio object
158
159
=cut
160
161
sub populate_empty_callnumbers {
162
    my $c = shift->openapi->valid_input or return;
163
164
    my $biblio = Koha::Biblios->find( $c->param('biblio_id') );
165
166
    if ( not defined $biblio ) {
167
        return $c->render(
168
            status  => 404,
169
            openapi => { error => "Biblio not found" }
170
        );
171
    }
172
173
    my $items;
174
    if ( $c->param('item_id') ) {
175
        $items = Koha::Items->search(
176
            {
177
                -and => [
178
                    biblionumber => $biblio->id,
179
                    itemnumber   => $c->param('item_id'),
180
                    -or          => [
181
                        itemcallnumber => undef,
182
                        itemcallnumber => q{},
183
                    ]
184
                ]
185
            }
186
        );
187
    } else {
188
        $items = Koha::Items->search(
189
            {
190
                -and => [
191
                    biblionumber => $biblio->id,
192
                    -or          => [
193
                        itemcallnumber => undef,
194
                        itemcallnumber => q{},
195
                    ]
196
                ]
197
            }
198
        );
199
    }
200
201
    my $cn_fields = C4::Context->preference('itemcallnumber');
202
    return $c->render(
203
        status  => 404,
204
        openapi => { error => "Callnumber fields not found" }
205
    ) unless $cn_fields;
206
207
    my $record = $biblio->record;
208
    my $callnumber;
209
    foreach my $callnumber_marc_field ( split( /,/, $cn_fields ) ) {
210
        my $callnumber_tag       = substr( $callnumber_marc_field, 0, 3 );
211
        my $callnumber_subfields = substr( $callnumber_marc_field, 3 );
212
213
        next unless $callnumber_tag && $callnumber_subfields;
214
215
        my $field = $record->field($callnumber_tag);
216
217
        next unless $field;
218
219
        $callnumber = $field->as_string( $callnumber_subfields, ' ' );
220
        last if $callnumber;
221
    }
222
223
    return $c->render(
224
        status  => 200,
225
        openapi => { items_updated => 0, callnumber => $callnumber },
226
    ) unless $items->count;
227
228
    return try {
229
        my $items_updated = $items->update( { itemcallnumber => $callnumber }, { no_triggers => 1 } );
230
231
        return $c->render(
232
            status  => 200,
233
            openapi => { items_updated => $items_updated, callnumber => $callnumber },
234
        );
235
    } catch {
236
        $c->unhandled_exception($_);
237
    };
238
}
239
155
=head3 get_public
240
=head3 get_public
156
241
157
Controller function that handles retrieving a single biblio object
242
Controller function that handles retrieving a single biblio object
(-)a/api/v1/swagger/paths/biblios.yaml (+119 lines)
Lines 462-467 Link Here
462
    x-koha-authorization:
462
    x-koha-authorization:
463
      permissions:
463
      permissions:
464
        editcatalogue: edit_catalogue
464
        editcatalogue: edit_catalogue
465
"/biblios/{biblio_id}/items/populate_empty_callnumbers":
466
  post:
467
    x-mojo-to: Biblios#populate_empty_callnumbers
468
    operationId: itemsPopulateEmptyCallnumbers
469
    tags:
470
      - biblios
471
      - items
472
      - callnumbers
473
    summary: Populate empty item callnumbers from biblio callnumber for all items on record with no callnumber
474
    parameters:
475
      - $ref: "../swagger.yaml#/parameters/biblio_id_pp"
476
    produces:
477
      - application/json
478
    responses:
479
      "200":
480
        description: Callnumbers updated
481
        schema:
482
          type: object
483
          properties:
484
            items_updated:
485
              description: Number of items updated
486
              type: integer
487
            callnumber:
488
              description: Callnumber added to updated items
489
              type: string
490
      "400":
491
        description: Bad request
492
        schema:
493
          $ref: "../swagger.yaml#/definitions/error"
494
      "401":
495
        description: Authentication required
496
        schema:
497
          $ref: "../swagger.yaml#/definitions/error"
498
      "403":
499
        description: Access forbidden
500
        schema:
501
          $ref: "../swagger.yaml#/definitions/error"
502
      "404":
503
        description: Not found
504
        schema:
505
          $ref: "../swagger.yaml#/definitions/error"
506
      "409":
507
        description: Conflict
508
        schema:
509
          $ref: "../swagger.yaml#/definitions/error"
510
      "500":
511
        description: |
512
          Internal server error. Possible `error_code` attribute values:
513
514
          * `internal_server_error`
515
        schema:
516
          $ref: "../swagger.yaml#/definitions/error"
517
      "503":
518
        description: Under maintenance
519
        schema:
520
          $ref: "../swagger.yaml#/definitions/error"
521
    x-koha-authorization:
522
      permissions:
523
        editcatalogue: edit_catalogue
524
"/biblios/{biblio_id}/items/{item_id}/populate_empty_callnumbers":
525
  post:
526
    x-mojo-to: Biblios#populate_empty_callnumbers
527
    operationId: itemPopulateEmptyCallnumbers
528
    tags:
529
      - biblios
530
      - items
531
      - callnumbers
532
    summary: Populate empty item callnumber from biblio callnumber for a given item
533
    parameters:
534
      - $ref: "../swagger.yaml#/parameters/biblio_id_pp"
535
      - $ref: "../swagger.yaml#/parameters/item_id_pp"
536
    produces:
537
      - application/json
538
    responses:
539
      "200":
540
        description: Callnumbers updated
541
        schema:
542
          type: object
543
          properties:
544
            items_updated:
545
              description: Number of items updated
546
              type: integer
547
            callnumber:
548
              description: Callnumber added to updated items
549
              type: string
550
      "400":
551
        description: Bad request
552
        schema:
553
          $ref: "../swagger.yaml#/definitions/error"
554
      "401":
555
        description: Authentication required
556
        schema:
557
          $ref: "../swagger.yaml#/definitions/error"
558
      "403":
559
        description: Access forbidden
560
        schema:
561
          $ref: "../swagger.yaml#/definitions/error"
562
      "404":
563
        description: Not found
564
        schema:
565
          $ref: "../swagger.yaml#/definitions/error"
566
      "409":
567
        description: Conflict
568
        schema:
569
          $ref: "../swagger.yaml#/definitions/error"
570
      "500":
571
        description: |
572
          Internal server error. Possible `error_code` attribute values:
573
574
          * `internal_server_error`
575
        schema:
576
          $ref: "../swagger.yaml#/definitions/error"
577
      "503":
578
        description: Under maintenance
579
        schema:
580
          $ref: "../swagger.yaml#/definitions/error"
581
    x-koha-authorization:
582
      permissions:
583
        editcatalogue: edit_catalogue
465
"/biblios/{biblio_id}/items/{item_id}":
584
"/biblios/{biblio_id}/items/{item_id}":
466
  put:
585
  put:
467
    x-mojo-to: Biblios#update_item
586
    x-mojo-to: Biblios#update_item
(-)a/api/v1/swagger/swagger.yaml (+4 lines)
Lines 179-184 paths: Link Here
179
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1checkouts"
179
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1checkouts"
180
  "/biblios/{biblio_id}/items":
180
  "/biblios/{biblio_id}/items":
181
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
181
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
182
  "/biblios/{biblio_id}/items/populate_empty_callnumbers":
183
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items~1populate_empty_callnumbers"
184
  "/biblios/{biblio_id}/items/{item_id}/populate_empty_callnumbers":
185
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items~1{item_id}~1populate_empty_callnumbers"
182
  "/biblios/{biblio_id}/items/{item_id}":
186
  "/biblios/{biblio_id}/items/{item_id}":
183
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items~1{item_id}"
187
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items~1{item_id}"
184
  "/biblios/{biblio_id}/pickup_locations":
188
  "/biblios/{biblio_id}/pickup_locations":
(-)a/t/db_dependent/api/v1/biblios.t (-2 / +78 lines)
Lines 20-26 use Modern::Perl; Link Here
20
use utf8;
20
use utf8;
21
use Encode;
21
use Encode;
22
22
23
use Test::More tests => 13;
23
use Test::More tests => 14;
24
use Test::MockModule;
24
use Test::MockModule;
25
use Test::Mojo;
25
use Test::Mojo;
26
use Test::Warn;
26
use Test::Warn;
Lines 1811-1813 subtest 'update_item() tests' => sub { Link Here
1811
1811
1812
  $schema->storage->txn_rollback;
1812
  $schema->storage->txn_rollback;
1813
};
1813
};
1814
- 
1814
1815
subtest 'populate_empty_callnumbers() tests' => sub {
1816
  plan tests => 23;
1817
1818
  t::lib::Mocks::mock_preference( 'itemcallnumber', '' );
1819
1820
  $schema->storage->txn_begin;
1821
1822
    my $biblio = $builder->build_sample_biblio();
1823
1824
  my $patron = $builder->build_object(
1825
      {
1826
          class => 'Koha::Patrons',
1827
          value => { flags => 0 }
1828
      }
1829
  );
1830
  my $password = 'thePassword123';
1831
  $patron->set_password( { password => $password, skip_validation => 1 } );
1832
  my $userid = $patron->userid;
1833
1834
  my $biblio_id = $biblio->id;
1835
1836
  $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/populate_empty_callnumbers" => json => { external_id => 'something' })
1837
    ->status_is(403, 'Not enough permissions to update an item');
1838
1839
  # Add permissions
1840
  $builder->build(
1841
      {
1842
          source => 'UserPermission',
1843
          value  => {
1844
              borrowernumber => $patron->borrowernumber,
1845
              module_bit     => 9,
1846
              code           => 'edit_catalogue'
1847
          }
1848
      }
1849
  );
1850
1851
  $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
1852
    ->status_is(200, 'Item updated')
1853
    ->json_is('/items_updated', 0);
1854
1855
    my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
1856
    my $item_2 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
1857
    my $item_3 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
1858
    my $item_4 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{someCallNumber} } );
1859
1860
  my $item1_id = $item_1->id;
1861
1862
  $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} )
1863
    ->status_is(404, 'Callnumber fields not found');
1864
1865
  t::lib::Mocks::mock_preference( 'itemcallnumber', '245$a' );
1866
1867
  $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} )
1868
    ->status_is(200, 'Item updated')
1869
    ->json_is('/items_updated', 1)
1870
    ->json_is('/callnumber', 'Some boring read');
1871
1872
  $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
1873
    ->status_is(200, 'Items updated')
1874
    ->json_is('/items_updated', 2)
1875
    ->json_is('/callnumber', 'Some boring read');
1876
1877
  $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
1878
    ->status_is(200, 'Items updated')
1879
    ->json_is('/items_updated', 0);
1880
1881
  $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} )
1882
    ->status_is(200, 'Item updated')
1883
    ->json_is('/items_updated', 0);
1884
1885
  $t->post_ok("//$userid:$password@/api/v1/biblios/0/items/$item1_id/populate_empty_callnumbers" => json => {} )
1886
    ->status_is(404, 'Record not found');
1887
1888
  $schema->storage->txn_rollback;
1889
1890
};

Return to bug 34784