From 64eb97a84eeef411e01b0a1a1209bb8e6ccfa442 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 30 Apr 2025 15:55:23 +0200 Subject: [PATCH] Bug 38010: Embed baskets+count and subscriptions+count We don't need to whole list of baskets and subscriptions (cherry picked from commit 391b4d3b32b57c97bfb7e66ab394020ae7eed0e5) Signed-off-by: Jonathan Druart --- api/v1/swagger/definitions/vendor.yaml | 12 ++++++- .../swagger/paths/acquisitions_vendors.yaml | 8 ++--- .../prog/js/fetch/acquisition-api-client.js | 4 +-- .../vue/components/Vendors/VendorFormAdd.vue | 1 + .../js/vue/components/Vendors/VendorList.vue | 17 ++++------ .../js/vue/components/Vendors/VendorShow.vue | 9 +++-- t/db_dependent/api/v1/acquisitions_vendors.t | 33 ++++++++++++------- 7 files changed, 50 insertions(+), 34 deletions(-) diff --git a/api/v1/swagger/definitions/vendor.yaml b/api/v1/swagger/definitions/vendor.yaml index 163276fdbbf..717d958bf78 100644 --- a/api/v1/swagger/definitions/vendor.yaml +++ b/api/v1/swagger/definitions/vendor.yaml @@ -122,6 +122,11 @@ properties: baskets: type: array description: List of baskets + baskets_count: + type: + - integer + - "null" + description: Number of linked baskets subscriptions_count: type: - integer @@ -139,6 +144,11 @@ properties: invoices: type: array description: List of invoices + invoices_count: + type: + - integer + - "null" + description: Number of linked invoices additionalProperties: false required: - - name \ No newline at end of file + - name diff --git a/api/v1/swagger/paths/acquisitions_vendors.yaml b/api/v1/swagger/paths/acquisitions_vendors.yaml index 1cefcf82dee..bae07ec8aab 100644 --- a/api/v1/swagger/paths/acquisitions_vendors.yaml +++ b/api/v1/swagger/paths/acquisitions_vendors.yaml @@ -35,9 +35,9 @@ type: string enum: - aliases - - baskets + - baskets+count - subscriptions+count - - invoices + - invoices+count collectionFormat: csv responses: "200": @@ -149,7 +149,7 @@ type: string enum: - aliases - - baskets + - baskets+count - subscriptions+count - interfaces - contacts @@ -285,4 +285,4 @@ $ref: "../swagger.yaml#/definitions/error" x-koha-authorization: permissions: - acquisition: vendors_manage \ No newline at end of file + acquisition: vendors_manage diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/acquisition-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/acquisition-api-client.js index 165d93948e2..fad44c00a75 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/acquisition-api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/acquisition-api-client.js @@ -21,7 +21,7 @@ export class AcquisitionAPIClient { endpoint: "vendors/" + id, headers: { "x-koha-embed": - "aliases,subscriptions+count,interfaces,contacts,contracts,baskets", + "aliases,subscriptions+count,interfaces,contacts,contracts,baskets+count", }, }), getAll: (query, params) => @@ -30,7 +30,7 @@ export class AcquisitionAPIClient { query, params: { _order_by: "name", ...params }, headers: { - "x-koha-embed": "aliases,baskets", + "x-koha-embed": "aliases,baskets+count", }, }), delete: id => diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorFormAdd.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorFormAdd.vue index d0fe88f87fe..3c042ca82d6 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorFormAdd.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorFormAdd.vue @@ -128,6 +128,7 @@ export default { delete vendor.physical; delete vendor.subscriptions_count; delete vendor.baskets; + delete vendor.baskets_count; delete vendor.subscriptions; delete vendor.contracts; diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorList.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorList.vue index b59ff1c556c..1441e21c5c2 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorList.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorList.vue @@ -67,7 +67,7 @@ export default { tableOptions: { columns: this.getTableColumns(), options: { - embed: "aliases,baskets,subscriptions+count,invoices", + embed: "aliases,baskets+count,subscriptions+count,invoices+count", }, url: () => this.tableURL(), add_filters: true, @@ -89,12 +89,9 @@ export default { text: this.$__("Delete"), icon: "fa fa-trash", should_display: row => - (!row.baskets || - row.baskets.length === 0) && - (!row.subscriptions_count || - row.subscriptions_count === 0) && - (!row.invoices || - row.invoices.length === 0), + row.baskets_count === 0 && + row.subscriptions_count === 0 && + row.invoices_count === 0, }, }, { @@ -103,7 +100,7 @@ export default { icon: "fa fa-inbox", should_display: row => row.active && - row.baskets?.length > 0 && + row.baskets_count > 0 && this.isUserPermitted( "CAN_user_acquisition_order_receive" ), @@ -241,13 +238,13 @@ export default { searchable: false, orderable: false, render(data, type, row, meta) { - return row.baskets.length + return row.baskets_count ? '' + escape_str( __("%s basket(s)").format( - row.baskets.length + row.baskets_count ) ) + "" diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorShow.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorShow.vue index 77fdcbb3853..456250ad2eb 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorShow.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Vendors/VendorShow.vue @@ -48,7 +48,7 @@ new('Koha::REST::V1'); subtest 'list() and delete() tests | authorized user' => sub { - plan tests => 44; + plan tests => 39; $schema->storage->txn_begin; @@ -92,23 +92,32 @@ subtest 'list() and delete() tests | authorized user' => sub { ->json_is( '/0/aliases/0/alias' => 'alias 1', 'alias 1 is embedded' ) ->json_is( '/0/aliases/1/alias' => 'alias 2', 'alias 2 is embedded' ); + my @cleanup; for ( 0 .. 1 ) { - $builder->build_object( { class => 'Koha::Subscriptions', value => { aqbooksellerid => $vendor->id } } ); + push @cleanup, + $builder->build_object( { class => 'Koha::Subscriptions', value => { aqbooksellerid => $vendor->id } } ); + push @cleanup, + $builder->build_object( + { class => 'Koha::Acquisition::Baskets', value => { booksellerid => $vendor->id } } ); + push @cleanup, + $builder->build_object( + { class => 'Koha::Acquisition::Invoices', value => { booksellerid => $vendor->id } } ); } - $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors" => { 'x-koha-embed' => 'subscriptions+count' } ) - ->status_is(200)->json_has( '/0/subscriptions_count', 'subscriptions_count is embedded' ) - ->json_is( '/0/subscriptions_count' => '2', 'subscription count is 2' ); + $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors" => + { 'x-koha-embed' => 'subscriptions+count,baskets+count,invoices+count' } )->status_is(200) + ->json_is( '/0/subscriptions_count', 2, 'subscriptions_count is 2' ) + ->json_is( '/0/baskets_count', 2, 'baskets_count is 2' ) + ->json_is( '/0/invoices_count', 2, 'invoices_count is 2' ); + + # FIXME We need to add more tests/code here + # A vendor deletion should be rejected if it has baskets or subscriptions (or invoices?) linked to it + $_->delete for @cleanup; $t->delete_ok( "//$userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id )->status_is( 204, 'REST3.2.4' ) ->content_is( '', 'REST3.3.4' ); - $t->get_ok("//$userid:$password@/api/v1/acquisitions/vendors")->status_is(200) - ->json_like( '/0/name' => qr/$other_vendor_name/ )->json_hasnt( '/1', 'Only one vendor' ); - - $t->delete_ok( "//$userid:$password@/api/v1/acquisitions/vendors/" . $other_vendor->id ) - ->status_is( 204, 'REST3.2.4' )->content_is( '', 'REST3.3.4' ); - - $t->get_ok("//$userid:$password@/api/v1/acquisitions/vendors")->status_is(200)->json_is( [] ); + $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors/" . $other_vendor->id )->status_is(200); + $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id )->status_is(404); $schema->storage->txn_rollback; }; -- 2.34.1