diff --git a/Koha/Plugin/Fi/KohaSuomi/DI/PatronController.pm b/Koha/Plugin/Fi/KohaSuomi/DI/PatronController.pm index e9eb353..bacf00c 100644 --- a/Koha/Plugin/Fi/KohaSuomi/DI/PatronController.pm +++ b/Koha/Plugin/Fi/KohaSuomi/DI/PatronController.pm @@ -151,6 +151,7 @@ sub get { my $api_record; $api_record->{date} = $message->message_date; $api_record->{message} = $message->message; + $api_record->{message_id} = $message->message_id; $api_record->{library_id} = $message->branchcode; push @messages, $api_record; } @@ -377,6 +378,54 @@ List Koha::Checkout objects including renewability (for checked out items) < =cut +sub delete_messages { + my $c = shift->openapi->valid_input or return; + + my $borrowernumber = $c->validation->param('patron_id'); + my $message_id = $c->validation->param('message_id'); + + my $patron; + my $message; + + try { + $patron = Koha::Patrons->find($borrowernumber); + if ($patron) { + $message = Koha::Patron::Messages->find($message_id); + + if (($message) && ($message->message_type eq "B")){ + if ($patron->borrowernumber == $message->borrowernumber){ + $message->delete; + return $c->render( status => 204, openapi => {} ); + } + else { + return $c->render( status => 403, openapi => { + error => "Borrowernumber does not match message borrowernumber" + }); + } + } + else { + if ($message){ + return $c->render( status => 403, openapi => { + error => "Forbidden message type" + }); + } + else { + return $c->render( status => 404, openapi => { + error => "No such message for patron" + }); + } + } + } + } + catch { + unless ($patron) { + return $c->render( status => 404, openapi => { + error => "Patron doesn't exist" + }); + } + }; +} + sub list_checkouts { my $c = shift->openapi->valid_input or return; diff --git a/Koha/Plugin/Fi/KohaSuomi/DI/openapi.yaml b/Koha/Plugin/Fi/KohaSuomi/DI/openapi.yaml index d477259..32e7508 100644 --- a/Koha/Plugin/Fi/KohaSuomi/DI/openapi.yaml +++ b/Koha/Plugin/Fi/KohaSuomi/DI/openapi.yaml @@ -1161,6 +1161,44 @@ paths: permissions: borrowers: "edit_borrowers" x-mojo-to: Fi::KohaSuomi::DI::PatronController#edit_messaging_preferences + "/patrons/{patron_id}/messages/{message_id}": + delete: + description: Deletes patron's messages by message id. + operationId: deleteMessageDI + parameters: + - $ref: ./openapi/parameters.yaml#/patron_id_pp + - $ref: ./openapi/parameters.yaml#/message_id_pp + produces: + - application/json + responses: + 204: + description: Message deleted successfully + 401: + description: Authentication required + schema: + $ref: ./openapi/definitions.yaml#/error + 403: + description: Access forbidden + schema: + $ref: ./openapi/definitions.yaml#/error + 404: + description: Message or patron not found + schema: + $ref: ./openapi/definitions.yaml#/error + 500: + description: Internal error + schema: + $ref: ./openapi/definitions.yaml#/error + 503: + description: Under maintenance + schema: + $ref: ./openapi/definitions.yaml#/error + tags: + - messages + x-koha-authorization: + permissions: + circulate: circulate_remaining_permissions + x-mojo-to: Fi::KohaSuomi::DI::PatronController#delete_messages info: title: Koha REST API Discovery Interface Plugin version: "1" diff --git a/Koha/Plugin/Fi/KohaSuomi/DI/openapi/parameters.yaml b/Koha/Plugin/Fi/KohaSuomi/DI/openapi/parameters.yaml index d8ab1e9..f0a43be 100644 --- a/Koha/Plugin/Fi/KohaSuomi/DI/openapi/parameters.yaml +++ b/Koha/Plugin/Fi/KohaSuomi/DI/openapi/parameters.yaml @@ -37,6 +37,8 @@ patron_id_pp: $ref: parameters/patron.yaml#/patron_id_pp patron_id_qp: $ref: parameters/patron.yaml#/patron_id_qp +message_id_pp: + $ref: parameters/message.yaml#/message_id_pp per_page: description: 'Page size, for paginated object listing' in: query diff --git a/Koha/Plugin/Fi/KohaSuomi/DI/openapi/parameters/message.yaml b/Koha/Plugin/Fi/KohaSuomi/DI/openapi/parameters/message.yaml new file mode 100644 index 0000000..f9a4e00 --- /dev/null +++ b/Koha/Plugin/Fi/KohaSuomi/DI/openapi/parameters/message.yaml @@ -0,0 +1,7 @@ +--- +message_id_pp: + description: Internal message identifier + in: path + name: message_id + type: integer + required: true