diff options
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 14 | ||||
-rw-r--r-- | t/open311/getservicerequestupdates.t | 54 |
2 files changed, 66 insertions, 2 deletions
diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm index 1d07e7897..06b5ce321 100644 --- a/perllib/Open311/GetServiceRequestUpdates.pm +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -91,7 +91,7 @@ sub update_comments { # If there's no request id then we can't work out # what problem it belongs to so just skip - next unless $request_id; + next unless $request_id || $request->{fixmystreet_id}; my $comment_time = eval { DateTime::Format::W3CDTF->parse_datetime( $request->{updated_datetime} || "" ); @@ -101,9 +101,19 @@ sub update_comments { next if @args && ($updated lt $args[0] || $updated gt $args[1]); my $problem; + my $match_field = 'external_id'; my $criteria = { external_id => $request_id, }; + + # in some cases we only have the FMS id and not the request id so use that + if ( $request->{fixmystreet_id} ) { + $criteria = { + id => $request->{fixmystreet_id}, + }; + $match_field = 'fixmystreet id'; + } + $problem = $self->schema->resultset('Problem')->to_body($body)->search( $criteria ); if (my $p = $problem->first) { @@ -198,7 +208,7 @@ sub update_comments { # we get lots of comments that are not related to FMS issues from Lewisham so ignore those otherwise # way too many warnings. } elsif (FixMyStreet->config('STAGING_SITE') and $body->name !~ /Lewisham/) { - warn "Failed to match comment to problem with external_id $request_id for " . $body->name . "\n"; + warn "Failed to match comment to problem with $match_field $request_id for " . $body->name . "\n"; } } diff --git a/t/open311/getservicerequestupdates.t b/t/open311/getservicerequestupdates.t index 54a7f192d..055c5ea5f 100644 --- a/t/open311/getservicerequestupdates.t +++ b/t/open311/getservicerequestupdates.t @@ -1037,6 +1037,60 @@ foreach my $test ( { } } +subtest 'check matching on fixmystreet_id overrides service_request_id' => sub { + my $requests_xml = qq{<?xml version="1.0" encoding="utf-8"?> + <service_requests_updates> + <request_update> + <update_id>638344</update_id> + <service_request_id>8888888888888</service_request_id> + <fixmystreet_id>@{[ $problem->id ]}</fixmystreet_id> + <status>open</status> + <description>This is a note</description> + <updated_datetime>UPDATED_DATETIME</updated_datetime> + </request_update> + <request_update> + <update_id>638354</update_id> + <service_request_id>@{[ $problem->external_id ]}</service_request_id> + <fixmystreet_id>999999999</fixmystreet_id> + <status>open</status> + <description>This is a different note</description> + <updated_datetime>UPDATED_DATETIME2</updated_datetime> + </request_update> + <request_update> + <update_id>638356</update_id> + <service_request_id></service_request_id> + <fixmystreet_id>@{[ $problem->id ]}</fixmystreet_id> + <status>investigating</status> + <description>This is a last note</description> + <updated_datetime>UPDATED_DATETIME3</updated_datetime> + </request_update> + </service_requests_updates> + }; + + $problem->comments->delete; + + my $dt2 = $dt->clone->subtract( minutes => 30 ); + my $dt3 = $dt2->clone->subtract( minutes => 30 ); + $requests_xml =~ s/UPDATED_DATETIME3/$dt/; + $requests_xml =~ s/UPDATED_DATETIME2/$dt2/; + $requests_xml =~ s/UPDATED_DATETIME/$dt3/; + + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $requests_xml } ); + + my $update = Open311::GetServiceRequestUpdates->new( + system_user => $user, + ); + + $update->update_comments( $o, $bodies{2482} ); + + $problem->discard_changes; + is $problem->comments->count, 2, 'two comments after fetching updates'; + + my @comments = $problem->comments->search(undef, { order_by => [ 'created' ] } )->all; + + is $comments[0]->external_id, 638344, "correct first comment added"; + is $comments[1]->external_id, 638356, "correct second comment added"; +}; done_testing(); sub setup_xml { |