Skip to content

Commit

Permalink
Add endpoint for borrower message deletion, pass message id with mess…
Browse files Browse the repository at this point in the history
…ages (#30)
  • Loading branch information
lmstrand committed Feb 2, 2024
1 parent 119fede commit 3facced
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Koha/Plugin/Fi/KohaSuomi/DI/PatronController.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down
38 changes: 38 additions & 0 deletions Koha/Plugin/Fi/KohaSuomi/DI/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 2 additions & 0 deletions Koha/Plugin/Fi/KohaSuomi/DI/openapi/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Koha/Plugin/Fi/KohaSuomi/DI/openapi/parameters/message.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
message_id_pp:
description: Internal message identifier
in: path
name: message_id
type: integer
required: true

0 comments on commit 3facced

Please sign in to comment.