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

(-)a/Koha/REST/V1/RPC/Biblios.pm (+119 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;
43
44
    if ( $c->param('item_id') ) {
45
        $items = $items->search( { itemnumber => $c->param('item_id') } );
46
        return $c->render_resource_not_found("Item")
47
            unless $items->count;
48
    }
49
50
    $items = $items->search(
51
        {
52
            -or => [
53
                itemcallnumber => undef,
54
                itemcallnumber => q{},
55
            ]
56
        }
57
    );
58
59
    return try {
60
61
        my $cn_fields = C4::Context->preference('itemcallnumber');
62
        return $c->render(
63
            status  => 409,
64
            openapi => {
65
                error      => "Callnumber fields not found",
66
                error_code => 'missing_configuration',
67
            }
68
        ) unless $cn_fields;
69
70
        my $record = $biblio->record;
71
        my $callnumber;
72
73
        foreach my $callnumber_marc_field ( split( /,/, $cn_fields ) ) {
74
            my $callnumber_tag       = substr( $callnumber_marc_field, 0, 3 );
75
            my $callnumber_subfields = substr( $callnumber_marc_field, 3 );
76
77
            next unless $callnumber_tag && $callnumber_subfields;
78
79
            my $field = $record->field($callnumber_tag);
80
81
            next unless $field;
82
83
            $callnumber = $field->as_string( $callnumber_subfields, '' );
84
            last if $callnumber;
85
        }
86
87
        return $c->render(
88
            status  => 409,
89
            openapi => {
90
                error      => "Callnumber empty in bibliographic record",
91
                error_code => 'callnumber_empty',
92
            }
93
        ) unless $callnumber;
94
95
        return $c->render(
96
            status  => 200,
97
            openapi => {
98
                updated_items_count => 0,
99
                callnumber          => $callnumber
100
            },
101
        ) unless $items->count;
102
103
        my ($res) = $items->batch_update( { new_values => { itemcallnumber => $callnumber } } );
104
        my @modified_itemnumbers = @{ $res->{modified_itemnumbers} };
105
106
        return $c->render(
107
            status  => 200,
108
            openapi => {
109
                updated_items_count => scalar @modified_itemnumbers,
110
                callnumber          => $callnumber,
111
                modified_item_ids   => \@modified_itemnumbers,
112
            },
113
        );
114
    } catch {
115
        $c->unhandled_exception($_);
116
    };
117
}
118
119
1;
(-)a/t/db_dependent/api/v1/rpc/biblios.t (-1 / +138 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 => 34;
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' )
78
        ->json_is( '/error_code', q{missing_configuration} );
79
80
    t::lib::Mocks::mock_preference( 'itemcallnumber', '050ab' );
81
82
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
83
        ->status_is( 409, 'Callnumber empty' )
84
        ->json_is( '/error_code', q{callnumber_empty} );
85
86
    ModBiblio( $record, $biblio_id );
87
88
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
89
        ->status_is( 200, 'No items but request successful' )
90
        ->json_is( '/updated_items_count', 0 );
91
92
    my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
93
    my $item_2 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
94
    my $item_3 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } );
95
    my $item_4 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{someCallNumber} } );
96
97
    my $item1_id = $item_1->id;
98
99
    $t->post_ok(
100
        "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} )
101
        ->status_is( 200, 'Item updated' )
102
        ->json_is( '/updated_items_count', 1 )
103
        ->json_is( '/callnumber',          $expected_callnumber );
104
105
    my @items_to_update = ( $item_2->id, $item_3->id );
106
107
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
108
        ->status_is( 200, 'Items updated' )
109
        ->json_is( '/updated_items_count', 2 )
110
        ->json_is( '/callnumber',          $expected_callnumber )
111
        ->json_is( '/modified_item_ids',   \@items_to_update );
112
113
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
114
        ->status_is( 200, 'Items updated' )
115
        ->json_is( '/updated_items_count', 0 );
116
117
    $t->post_ok(
118
        "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} )
119
        ->status_is( 200, 'Item updated' )
120
        ->json_is( '/updated_items_count', 0 );
121
122
    $item_1->delete();
123
124
    $t->post_ok(
125
        "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} )
126
        ->status_is( 404, 'Item not found' )
127
        ->json_is( '/error'      => q{Item not found} )
128
        ->json_is( '/error_code' => q{not_found} );
129
130
    $biblio->delete();
131
132
    $t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} )
133
        ->status_is( 404, 'Record not found' )
134
        ->json_is( '/error'      => q{Bibliographic record not found} )
135
        ->json_is( '/error_code' => q{not_found} );
136
137
    $schema->storage->txn_rollback;
138
};

Return to bug 38226