Skip to content

Commit

Permalink
[Northumberland] Add comment when assigned user or extra details updated
Browse files Browse the repository at this point in the history
  • Loading branch information
nephila-nacrea authored and dracos committed Sep 18, 2024
1 parent 3a44bbd commit 11cc647
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 2 deletions.
18 changes: 17 additions & 1 deletion perllib/FixMyStreet/App/Controller/Report.pm
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ sub inspect : Private {
if ($permissions->{report_inspect}) {
$problem->set_extra_metadata( traffic_information => $c->get_param('traffic_information') );

my $old_detailed_information
= $problem->get_extra_metadata('detailed_information') // '';
if ( my $info = $c->get_param('detailed_information') ) {
$problem->set_extra_metadata( detailed_information => $info );
if ($c->cobrand->max_detailed_info_length &&
Expand All @@ -476,6 +478,20 @@ sub inspect : Private {
$problem->unset_extra_metadata('detailed_information');
}

my $handler
= $c->cobrand->call_hook(
get_body_handler_for_problem => $problem
) || $c->cobrand;
my $record_extra
= $handler->call_hook('record_update_extra_fields');
if (
$record_extra->{detailed_information}
&& ( $problem->get_extra_metadata('detailed_information') // '' )
ne $old_detailed_information
) {
$update_params{extra}{detailed_information} = 1;
}

if ( $c->get_param('include_update') or $c->get_param('raise_defect') ) {
$update_text = Utils::cleanup_text( $c->get_param('public_update'), { allow_multiline => 1 } );
if (!$update_text) {
Expand Down Expand Up @@ -523,7 +539,7 @@ sub inspect : Private {
# If the state has been changed to action scheduled and they've said
# they want to raise a defect, consider the report to be inspected.
if ($problem->state eq 'action scheduled' && $c->get_param('raise_defect') && !$problem->get_extra_metadata('inspected')) {
$update_params{extra} = { 'defect_raised' => 1 };
$update_params{extra}{defect_raised} = 1;
$problem->set_extra_metadata( inspected => 1 );
$c->forward( '/admin/log_edit', [ $problem->id, 'problem', 'inspected' ] );
}
Expand Down
13 changes: 13 additions & 0 deletions perllib/FixMyStreet/Cobrand/Northumberland.pm
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ sub should_skip_sending_update {
=cut

=head2 record_update_extra_fields
We want to create comments when assigned (= shortlisted) user or
extra details (= detail_information) are updated for a report.
=cut

sub record_update_extra_fields {
{ shortlisted_user => 1,
detailed_information => 1,
};
}

=head2 open311_munge_update_params
We pass a report's 'detailed_information' (from its
Expand Down
38 changes: 38 additions & 0 deletions perllib/FixMyStreet/DB/Result/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,25 @@ around add_to_planned_reports => sub {
return $self->$orig(@_);
};

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 $report_extra = $cobrand->call_hook('record_update_extra_fields');
$report->add_to_comments(
{ text => '',
user => $self,
extra => { shortlisted_user => $self->email },
}
) if $report_extra->{shortlisted_user};
}
};

# Override the default auto-created function as we don't want to ever delete anything
around remove_from_planned_reports => sub {
my ($orig, $self, $report) = @_;
Expand All @@ -656,6 +675,25 @@ around remove_from_planned_reports => sub {
$report->update;
};

after remove_from_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 $report_extra = $cobrand->call_hook('record_update_extra_fields');
$report->add_to_comments(
{ text => '',
user => $self,
extra => { shortlisted_user => undef },
}
) if $report_extra->{shortlisted_user};
}
};

sub active_planned_reports {
my $self = shift;
$self->planned_reports->search({ removed => undef });
Expand Down
4 changes: 3 additions & 1 deletion perllib/Open311/UpdatesBase.pm
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ sub _process_update {
);
}

$assigned_user->add_to_planned_reports($p);
$assigned_user->add_to_planned_reports($p, 'no_comment');

# TODO Unassign?
}
if ( exists $request->{extras}{detailed_information} ) {
$request->{extras}{detailed_information}
Expand Down
10 changes: 10 additions & 0 deletions t/app/model/problem.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ use FixMyStreet::DB;
use FixMyStreet::Script::Reports;
use Open311::GetUpdates;
use Sub::Override;
use Test::MockModule;

my $cobrand = Test::MockModule->new('FixMyStreet::Cobrand::Default');
$cobrand->mock(
record_update_extra_fields => sub { { shortlisted_user => 1 } }
);

my $problem_rs = FixMyStreet::DB->resultset('Problem');

Expand Down Expand Up @@ -284,6 +290,10 @@ for my $test (
is $problem->shortlisted_user->email,
$assigned_user_email,
'assigned user actually assigned to problem';
is $problem->comments->count, 1, 'initial comment only';
for ( $problem->comments ) {
is $_->extra, undef, 'No extra data';
}
}

my $detailed_information = $extras->{detailed_information};
Expand Down
119 changes: 119 additions & 0 deletions t/cobrand/northumberland.t
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use CGI::Simple;
use FixMyStreet::TestMech;
use FixMyStreet::Script::CSVExport;
use Test::MockModule;
Expand Down Expand Up @@ -111,4 +112,122 @@ FixMyStreet::override_config {
};
};

FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'northumberland', 'fixmystreet' ],
MAPIT_URL => 'http://mapit.uk/',
}, sub {
my $o = Open311->new( fixmystreet_body => $body );

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

for my $host ( 'northumberland', 'fixmystreet' ) {
ok $mech->host($host), "change host to $host";

my ($problem_to_update) = $mech->create_problems_for_body(
1,
$body->id,
'Test',
{ cobrand => $host },
);

subtest "User assignment on $host site" => sub {
$mech->get_ok( '/report/' . $problem_to_update->id );
$mech->click_ok('.btn--shortlist');
my $comment
= $problem_to_update->comments->order_by('-id')->first;
is_deeply $comment->get_extra_metadata, {
shortlisted_user => $superuser->email,
is_superuser => 1,
}, 'Comment created for user assignment';

my $id = $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,
'correct assigned_to_user_email attribute';
is $cgi->param('attribute[extra_details]'),
'',
'correct extra_details attribute';

$mech->get_ok( '/report/' . $problem_to_update->id );
$mech->click_ok('.btn--shortlisted');
$comment
= $problem_to_update->comments->order_by('-id')->first;
is_deeply $comment->get_extra_metadata, {
shortlisted_user => undef,
is_superuser => 1,
}, 'Comment created for user un-assignment';

$id = $o->post_service_request_update($comment);
$cgi = CGI::Simple->new($o->test_req_used->content);
is $cgi->param('attribute[assigned_to_user_email]'),
'',
'correct assigned_to_user_email attribute';
is $cgi->param('attribute[extra_details]'),
'',
'correct extra_details attribute';
};

subtest "Extra details on $host site" => sub {
$mech->get_ok( '/report/' . $problem_to_update->id );
$mech->submit_form(
button => 'save',
form_id => 'report_inspect_form',
fields => {
detailed_information => 'ABC',
include_update => 0,
},
);
my $comment
= $problem_to_update->comments->order_by('-id')->first;
is_deeply $comment->get_extra_metadata, {
detailed_information => 1,
is_superuser => 1,
}, 'Comment created for extra details';

my $id = $o->post_service_request_update($comment);
my $cgi = CGI::Simple->new($o->test_req_used->content);
is $cgi->param('attribute[assigned_to_user_email]'),
'',
'correct assigned_to_user_email attribute';
is $cgi->param('attribute[extra_details]'),
'ABC',
'correct extra_details attribute';

$mech->get_ok( '/report/' . $problem_to_update->id );
$mech->submit_form(
button => 'save',
form_id => 'report_inspect_form',
fields => {
detailed_information => '',
include_update => 0,
},
);
$comment
= $problem_to_update->comments->order_by('-id')->first;
is_deeply $comment->get_extra_metadata, {
detailed_information => 1,
is_superuser => 1,
}, 'Comment created for extra details unsetting';
is_deeply $problem_to_update->get_extra_metadata,
{},
'Extra details unset on problem';

$id = $o->post_service_request_update($comment);
$cgi = CGI::Simple->new($o->test_req_used->content);
is $cgi->param('attribute[assigned_to_user_email]'),
'',
'correct assigned_to_user_email attribute';
is $cgi->param('attribute[extra_details]'),
'',
'correct extra_details attribute';
};
}
};

done_testing();

0 comments on commit 11cc647

Please sign in to comment.