View | Details | Raw Unified | Return to bug 14843
Collapse All | Expand All

(-)a/C4/Letters.pm (-1 / +117 lines)
Lines 935-940 ENDSQL Link Here
935
    return $dbh->last_insert_id(undef,undef,'message_queue', undef);
935
    return $dbh->last_insert_id(undef,undef,'message_queue', undef);
936
}
936
}
937
937
938
=head2 DequeueLetter
939
940
  my $success = DequeueLetter( { message_id => $message_id } )
941
942
Removes a message from message_queue
943
944
return 1 on success, otherwise 0
945
946
=cut
947
948
sub DequeueLetter {
949
    my $params = shift or return;
950
951
    return unless exists $params->{'message_id'} or not $params->{'message_id'};
952
953
    my $dbh       = C4::Context->dbh();
954
    my $statement = << 'ENDSQL';
955
DELETE FROM message_queue
956
WHERE message_id = ?
957
ENDSQL
958
959
    my $sth    = $dbh->prepare($statement);
960
    my $result = $sth->execute(
961
        $params->{'message_id'},              # message_id
962
    );
963
    return ($result > 0);
964
}
965
938
=head2 SendQueuedMessages ([$hashref]) 
966
=head2 SendQueuedMessages ([$hashref]) 
939
967
940
  my $sent = SendQueuedMessages( { verbose => 1 } );
968
  my $sent = SendQueuedMessages( { verbose => 1 } );
Lines 1026-1032 sub GetQueuedMessages { Link Here
1026
1054
1027
    my $dbh = C4::Context->dbh();
1055
    my $dbh = C4::Context->dbh();
1028
    my $statement = << 'ENDSQL';
1056
    my $statement = << 'ENDSQL';
1029
SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued, delivery_note
1057
SELECT message_id, borrowernumber, subject, content, metadata, letter_code, message_transport_type, status, time_queued, to_address, from_address, content_type, delivery_note
1030
FROM message_queue
1058
FROM message_queue
1031
ENDSQL
1059
ENDSQL
1032
1060
Lines 1051-1056 ENDSQL Link Here
1051
    return $sth->fetchall_arrayref({});
1079
    return $sth->fetchall_arrayref({});
1052
}
1080
}
1053
1081
1082
1083
=head2 UpdateQueuedMessage ([$hashref])
1084
1085
  my $message = UpdateQueuedMessage( {
1086
        borrowernumber => '123', message_id => '100',
1087
        letter => { subject => "New subject", content => "New content" },
1088
    } );
1089
1090
Updates the message in message_queue. Message is identified by
1091
required parameter message_id.
1092
1093
returns:
1094
1 on success, otherwise 0
1095
1096
=cut
1097
1098
sub UpdateQueuedMessage {
1099
    my $params = shift;
1100
1101
    if (not $params->{'message_id'}) {
1102
        warn __PACKAGE__."::UpdateQueuedMessage: param message_id is required";
1103
        return;
1104
    }
1105
    
1106
    my $dbh = C4::Context->dbh();
1107
    my $statement = << 'ENDSQL';
1108
UPDATE message_queue
1109
SET
1110
ENDSQL
1111
1112
    my @query_params;
1113
    
1114
    if ($params->{'borrowernumber'}) {
1115
         $statement .= " borrowernumber = ?, ";
1116
         push @query_params, $params->{'borrowernumber'};
1117
    }
1118
    if ($params->{'subject'}) {
1119
         $statement .= " subject = ?, ";
1120
         push @query_params, $params->{'subject'};
1121
    }
1122
    if ($params->{'content'}) {
1123
         $statement .= " content = ?, ";
1124
         push @query_params, $params->{'content'};
1125
    }
1126
    if ($params->{'metadata'}) {
1127
         $statement .= " metadata = ?, ";
1128
         push @query_params, $params->{'metadata'};
1129
    }
1130
    if ($params->{'letter_code'}) {
1131
         $statement .= " letter_code = ?, ";
1132
         push @query_params, $params->{'letter_code'};
1133
    }
1134
    if ($params->{'content_type'}) {
1135
         $statement .= " content_type = ?, ";
1136
         push @query_params, $params->{'content_type'};
1137
    }
1138
    if ($params->{'message_transport_type'}) {
1139
         $statement .= " message_transport_type = ?, ";
1140
         push @query_params, $params->{'message_transport_type'};
1141
    }
1142
    if ($params->{'status'}) {
1143
         $statement .= " status = ?, ";
1144
         push @query_params, $params->{'status'};
1145
    }
1146
    if ($params->{'to_address'}) {
1147
         $statement .= " to_address = ?, ";
1148
         push @query_params, $params->{'to_address'};
1149
    }
1150
    if ($params->{'from_address'}) {
1151
         $statement .= " from_address = ?, ";
1152
         push @query_params, $params->{'from_address'};
1153
    }
1154
    if ($params->{'delivery_note'}) {
1155
         $statement .= " delivery_note = ?, ";
1156
         push @query_params, $params->{'delivery_note'};
1157
    }
1158
    return 0 if $statement =~ /SET(\s*)$/;
1159
    $statement =~ s/,(\s*)$//g; # remove last ,
1160
    
1161
    $statement .= " WHERE message_id = ?";
1162
    push @query_params, $params->{'message_id'};
1163
1164
    my $sth = $dbh->prepare( $statement );
1165
    my $result = $sth->execute( @query_params );
1166
    return ($result > 0);
1167
}
1168
1169
1054
=head2 GetMessageTransportTypes
1170
=head2 GetMessageTransportTypes
1055
1171
1056
  my @mtt = GetMessageTransportTypes();
