Bug 37256: Add 'PUT' for circulation rule sets

Sponsored-by: Glasgow Colleges Library Group <https://library.cityofglasgowcollege.ac.uk>
Signed-off-by: George Harkins <George.Harkins@cityofglasgowcollege.ac.uk>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Martin Renvoize 2024-08-02 14:46:43 +01:00 committed by Katrin Fischer
parent df21c0618e
commit adae95debe
Signed by: kfischer
GPG key ID: 0EF6E2C03357A834
2 changed files with 140 additions and 0 deletions

View file

@ -179,4 +179,90 @@ sub list_rules {
};
}
=head3 set_rules
Set rules for the given patron/item/branch combination
=cut
sub set_rules {
my $c = shift->openapi->valid_input or return;
return try {
my $body = $c->req->json;
my $item_type = $body->{context}->{item_type_id};
my $branchcode = $body->{context}->{library_id};
my $patron_category = $body->{context}->{patron_category_id};
if ( $item_type eq '*' ) {
$item_type = undef;
} else {
my $type = Koha::ItemTypes->find($item_type);
return $c->render_invalid_parameter_value(
{
path => '/body/context/item_type_id',
values => {
uri => '/api/v1/item_types',
field => 'item_type_id'
}
}
) unless $type;
}
if ( $branchcode eq '*' ) {
$branchcode = undef;
} else {
my $library = Koha::Libraries->find($branchcode);
return $c->render_invalid_parameter_value(
{
path => '/body/context/library_id',
values => {
uri => '/api/v1/libraries',
field => 'library_id'
}
}
) unless $library;
}
if ( $patron_category eq '*' ) {
$patron_category = undef;
} else {
my $category = Koha::Patron::Categories->find($patron_category);
return $c->render_invalid_parameter_value(
{
path => '/body/context/patron_category_id',
values => {
uri => '/api/v1/patron_categories',
field => 'patron_category_id'
}
}
) unless $category;
}
my $rules = {%$body};
delete $rules->{context};
my $new_rules = Koha::CirculationRules->set_rules(
{
categorycode => $patron_category,
itemtype => $item_type,
branchcode => $branchcode,
rules => $rules,
}
);
# TODO: Add error handling for rule scope exceptions thrown in Koha::CirculationRules::set_rule
my $return = { map { $_->rule_name => $_->rule_value } @{$new_rules} };
$return->{context} =
{ library_id => $branchcode, patron_category_id => $patron_category, item_type_id => $item_type };
return $c->render(
status => 200,
openapi => $return
);
}
}
1;

View file

@ -67,6 +67,60 @@
x-koha-authorization:
permissions:
- circulate: circulate_remaining_permissions
put:
x-mojo-to: CirculationRules#set_rules
operationId: setCirculationRules
tags:
- circulation_rules
summary: Update circulation rules
parameters:
- name: body
in: body
description: A JSON object containing new information about circulation rules
required: true
schema:
$ref: "../swagger.yaml#/definitions/circulation_rules"
consumes:
- application/json
produces:
- application/json
responses:
"200":
description: A successfully updated circulation rules set
schema:
items:
$ref: "../swagger.yaml#/definitions/circulation_rules"
"400":
description: |
Bad request.
schema:
$ref: "../swagger.yaml#/definitions/error"
"403":
description: Access forbidden
schema:
$ref: "../swagger.yaml#/definitions/error"
"404":
description: Resource not found
schema:
$ref: "../swagger.yaml#/definitions/error"
"409":
description: Conflict in updating resource
schema:
$ref: "../swagger.yaml#/definitions/error"
"500":
description: |
Internal server error. Possible `error_code` attribute values:
* `internal_server_error`
schema:
$ref: "../swagger.yaml#/definitions/error"
"503":
description: Under maintenance
schema:
$ref: "../swagger.yaml#/definitions/error"
x-koha-authorization:
permissions:
- circulate: circulate_remaining_permissions
/circulation_rules/kinds:
get:
x-mojo-to: CirculationRules#get_kinds