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 534-539 Link Here
534
    x-koha-authorization:
534
    x-koha-authorization:
535
      permissions:
535
      permissions:
536
        editcatalogue: edit_catalogue
536
        editcatalogue: edit_catalogue
537
"/biblios/{biblio_id}/items/populate_empty_callnumbers":
538
  post:
539
    x-mojo-to: Biblios#populate_empty_callnumbers
540
    operationId: itemsPopulateEmptyCallnumbers
541
    tags:
542
      - biblios
543
      - items
544
      - callnumbers
545
    summary: Populate empty item callnumbers from biblio callnumber for all items on record with no callnumber
546
    parameters:
547
      - $ref: "../swagger.yaml#/parameters/biblio_id_pp"
548
    produces:
549
      - application/json
550
    responses:
551
      "200":
552
        description: Callnumbers updated
553
        schema:
554
          type: object
555
          properties:
556
            items_updated:
557
              description: Number of items updated
558
              type: integer
559
            callnumber:
560
              description: Callnumber added to updated items
561
              type: string
562
      "400":
563
        description: Bad request
564
        schema:
565
          $ref: "../swagger.yaml#/definitions/error"
566
      "401":
567
        description: Authentication required
568
        schema:
569
          $ref: "../swagger.yaml#/definitions/error"
570
      "403":
571
        description: Access forbidden
572
        schema:
573
          $ref: "../swagger.yaml#/definitions/error"
574
      "404":
575
        description: Not found
576
        schema:
577
          $ref: "../swagger.yaml#/definitions/error"
578
      "409":
579
        description: Conflict
580
        schema:
581
          $ref: "../swagger.yaml#/definitions/error"
582
      "500":
583
        description: |
584
          Internal server error. Possible `error_code` attribute values:
585
586
          * `internal_server_error`
587
        schema:
588
          $ref: "../swagger.yaml#/definitions/error"
589
      "503":
590
        description: Under maintenance
591
        schema:
592
          $ref: "../swagger.yaml#/definitions/error"
593
    x-koha-authorization:
594
      permissions:
595
        editcatalogue: edit_catalogue
596
"/biblios/{biblio_id}/items/{item_id}/populate_empty_callnumbers":
597
  post:
598
    x-mojo-to: Biblios#populate_empty_callnumbers
599
    operationId: itemPopulateEmptyCallnumbers
600
    tags:
601
      - biblios
602
      - items
603
      - callnumbers
604
    summary: Populate empty item callnumber from biblio callnumber for a given item
605
    parameters:
606
      - $ref: "../swagger.yaml#/parameters/biblio_id_pp"
607
      - $ref: "../swagger.yaml#/parameters/item_id_pp"
608
    produces:
609
      - application/json
610
    responses:
611
      "200":
612
        description: Callnumbers updated
613
        schema:
614
          type: object
615
          properties:
616
            items_updated:
617
              description: Number of items updated
618
              type: integer
619
            callnumber:
620
              description: Callnumber added to updated items
621
              type: string
622
      "400":
623
        description: Bad request
624
        schema:
625
          $ref: "../swagger.yaml#/definitions/error"
626
      "401":
627
        description: Authentication required
628
        schema:
629
          $ref: "../swagger.yaml#/definitions/error"
630
      "403":
631
        description: Access forbidden
632
        schema:
633
          $ref: "../swagger.yaml#/definitions/error"
634
      "404":
635
        description: Not found
636
        schema:
637
          $ref: "../swagger.yaml#/definitions/error"
638
      "409":
639
        description: Conflict
640
        schema:
641
          $ref: "../swagger.yaml#/definitions/error"
642
      "500":
643
        description: |
644
          Internal server error. Possible `error_code` attribute values:
645
646
          * `internal_server_error`
647
        schema:
648
          $ref: "../swagger.yaml#/definitions/error"
649
      "503":
650
        description: Under maintenance
651
        schema:
652
          $ref: "../swagger.yaml#/definitions/error"
653
    x-koha-authorization:
654
      permissions:
655
        editcatalogue: edit_catalogue
537
"/biblios/{biblio_id}/items/{item_id}":
656
"/biblios/{biblio_id}/items/{item_id}":
538
  put:
657
  put:
539
    x-mojo-to: Biblios#update_item
658
    x-mojo-to: Biblios#update_item
(-)a/api/v1/swagger/swagger.yaml (+4 lines)
Lines 231-236 paths: Link Here
231
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1checkouts"
231
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1checkouts"
232
  "/biblios/{biblio_id}/items":
232
  "/biblios/{biblio_id}/items":
233
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
233
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
234
  "/biblios/{biblio_id}/items/populate_empty_callnumbers":
235
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items~1populate_empty_callnumbers"
236
  "/biblios/{biblio_id}/items/{item_id}/populate_empty_callnumbers":
237
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items~1{item_id}~1populate_empty_callnumbers"
234
  "/biblios/{biblio_id}/items/{item_id}":
238
  "/biblios/{biblio_id}/items/{item_id}":
235
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items~1{item_id}"
239
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items~1{item_id}"
236
  "/biblios/{biblio_id}/pickup_locations":
240
  "/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', '245a' );
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 =>
1837
            { external_id => 'something' } )->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::lib::Mocks::mock_preference( 'itemcallnumber', '245a' );
1852
    $t->post_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
1853
        ->status_is( 200, 'Item updated' )->json_is( '/items_updated', 0 );
1854
    t::lib::Mocks::mock_preference( 'itemcallnumber', '' );
1855
1856
    my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
1857
    my $item_2 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
1858
    my $item_3 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
1859
    my $item_4 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{someCallNumber} } );
1860
1861
    my $item1_id = $item_1->id;
1862
1863
    $t->post_ok(
1864
        "//$userid:$password@/api/v1/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} )
1865
        ->status_is( 404, 'Callnumber fields not found' );
1866
1867
    t::lib::Mocks::mock_preference( 'itemcallnumber', '245$a' );
1868
1869
    $t->post_ok(
1870
        "//$userid:$password@/api/v1/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} )
1871
        ->status_is( 200, 'Item updated' )->json_is( '/items_updated', 1 )
1872
        ->json_is( '/callnumber', 'Some boring read' );
1873
1874
    $t->post_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
1875
        ->status_is( 200, 'Items updated' )->json_is( '/items_updated', 2 )
1876
        ->json_is( '/callnumber', 'Some boring read' );
1877
1878
    $t->post_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
1879
        ->status_is( 200, 'Items updated' )->json_is( '/items_updated', 0 );
1880
1881
    $t->post_ok(
1882
        "//$userid:$password@/api/v1/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} )
1883
        ->status_is( 200, 'Item updated' )->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