1172
  my @mtt = GetMessageTransportTypes();
(-)a/Koha/REST/V1/Messages.pm (+292 lines)
Line 0 Link Here
1
package Koha::REST::V1::Messages;
2
3
use Modern::Perl;
4
5
use Mojo::Base 'Mojolicious::Controller';
6
7
use C4::Letters;
8
use Data::Dumper;
9
use C4::Log;
10
11
12
=head2 list_messages($c, $args, $cb)
13
14
Lists all messages from message_queue.
15
16
=cut
17
sub list_messages {
18
    my ($c, $args, $cb) = @_;
19
20
    my $messages = C4::Letters::GetQueuedMessages();
21
22
    if (@$messages > 0) {
23
        my $return_messages;
24
        foreach my $message (@$messages){
25
            push @$return_messages, _set_default_values({ message => $message });
26
        }
27
        return $c->$cb($return_messages , 200);
28
    }
29
    $c->$cb({ error => "No messages found." }, 404);
30
}
31
32
33
34
=head2 create_message($c, $args, $cb)
35
36
Creates a new message into message queue.
37
38
=cut
39
sub create_message {
40
    my ($c, $args, $cb) = @_;
41
42
    # Content is required. Check for missing content.
43
    if (exists $args->{body}->{content}) {
44
        my $content = $args->{body}->{content};
45
        $content =~ s/\s+//g if(defined $content);
46
        if ( not $content ) {
47
            return $c->$cb({ error => "Property cannot be empty in: /body/content" }, 400);
48
        }
49
    } else {
50
        return $c->$cb({ error => "Missing property: /body/content" }, 400);
51
    }
52
    
53
    if (exists $args->{body}->{borrowernumber}) {
54
        my $content = $args->{body}->{borrowernumber};
55
        $content =~ s/\s+//g if(defined $content);
56
        if ( not $content ) {
57
            return $c->$cb({ error => "Property cannot be empty in: /body/borrowernumber" }, 400);
58
        }
59
    } else {
60
        return $c->$cb({ error => "Missing property: /body/borrowernumber" }, 400);
61
    }
62
    
63
    
64
    # message_transport_type is required. Check for missing parameter.
65
    my $transport_type = $args->{body}->{message_transport_type};
66
    $transport_type =~ s/\s+//g if(defined $transport_type);
67
    if ( not $transport_type ) {
68
        return $c->$cb({ error => "Missing property: /body/message_transport_type" }, 400);
69
    }
70
    
71
    # message_transport_type also must be one of the predefined values
72
    my $transport_types = C4::Letters::GetMessageTransportTypes();
73
    if ( !grep( /^$transport_type$/, @$transport_types ) ) {
74
        return $c->$cb({ error => "Invalid value '".$transport_type."' in: /body/message_transport_type. Valid values are ". join(', ', @$transport_types) }, 400);
75
    }
76
    
77
    # Make some changes in names of parameters due to inconsistent naming in C4::Letters
78
    
79
    # Enqueue the letter
80
    my $message = C4::Letters::EnqueueLetter(_convert_to_enqueue({ message => $args->{'body'} }));
81
    
82
    # Check if the message was enqueued
83
    if ($message){
84
        # Get the newly created message and return it
85
        $message = C4::Letters::GetMessage( $message );
86
        
87
        $message = _set_default_values({ message => $message });
88
        
89
        &logaction(
90
        "NOTICES",
91
        "CREATE",
92
        $message->{message_id},
93
        ""
94
        );
95
        
96
        return $c->$cb($message, 201);
97
    }
98
    $c->$cb({ error => "Message could not be created." }, 404)
99
}
100
101
102
103
=head2 update_message($c, $args, $cb)
104
105
Updates the message in message_queue.
106
107
=cut
108
sub update_message {
109
    my ($c, $args, $cb) = @_;
110
111
    # Content is required. Check for missing content.
112
    if (exists $args->{body}->{content}) {
113
        my $content = $args->{body}->{content};
114
        $content =~ s/\s+//g if(defined $content);
115
        if ( $content eq '' ) {
116
            return $c->$cb({ error => "Property cannot be empty in: /body/content" }, 400);
117
        }
118
    }
119
    
120
    if (exists $args->{body}->{message_transport_type}) {
121
        my $transport_type = $args->{body}->{message_transport_type};
122
        # message_transport_type also must be one of the predefined values
123
        my $transport_types = C4::Letters::GetMessageTransportTypes();
124
        if ( !grep( /^$transport_type$/, @$transport_types ) ) {
125
            return $c->$cb({ error => "Invalid value '".$transport_type
126
                            ."' in: /body/message_transport_type. Valid values are "
127
                            . join(', ', @$transport_types) }, 400);
128
        }
129
    }
130
131
    # create update hash
132
    my $update = $args->{'body'};
133
    $update->{'message_id'} = $args->{'messagenumber'};
134
    
135
    my $message = C4::Letters::UpdateQueuedMessage( $update );
136
    return $c->$cb({ error => "Message could not be found." }, 404) if not $message;
137
    
138
    $message = C4::Letters::GetMessage($args->{'messagenumber'});
139
    
140
    if ($message){
141
        $message = _set_default_values({ message => $message });
142
        
143
        &logaction(
144
        "NOTICES",
145
        "UPDATE",
146
        $message->{message_id},
147
        ""
148
        );
149
        
150
        return $c->$cb($message, 200);
151
    }
152
    $c->$cb({ error => "Message could not be updated." }, 404);
153
}
154
155
156
157
=head2 delete_message($c, $args, $cb)
158
159
Delete the message from message_queue.
160
161
=cut
162
sub delete_message {
163
    my ($c, $args, $cb) = @_;
164
165
    my $message = C4::Letters::DequeueLetter({ message_id => $args->{'messagenumber'} });
166
    
167
    if ($message){
168
        
169
        &logaction(
170
        "NOTICES",
171
        "DELETE",
172
        $args->{'messagenumber'},
173
        ""
174
        );
175
        
176
        return $c->$cb('', 204);
177
    }
178
    
179
    $c->$cb({ error => "Message could not be found." }, 404)
180
}
181
182
183
184
=head2 get_message($c, $args, $cb)
185
186
Returns a message from message_queue.
187
188
=cut
189
sub get_message {
190
    my ($c, $args, $cb) = @_;
191
192
    my $message = C4::Letters::GetMessage($args->{'messagenumber'});
193
194
    if ($message) {
195
        my $message = _set_default_values({ message => $message });
196
        
197
        return $c->$cb($message, 200);
198
    }
199
    
200
    return $c->$cb({ error => "Could not find the message" }, 404);
201
}
202
203
204
205
=head2 create_resend($c, $args, $cb)
206
207
Attempts to resend a message.
208
209
=cut
210
sub create_resend {
211
    my ($c, $args, $cb) = @_;
212
213
214
    my $message = C4::Letters::GetMessage($args->{'messagenumber'});
215
216
    if ($message) {
217
        if (C4::Letters::ResendMessage($args->{'messagenumber'})) {
218
219
            &logaction(
220
            "NOTICES",
221
            "RESEND",
222
            $message->{message_id},
223
            ""
224
            );
225
226
            return $c->$cb('', 204);
227
        }
228
    }
229
230
    return $c->$cb({ error => "Could not find the message" }, 404);
231
}
232
233
234
235
=head2 _set_default_values({ message => $message })
236
237
=cut
238
sub _set_default_values {
239
    my $params = shift;
240
    
241
    return unless exists $params->{'message'} or not defined $params->{'message'};
242
    
243
    # Convert the returned message into C4::Letters::EnqueueLetter format
244
    return {
245
        message_id => int($params->{'message'}->{'message_id'}),
246
        borrowernumber => int($params->{'message'}->{'borrowernumber'}),
247
        subject => $params->{'message'}->{'subject'} || '',
248
        content => $params->{'message'}->{'content'} || '',
249
        letter_code => $params->{'message'}->{'letter_code'} || '',
250
        content_type => $params->{'message'}->{'content_type'} || '',
251
        metadata => $params->{'message'}->{'metadata'} || '',
252
        message_transport_type => $params->{'message'}->{'message_transport_type'} || '',
253
        to_address => $params->{'message'}->{'to_address'} || '',
254
        from_address => $params->{'message'}->{'from_address'} || '',
255
        delivery_note => $params->{'message'}->{'delivery_note'} || '',
256
        status => $params->{'message'}->{'status'} || '',
257
        time_queued => $params->{'message'}->{'time_queued'} || ''
258
    };
259
}
260
261
262
263
=head2 _convert_to_message({ message => $message })
264
265
Due to inconsistent naming in C4::Letters::EnqueueLetter, we use the
266
database format and convert it in this function into the format that
267
EnqueueLetter supports.
268
269
=cut
270
sub _convert_to_enqueue {
271
    my $params = shift;
272
    
273
    return unless exists $params->{'message'} or not defined $params->{'message'};
274
    
275
    # Convert the returned message into C4::Letters::EnqueueLetter format
276
    return {
277
        borrowernumber => int($params->{'message'}->{'borrowernumber'}),
278
        letter => {
279
            title => $params->{'message'}->{'subject'} || '',
280
            content => $params->{'message'}->{'content'} || '',
281
            code => $params->{'message'}->{'letter_code'} || '',
282
            'content-type' => $params->{'message'}->{'content_type'} || '',
283
            metadata => $params->{'message'}->{'metadata'} || '',
284
        },
285
        message_transport_type => $params->{'message'}->{'message_transport_type'} || '',
286
        to_address => $params->{'message'}->{'to_address'} || '',
287
        from_address => $params->{'message'}->{'from_address'} || '',
288
        delivery_note => $params->{'message'}->{'delivery_note'} || ''
289
    };
290
}
291
292
1;
(-)a/api/v1/swagger.json (+261 lines)
Lines 309-314 Link Here
309
          }
