Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions perllib/FixMyStreet/Cobrand/Northumberland.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use base 'FixMyStreet::Cobrand::UKCouncils';
use strict;
use warnings;
use DateTime::Format::W3CDTF;
use FixMyStreet::Email;
use FixMyStreet::SMS;
use Utils;

=head2 Defaults
Expand Down Expand Up @@ -292,6 +294,63 @@ sub record_update_extra_fields {
};
}

=head2 inform_about_shortlisted

We send email and text to inspector when report (re)allocated to them.

=cut

sub inform_about_shortlisted {
my ( $self, $user, $report ) = @_;

# XXX In future we may want to do some extra filtering here, to only send
# to users with specific roles or
# for reports with specific categories

my $email = $user->email;
my $phone = $user->phone;
my $report_url = $self->base_url_for_report($report) . $report->url;

if ( $email && $user->email_verified ) {
my $h = {
cobrand => $self,
report => $report,
report_url => $report_url,
};

my $result = FixMyStreet::Email::send_cron(
FixMyStreet::DB->schema,
'inform-shortlisted.txt',
$h,
{ To => $email },
undef, # env_from
0, # nomail
$self,
$report->lang,
);

warn 'Email sending failed' if $result;
}

if ( $phone && $user->phone_verified ) {
my $parsed = FixMyStreet::SMS->parse_username($phone);

if ( $parsed->{may_be_mobile} ) {
my $result = FixMyStreet::SMS->new( cobrand => $self )->send(
to => $phone,
body => sprintf(
_("You have been assigned to report %s (reference %s); to view: %s"),
$report->title,
$report->id,
$report_url,
),
);

warn 'SMS sending failed' if $result;
}
}
}

=head2 open311_munge_update_params

