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
We don't want to do this when pulling update from Alloy, hence the
'no_comment' flag.
  • Loading branch information
nephila-nacrea committed Sep 10, 2024
1 parent b0cecdd commit 10ac3cd
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 2 deletions.
20 changes: 19 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 @@ -472,6 +474,22 @@ sub inspect : Private {
$c->cobrand->max_detailed_info_length
);
}
} elsif ( $c->get_param('detailed_information') eq '' ) {
$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') ) {
Expand Down Expand Up @@ -521,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 @@ -636,6 +636,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 @@ -644,6 +663,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
80 changes: 80 additions & 0 deletions t/cobrand/northumberland.t
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,84 @@ FixMyStreet::override_config {
};
};

FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'northumberland', 'fixmystreet' ],
MAPIT_URL => 'http://mapit.uk/',
}, sub {
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';

$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';
};

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 => 'A',
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';

$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';
};
}
};

done_testing();

0 comments on commit 10ac3cd

Please sign in to comment.