Skip to content

Commit 3facced

Browse files
authored
Add endpoint for borrower message deletion, pass message id with messages (#30)
1 parent 119fede commit 3facced

4 files changed

Lines changed: 96 additions & 0 deletions

File tree

Koha/Plugin/Fi/KohaSuomi/DI/PatronController.pm

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ sub get {
151151
my $api_record;
152152
$api_record->{date} = $message->message_date;
153153
$api_record->{message} = $message->message;
154+
$api_record->{message_id} = $message->message_id;
154155
$api_record->{library_id} = $message->branchcode;
155156
push @messages, $api_record;
156157
}
@@ -377,6 +378,54 @@ List Koha::Checkout objects including renewability (for checked out items)
377378
<
378379
=cut
379380

381+
sub delete_messages {
382+
my $c = shift->openapi->valid_input or return;
383+
384+
my $borrowernumber = $c->validation->param('patron_id');
385+
my $message_id = $c->validation->param('message_id');
386+
387+
my $patron;
388+
my $message;
389+
390+
try {
391+
$patron = Koha::Patrons->find($borrowernumber);
392+
if ($patron) {
393+
$message = Koha::Patron::Messages->find($message_id);
394+
395+
if (($message) && ($message->message_type eq "B")){
396+
if ($patron->borrowernumber == $message->borrowernumber){
397+
$message->delete;
398+
return $c->render( status => 204, openapi => {} );
399+
}
400+
else {
401+
return $c->render( status => 403, openapi => {
402+
error => "Borrowernumber does not match message borrowernumber"
403+
});
404+
}
405+
}
406+
else {
407+
if ($message){
408+
return $c->render( status => 403, openapi => {
409+
error => "Forbidden message type"
410+
});
411+
}
412+
else {
413+
return $c->render( status => 404, openapi => {
414+
error => "No such message for patron"
415+
});
416+
}
417+
}
418+
}
419+
}
420+
catch {
421+
unless ($patron) {
422+
return $c->render( status => 404, openapi => {
423+
error => "Patron doesn't exist"
424+
});
425+
}
426+
};
427+
}
428+
380429
sub list_checkouts {
381430
my $c = shift->openapi->valid_input or return;
382431

Koha/Plugin/Fi/KohaSuomi/DI/openapi.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,44 @@ paths:
11611161
permissions:
11621162
borrowers: "edit_borrowers"
11631163
x-mojo-to: Fi::KohaSuomi::DI::PatronController#edit_messaging_preferences
1164+
"/patrons/{patron_id}/messages/{message_id}":
1165+
delete:
1166+
description: Deletes patron's messages by message id.
1167+
operationId: deleteMessageDI
1168+
parameters:
1169+
- $ref: ./openapi/parameters.yaml#/patron_id_pp
1170+
- $ref: ./openapi/parameters.yaml#/message_id_pp
1171+
produces:
1172+
- application/json
1173+
responses:
1174+
204:
1175+
description: Message deleted successfully
1176+
401:
1177+
description: Authentication required
1178+
schema:
1179+
$ref: ./openapi/definitions.yaml#/error
1180+
403:
1181+
description: Access forbidden
1182+
schema:
1183+
$ref: ./openapi/definitions.yaml#/error
1184+
404:
1185+
description: Message or patron not found
1186+
schema:
1187+
$ref: ./openapi/definitions.yaml#/error
1188+
500:
1189+
description: Internal error
1190+
schema:
1191+
$ref: ./openapi/definitions.yaml#/error
1192+
503:
1193+
description: Under maintenance
1194+
schema:
1195+
$ref: ./openapi/definitions.yaml#/error
1196+
tags:
1197+
- messages
1198+
x-koha-authorization:
1199+
permissions:
1200+
circulate: circulate_remaining_permissions
1201+
x-mojo-to: Fi::KohaSuomi::DI::PatronController#delete_messages
11641202
info:
11651203
title: Koha REST API Discovery Interface Plugin
11661204
version: "1"

Koha/Plugin/Fi/KohaSuomi/DI/openapi/parameters.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ patron_id_pp:
3737
$ref: parameters/patron.yaml#/patron_id_pp
3838
patron_id_qp:
3939
$ref: parameters/patron.yaml#/patron_id_qp
40+
message_id_pp:
41+
$ref: parameters/message.yaml#/message_id_pp
4042
per_page:
4143
description: 'Page size, for paginated object listing'
4244
in: query
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
message_id_pp:
3+
description: Internal message identifier
4+
in: path
5+
name: message_id
6+
type: integer
7+
required: true

0 commit comments

Comments
 (0)