Skip to content

Commit

Permalink
Merge branch 'bexley-ww-container-requests' into commercial-staging
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Nov 1, 2024
2 parents 6a5ac25 + e623f62 commit 9b70a29
Show file tree
Hide file tree
Showing 16 changed files with 454 additions and 124 deletions.
76 changes: 66 additions & 10 deletions perllib/FixMyStreet/App/Controller/Waste/Whitespace.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,75 @@ has log_ident => (
sub receive_whitespace_event_notification : Path('/waste/whitespace') : Args(0) {
my ($self, $c) = @_;

my %headers = $c->req->headers->flatten;
$self->log($c->req->method);
$self->log(\%headers);
$self->log($c->req->parameters);
if ($c->req->body) {
my $soap = join('', $c->req->body->getlines);
$self->log($soap);
} else {
$self->log('No body');
}
require SOAP::Lite;

$c->detach('/waste/echo/soap_error', [ 'Invalid method', 405 ]) unless $c->req->method eq 'POST';

my $whitespace = $c->cobrand->feature('whitespace');
$c->detach('/waste/echo/soap_error', [ 'Missing config', 500 ]) unless $whitespace;

$c->detach('/waste/echo/soap_error', [ 'Missing body' ]) unless $c->req->body;
my $soap = join('', $c->req->body->getlines);
$self->log($soap);

my $body = $c->cobrand->body;
$c->detach('soap_error', [ 'Bad jurisdiction' ]) unless $body;

my $env = SOAP::Deserializer->deserialize($soap);
if ($env->valueof('//WorksheetPoke/secret') ne $whitespace->{push_secret}) {
return $c->detach('/waste/echo/soap_error', [ 'Unauthorized', 401 ]);
};

# Return okay if we're in endpoint test mode
my $cobrand_check = $c->cobrand->feature('waste');
$c->detach('soap_ok') if $cobrand_check eq 'echo-push-only';

my $worksheet = {
id => $env->valueof('//WorksheetPoke/worksheetId'),
ref => $env->valueof('//WorksheetPoke/worksheetReference'),
completed => $env->valueof('//WorksheetPoke/completedDate'),
};
$c->detach('/waste/echo/soap_error', [ 'Bad request', 400 ]) if _check_params($worksheet);

$c->forward('update_report', [ $worksheet ]);
$c->forward('soap_ok');
}

sub soap_ok : Private {
my ($self, $c) = @_;
$c->response->status(200);
$c->response->body('OK');
}

sub update_report : Private {
my ($self, $c, $worksheet) = @_;

my $request = $c->cobrand->construct_waste_open311_update({}, $worksheet);
return if !$request->{status} || $request->{status} eq 'confirmed';

my $report = delete $request->{report};
return unless $report;

$request->{comment_time} =
DateTime::Format::W3CDTF->parse_datetime($worksheet->{completed})
->set_time_zone(FixMyStreet->local_time_zone);

my $body = $c->cobrand->body;
my $updates = Open311::GetServiceRequestUpdates->new(
current_body => $body,
system_user => $body->comment_user
);

return unless $c->cobrand->waste_check_last_update({}, $report, $request->{status});
$updates->process_update($request, $report);
}

sub _check_params {
my $hash = shift;

for my $key (keys %{$hash}) {
return 1 unless $hash->{$key};
}
}

1;
109 changes: 58 additions & 51 deletions perllib/FixMyStreet/Cobrand/Bexley/Waste.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ C<0001-01-01T00:00:00> represents an undefined date in Whitespace.
=cut

use constant WHITESPACE_UNDEF_DATE => '0001-01-01T00:00:00';
use constant MISSED_COLLECTION_SERVICE_PROPERTY_ID => 68;

sub waste_fetch_events {
my ( $self, $params ) = @_;
Expand All @@ -70,59 +71,18 @@ sub waste_fetch_events {
{ order_by => 'id' },
);

my $missed_collection_service_property_id = 68;
my $db = FixMyStreet::DB->schema->storage;

while ( my $report = $missed_collection_reports->next ) {
print 'Fetching data for report ' . $report->id . "\n" if $params->{verbose};

my $worksheet_id = $report->external_id =~ s/Whitespace-//r;
my $worksheet
= $self->whitespace->GetFullWorksheetDetails($worksheet_id);

# Get info for missed collection
my $missed_collection_properties;
for my $service_properties (
@{ $worksheet->{WSServiceProperties}{WorksheetServiceProperty}
// []
}
) {
next
unless $service_properties->{ServicePropertyID}
== $missed_collection_service_property_id;
my $request = $self->construct_waste_open311_update($params, {
id => $worksheet_id,
report => $report,
});
next if !$request->{status} || $request->{status} eq 'confirmed'; # Still in initial state
next unless $self->waste_check_last_update($params, $report, $request->{status});

$missed_collection_properties = $service_properties;
}

my $whitespace_state_string
= $missed_collection_properties
? $missed_collection_properties->{ServicePropertyValue}
: '';

my $config = $self->feature('whitespace');
my $new_state
= $config->{missed_collection_state_mapping}
{$whitespace_state_string};
unless ($new_state) {
print " No new state, skipping\n" if $params->{verbose};
next;
}

next
unless $self->waste_check_last_update( $params, $report,
$new_state );

my $request = {
description => $new_state->{text},
# No data from Whitespace for this, so make it now
comment_time =>
DateTime->now->set_time_zone( FixMyStreet->local_time_zone ),
external_status_code => $whitespace_state_string,
prefer_template => 1,
status => $new_state->{fms_state},
# TODO Is there an ID for specific worksheet update?
update_id => $report->external_id,
};
$request->{comment_time} = DateTime->now->set_time_zone( FixMyStreet->local_time_zone );

print
" Updating report to state '$request->{status}' - '$request->{description}' ($request->{external_status_code})\n"
Expand All @@ -135,14 +95,61 @@ sub waste_fetch_events {
}
}

sub construct_waste_open311_update {
my ($self, $params, $worksheet) = @_;

my $report = $worksheet->{report} || $self->problems->find($worksheet->{ref});
return unless $report;

$worksheet = $self->whitespace->GetFullWorksheetDetails($worksheet->{id});

# Get info for missed collection
my $missed_collection_properties;
for my $service_properties (
@{ $worksheet->{WSServiceProperties}{WorksheetServiceProperty}
// []
}
) {
next
unless $service_properties->{ServicePropertyID}
== MISSED_COLLECTION_SERVICE_PROPERTY_ID;

$missed_collection_properties = $service_properties;
}

my $whitespace_state_string
= $missed_collection_properties
? $missed_collection_properties->{ServicePropertyValue}
: '';

my $config = $self->feature('whitespace');
my $new_state
= $config->{missed_collection_state_mapping}
{$whitespace_state_string};
unless ($new_state) {
print " No new state, skipping\n" if $params->{verbose};
return;
}

my $request = {
description => $new_state->{text},
status => $new_state->{fms_state},
update_id => 'waste',
external_status_code => $whitespace_state_string,
prefer_template => 1,
report => $report,
};
return $request;
}

sub waste_check_last_update {
my ( $self, $params, $report, $new_state ) = @_;
my ( $self, $params, $report, $state ) = @_;

my $last_update = $report->comments->search(
{ external_id => { like => 'Whitespace%' } },
{ external_id => 'waste', },
)->order_by('-id')->first;

if ( $last_update && $new_state->{fms_state} eq $last_update->problem_state ) {
if ( $last_update && $state eq $last_update->problem_state ) {
print " Latest update matches fetched state, skipping\n" if $params->{verbose};
return;
}
Expand Down
4 changes: 1 addition & 3 deletions perllib/FixMyStreet/Cobrand/Borsetshire.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ sub path_to_pin_icons {
return '/cobrands/oxfordshire/images/';
}

sub send_questionnaires {
return 0;
}
sub send_questionnaires { 0 }

sub bypass_password_checks { 1 }

Expand Down
4 changes: 1 addition & 3 deletions perllib/FixMyStreet/Cobrand/Bromley.pm
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ sub recent_photos {
});
}

sub send_questionnaires {
return 0;
}
sub send_questionnaires { 0 }

sub ask_ever_reported {
return 0;
Expand Down
4 changes: 1 addition & 3 deletions perllib/FixMyStreet/Cobrand/Buckinghamshire.pm
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ sub permission_body_override {
# Assume that any category change means the report should be resent
sub category_change_force_resend { 1 }

sub send_questionnaires {
return 0;
}
sub send_questionnaires { 0 }

=head2 post_report_sent
Expand Down
4 changes: 1 addition & 3 deletions perllib/FixMyStreet/Cobrand/Camden.pm
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ sub new_report_title_field_hint {
=cut

sub send_questionnaires {
return 0;
}
sub send_questionnaires { 0 }

=item * We link to Camden's site for the privacy policy
Expand Down
4 changes: 1 addition & 3 deletions perllib/FixMyStreet/Cobrand/Hart.pm
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ sub categories_restriction {
=cut

sub send_questionnaires {
return 0;
}
sub send_questionnaires { 0 }

sub ask_ever_reported {
return 0;
Expand Down
2 changes: 1 addition & 1 deletion perllib/FixMyStreet/Cobrand/Oxfordshire.pm
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ sub disambiguate_location {
}

# don't send questionnaires to people who used the OCC cobrand to report their problem
sub send_questionnaires { return 0; }
sub send_questionnaires { 0 }

# increase map zoom level so street names are visible
sub default_map_zoom { 5 }
Expand Down
4 changes: 1 addition & 3 deletions perllib/FixMyStreet/Cobrand/Rutland.pm
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ sub disambiguate_location {
=cut

sub send_questionnaires {
return 0;
}
sub send_questionnaires { 0 }

sub ask_ever_reported {
return 0;
Expand Down
13 changes: 13 additions & 0 deletions perllib/FixMyStreet/Cobrand/WestNorthants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ sub pin_colour {
return $self->SUPER::pin_colour($p, $context);
}

=item * We allow staff to bypass stoppers.
=cut

sub staff_ignore_form_disable_form {
my $self = shift;

my $c = $self->{c};

return $c->user_exists
&& $c->user->belongs_to_body( $self->body->id );
}

around 'munge_sendreport_params' => sub {
my ($orig, $self, $row, $h, $params) = @_;

Expand Down
25 changes: 17 additions & 8 deletions perllib/FixMyStreet/DB/Result/Problem.pm
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,15 @@ around service => sub {
return $s;
};

sub service_display {
my $self = shift;
my $service = $self->service;
return '' if $service eq 'Open311' || $service eq 'unknown' || $service eq 'test';
$service =~ s/PWA \((.*)\)/$1/;
$service =~ s/PWA/mobile/; # Might as well
return $service;
}

sub title_safe {
my $self = shift;
return _('Awaiting moderation') if $self->cobrand eq 'zurich' && $self->state eq 'submitted';
Expand Down Expand Up @@ -768,13 +777,13 @@ sub meta_line {
my $anonymous = $cobrand->call_hook('is_problem_anonymous');

if ( $problem->anonymous || $anonymous ) {
if ( $problem->service and $category && $category ne _('Other') ) {
if ( $problem->service_display && $category && $category ne _('Other') ) {
$meta =
sprintf( _('Reported via %s in the %s category anonymously at %s'),
$problem->service, $category, $date_time );
} elsif ( $problem->service ) {
$problem->service_display, $category, $date_time );
} elsif ( $problem->service_display ) {
$meta = sprintf( _('Reported via %s anonymously at %s'),
$problem->service, $date_time );
$problem->service_display, $date_time );
} elsif ( $category and $category ne _('Other') ) {
$meta = sprintf( _('Reported in the %s category anonymously at %s'),
$category, $date_time );
Expand All @@ -790,15 +799,15 @@ sub meta_line {
$problem_name = sprintf('%s (%s)', $problem->name, $problem->user->name );
}

if ( $problem->service and $category && $category ne _('Other') ) {
if ( $problem->service_display && $category && $category ne _('Other') ) {
$meta = sprintf(
_('Reported via %s in the %s category by %s at %s'),
$problem->service, $category,
$problem->service_display, $category,
$problem_name, $date_time
);
} elsif ( $problem->service ) {
} elsif ( $problem->service_display ) {
$meta = sprintf( _('Reported via %s by %s at %s'),
$problem->service, $problem_name, $date_time );
$problem->service_display, $problem_name, $date_time );
} elsif ( $category and $category ne _('Other') ) {
$meta = sprintf( _('Reported in the %s category by %s at %s'),
$category, $problem_name, $date_time );
Expand Down
14 changes: 0 additions & 14 deletions perllib/FixMyStreet/Roles/Cobrand/Northants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,6 @@ sub report_validation {
}
}

=item * We allow staff to bypass stoppers.
=cut

sub staff_ignore_form_disable_form {
my $self = shift;

my $c = $self->{c};

return $c->user_exists
&& $c->user->belongs_to_body( $self->body->id );
}


=item * We always apply state changes from Open311 updates.
=cut
Expand Down
Loading

0 comments on commit 9b70a29

Please sign in to comment.