309
          }
310
        }
310
        }
311
      }
311
      }
312
    },
313
    "/messages": {
314
      "get": {
315
        "x-mojo-controller": "Koha::REST::V1::Messages",
316
        "operationId": "listMessages",
317
        "x-koha-permission": {
318
          "messages": "get_message"
319
        },
320
        "tags": ["messages"],
321
        "produces": [
322
          "application/json"
323
        ],
324
        "responses": {
325
          "200": {
326
            "description": "A list of messages",
327
            "schema": {
328
              "type": "array",
329
              "items": {
330
                "$ref" : "#/definitions/message"
331
              }
332
            }
333
          },
334
          "404": {
335
            "description": "Messages not found",
336
            "schema": {
337
              "$ref": "#/definitions/error"
338
            }
339
          }
340
        }
341
      },
342
      "post": {
343
        "x-mojo-controller": "Koha::REST::V1::Messages",
344
        "operationId": "createMessage",
345
        "x-koha-permission": {
346
          "messages": "create_message"
347
        },
348
        "tags": ["messages"],
349
        "parameters": [
350
          {
351
            "name": "body",
352
            "in": "body",
353
            "description": "A JSON object containing informations about the new message",
354
            "required": true,
355
            "schema": { "$ref": "#/definitions/message" }
356
          }
357
        ],
358
        "produces": [
359
          "application/json"
360
        ],
361
        "responses": {
362
          "201": {
363
            "description": "A message",
364
            "schema": {
365
                "$ref": "#/definitions/message"
366
            }
367
          },
368
          "400": {
369
            "description": "Missing or wrong parameters",
370
            "schema": { "$ref": "#/definitions/error" }
371
          }
372
        }
373
      }
374
    },
