Lines 178-181
sub list_rules {
Link Here
|
178 |
}; |
178 |
}; |
179 |
} |
179 |
} |
180 |
|
180 |
|
|
|
181 |
sub set_rules { |
182 |
my $c = shift->openapi->valid_input or return; |
183 |
|
184 |
return try { |
185 |
my $body = $c->req->json; |
186 |
|
187 |
my $item_type = $body->{context}->{item_type_id}; |
188 |
my $branchcode = $body->{context}->{library_id}; |
189 |
my $patron_category = $body->{context}->{patron_category_id}; |
190 |
|
191 |
if ( $item_type eq '*' ) { |
192 |
$item_type = undef; |
193 |
} else { |
194 |
my $type = Koha::ItemTypes->find($item_type); |
195 |
return $c->render_invalid_parameter_value( |
196 |
{ |
197 |
path => '/body/context/item_type_id', |
198 |
values => { |
199 |
uri => '/api/v1/item_types', |
200 |
field => 'item_type_id' |
201 |
} |
202 |
} |
203 |
) unless $type; |
204 |
} |
205 |
|
206 |
if ( $branchcode eq '*' ) { |
207 |
$branchcode = undef; |
208 |
} else { |
209 |
my $library = Koha::Libraries->find($branchcode); |
210 |
return $c->render_invalid_parameter_value( |
211 |
{ |
212 |
path => '/body/context/library_id', |
213 |
values => { |
214 |
uri => '/api/v1/libraries', |
215 |
field => 'library_id' |
216 |
} |
217 |
} |
218 |
) unless $library; |
219 |
} |
220 |
|
221 |
if ( $patron_category eq '*' ) { |
222 |
$patron_category = undef; |
223 |
} else { |
224 |
my $category = Koha::Patron::Categories->find($patron_category); |
225 |
return $c->render_invalid_parameter_value( |
226 |
{ |
227 |
path => '/body/context/patron_category_id', |
228 |
values => { |
229 |
uri => '/api/v1/patron_categories', |
230 |
field => 'patron_category_id' |
231 |
} |
232 |
} |
233 |
) unless $category; |
234 |
} |
235 |
|
236 |
my $rules = %{$body}; |
237 |
delete $rules->{context}; |
238 |
|
239 |
my $new_rules = Koha::CirculationRules->set_rules( |
240 |
$body->{rules}, |
241 |
{ |
242 |
categorycode => $patron_category, |
243 |
itemtype => $item_type, |
244 |
branchcode => $branchcode |
245 |
} |
246 |
); |
247 |
|
248 |
my $return = { map { $_->rule_name => $_->rule_value } @{$new_rules} }; |
249 |
$return->{context} = { library_id => $branchcode, patron_category_id => $patron_category, item_type_id => $item_type }; |
250 |
|
251 |
return $c->render( |
252 |
status => 200, |
253 |
openapi => $return |
254 |
); |
255 |
} |
256 |
} |
257 |
|
181 |
1; |
258 |
1; |