Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Northumberland] Fetch 'detailed_information' (extra details) to/from Alloy & assigned user to Alloy #5147

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
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 @@
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 @@
$c->cobrand->max_detailed_info_length
);
}
} elsif ( $c->get_param('detailed_information') eq '' ) {
dracos marked this conversation as resolved.
Show resolved Hide resolved
$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;
dracos marked this conversation as resolved.
Show resolved Hide resolved
}

if ( $c->get_param('include_update') or $c->get_param('raise_defect') ) {
Expand Down Expand Up @@ -521,7 +539,7 @@
# 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;

Check warning on line 542 in perllib/FixMyStreet/App/Controller/Report.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Report.pm#L542

Added line #L542 was not covered by tests
$problem->set_extra_metadata( inspected => 1 );
$c->forward( '/admin/log_edit', [ $problem->id, 'problem', 'inspected' ] );
}
Expand Down
39 changes: 39 additions & 0 deletions perllib/FixMyStreet/Cobrand/Northumberland.pm
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,43 @@ 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
extra_metadata) to Alloy, as an 'extra_details' attribute.

We pass the name and email address of the user assigned to the report (the
user who has shortlisted the report).

=cut

sub open311_munge_update_params {
my ( $self, $params, $comment, undef ) = @_;

my $p = $comment->problem;

my $detailed_information
= $p->get_extra_metadata('detailed_information') // '';
$params->{'attribute[extra_details]'} = $detailed_information;

my $assigned_to = $p->shortlisted_user;
$params->{'attribute[assigned_to_user_email]'}
= $assigned_to
? $assigned_to->email
: '';
}

1;
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
64 changes: 37 additions & 27 deletions perllib/Open311/UpdatesBase.pm
Original file line number Diff line number Diff line change
Expand Up @@ -188,34 +188,44 @@ sub _process_update {
$request->{comment_time} = $p->whensent + DateTime::Duration->new( seconds => 1 );
}

# Assign admin user to report if 'assigned_user_*' fields supplied
if ( $request->{extras} && $request->{extras}{assigned_user_email} ) {
my $assigned_user_email = $request->{extras}{assigned_user_email};
my $assigned_user_name = $request->{extras}{assigned_user_name};

my $assigned_user
= FixMyStreet::DB->resultset('User')
->find( { email => $assigned_user_email } );

unless ($assigned_user) {
$assigned_user = FixMyStreet::DB->resultset('User')->create(
{ email => $assigned_user_email,
name => $assigned_user_name,
from_body => $body->id,
email_verified => 1,
},
);

# Make them an inspector
# TODO Other permissions required?
$assigned_user->user_body_permissions->create(
{ body_id => $body->id,
permission_type => 'report_inspect',
}
);
}
if ( $request->{extras} ) {
# Assign admin user to report if 'assigned_user_*' fields supplied
if ( $request->{extras}{assigned_user_email} ) {
my $assigned_user_email = $request->{extras}{assigned_user_email};
my $assigned_user_name = $request->{extras}{assigned_user_name};

my $assigned_user
= FixMyStreet::DB->resultset('User')
->find( { email => $assigned_user_email } );

unless ($assigned_user) {
$assigned_user = FixMyStreet::DB->resultset('User')->create(
{ email => $assigned_user_email,
name => $assigned_user_name,
from_body => $body->id,
email_verified => 1,
},
);

# Make them an inspector
# TODO Other permissions required?
$assigned_user->user_body_permissions->create(
{ body_id => $body->id,
permission_type => 'report_inspect',
}
);
}

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

$assigned_user->add_to_planned_reports($p);
# TODO Unassign?
}
if ( exists $request->{extras}{detailed_information} ) {
$request->{extras}{detailed_information}
? $p->set_extra_metadata( detailed_information =>
$request->{extras}{detailed_information} )
: $p->unset_extra_metadata('detailed_information');
}
}

my $comment = $self->schema->resultset('Comment')->new(
Expand Down
92 changes: 71 additions & 21 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 @@ -203,13 +209,42 @@ for my $test (
},
state => 'confirmed',
},
{
desc => 'adding detailed information',
request => {
comment_time => $comment_time,
status => 'open',
description => 'Adding detailed information',
extras => {
detailed_information => 'Hello there',
},
},
state => 'confirmed',
},
{
desc => 'deleting detailed information',
request => {
comment_time => $comment_time,
status => 'open',
description => 'Deleting detailed information',
extras => {
detailed_information => '',
},
},
state => 'confirmed',
start_detailed_information => 'Goodbye here',
},
) {
subtest $test->{desc} => sub {
# makes testing easier;
$problem->comments->delete;
$problem->created( DateTime->now()->subtract( days => 1 ) );
$problem->lastupdate( DateTime->now()->subtract( days => 1 ) );
$problem->state( $test->{start_state} || 'confirmed' );
$problem->unset_extra_metadata('detailed_information');
$problem->set_extra_metadata( 'detailed_information',
$test->{start_detailed_information} )
if $test->{start_detailed_information};
$problem->update;
my $w3c = DateTime::Format::W3CDTF->new();

Expand All @@ -228,29 +263,44 @@ for my $test (
is $problem->state, $test->{state}, 'problem state';
is $update->text, $test->{request}->{description}, 'update text';

if ( my $assigned = $test->{request}{extras} ) {
my $assigned_user = FixMyStreet::DB->resultset('User')
->search( { email => $assigned->{assigned_user_email} } )
->first;

# Check certain fields set for new user
if ( $assigned->{assigned_user_email} ne $existing_user->email ) {
is $assigned_user->from_body->id, $body->id,
'assigned user body';
is $assigned_user->email_verified, 1,
'assigned user email verified';
is_deeply $assigned_user->body_permissions,
[ { body_id => $body->id, permission => 'report_inspect' }
],
'assigned user permissions';
if ( my $extras = $test->{request}{extras} ) {
my $assigned_user_name = $extras->{assigned_user_name};
my $assigned_user_email = $extras->{assigned_user_email};

if ( $assigned_user_email ) {
my $assigned_user = FixMyStreet::DB->resultset('User')
->search( { email => $assigned_user_email } )
->first;

# Check certain fields set for new user
if ( $assigned_user_email ne $existing_user->email ) {
is $assigned_user->from_body->id, $body->id,
'assigned user body';
is $assigned_user->email_verified, 1,
'assigned user email verified';
is_deeply $assigned_user->body_permissions,
[ { body_id => $body->id, permission => 'report_inspect' }
],
'assigned user permissions';
}

is $assigned_user->name, $assigned_user_name,
'assigned user name';

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

is $assigned_user->name, $assigned->{assigned_user_name},
'assigned user name';

is $problem->shortlisted_user->email,
$assigned->{assigned_user_email},
'assigned user actually assigned to problem';
my $detailed_information = $extras->{detailed_information};
if ( defined $detailed_information ) {
is $problem->get_extra_metadata('detailed_information'),
$detailed_information || undef;
}
}
};
}
Expand Down
Loading
Loading