375
    "/messages/{messagenumber}": {
376
      "put": {
377
        "x-mojo-controller": "Koha::REST::V1::Messages",
378
        "operationId": "updateMessage",
379
        "x-koha-permission": {
380
          "messages": "update_message"
381
        },
382
        "tags": ["messages"],
383
        "parameters": [
384
          {
385
            "name": "body",
386
            "in": "body",
387
            "description": "A JSON object containing informations about the new message",
388
            "required": false,
389
            "schema": { "$ref": "#/definitions/message" }
390
          },
391
          {
392
            "$ref": "#/parameters/messagenumberPathParam"
393
          }
394
        ],
395
        "produces": [
396
          "application/json"
397
        ],
398
        "responses": {
399
          "200": {
400
            "description": "A message",
401
            "schema": {
402
              "$ref" : "#/definitions/message"
403
            }
404
          },
405
          "400": {
406
            "description": "Missing or wrong parameters",
407
            "schema": { "$ref": "#/definitions/error" }
408
          },
409
          "404": {
410
            "description": "Message not found",
411
            "schema": {
412
              "$ref": "#/definitions/error"
413
            }
414
          }
415
        }
416
      },
417
      "delete": {
418
        "x-mojo-controller": "Koha::REST::V1::Messages",
419
        "operationId": "deleteMessage",
420
        "x-koha-permission": {
421
          "messages": "delete_message"
422
        },
423
        "tags": ["messages"],
424
        "parameters": [
425
          {
426
            "$ref": "#/parameters/messagenumberPathParam"
427
          }
428
        ],
429
        "produces": [
430
          "application/json"
431
        ],
432
        "responses": {
433
          "204": {
434
            "description": "Deleting the message succeeded.",
435
            "schema": {
436
              "type": "string"
437
            }
438
          },
439
          "404": {
440
            "description": "Message not found.",
441
            "schema": {
442
              "$ref": "#/definitions/error"
443
            }
444
          }
445
        }
446
      },
447
      "get": {
448
        "x-mojo-controller": "Koha::REST::V1::Messages",
449
        "operationId": "getMessage",
450
        "x-koha-permission": {
451
          "messages": "get_message"
452
        },
453
        "tags": ["messages"],
454
        "parameters": [
455
          {
456
            "$ref": "#/parameters/messagenumberPathParam"
457
          }
458
        ],
459
        "produces": [
460
          "application/json"
461
        ],
462
        "responses": {
463
          "200": {
464
            "description": "A message",
465
            "schema": {
466
              "$ref" : "#/definitions/message"
467
            }
468
          },
469
          "404": {
470
            "description": "Message not found",
471
            "schema": {
472
              "$ref": "#/definitions/error"
473
            }
474
          }
475
        }
476
      }
477
    },
