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

(-)a/Koha/REST/V1/RPC/Biblios.pm (+114 lines)
Line 0 Link Here
1
package Koha::REST::V1::RPC::Biblios;
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 Try::Tiny qw( catch try );
23
24
=head1 API
25
26
=head2 Methods
27
28
=head3 populate_empty_callnumbers
29
30
Controller function that handles populating empty callnumbers
31
32
=cut
33
34
sub populate_empty_callnumbers {
35
    my $c = shift->openapi->valid_input or return;
36
37
    my $biblio = Koha::Biblios->find( $c->param('biblio_id') );
38
39
    return $c->render_resource_not_found("Bibliographic record")
40
        unless $biblio;
41
42
    my $items = $biblio->items->search(
43
        {
44
            -or => [
45
                itemcallnumber => undef,
46
                itemcallnumber => q{},
47
            ]
48
        }
49
    );
50
51
    $items = $items->search( { itemnumber => $c->param('item_id') } )
52
        if $c->param('item_id');
53
54
    return try {
55
56
        my $cn_fields = C4::Context->preference('itemcallnumber');
57
        return $c->render(
58
            status  => 409,
59
            openapi => {
60
                error      => "Callnumber fields not found",
61
                error_code => 'missing_configuration',
62
            }
63
        ) unless $cn_fields;
64
65
        my $record = $biblio->record;
66
        my $callnumber;
67
68
        foreach my $callnumber_marc_field ( split( /,/, $cn_fields ) ) {
69
            my $callnumber_tag       = substr( $callnumber_marc_field, 0, 3 );
70
            my $callnumber_subfields = substr( $callnumber_marc_field, 3 );
71
72
            next unless $callnumber_tag && $callnumber_subfields;
73
74
            my $field = $record->field($callnumber_tag);
75
76
            next unless $field;
77
78
            $callnumber = $field->as_string( $callnumber_subfields, '' );
79
            last if $callnumber;
80
        }
81
82
        return $c->render(
83
            status  => 409,
84
            openapi => {
85
                error      => "Callnumber empty in bibliographic record",
86
                error_code => 'callnumber_empty',
87
            }
88
        ) unless $callnumber;
89
90
        return $c->render(
91
            status  => 200,
92
            openapi => {
93
                updated_items_count => 0,
94
                callnumber          => $callnumber
95
            },
96
        ) unless $items->count;
97
98
        my ($res) = $items->batch_update( { new_values => { itemcallnumber => $callnumber } } );
99
        my @modified_itemnumbers = @{ $res->{modified_itemnumbers} };
100
101
        return $c->render(
102
            status  => 200,
103
            openapi => {
104
                updated_items_count => scalar @modified_itemnumbers,
105
                callnumber          => $callnumber,
106
                modified_item_ids   => \@modified_itemnumbers,
107
            },
108
        );
109
    } catch {
110
        $c->unhandled_exception($_);
111
    };
112
}
113
114
1;
(-)a/t/db_dependent/api/v1/rpc/biblios.t (-1 / +121 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 => 1;
21
use Test::Mojo;
22
23
use C4::Biblio qw{ModBiblio};
24
25
use t::lib::Mocks;
26
use t::lib::TestBuilder;
27
28
my $schema  = Koha::Database->new->schema;
29
my $builder = t::lib::TestBuilder->new;
30
31
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
32
33
my $t = Test::Mojo->new('Koha::REST::V1');
34
35
subtest 'populate_empty_callnumbers() tests' => sub {
36
37
    plan tests => 30;
38
39
    $schema->storage->txn_begin;
40
41
    my $biblio = $builder->build_sample_biblio();
42
    my $record = $biblio->record;
43
44
    my $cn = q{PA522};
45
    my $in = q{.M38 1993};
46
47
    $record->insert_fields_ordered( MARC::Field->new( '050', ' ', ' ', a => $cn, b => $in ) );
48
49
    my $expected_callnumber = $cn . $in;
50
51
    my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } );
52
53
    my $password = 'thePassword123';
54
    $patron->set_password( { password => $password, skip_validation => 1 } );
55
    my $userid = $patron->userid;
56
57
    my $biblio_id = $biblio->id;
58
59
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json =>
60
            { external_id => 'something' } )->status_is( 403, 'Not enough permissions to update an item' );
61
62
    # Add permissions
63
    $builder->build(
64
        {
65
            source => 'UserPermission',
66
            value  => {
67
                borrowernumber => $patron->borrowernumber,
68
                module_bit     => 9,
69
                code           => 'edit_catalogue'
70
            }
71
        }
72
    );
73
74
    t::lib::Mocks::mock_preference( 'itemcallnumber', '' );
75
76
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
77
        ->status_is( 409, 'Wrong configuration' )->json_is( '/error_code', q{missing_configuration} );
78
79
    t::lib::Mocks::mock_preference( 'itemcallnumber', '050ab' );
80
81
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
82
        ->status_is( 409, 'Callnumber empty' )->json_is( '/error_code', q{callnumber_empty} );
83
84
    ModBiblio( $record, $biblio_id );
85
86
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
87
        ->status_is( 200, 'No items but request successful' )->json_is( '/updated_items_count', 0 );
88
89
    my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
90
    my $item_2 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
91
    my $item_3 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
92
    my $item_4 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{someCallNumber} } );
93
94
    my $item1_id = $item_1->id;
95
96
    $t->post_ok(
97
        "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} )
98
        ->status_is( 200, 'Item updated' )->json_is( '/updated_items_count', 1 )
99
        ->json_is( '/callnumber', $expected_callnumber );
100
101
    my @items_to_update = ( $item_2->id, $item_3->id );
102
103
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
104
        ->status_is( 200, 'Items updated' )->json_is( '/updated_items_count', 2 )
105
        ->json_is( '/callnumber', $expected_callnumber )->json_is( '/modified_item_ids', \@items_to_update );
106
107
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
108
        ->status_is( 200, 'Items updated' )->json_is( '/updated_items_count', 0 );
109
110
    $t->post_ok(
111
        "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} )
112
        ->status_is( 200, 'Item updated' )->json_is( '/updated_items_count', 0 );
113
114
    $biblio->delete;
115
116
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
117
        ->status_is( 404, 'Record not found' )->json_is( '/error' => q{Bibliographic record not found} )
118
        ->json_is( '/error_code' => q{not_found} );
119
120
    $schema->storage->txn_rollback;
121
};

Return to bug 38226