From f7e6d2f1bd8642258c7f7be452e5c7fef74b6185 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 6 Apr 2023 14:50:42 +0200 Subject: [PATCH] Bug 30708: Add user_train_item_id Sponsored-by: BULAC - http://www.bulac.fr/ Signed-off-by: BULAC - http://www.bulac.fr/ Signed-off-by: Heather Hernandez Signed-off-by: Laurence Rault Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- Koha/Preservation/Train.pm | 5 ++ Koha/REST/V1/Preservation/Trains.pm | 42 ++++++++++ api/v1/swagger/paths/preservation_trains.yaml | 78 +++++++++++++++++++ api/v1/swagger/swagger.yaml | 2 + installer/data/mysql/kohastructure.sql | 1 + .../components/Preservation/TrainsShow.vue | 28 ++++--- .../components/Preservation/WaitingList.vue | 52 +++++++------ .../js/vue/fetch/preservation-api-client.js | 5 ++ t/db_dependent/api/v1/preservation_Trains.t | 21 ++++- 9 files changed, 198 insertions(+), 36 deletions(-) diff --git a/Koha/Preservation/Train.pm b/Koha/Preservation/Train.pm index f52cb5c15a..dadb1dd13d 100644 --- a/Koha/Preservation/Train.pm +++ b/Koha/Preservation/Train.pm @@ -71,10 +71,15 @@ sub add_item { Koha::Exceptions::Preservation::ItemNotFound->throw unless $item; Koha::Exceptions::Preservation::ItemNotInWaitingList->throw if $item->notforloan != $not_for_loan; + # FIXME We need a LOCK here + # Not important for now as we have add_items + # Note that there are several other places in Koha with this max+1 problem + my $max = $self->items->search->_resultset->get_column("user_train_item_id")->max || 0; my $train_item_rs = $self->_result->add_to_preservation_trains_items( { item_id => $item->itemnumber, processing_id => $train_item->{processing_id} || $self->default_processing_id, + user_train_item_id => $max + 1, added_on => \'NOW()', } ); diff --git a/Koha/REST/V1/Preservation/Trains.pm b/Koha/REST/V1/Preservation/Trains.pm index 51dc298246..644a7a9721 100644 --- a/Koha/REST/V1/Preservation/Trains.pm +++ b/Koha/REST/V1/Preservation/Trains.pm @@ -288,6 +288,48 @@ sub get_item { }; } +=head3 add_items + +Controller function that handles adding items in batch to a train + +=cut + +sub add_items { + my $c = shift->openapi->valid_input or return; + + my $train_id = $c->validation->param('train_id'); + my $train = Koha::Preservation::Trains->find( $train_id ); + + unless ($train) { + return $c->render( + status => 404, + openapi => { error => "Train not found" } + ); + } + + my $body = $c->validation->every_param('body'); + return try { + Koha::Database->new->schema->txn_do( + sub { + my $train_items = $train->add_items($body); + return $c->render( status => 201, openapi => $train_items ); + } + ); + } + catch { + if ( blessed $_ ) { + if ( $_->isa('Koha::Exceptions::Preservation::MissingSettings') ) { + return $c->render( + status => 400, + openapi => { error => "MissingSettings", parameter => $_->parameter } + ); + } + } + + $c->unhandled_exception($_); + }; +} + =head3 add_item Controller function that handles adding items in batch to a train diff --git a/api/v1/swagger/paths/preservation_trains.yaml b/api/v1/swagger/paths/preservation_trains.yaml index 6890adf8d3..e7fc5c90f8 100644 --- a/api/v1/swagger/paths/preservation_trains.yaml +++ b/api/v1/swagger/paths/preservation_trains.yaml @@ -376,6 +376,84 @@ x-koha-authorization: permissions: preservation: 1 +"/preservation/trains/{train_id}/items/batch": + post: + x-mojo-to: Preservation::Trains#add_items + operationId: addItemsToTrain + tags: + - preservation_train + summary: Add items to train + consumes: + - application/json + produces: + - application/json + parameters: + - $ref: "../swagger.yaml#/parameters/preservation_train_id_pp" + - description: A list of items + in: body + name: body + required: true + schema: + type: array + items: + type: object + responses: + 201: + description: A successfully added list of items + schema: + type: array + items: + type: object + 400: + description: Bad parameter + schema: + $ref: "../swagger.yaml#/definitions/error" + 401: + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + 403: + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + 404: + description: Ressource not found + schema: + $ref: "../swagger.yaml#/definitions/error" + 409: + description: Conflict in creating resource + schema: + $ref: "../swagger.yaml#/definitions/error" + 413: + description: Payload too large + 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: + preservation: 1 + 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: + preservation: 1 + "/preservation/trains/{train_id}/items/{train_item_id}": put: x-mojo-to: Preservation::Trains#update_item diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index 580938a814..421274260d 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -343,6 +343,8 @@ paths: $ref: "./paths/preservation_trains.yaml#/~1preservation~1trains~1{train_id}" "/preservation/trains/{train_id}/items": $ref: "./paths/preservation_trains.yaml#/~1preservation~1trains~1{train_id}~1items" + "/preservation/trains/{train_id}/items/batch": + $ref: "./paths/preservation_trains.yaml#/~1preservation~1trains~1{train_id}~1items~1batch" "/preservation/trains/{train_id}/items/{train_item_id}": $ref: "./paths/preservation_trains.yaml#/~1preservation~1trains~1{train_id}~1items~1{train_item_id}" /preservation/processings: diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index ecc9b72fdc..9f0ef941c5 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -4881,6 +4881,7 @@ CREATE TABLE `preservation_trains_items` ( `train_id` int(11) NOT NULL COMMENT 'link with preservation_train', `item_id` int(11) NOT NULL COMMENT 'link with items', `processing_id` int(11) NULL COMMENT 'specific processing for this item', + `user_train_item_id` int(11) NOT NULL COMMENT 'train item id for this train, starts from 1', `added_on` datetime DEFAULT NULL COMMENT 'added date', PRIMARY KEY (`train_item_id`), UNIQUE KEY (`train_id`,`item_id`), diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Preservation/TrainsShow.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Preservation/TrainsShow.vue index 4038b43fc6..1ee9dfdb84 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Preservation/TrainsShow.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Preservation/TrainsShow.vue @@ -146,9 +146,8 @@ v-for="(item, counter) in train.items" v-bind:key="counter" > -