Bugzilla – Attachment 178606 Details for
Bug 38924
Introduce an organization level loan 'Quota' system for Koha
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 38924: Add quota usage api endpoint
Bug-38924-Add-quota-usage-api-endpoint.patch (text/plain), 10.92 KB, created by
Martin Renvoize (ashimema)
on 2025-02-24 16:30:15 UTC
(
hide
)
Description:
Bug 38924: Add quota usage api endpoint
Filename:
MIME Type:
Creator:
Martin Renvoize (ashimema)
Created:
2025-02-24 16:30:15 UTC
Size:
10.92 KB
patch
obsolete
>From a0c2d1f5aaa22af472ddfdec584ea3c8a7acd437 Mon Sep 17 00:00:00 2001 >From: Martin Renvoize <martin.renvoize@ptfs-europe.com> >Date: Mon, 10 Feb 2025 17:41:05 +0000 >Subject: [PATCH] Bug 38924: Add quota usage api endpoint > >--- > Koha/Patron/Quota/Usage.pm | 25 ++++++++ > Koha/REST/V1/Patrons/Quotas.pm | 66 +++++++++++++-------- > Koha/Schema/Result/PatronQuotaUsage.pm | 8 +-- > api/v1/swagger/definitions/quota_usage.yaml | 37 ++++++++++++ > api/v1/swagger/paths/patrons_quotas.yaml | 65 ++++++++++++++++++++ > api/v1/swagger/swagger.yaml | 5 ++ > 6 files changed, 178 insertions(+), 28 deletions(-) > create mode 100644 api/v1/swagger/definitions/quota_usage.yaml > >diff --git a/Koha/Patron/Quota/Usage.pm b/Koha/Patron/Quota/Usage.pm >index 7d3696fc7f7..b7fa6038473 100644 >--- a/Koha/Patron/Quota/Usage.pm >+++ b/Koha/Patron/Quota/Usage.pm >@@ -39,6 +39,20 @@ sub quota { > return Koha::Patron::Quota->_new_from_dbic($rs); > } > >+=head3 checkout >+ >+ my $checkout = $usage->checkout; >+ >+=cut >+ >+sub checkout { >+ my ($self) = @_; >+ >+ my $checkout_rs = $self->_result->checkout; >+ return unless $checkout_rs; >+ return Koha::Checkout->_new_from_dbic($checkout_rs); >+} >+ > =head3 store > > Overloaded I<store> method to set implement issue_id foreign key in code >@@ -62,6 +76,17 @@ sub store { > return $self->SUPER::store(); > } > >+=head3 to_api_mapping >+ >+This method returns the mapping for representing a Koha::Patron::Quota::Usage >+object on the API. >+ >+=cut >+ >+sub to_api_mapping { >+ return { issue_id => 'checkout_id' }; >+} >+ > =head2 Internal methods > > =head3 _type >diff --git a/Koha/REST/V1/Patrons/Quotas.pm b/Koha/REST/V1/Patrons/Quotas.pm >index 3d1ac6a879e..b2c041fa586 100644 >--- a/Koha/REST/V1/Patrons/Quotas.pm >+++ b/Koha/REST/V1/Patrons/Quotas.pm >@@ -21,7 +21,7 @@ sub list { > my $c = shift->openapi->valid_input or return; > > return try { >- my $patron_id = $c->param('patron_id'); >+ my $patron_id = $c->param('patron_id'); > my $only_active = $c->param('only_active'); > > # Remove params we've handled >@@ -30,7 +30,7 @@ sub list { > my $quotas_set = Koha::Patron::Quotas->new; > > if ($patron_id) { >- $quotas_set = $quotas_set->search({ patron_id => $patron_id }); >+ $quotas_set = $quotas_set->search( { patron_id => $patron_id } ); > } > > if ($only_active) { >@@ -38,11 +38,10 @@ sub list { > } > > return $c->render( >- status => 200, >+ status => 200, > openapi => $c->objects->search($quotas_set) > ); >- } >- catch { >+ } catch { > $c->unhandled_exception($_); > }; > } >@@ -58,12 +57,11 @@ sub get { > > return try { > my $quota = Koha::Patron::Quotas->find( $c->param('quota_id') ); >- >+ > return $c->render_resource_not_found("Quota") unless $quota; > > return $c->render( status => 200, openapi => $c->objects->to_api($quota) ); >- } >- catch { >+ } catch { > $c->unhandled_exception($_); > }; > } >@@ -80,18 +78,17 @@ sub add { > return try { > my $body = $c->req->json; > $body->{patron_id} = $c->param('patron_id'); >- >+ > my $quota = Koha::Patron::Quota->new_from_api($body); > $quota->store; > >- $c->res->headers->location($c->req->url->to_string . '/' . $quota->id); >+ $c->res->headers->location( $c->req->url->to_string . '/' . $quota->id ); > return $c->render( > status => 201, > openapi => $c->objects->to_api($quota) > ); >- } >- catch { >- if (ref($_) eq 'Koha::Exceptions::Quota::Clash') { >+ } catch { >+ if ( ref($_) eq 'Koha::Exceptions::Quota::Clash' ) { > return $c->render( > status => 409, > openapi => { error => "Quota period overlaps with existing quota" } >@@ -110,17 +107,16 @@ Controller method for updating a quota > sub update { > my $c = shift->openapi->valid_input or return; > >- my $quota = Koha::Patron::Quotas->find($c->param('quota_id')); >- >+ my $quota = Koha::Patron::Quotas->find( $c->param('quota_id') ); >+ > return $c->render_resource_not_found("Quota") unless $quota; > > return try { >- $quota->set_from_api($c->req->json); >+ $quota->set_from_api( $c->req->json ); > $quota->store; >- return $c->render(status => 200, openapi => $c->objects->to_api($quota)); >- } >- catch { >- if (ref($_) eq 'Koha::Exceptions::Quota::Clash') { >+ return $c->render( status => 200, openapi => $c->objects->to_api($quota) ); >+ } catch { >+ if ( ref($_) eq 'Koha::Exceptions::Quota::Clash' ) { > return $c->render( > status => 409, > openapi => { error => "Quota period overlaps with existing quota" } >@@ -139,15 +135,37 @@ Controller method for deleting a quota > sub delete { > my $c = shift->openapi->valid_input or return; > >- my $quota = Koha::Patron::Quotas->find($c->param('quota_id')); >- >+ my $quota = Koha::Patron::Quotas->find( $c->param('quota_id') ); >+ > return $c->render_resource_not_found("Quota") unless $quota; > > return try { > $quota->delete; > return $c->render_resource_deleted; >- } >- catch { >+ } catch { >+ $c->unhandled_exception($_); >+ }; >+} >+ >+=head3 get_usage >+ >+Controller method for getting usage for a quota >+ >+=cut >+ >+sub get_usage { >+ my $c = shift->openapi->valid_input or return; >+ >+ my $quota = Koha::Patron::Quotas->find( $c->param('quota_id') ); >+ return $c->render_resource_not_found("Quota") unless $quota; >+ >+ return try { >+ my $usage_set = $quota->usages; >+ return $c->render( >+ status => 200, >+ openapi => $c->objects->search($usage_set) >+ ); >+ } catch { > $c->unhandled_exception($_); > }; > } >diff --git a/Koha/Schema/Result/PatronQuotaUsage.pm b/Koha/Schema/Result/PatronQuotaUsage.pm >index c2d3983ddbf..7560dd5a1a7 100644 >--- a/Koha/Schema/Result/PatronQuotaUsage.pm >+++ b/Koha/Schema/Result/PatronQuotaUsage.pm >@@ -144,7 +144,7 @@ __PACKAGE__->belongs_to( > # Created by DBIx::Class::Schema::Loader v0.07051 @ 2025-02-07 11:22:06 > # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:WSsSTNOfNnTSyP3AGS9NXA > >-=head2 issue >+=head2 checkout > > Type: belongs_to > >@@ -153,12 +153,12 @@ Related object: L<Koha::Schema::Result::Issue> > =cut > > __PACKAGE__->belongs_to( >- issue => 'Koha::Schema::Result::Issue', >+ checkout => 'Koha::Schema::Result::Issue', > 'issue_id', > { join_type => 'left' } > ); > >-=head2 old_issue >+=head2 old_checkout > > Type: belongs_to > >@@ -167,7 +167,7 @@ Related object: L<Koha::Schema::Result::OldIssue> > =cut > > __PACKAGE__->belongs_to( >- old_issue => 'Koha::Schema::Result::OldIssue', >+ old_checkout => 'Koha::Schema::Result::OldIssue', > 'issue_id', > { join_type => 'left' } > ); >diff --git a/api/v1/swagger/definitions/quota_usage.yaml b/api/v1/swagger/definitions/quota_usage.yaml >new file mode 100644 >index 00000000000..4012d600f83 >--- /dev/null >+++ b/api/v1/swagger/definitions/quota_usage.yaml >@@ -0,0 +1,37 @@ >+--- >+type: object >+properties: >+ id: >+ type: integer >+ description: Internal quota identifier >+ readOnly: true >+ patron_quota_id: >+ type: integer >+ description: Link to quota >+ patron_id: >+ type: integer >+ description: Link to patron >+ patron: >+ type: object >+ checkout_id: >+ type: >+ - integer >+ - "null" >+ description: Link to issue >+ checkout: >+ type: >+ - object >+ - "null" >+ creation_date: >+ type: string >+ format: date-time >+ description: Date of increment >+ type: >+ type: string >+ enum: >+ - 'ISSUE' >+ - 'RENEW' >+additionalProperties: false >+required: >+ - id >+ >diff --git a/api/v1/swagger/paths/patrons_quotas.yaml b/api/v1/swagger/paths/patrons_quotas.yaml >index 5de865dff7e..db09a6ac101 100644 >--- a/api/v1/swagger/paths/patrons_quotas.yaml >+++ b/api/v1/swagger/paths/patrons_quotas.yaml >@@ -197,3 +197,68 @@ > x-koha-authorization: > permissions: > borrowers: manage_borrower_quotas >+/patrons/{patron_id}/quotas/{quota_id}/usages: >+ get: >+ x-mojo-to: Patrons::Quotas#get_usage >+ operationId: getPatronQuotaUsage >+ tags: >+ - quotas >+ summary: Get quota usage details >+ parameters: >+ - $ref: "../swagger.yaml#/parameters/patron_id_pp" >+ - name: quota_id >+ in: path >+ description: Internal quota identifier >+ required: true >+ type: string >+ - $ref: "../swagger.yaml#/parameters/page" >+ - $ref: "../swagger.yaml#/parameters/per_page" >+ - $ref: "../swagger.yaml#/parameters/match" >+ - $ref: "../swagger.yaml#/parameters/order_by" >+ - $ref: "../swagger.yaml#/parameters/q_param" >+ - $ref: "../swagger.yaml#/parameters/q_body" >+ - $ref: "../swagger.yaml#/parameters/request_id_header" >+ - name: x-koha-embed >+ in: header >+ required: false >+ description: Embed list sent as a request header >+ type: array >+ items: >+ type: string >+ enum: >+ - checkout >+ - checkout.item >+ - checkout.item.biblio >+ - patron >+ collectionFormat: csv >+ produces: >+ - application/json >+ responses: >+ "200": >+ description: A quota usage >+ schema: >+ type: array >+ items: >+ $ref: "../swagger.yaml#/definitions/quota_usage" >+ "400": >+ description: | >+ Bad request. Possible `error_code` attribute values: >+ * `bad_request` >+ * `invalid_query` >+ schema: >+ $ref: "../swagger.yaml#/definitions/error" >+ "403": >+ description: Access forbidden >+ schema: >+ $ref: "../swagger.yaml#/definitions/error" >+ "404": >+ description: Quota not found >+ schema: >+ $ref: "../swagger.yaml#/definitions/error" >+ "500": >+ description: Internal server error >+ schema: >+ $ref: "../swagger.yaml#/definitions/error" >+ x-koha-authorization: >+ permissions: >+ borrowers: view_borrower_quotas >diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml >index 9c64afaf02b..ccaf8f09d33 100644 >--- a/api/v1/swagger/swagger.yaml >+++ b/api/v1/swagger/swagger.yaml >@@ -162,6 +162,8 @@ definitions: > $ref: ./definitions/quote.yaml > quota: > $ref: ./definitions/quota.yaml >+ quota_usage: >+ $ref: ./definitions/quota_usage.yaml > recall: > $ref: ./definitions/recall.yaml > recalls: >@@ -479,6 +481,9 @@ paths: > $ref: "./paths/patrons_quotas.yaml#/~1patrons~1{patron_id}~1quotas" > "/patrons/{patron_id}/quotas/{quota_id}": > $ref: "./paths/patrons_quotas.yaml#/~1patrons~1{patron_id}~1quotas~1{quota_id}" >+ "/patrons/{patron_id}/quotas/{quota_id}/usages": >+ $ref: "./paths/patrons_quotas.yaml#/~1patrons~1{patron_id}~1quotas~1{quota_id}~1usages" >+ > "/patrons/{patron_id}/recalls": > $ref: "./paths/patrons_recalls.yaml#/~1patrons~1{patron_id}~1recalls" > /patron_categories: >-- >2.48.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 38924
:
178582
|
178583
|
178584
|
178585
|
178586
|
178587
|
178588
|
178589
|
178590
|
178591
|
178592
|
178593
|
178594
|
178595
|
178596
|
178597
|
178598
|
178599
|
178600
|
178601
|
178602
|
178603
|
178604
|
178605
| 178606 |
178607
|
178608