478
    "/messages/{messagenumber}/resend": {
479
      "post": {
480
        "x-mojo-controller": "Koha::REST::V1::Messages",
481
        "operationId": "createResend",
482
        "description": "Resends a message.",
483
        "x-koha-permission": {
484
          "messages": "resend_message"
485
        },
486
        "tags": ["messages"],
487
        "parameters": [
488
          {
489
            "$ref": "#/parameters/messagenumberPathParam"
490
          }
491
        ],
492
        "produces": [
493
          "application/json"
494
        ],
495
        "responses": {
496
          "204": {
497
            "description": "Resending the message succeeded.",
498
            "schema": {
499
              "type": "string"
500
            }
501
          },
502
          "404": {
503
            "description": "Message not found",
504
            "schema": {
505
              "$ref": "#/definitions/error"
506
            }
507
          }
508
        }
509
      }
312
    }
510
    }
313
  },
511
  },
314
  "definitions": {
512
  "definitions": {
Lines 491-496 Link Here
491
    "itemnumber": {
689
    "itemnumber": {
492
      "description": "Item internal identifier"
690
      "description": "Item internal identifier"
493
    },
691
    },
692
    "message": {
693
      "type": "object",
694
      "properties": {
695
        "message_id": {
696
          "description": "Message internal identifier",
697
          "type": "integer"
698
        },
699
        "time_queued": {
700
          "description": "Date and time of when message was placed in queue",
701
          "type": "string"
702
        },
703
        "borrowernumber": {
704
          "$ref": "#/definitions/borrowernumber"
705
        },
706
        "subject": {
707
          "description": "Subject of the message",
708
          "type": "string"
709
        },
710
        "content": {
711
          "description": "Content of the message",
712
          "type": "string"
713
        },
714
        "metadata": {
715
          "description": "",
716
          "type": "string"
717
        },
718
        "letter_code": {
719
          "description": "",
720
          "type": "string"
721
        },
722
        "content_type": {
723
          "description": "Content type",
724
          "type": "string"
725
        },
726
        "message_transport_type": {
727
          "description": "Transport method. Values accepted by default are 'email', 'sms', 'phone' and 'print'",
728
          "type": "string"
729
        },
730
        "status": {
731
          "description": "Delivery status",
732
          "type": "string"
733
        },
734
        "to_address": {
735
          "description": "Destination email address",
736
          "type": "string"
737
        },
738
        "from_address": {
739
          "description": "Source address of email",
740
          "type": "string"
741
        },
742
        "delivery_note": {
743
          "description": "Additional delivery notes",
744
          "type": "string"
745
        }
746
      }
747
    },
494
    "error": {
748
    "error": {
495
      "type": "object",
749
      "type": "object",
496
      "properties": {
750
      "properties": {
Lines 529-534 Link Here
529
      "description": "Internal item identifier",
783
      "description": "Internal item identifier",
530
      "required": "true",
784
      "required": "true",
531
      "type": "integer"
785
      "type": "integer"
786
    },
787
    "messagenumberPathParam": {
788
      "name": "messagenumber",
789
      "in": "path",
790
      "description": "Internal message identifier",
791
      "required": "true",
792
      "type": "integer"
532
    }
793
    }
533
  },
794
  },
534
  "securityDefinitions": {
795
  "securityDefinitions": {
(-)a/installer/data/mysql/atomicupdate/Bug14843-Add_Permissions_for_message_queue_REST_operations.pl (+38 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright Open Source Freedom Fighters
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use C4::Context;
21
use Koha::AtomicUpdater;
22
23
my $dbh = C4::Context->dbh();
24
my $atomicUpdater = Koha::AtomicUpdater->new();
25
26
unless($atomicUpdater->find('Bug14843')) {
27
28
    use Koha::Auth::PermissionManager;
29
    my $pm = Koha::Auth::PermissionManager->new();
30
    $pm->addPermissionModule({module => 'messages', description => 'Permission regarding notifications and messages in message queue.'});
31
    $pm->addPermission({module => 'messages', code => 'get_message', description => "Allows to get the messages in message queue."});
32
    $pm->addPermission({module => 'messages', code => 'create_message', description => "Allows to create a new message and queue it."});
33
    $pm->addPermission({module => 'messages', code => 'update_message', description => "Allows to update messages in message queue."});
34
    $pm->addPermission({module => 'messages', code => 'delete_message', description => "Allows to delete a message and queue it."});
35
    $pm->addPermission({module => 'messages', code => 'resend_message', description => "Allows to resend messages in message queue."});
36
37
    print "Upgrade done (Bug 14843: Add Pemissions for message queue REST operations)\n";
38
}
(-)a/t/db_dependent/Api/V1/Messages.pm (+258 lines)
Line 0 Link Here
1
package t::db_dependent::Api::V1::Messages;
2
3
use Modern::Perl;
4
use Test::More;
5
6
use t::lib::TestObjects::ObjectFactory;
7
use t::lib::TestObjects::MessageQueueFactory;
8
9
use C4::Letters;
10
11
sub get200 {
12
    my ($class, $restTest, $driver) = @_;
13
    my $testContext = $restTest->get_testContext();
14
    my $activeUser = $restTest->get_activeBorrower();
15
    my $path = $restTest->get_routePath();
16
    
17
    #Create the test context.
18
    my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup(
19
                        {
20
                            subject => "The quick brown fox",
21
                            content => "Jumps over the lazy dog.",
22
                            cardnumber => $activeUser->{'_result'}->{'_column_data'}->{cardnumber},
23
                            message_transport_type => 'sms',
24
                            from_address => '11A001@example.com',
25
                        }, undef, $testContext, undef, undef);
26
27
    my $messagenumber = $messages->{'11A001@example.com'}->{message_id};
28
    
29
    $driver->get_ok($path => {Accept => 'text/json'});
30
    $driver->status_is(200);
31
32
    $driver->json_has("message_id", "Message id in JSON.");
33
34
    return 1;
35
}
36
37
sub get404 {
38
    my ($class, $restTest, $driver) = @_;
39
    my $path = $restTest->get_routePath();
40
    my $testContext = $restTest->get_testContext();
41
    
42
    $driver->get_ok($path => {Accept => 'text/json'});
43
    $driver->status_is(404);
44
    
45
    return 1;
46
}
47
48
sub get_n_200 {
49
    my ($class, $restTest, $driver) = @_;
50
    my $testContext = $restTest->get_testContext();
51
    my $activeUser = $restTest->get_activeBorrower();
52
    my $path = $restTest->get_routePath();
53
    
54
    #Create the test context.
55
    my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup(
56
                        {
57
                            subject => "The quick brown fox",
58
                            content => "Jumps over the lazy dog.",
59
                            cardnumber => $activeUser->{'_result'}->{'_column_data'}->{cardnumber},
60
                            message_transport_type => 'sms',
61
                            from_address => '11A002@example.com',
62
                        }, undef, $testContext, undef, undef);
63
64
    my $messagenumber = $messages->{'11A002@example.com'}->{message_id};
65
    $path =~ s/\{messagenumber\}/$messagenumber/;
66
           
67
    $driver->get_ok($path => {Accept => 'text/json'});
68
    $driver->status_is(200);
69
70
    $driver->json_is('/message_id' => $messagenumber, "Message $messagenumber found!");
71
72
    return 1;
73
}
74
75
sub get_n_404 {
76
    my ($class, $restTest, $driver) = @_;
77
    my $path = $restTest->get_routePath();
78
           
79
    $path =~ s/\{messagenumber\}/12349329003007/;
80
    $driver->get_ok($path => {Accept => 'text/json'});
81
    $driver->status_is(404);
82
    
83
    return 1;
84
}
85
86
sub post201 {
87
    my ($class, $restTest, $driver) = @_;
88
    my $path = $restTest->get_routePath();
89
    my $activeUser = $restTest->get_activeBorrower();
90
    
91
    $driver->post_ok($path => {Accept => 'text/json'} => json => {
92
            subject => "hello",
93
            content => "world",
94
            message_transport_type => "email",
95
            borrowernumber => $activeUser->{'_result'}->{'_column_data'}->{borrowernumber},
96
        });
97
    $driver->status_is(201);
98
    
99
    # dequeue the message right after so that get 404 tests will work
100
    my $json = $driver->tx->res->json();
101
    C4::Letters::DequeueLetter({ message_id => $json->{message_id} });
102
    
103
    return 1;
104
}
105
106
sub post400 {
107
    my ($class, $restTest, $driver) = @_;
108
    my $path = $restTest->get_routePath();
109
    my $activeUser = $restTest->get_activeBorrower();
110
    
111
    #post with missing mandatory parameters
112
    $driver->post_ok($path => {Accept => 'text/json'} => json => {
113
            subject => "hello"
114
        });
115
    $driver->status_is(400);
116
    
117
    return 1;
118
}
119
120
sub post_n_resend204 {
121
    my ($class, $restTest, $driver) = @_;
122
    my $testContext = $restTest->get_testContext();
123
    my $activeUser = $restTest->get_activeBorrower();
124
    my $path = $restTest->get_routePath();
125
    
126
    #Create the test context.
127
    my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup(
128
                        {
129
                            subject => "The quick brown fox",
130
                            content => "Jumps over the lazy dog.",
131
                            cardnumber => $activeUser->{'_result'}->{'_column_data'}->{cardnumber},
132
                            message_transport_type => 'sms',
133
                            from_address => '11A003@example.com',
134
                        }, undef, $testContext, undef, undef);
135
136
    # send and get the message
137
    C4::Letters::SendQueuedMessages();
138
    my $message = C4::Letters::GetMessage($messages->{'11A003@example.com'}->{message_id});
139
    my $messagenumber = $messages->{'11A003@example.com'}->{message_id};
140
    $path =~ s/\{messagenumber\}/$messagenumber/;
141
           
142
    # sms status should be failed
143
    is($message->{status}, "failed", "Send failed correctly");
144
    
145
    $driver->post_ok($path => {Accept => 'text/json'});
146
    $driver->status_is(204);
147
148
    $message = C4::Letters::GetMessage($messages->{'11A003@example.com'}->{message_id});
149
    is($message->{status}, "pending", "Resend set message status to pending correctly");
150
151
    return 1;
152
}
153
154
sub post_n_resend404 {
155
    my ($class, $restTest, $driver) = @_;
156
    my $path = $restTest->get_routePath();
157
158
    $path =~ s/\{messagenumber\}/12349329003007/;
159
    $driver->post_ok($path => {Accept => 'text/json'});
160
    $driver->status_is(404);
161
162
    return 1;
163
}
164
165
sub put_n_200 {
166
    my ($class, $restTest, $driver) = @_;
167
    my $testContext = $restTest->get_testContext();
168
    my $activeUser = $restTest->get_activeBorrower();
169
    my $path = $restTest->get_routePath();
170
    
171
    #Create the test context.
172
    my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup(
173
                        {
174
                            subject => "The quick brown fox",
175
                            content => "Jumps over the lazy dog.",
176
                            cardnumber => $activeUser->{'_result'}->{'_column_data'}->{cardnumber},
177
                            message_transport_type => 'sms',
178
                            from_address => '11A003@example.com',
179
                        }, undef, $testContext, undef, undef);
180
181
    my $messagenumber = $messages->{'11A003@example.com'}->{message_id};
182
    $path =~ s/\{messagenumber\}/$messagenumber/;
183
           
184
    $driver->put_ok($path => {Accept => 'text/json'} => json => {
185
            subject => "hello",
186
            content => "world",
187
        });
188
    $driver->status_is(200);
189
    
190
    my $json = $driver->tx->res->json();
191
    is($json->{subject}, "hello", "Put updated message subject correctly");
192
193
    return 1;
194
}
195
196
sub put_n_400 {
197
    my ($class, $restTest, $driver) = @_;
198
    my $testContext = $restTest->get_testContext();
199
    my $activeUser = $restTest->get_activeBorrower();
200
    my $path = $restTest->get_routePath();
201
    
202
    $path =~ s/\{messagenumber\}/12349329003007/;
203
    $driver->put_ok($path => {Accept => 'text/json'} => json => {
204
            message_transport_type => "hello", #invalid transport type
205
        });
206
    $driver->status_is(400);
207
    
208
    return 1;
209
}
210
211
sub put_n_404 {
212
    my ($class, $restTest, $driver) = @_;
213
    my $path = $restTest->get_routePath();
214
           
215
    $path =~ s/\{messagenumber\}/12349329003007/;
216
    $driver->put_ok($path => {Accept => 'text/json'});
217
    $driver->status_is(404);
218
    
219
    return 1;
220
}
221
222
sub delete_n_204 {
223
    my ($class, $restTest, $driver) = @_;
224
    my $testContext = $restTest->get_testContext();
225
    my $activeUser = $restTest->get_activeBorrower();
226
    my $path = $restTest->get_routePath();
227
    
228
    #Create the test context.
229
    my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup(
230
                        {
231
                            subject => "The quick brown fox",
232
                            content => "Jumps over the lazy dog.",
233
                            cardnumber => $activeUser->{'_result'}->{'_column_data'}->{cardnumber},
234
                            message_transport_type => 'sms',
235
                            from_address => '11A004@example.com',
236
                        }, undef, $testContext, undef, undef);
237
238
    my $messagenumber = $messages->{'11A004@example.com'}->{message_id};
239
    $path =~ s/\{messagenumber\}/$messagenumber/;
240
           
241
    $driver->delete_ok($path => {Accept => 'text/json'});
242
    $driver->status_is(204);
243
    
244
    return 1;
245
}
246
247
sub delete_n_404 {
248
    my ($class, $restTest, $driver) = @_;
249
    my $path = $restTest->get_routePath();
250
           
251
    $path =~ s/\{messagenumber\}/12349329003007/;
252
    $driver->delete_ok($path => {Accept => 'text/json'});
253
    $driver->status_is(404);
254
    
255
    return 1;
256
}
257
258
1;
(-)a/t/db_dependent/Letters.t (-2 / +11 lines)
Lines 18-24 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Test::More tests => 74;
21
use Test::More tests => 78;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
24
Lines 158-163 is( $resent, undef, 'ResendMessage should return undef if not message_id given' Link Here
158
# Delivery notes
158
# Delivery notes
159
is($messages->[0]->{delivery_note}, 'Missing SMS number', 'Delivery note for no smsalertnumber correctly set');
159
is($messages->[0]->{delivery_note}, 'Missing SMS number', 'Delivery note for no smsalertnumber correctly set');
160
160
161
# UpdateQueuedMessage
162
is(C4::Letters::UpdateQueuedMessage({ message_id => $messages->[0]->{message_id}, content => "changed content" } ), 1, "Message updated correctly");
163
$messages = C4::Letters::GetQueuedMessages();
164
is($messages->[0]->{content}, "changed content", "Message content was changed correctly");
165
161
# Test connectivity Exception (Bug 14791)
166
# Test connectivity Exception (Bug 14791)
162
ModMember(borrowernumber => $borrowernumber, smsalertnumber => "+1234567890");
167
ModMember(borrowernumber => $borrowernumber, smsalertnumber => "+1234567890");
163
warning_is { $messages_processed = C4::Letters::SendQueuedMessages(); }
168
warning_is { $messages_processed = C4::Letters::SendQueuedMessages(); }
Lines 169-174 is( $messages->[0]->{status}, 'pending', Link Here
169
is( $messages->[0]->{delivery_note}, 'Connection failed. Attempting to resend.',
174
is( $messages->[0]->{delivery_note}, 'Connection failed. Attempting to resend.',
170
    'Message has correct delivery note about resending' );
175
    'Message has correct delivery note about resending' );
171
176
177
#DequeueLetter
178
is(C4::Letters::DequeueLetter( { message_id => $messages->[0]->{message_id} } ), 1, "message successfully dequeued");
179
$messages = C4::Letters::GetQueuedMessages();
180
is( @$messages, 0, 'no messages left after dequeue' );
181
172
# GetLetters
182
# GetLetters
173
my $letters = C4::Letters::GetLetters();
183
my $letters = C4::Letters::GetLetters();
174
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
184
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
175
- 

Return to bug 14843