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

(-)a/C4/Letters.pm (-9 / +18 lines)
Lines 1021-1027 sub GetQueuedMessages { Link Here
1021
1021
1022
    my $dbh = C4::Context->dbh();
1022
    my $dbh = C4::Context->dbh();
1023
    my $statement = << 'ENDSQL';
1023
    my $statement = << 'ENDSQL';
1024
SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued
1024
SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued, sentto_address
1025
FROM message_queue
1025
FROM message_queue
1026
ENDSQL
1026
ENDSQL
1027
1027
Lines 1150-1166 sub _send_message_by_email { Link Here
1150
1150
1151
    my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} );
1151
    my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} );
1152
    my $to_address = $message->{'to_address'};
1152
    my $to_address = $message->{'to_address'};
1153
    if (!$to_address && $member) {
1154
        $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} );
1155
    }
1153
    my $sendAllEmailsTo = C4::Context->preference('SendAllEmailsTo');
1156
    my $sendAllEmailsTo = C4::Context->preference('SendAllEmailsTo');
1154
    $to_address = $sendAllEmailsTo if ($sendAllEmailsTo && $sendAllEmailsTo =~ /@/ ); # some validation.  This could be improved.
1157
    $to_address = $sendAllEmailsTo if ($sendAllEmailsTo && $sendAllEmailsTo =~ /@/ ); # some validation.  This could be improved.
1155
    unless ($to_address) {
1158
    unless ($to_address) {
1156
        unless ($member) {
1159
        if (!$member) {
1157
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1160
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1158
            _set_message_status( { message_id => $message->{'message_id'},
1161
            _set_message_status( { message_id => $message->{'message_id'},
1159
                                   status     => 'failed' } );
1162
                                   status     => 'failed' } );
1160
            return;
1163
            return;
1161
        }
1164
        }
1162
        $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} );
1165
        else {
1163
        unless ($to_address) {  
1164
            # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1166
            # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1165
            # warning too verbose for this more common case?
1167
            # warning too verbose for this more common case?
1166
            _set_message_status( { message_id => $message->{'message_id'},
1168
            _set_message_status( { message_id => $message->{'message_id'},
Lines 1204-1211 sub _send_message_by_email { Link Here
1204
1206
1205
    _update_message_to_address($message->{'message_id'},$to_address) unless $message->{to_address}; #if initial message address was empty, coming here means that a to address was found and queue should be updated
1207
    _update_message_to_address($message->{'message_id'},$to_address) unless $message->{to_address}; #if initial message address was empty, coming here means that a to address was found and queue should be updated
1206
    if ( sendmail( %sendmail_params ) ) {
1208
    if ( sendmail( %sendmail_params ) ) {
1207
        _set_message_status( { message_id => $message->{'message_id'},
1209
        _set_message_status( { message_id     => $message->{'message_id'},
1208
                status     => 'sent' } );
1210
                               status         => 'sent',
1211
                               sentto_address => $to_address } );
1209
        return 1;
1212
        return 1;
1210
    } else {
1213
    } else {
1211
        _set_message_status( { message_id => $message->{'message_id'},
1214
        _set_message_status( { message_id => $message->{'message_id'},
Lines 1289-1299 sub _set_message_status { Link Here
1289
        return unless exists $params->{ $required_parameter };
1292
        return unless exists $params->{ $required_parameter };
1290
    }
1293
    }
1291
1294
1292
    my $dbh = C4::Context->dbh();
1293
    my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1295
    my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1296
    my @values;
1297
    push @values,$params->{'status'};
1298
    if (exists($params->{'sentto_address'})) {
1299
        $statement = 'UPDATE message_queue SET status=?, sentto_address=? WHERE message_id = ?';
1300
        push @values,$params->{'sentto_address'};
1301
    }
1302
    push @values,$params->{'message_id'};
1303
    my $dbh = C4::Context->dbh();
1294
    my $sth = $dbh->prepare( $statement );
1304
    my $sth = $dbh->prepare( $statement );
1295
    my $result = $sth->execute( $params->{'status'},
1305
    my $result = $sth->execute( @values );
1296
                                $params->{'message_id'} );
1297
    return $result;
1306
    return $result;
1298
}
1307
}
1299
1308
(-)a/t/db_dependent/Letters.t (-2 / +31 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 => 63;
21
use Test::More tests => 67;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
24
Lines 111-117 $my_message->{message_transport_type} = 'sms'; Link Here
111
$message_id = C4::Letters::EnqueueLetter($my_message);
111
$message_id = C4::Letters::EnqueueLetter($my_message);
112
ok(defined $message_id && $message_id > 0, 'new message successfully queued');
112
ok(defined $message_id && $message_id > 0, 'new message successfully queued');
113
113
114
# This is will force C4::Context->preference() to read the DB.
115
C4::Context->disable_syspref_cache();
116
114
# used to trigger _send_message_by_email in C4::Letter
117
# used to trigger _send_message_by_email in C4::Letter
118
# first override.
119
$dbh->do("UPDATE systempreferences SET value='override\@example.com' WHERE variable='SendAllEmailsTo';");
115
my $message_id2 = C4::Letters::EnqueueLetter({
120
my $message_id2 = C4::Letters::EnqueueLetter({
116
    borrowernumber         => $borrowernumber,
121
    borrowernumber         => $borrowernumber,
117
    message_transport_type => 'email',
122
    message_transport_type => 'email',
Lines 150-160 is($messages_processed, 2, 'all queued messages processed'); Link Here
150
$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
155
$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
151
is(scalar(@$messages), 2, 'two messages stored for the borrower');
156
is(scalar(@$messages), 2, 'two messages stored for the borrower');
152
157
158
# used to trigger _send_message_by_email in C4::Letter
159
# second no override.
160
$dbh->do("UPDATE systempreferences SET value='' WHERE variable='SendAllEmailsTo';");
161
my $message_id3 = C4::Letters::EnqueueLetter({
162
    borrowernumber         => $borrowernumber,
163
    message_transport_type => 'email',
164
    to_address             => 'to@example.com',
165
    from_address           => 'from@example.com',
166
    letter => {
167
        content      => 'a message',
168
        title        => 'message title',
169
        metadata     => 'metadata',
170
        code         => 'TEST_MESSAGE',
171
        content_type => 'text/plain',
172
    },
173
});
174
175
$messages_processed = C4::Letters::SendQueuedMessages();
176
is($messages_processed, 1, 'third queued message processed');
177
178
$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
179
is(scalar(@$messages), 3, 'three messages stored for the borrower');
180
153
is(
181
is(
154
    $messages->[0]->{status},
182
    $messages->[0]->{status},
155
    'failed',
183
    'failed',
156
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
184
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
157
);
185
);
186
is ( $messages->[1]->{sentto_address},'override@example.com','Second message sent to override.');
187
is ( $messages->[2]->{sentto_address},'to@example.com','Third message sent to default.');
158
188
159
# GetLetters
189
# GetLetters
160
my $letters = C4::Letters::GetLetters();
190
my $letters = C4::Letters::GetLetters();
161
- 

Return to bug 8000