From a77fcfbd5d596674f3673c666c72ecbde35aadf8 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 2 Aug 2024 14:46:43 +0100 Subject: [PATCH] Bug 37256: Add 'PUT' for circulation rule sets --- Koha/REST/V1/CirculationRules.pm | 86 +++++++++++++++++++++ api/v1/swagger/paths/circulation_rules.yaml | 54 +++++++++++++ 2 files changed, 140 insertions(+) diff --git a/Koha/REST/V1/CirculationRules.pm b/Koha/REST/V1/CirculationRules.pm index 92c38d7f84a..bf308ca3463 100644 --- a/Koha/REST/V1/CirculationRules.pm +++ b/Koha/REST/V1/CirculationRules.pm @@ -178,4 +178,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; diff --git a/api/v1/swagger/paths/circulation_rules.yaml b/api/v1/swagger/paths/circulation_rules.yaml index 53eadc17afb..4b7e5e1cd65 100644 --- a/api/v1/swagger/paths/circulation_rules.yaml +++ b/api/v1/swagger/paths/circulation_rules.yaml @@ -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 -- 2.46.0