We pass a report's 'detailed_information' (from its
Expand Down
12 changes: 7 additions & 5 deletions perllib/FixMyStreet/DB/Result/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,12 @@ around add_to_planned_reports => sub {
after add_to_planned_reports => sub {
my ( $self, $report, $no_comment ) = @_;

unless ($no_comment) {
my $cobrand = $report->get_cobrand_logged;
$cobrand
= $cobrand->call_hook( get_body_handler_for_problem => $report )
|| $cobrand;
my $cobrand = $report->get_cobrand_logged;
$cobrand
= $cobrand->call_hook( get_body_handler_for_problem => $report )
|| $cobrand;

unless ($no_comment) {
my $report_extra = $cobrand->call_hook('record_update_extra_fields');
$report->add_to_comments(
{ text => '',
Expand All @@ -665,6 +665,8 @@ after add_to_planned_reports => sub {
}
) if $report_extra->{shortlisted_user};
}

$cobrand->call_hook( inform_about_shortlisted => $self, $report );
};

# Override the default auto-created function as we don't want to ever delete anything
Expand Down
48 changes: 42 additions & 6 deletions t/cobrand/northumberland.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use CGI::Simple;
use FixMyStreet::TestMech;
use FixMyStreet::Map::OS::Leisure;
use FixMyStreet::Script::CSVExport;
use JSON::MaybeXS;
use Text::CSV;
use Test::MockModule;
use File::Temp 'tempdir';
Expand Down Expand Up @@ -174,15 +175,29 @@ FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'northumberland', 'fixmystreet' ],
MAPIT_URL => 'http://mapit.uk/',
COBRAND_FEATURES => {
os_maps_leisure => { _fallback => 1 }
os_maps_leisure => { _fallback => 1 },
sms_authentication => { northumberland => 1 },
govuk_notify => { northumberland => { key => 'test-0123456789abcdefghijklmnopqrstuvwxyz-key-goes-here' } },
}
}, sub {
my $o = Open311->new( fixmystreet_body => $body );

my $mod_lwp = Test::MockModule->new('LWP::UserAgent');
my $sms_content;
$mod_lwp->mock('post', sub {
my ( $self, $url, %args ) = @_;
my $data = decode_json( $args{Content} );
$sms_content = $data->{personalisation}{text};
HTTP::Response->new( 200, 'OK', [], '{ "id": 123 }' );
});

my $superuser = $mech->create_user_ok(
'[email protected]',
name => 'Super User',
phone => '+447700900002',
is_superuser => 1,
email_verified => 1,
phone_verified => 1,
);
$mech->log_in_ok( $superuser->email );

Expand All @@ -192,12 +207,16 @@ FixMyStreet::override_config {
my ($problem_to_update) = $mech->create_problems_for_body(
1,
$body->id,
'Test',
'A Test Report',
{ cobrand => $host, external_id => 123 },
);

subtest "User assignment on $host site" => sub {
$mech->get_ok( '/report/' . $problem_to_update->id );
$mech->clear_emails_ok;
$sms_content = undef;

my $id = $problem_to_update->id;
$mech->get_ok( '/report/' . $id );
$mech->submit_form_ok({ form_id => 'planned_form' });
my $comment
= $problem_to_update->comments->order_by('-id')->first;
Expand All @@ -206,7 +225,17 @@ FixMyStreet::override_config {
is_superuser => 1,
}, 'Comment created for user assignment';

my $id = $o->post_service_request_update($comment);
ok $mech->email_count_is(1), 'email sent for user assignment';
my $email = $mech->get_email;
like $mech->get_html_body_from_email($email),
qr/assigned.*\/report\/$id.*A Test Report/i;
like $mech->get_html_body_from_email($email),
qr/Reference: $id/i, 'Correct email HTML';
like $sms_content,
qr/assigned.*A Test Report.*$id.*\/report\/$id/,
'SMS sent for user assignment';

$o->post_service_request_update($comment);
my $cgi = CGI::Simple->new($o->test_req_used->content);
is $cgi->param('attribute[assigned_to_user_email]'),
$superuser->email,
Expand All @@ -215,7 +244,10 @@ FixMyStreet::override_config {
'',
'correct extra_details attribute';

$mech->get_ok( '/report/' . $problem_to_update->id );
$mech->clear_emails_ok;
$sms_content = undef;

$mech->get_ok( '/report/' . $id );
$mech->submit_form_ok({ form_id => 'planned_form' });
$comment
= $problem_to_update->comments->order_by('-id')->first;
Expand All @@ -224,7 +256,11 @@ FixMyStreet::override_config {
is_superuser => 1,
}, 'Comment created for user un-assignment';

$id = $o->post_service_request_update($comment);
ok $mech->email_count_is(0),
'email not sent for user un-assignment';
ok !$sms_content, 'SMS not sent for user un-assignment';

$o->post_service_request_update($comment);
$cgi = CGI::Simple->new($o->test_req_used->content);
is $cgi->param('attribute[assigned_to_user_email]'),
'',
Expand Down
23 changes: 23 additions & 0 deletions templates/email/northumberland/inform-shortlisted.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[%

title = report.title;
email_columns = 2;

PROCESS '_email_settings.html';

INCLUDE '_email_top.html';

%]

<th style="[% td_style %][% primary_column_style %]" id="primary_column">
[% start_padded_box | safe %]
<h1 style="[% h1_style %]">You have been assigned to <a href="[% report_url %]">[% title %]</a></h1>
<p>Reference: [% report.id %]</p>
[% end_padded_box | safe %]
</th>
[% WRAPPER '_email_sidebar.html' object = report %]
<h2 style="[% h2_style %]">[% title %]</h2>
[% report.detail | html_para_email(secondary_p_style) %]
[% END %]

[% INCLUDE '_email_bottom.html' %]
5 changes: 5 additions & 0 deletions templates/email/northumberland/inform-shortlisted.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Subject: You have been assigned to a report (ref [% report.id %])

You have been assigned to a report: [% cobrand.base_url_for_report(report) %][% report.url %].

This email was sent automatically, from an unmonitored email account. Please do not reply to it.