1 package Koha::REST::V1::TransferLimits;
3 # This file is part of Koha.
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Controller';
21 use Koha::Item::Transfer::Limits;
24 use Koha::Exceptions::TransferLimit;
26 use Scalar::Util qw( blessed );
28 use Try::Tiny qw( catch try );
32 Koha::REST::V1::TransferLimits - Koha REST API for handling libraries (V1)
42 Controller function that handles listing Koha::Item::Transfer::Limits objects
47 my $c = shift->openapi->valid_input or return;
50 my $limits = $c->objects->search( Koha::Item::Transfer::Limits->new );
51 return $c->render( status => 200, openapi => $limits );
54 $c->unhandled_exception( $_ );
60 Controller function that handles adding a new transfer limit
65 my $c = shift->openapi->valid_input or return;
68 my $params = $c->req->json;
69 my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $params );
71 if ( Koha::Item::Transfer::Limits->search( $transfer_limit->attributes_from_api($params) )->count == 0 ) {
72 $transfer_limit->store;
74 Koha::Exceptions::TransferLimit::Duplicate->throw();
79 openapi => $transfer_limit->to_api
83 if ( blessed $_ && $_->isa('Koha::Exceptions::TransferLimit::Duplicate') ) {
86 openapi => { error => "$_" }
90 $c->unhandled_exception($_);
96 Controller function that handles deleting a transfer limit
102 my $c = shift->openapi->valid_input or return;
104 my $transfer_limit = Koha::Item::Transfer::Limits->find( $c->param( 'limit_id' ) );
106 if ( not defined $transfer_limit ) {
107 return $c->render( status => 404, openapi => { error => "Transfer limit not found" } );
111 $transfer_limit->delete;
112 return $c->render( status => 204, openapi => '');
115 $c->unhandled_exception($_);
121 Controller function that handles adding a new transfer limit
126 my $c = shift->openapi->valid_input or return;
129 my $params = $c->req->json;
131 if ( $params->{item_type} && $params->{collection_code} ) {
135 error => "You can only pass 'item_type' or 'collection_code' at a time",
140 if ( ( C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' && $params->{collection_code} )
141 || ( C4::Context->preference("BranchTransferLimitsType") eq 'ccode' && $params->{item_type} ) )
146 error => $params->{collection_code}
147 ? "You passed 'collection_code' but configuration expects 'item_type'"
148 : "You passed 'item_type' but configuration expects 'collection_code'"
153 my ( @from_branches, @to_branches );
154 if ( $params->{from_library_id} ) {
155 @from_branches = ( $params->{from_library_id} );
157 if ( $params->{to_library_id} ) {
158 @to_branches = ( $params->{to_library_id} );
160 unless ( $params->{from_library_id} && $params->{to_library_id} ) {
161 my @library_ids = Koha::Libraries->search->get_column('branchcode');
162 @from_branches = @library_ids unless $params->{from_library_id};
163 @to_branches = @library_ids unless $params->{to_library_id};
166 my $dbic_params = Koha::Item::Transfer::Limits->new->attributes_from_api($params);
167 my %existing_limits =
168 map { sprintf( "%s:%s:%s:%s", $_->fromBranch, $_->toBranch, $_->itemtype // q{}, $_->ccode // q{} ) => 1 }
169 Koha::Item::Transfer::Limits->search($dbic_params)->as_list;
172 foreach my $from (@from_branches) {
173 foreach my $to (@to_branches) {
174 my $limit_params = {%$params};
176 $limit_params->{from_library_id} = $from;
177 $limit_params->{to_library_id} = $to;
179 next if $to eq $from;
182 "%s:%s:%s:%s", $limit_params->{from_branch_id} || q{},
183 $limit_params->{to_branch_id} || q{}, $limit_params->{item_type} || q{},
184 $limit_params->{collection_code} || q{}
186 next if exists $existing_limits{$key};
188 my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api($limit_params);
189 $transfer_limit->store;
190 push( @results, $transfer_limit->to_api() );
199 $c->unhandled_exception($_);
205 Controller function that handles batch deleting transfer limits
211 my $c = shift->openapi->valid_input or return;
214 my $params = $c->req->json;
215 my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $params );
216 my $search_params = $transfer_limit->unblessed;
218 Koha::Item::Transfer::Limits->search($search_params)->delete;
220 return $c->render( status => 204, openapi => q{} );
223 $c->unhandled_exception($_);