From f4342c922d422aa1687f0e976e57eaa4bb8901cd Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 2 Aug 2024 14:19:18 +0100 Subject: [PATCH] Bug 37256: Defined 'context' of circulation rules This is a follow-up to the previous bug exposing circulation rules over the API. We formally define a definition for a circulation rules set including a new 'context' object the defines the context, library_id, patron_category_id and item_type_id combination of the rules returned. This is just a patch I missed in the last submission that I'd meant to add and it's important for the PUT part of this patchset. Sponsored-by: Glasgow Colleges Library Group Signed-off-by: George Harkins Signed-off-by: Kyle M Hall Signed-off-by: Katrin Fischer --- Koha/REST/V1/CirculationRules.pm | 15 +++++++++++- .../definitions/circulation_rules.yaml | 19 +++++++++++++++ api/v1/swagger/paths/circulation_rules.yaml | 4 ++-- api/v1/swagger/swagger.yaml | 4 +++- t/db_dependent/api/v1/circulation_rules.t | 23 ++++++++++++------- 5 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 api/v1/swagger/definitions/circulation_rules.yaml diff --git a/Koha/REST/V1/CirculationRules.pm b/Koha/REST/V1/CirculationRules.pm index b84803a1bc..4893f3846a 100644 --- a/Koha/REST/V1/CirculationRules.pm +++ b/Koha/REST/V1/CirculationRules.pm @@ -151,12 +151,25 @@ sub list_rules { { select => $select, as => $as, - group_by => [ 'branchcode', 'categorycode', 'itemtype' ] + group_by => [ 'branchcode', 'categorycode', 'itemtype' ], + order_by => [ 'branchcode', 'categorycode', 'itemtype' ], } )->unblessed; } + # Map context into rules + @{$rules} = map { + my %new_rule = %$_; + my %context = ( + "library_id" => delete $new_rule{"branchcode"} // "*", + "patron_category_id" => delete $new_rule{"categorycode"} // "*", + "item_type_id" => delete $new_rule{"itemtype"} // "*", + ); + $new_rule{"context"} = \%context; + \%new_rule; + } @{$rules}; + return $c->render( status => 200, openapi => $rules diff --git a/api/v1/swagger/definitions/circulation_rules.yaml b/api/v1/swagger/definitions/circulation_rules.yaml new file mode 100644 index 0000000000..13160f30f4 --- /dev/null +++ b/api/v1/swagger/definitions/circulation_rules.yaml @@ -0,0 +1,19 @@ +--- +type: object +properties: + context: + type: object + properties: + patron_category_id: + type: string + description: Patron category id + library_id: + type: string + description: Library branch id + item_type_id: + type: string + description: Item type id + description: Context of the ruleset +additionalProperties: true +required: + - context \ No newline at end of file diff --git a/api/v1/swagger/paths/circulation_rules.yaml b/api/v1/swagger/paths/circulation_rules.yaml index 1c689e2ee0..c7240a4918 100644 --- a/api/v1/swagger/paths/circulation_rules.yaml +++ b/api/v1/swagger/paths/circulation_rules.yaml @@ -40,7 +40,7 @@ schema: type: array items: - type: object + $ref: "../swagger.yaml#/definitions/circulation_rules" "400": description: Bad request schema: @@ -104,4 +104,4 @@ $ref: "../swagger.yaml#/definitions/error" x-koha-authorization: permissions: - - circulate: circulate_remaining_permissions + - circulate: circulate_remaining_permissions \ No newline at end of file diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index ff7b94a174..398b192a76 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -12,6 +12,8 @@ definitions: $ref: ./definitions/authorised_value.yaml authorised_value_category: $ref: ./definitions/authorised_value_category.yaml + circulation_rules: + $ref: ./definitions/circulation_rules.yaml identity_provider: $ref: ./definitions/identity_provider.yaml identity_provider_domain: @@ -1289,4 +1291,4 @@ tags: x-displayName: Two factor authentication - description: "Manage vendors for the acquisitions module\n" name: vendors - x-displayName: Vendors + x-displayName: Vendors \ No newline at end of file diff --git a/t/db_dependent/api/v1/circulation_rules.t b/t/db_dependent/api/v1/circulation_rules.t index 8be6e47fa4..4e5b1efa72 100755 --- a/t/db_dependent/api/v1/circulation_rules.t +++ b/t/db_dependent/api/v1/circulation_rules.t @@ -143,8 +143,12 @@ subtest 'list_rules() tests' => sub { ->json_is( '/0/finedays' => 5, "Default finedays rule returned when no library is added to request query" ); # Limit to only rules we're interested in - $t->get_ok("//$userid:$password@/api/v1/circulation_rules?rules=fine,finedays")->status_is(200) - ->json_is( '/0' => { fine => 2, finedays => 5 }, "Only the two rules we asked for are returned" ); + $t->get_ok("//$userid:$password@/api/v1/circulation_rules?rules=fine,finedays")->status_is(200)->json_is( + '/0' => { + context => { item_type_id => '*', patron_category_id => '*', library_id => '*' }, fine => 2, finedays => 5 + }, + "Only the two rules we asked for are returned" + ); # Warn on unsupported query parameter $t->get_ok("//$userid:$password@/api/v1/circulation_rules?rules_blah=blah")->status_is(400) @@ -236,9 +240,9 @@ subtest 'list_rules() tests' => sub { # First rule set should march default, default, default if ( $index == 0 ) { - ok( $pointer->get('/branchcode') eq "*" - && $pointer->get('/itemtype') eq '*' - && $pointer->get('/categorycode') eq '*', "Default rules returned first" ); + ok( $pointer->get('/context/library_id') eq "*" + && $pointer->get('/context/item_type_id') eq '*' + && $pointer->get('/context/patron_category_id') eq '*', "Default rules returned first" ); } # Iterate over the list of expected keys for each hash @@ -265,9 +269,12 @@ subtest 'list_rules() tests' => sub { # First (and only) rule set should match branchcode, default, default. if ( $index == 0 ) { - ok( $pointer->get('/branchcode') eq $branchcode - && $pointer->get('/itemtype') eq '*' - && $pointer->get('/categorycode') eq '*', "Branchcode rule set returned when filtered" ); + ok( + $pointer->get('/context/library_id') eq $branchcode + && $pointer->get('/context/item_type_id') eq '*' + && $pointer->get('/context/patron_category_id') eq '*', + "Branchcode rule set returned when filtered" + ); } # Iterate over the list of expected keys for each hash