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