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

(-)a/Koha/REST/V1/BiblioAvailability.pm (+145 lines)
Line 0 Link Here
1
package Koha::REST::V1::BiblioAvailability;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use C4::Auth qw( haspermission );
23
24
use Koha::Exceptions;
25
26
use Koha::Availability::Hold;
27
use Koha::Availability::Search;
28
29
use Try::Tiny;
30
31
sub search {
32
    my ($c, $args, $cb) = @_;
33
34
    my @availabilities;
35
36
    return try {
37
        my $biblios = $args->{'biblionumber'};
38
        foreach my $biblionumber (@$biblios) {
39
            if (my $biblio = Koha::Biblios->find($biblionumber)) {
40
                push @availabilities, Koha::Availability::Search->biblio({
41
                    biblio => $biblio
42
                })->in_opac->swaggerize;
43
            }
44
        }
45
        return $c->$cb(\@availabilities, 200);
46
    }
47
    catch {
48
        if ( $_->isa('DBIx::Class::Exception') ) {
49
            return $c->$cb( { error => $_->{msg} }, 500 );
50
        }
51
        else {
52
            return $c->$cb(
53
                { error => "Something went wrong, check the logs." }, 500 );
54
        }
55
    };
56
}
57
58
sub hold {
59
    my ($c, $args, $cb) = @_;
60
61
    my @availabilities;
62
    my $user = $c->stash('koha.user');
63
    my $borrowernumber = $args->{'borrowernumber'};
64
    my $to_branch = $args->{'branchcode'};
65
    my $limit_items = $args->{'limit_items'};
66
    my $patron;
67
    my $librarian;
68
69
    return try {
70
        ($patron, $librarian) = _get_patron($c, $user, $borrowernumber);
71
72
        my $biblios = $args->{'biblionumber'};
73
        my $params = {
74
            patron => $patron,
75
        };
76
77
        $params->{'to_branch'} = $to_branch if $to_branch;
78
        $params->{'limit'} = $limit_items if $limit_items;
79
80
        foreach my $biblionumber (@$biblios) {
81
            if (my $biblio = Koha::Biblios->find($biblionumber)) {
82
                $params->{'biblio'} = $biblio;
83
                my $availability = Koha::Availability::Hold->biblio($params);
84
                unless ($librarian) {
85
                    push @availabilities, $availability->in_opac->swaggerize;
86
                } else {
87
                    push @availabilities, $availability->in_intranet->swaggerize;
88
                }
89
            }
90
        }
91
92
        return $c->$cb(\@availabilities, 200);
93
    }
94
    catch {
95
        if ( $_->isa('DBIx::Class::Exception') ) {
96
            return $c->$cb( { error => $_->{msg} }, 500 );
97
        }
98
        elsif ($_->isa('Koha::Exceptions::AuthenticationRequired')) {
99
            return $c->$cb(
100
                { error => "Authentication required." }, 401 );
101
        }
102
        elsif ($_->isa('Koha::Exceptions::NoPermission')) {
103
            return $c->$cb({
104
                error => "Authorization failure. Missing required permission(s).",
105
                required_permissions => $_->required_permissions}, 403 );
106
        }
107
        else {
108
            return $c->$cb(
109
                { error => "Something went wrong, check the logs. $_" }, 500 );
110
        }
111
    };
112
}
113
114
sub _get_patron {
115
    my ($c, $user, $borrowernumber) = @_;
116
117
    my $patron;
118
    my $librarian = 0;
119
120
    unless ($user) {
121
        Koha::Exceptions::AuthenticationRequired->throw;
122
    }
123
    if (haspermission($user->userid, { borrowers => 1 })) {
124
        $librarian = 1;
125
    }
126
    if ($borrowernumber) {
127
        if ($borrowernumber == $user->borrowernumber) {
128
            $patron = $user;
129
        } else {
130
            if ($librarian) {
131
                $patron = Koha::Patrons->find($borrowernumber);
132
            } else {
133
                Koha::Exceptions::NoPermission->throw(
134
                    required_permissions => "borrowers"
135
                );
136
            }
137
        }
138
    } else {
139
        $patron = $user;
140
    }
141
142
    return ($patron, $librarian);
143
}
144
145
1;
(-)a/Koha/REST/V1/ItemAvailability.pm (-1 / +187 lines)
Line 0 Link Here
0
- 
1
package Koha::REST::V1::ItemAvailability;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use C4::Auth qw( haspermission );
23
24
use Koha::Exceptions;
25
26
use Koha::Availability::Checkout;
27
use Koha::Availability::Hold;
28
use Koha::Availability::Search;
29
30
use Try::Tiny;
31
32
sub checkout {
33
    my ($c, $args, $cb) = @_;
34
35
    my @availabilities;
36
    my $user = $c->stash('koha.user');
37
    my $borrowernumber = $args->{'borrowernumber'};
38
    my $patron;
39
    my $librarian;
40
41
    return try {
42
        ($patron, $librarian) = _get_patron($c, $user, $borrowernumber);
43
44
        my $items = $args->{'itemnumber'};
45
        foreach my $itemnumber (@$items) {
46
            if (my $item = Koha::Items->find($itemnumber)) {
47
                push @availabilities, Koha::Availability::Checkout->item({
48
                    patron => $patron,
49
                    item => $item
50
                })->in_intranet->swaggerize;
51
            }
52
        }
53
        return $c->$cb(\@availabilities, 200);
54
    }
55
    catch {
56
        if ( $_->isa('DBIx::Class::Exception') ) {
57
            return $c->$cb( { error => $_->{msg} }, 500 );
58
        }
59
        elsif ($_->isa('Koha::Exceptions::AuthenticationRequired')) {
60
            return $c->$cb(
61
                { error => "Authentication required." }, 401 );
62
        }
63
        elsif ($_->isa('Koha::Exceptions::NoPermission')) {
64
            return $c->$cb({
65
                error => "Authorization failure. Missing required permission(s).",
66
                required_permissions => $_->required_permissions}, 403 );
67
        }
68
        else {
69
            return $c->$cb(
70
                { error => "Something went wrong, check the logs. $_" }, 500 );
71
        }
72
    };
73
}
74
75
sub hold {
76
    my ($c, $args, $cb) = @_;
77
78
    my @availabilities;
79
    my $user = $c->stash('koha.user');
80
    my $borrowernumber = $args->{'borrowernumber'};
81
    my $to_branch = $args->{'branchcode'};
82
    my $patron;
83
    my $librarian;
84
85
    return try {
86
        ($patron, $librarian) = _get_patron($c, $user, $borrowernumber);
87
88
        my $items = $args->{'itemnumber'};
89
        my $params = {
90
            patron => $patron,
91
        };
92
        if ($to_branch) {
93
            $params->{'to_branch'} = $to_branch;
94
        }
95
        foreach my $itemnumber (@$items) {
96
            if (my $item = Koha::Items->find($itemnumber)) {
97
                $params->{'item'} = $item;
98
                my $availability = Koha::Availability::Hold->item($params);
99
                unless ($librarian) {
100
                    push @availabilities, $availability->in_opac->swaggerize;
101
                } else {
102
                    push @availabilities, $availability->in_intranet->swaggerize;
103
                }
104
            }
105
        }
106
107
        return $c->$cb(\@availabilities, 200);
108
    }
109
    catch {
110
        if ( $_->isa('DBIx::Class::Exception') ) {
111
            return $c->$cb( { error => $_->{msg} }, 500 );
112
        }
113
        elsif ($_->isa('Koha::Exceptions::AuthenticationRequired')) {
114
            return $c->$cb(
115
                { error => "Authentication required." }, 401 );
116
        }
117
        elsif ($_->isa('Koha::Exceptions::NoPermission')) {
118
            return $c->$cb({
119
                error => "Authorization failure. Missing required permission(s).",
120
                required_permissions => $_->required_permissions}, 403 );
121
        }
122
        else {
123
            return $c->$cb(
124
                { error => "Something went wrong, check the logs. $_" }, 500 );
125
        }
126
    };
127
}
128
129
sub search {
130
    my ($c, $args, $cb) = @_;
131
132
    my @availabilities;
133
134
    return try {
135
        my $items = $args->{'itemnumber'};
136
        foreach my $itemnumber (@$items) {
137
            if (my $item = Koha::Items->find($itemnumber)) {
138
                push @availabilities, Koha::Availability::Search->item({
139
                    item => $item
140
                })->in_opac->swaggerize;
141
            }
142
        }
143
        return $c->$cb(\@availabilities, 200);
144
    }
145
    catch {
146
        if ( $_->isa('DBIx::Class::Exception') ) {
147
            return $c->$cb( { error => $_->{msg} }, 500 );
148
        }
149
        else {
150
            return $c->$cb(
151
                { error => "Something went wrong, check the logs. $_" }, 500 );
152
        }
153
    };
154
}
155
156
sub _get_patron {
157
    my ($c, $user, $borrowernumber) = @_;
158
159
    my $patron;
160
    my $librarian = 0;
161
162
    unless ($user) {
163
        Koha::Exceptions::AuthenticationRequired->throw;
164
    }
165
    if (haspermission($user->userid, { borrowers => 1 })) {
166
        $librarian = 1;
167
    }
168
    if ($borrowernumber) {
169
        if ($borrowernumber == $user->borrowernumber) {
170
            $patron = $user;
171
        } else {
172
            if ($librarian) {
173
                $patron = Koha::Patrons->find($borrowernumber);
174
            } else {
175
                Koha::Exceptions::NoPermission->throw(
176
                    required_permissions => "borrowers"
177
                );
178
            }
179
        }
180
    } else {
181
        $patron = $user;
182
    }
183
184
    return ($patron, $librarian);
185
}
186
187
1;

Return to bug 16826