Skip to content

Commit

Permalink
[Echo] Try and fetch new expired slots on sending.
Browse files Browse the repository at this point in the history
Include a command line script to update a report manually.
  • Loading branch information
dracos committed Jan 9, 2024
1 parent e7ae467 commit a92d737
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 5 deletions.
22 changes: 22 additions & 0 deletions bin/fixmystreet.com/waste-echo-reservation-update
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env perl
#
# Fallback manual script to be used if a report needs resending to Echo but
# contains an expired task reservation, to fetch new data if possible

use v5.14;
use warnings;

BEGIN {
use File::Basename qw(dirname);
use File::Spec;
my $d = dirname(File::Spec->rel2abs($0));
require "$d/../../setenv.pl";
}

use FixMyStreet::Cobrand;
use FixMyStreet::DB;

my $id = shift @ARGV or die "Provide a report ID";
my $p = FixMyStreet::DB->resultset("Problem")->find($id) or die "Could not find ID $id";
my $cobrand = $p->get_cobrand_logged;
$cobrand->bulky_refetch_slots($p, 1);
3 changes: 3 additions & 0 deletions perllib/FixMyStreet/Cobrand/Bromley.pm
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ sub open311_post_send {
$row2->state('duplicate');
$row2->update;
$row->discard_changes;
} elsif ($error =~ /Selected reservations expired/) {
$self->bulky_refetch_slots($row2);
$row->discard_changes;
}
});
}
Expand Down
54 changes: 53 additions & 1 deletion perllib/FixMyStreet/Roles/CobrandEcho.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package FixMyStreet::Roles::CobrandEcho;

use strict;
use v5.14;
use warnings;
use DateTime;
use DateTime::Format::Strptime;
Expand Down Expand Up @@ -609,6 +609,58 @@ sub clear_cached_lookups_bulky_slots {
$echo->CancelReservedSlotsForEvent($guid);
}

sub bulky_refetch_slots {
my ($self, $row, $verbose) = @_;

my $property_id = $row->get_extra_field_value('property_id');
my $date = $self->collection_date($row);
my $guid = $row->get_extra_field_value('GUID');
my $window = $self->_bulky_collection_window();

my $cfg = $self->feature('echo');
my $echo = Integrations::Echo->new(%$cfg);

my $service_id = $cfg->{bulky_service_id};
my $event_type_id = $cfg->{bulky_event_type_id};

if ($row->whensent || !$guid) {
if ($row->whensent) {
say "Already sent, creating new GUID and fetching new reservation and resending" if $verbose;
} else {
say "No GUID? Creating new GUID and fetching new reservation and sending" if $verbose;
}
require UUID::Tiny;
$guid = UUID::Tiny::create_uuid_as_string();
$row->update_extra_field({ name => 'GUID', value => $guid });
$row->state('confirmed');
$row->resend;

Check warning on line 636 in perllib/FixMyStreet/Roles/CobrandEcho.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Roles/CobrandEcho.pm#L632-L636

Added lines #L632 - L636 were not covered by tests
} else {
say "Not already sent, fetching new reservation and trying again" if $verbose;
say "Cancelling existing slots for $guid" if $verbose;
$echo->CancelReservedSlotsForEvent($guid);
}

say "Getting more slots for $property_id $guid" if $verbose;
my $slots = $echo->ReserveAvailableSlotsForEvent($service_id, $event_type_id, $property_id, $guid, $window->{date_from}, $window->{date_to});

my $slot_found = 0;
foreach (@$slots) {
my $slot_date = construct_bin_date($_->{StartDate});
my $ref = $_->{Reference};
if ($slot_date->ymd eq $date->ymd) {
$slot_found = 1;
say "Updating reservation to slot $ref for $slot_date" if $verbose;
$row->update_extra_field({ name => 'reservation', value => $ref });
}
}
if ($slot_found) {
$row->send_fail_count(0); # Assuming it's been failing, for an instant retry
$row->update;
} else {
say "No replacement slot for $date could be found" if $verbose;
}
}

sub bulky_check_missed_collection {
my ($self, $events, $blocked_codes) = @_;

Expand Down
3 changes: 3 additions & 0 deletions perllib/FixMyStreet/Roles/CobrandSLWP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ sub open311_post_send {
$row2->state('duplicate');
$row2->update;
$row->discard_changes;
} elsif ($error =~ /Selected reservations expired/) {
$self->bulky_refetch_slots($row2);
$row->discard_changes;

Check warning on line 108 in perllib/FixMyStreet/Roles/CobrandSLWP.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Roles/CobrandSLWP.pm#L107-L108

Added lines #L107 - L108 were not covered by tests
} elsif ($error =~ /Duplicate Event! Original eventID: (\d+)/) {
my $id = $1;
my $cfg = $self->feature('echo');
Expand Down
38 changes: 34 additions & 4 deletions t/app/controller/waste_bromley_bulky.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ my $minimum_charge = 500;

my $user = $mech->create_user_ok('[email protected]');

my $body = $mech->create_body_ok( 2482, 'Bromley Council',
{}, { cobrand => 'bromley' } );
my $body = $mech->create_body_ok( 2482, 'Bromley Council', {
endpoint => 'http://endpoint.example.com', jurisdiction => 'FMS', api_key => 'test',
}, { cobrand => 'bromley' } );
$body->set_extra_metadata(
wasteworks_config => {
per_item_costs => 1,
Expand Down Expand Up @@ -144,7 +145,8 @@ FixMyStreet::override_config {
};
});
$echo->mock('GetServiceUnitsForObject', \&domestic_waste_service_units );
$echo->mock('ReserveAvailableSlotsForEvent', sub {

my $reserve_mock = sub {
my ($self, $service, $event_type, $property, $guid, $start, $end) = @_;
is $service, 413;
is $event_type, 2175;
Expand All @@ -166,7 +168,8 @@ FixMyStreet::override_config {
Expiry => { DateTime => '2023-06-25T10:10:00Z' },
Reference => 'reserve3==',
},
] });
] };
$echo->mock('ReserveAvailableSlotsForEvent', $reserve_mock);

my $sent_params;
my $call_params;
Expand Down Expand Up @@ -428,7 +431,34 @@ FixMyStreet::override_config {
$mech->content_lacks('Report a bulky waste collection as missed',
"Can't report missing when closed collection but after two working days");
};
};

subtest 'Bulky goods collection, reservation expired' => sub {
$report->discard_changes;
$report->update({ whensent => undef });
$report->discard_changes;
my $sender = FixMyStreet::SendReport::Open311->new(
bodies => [ $body ], body_config => { $body->id => $body },
);
$echo->mock('ReserveAvailableSlotsForEvent', sub {
[ {
StartDate => { OffsetMinutes => 0, DateTime => '2023-07-01T00:00:00Z' },
Expiry => { OffsetMinutes => 0, DateTime => '2023-07-02T00:00:00Z' },
Reference => 'NewRes',
} ];
});
Open311->_inject_response('/requests.xml', '<?xml version="1.0" encoding="utf-8"?><errors><error><code></code><description>Selected reservations expired</description></error></errors>', 500);
$sender->send($report, {
easting => 1,
northing => 2,
url => 'http://example.org/',
});
$report->discard_changes;
is $report->get_extra_field_value('reservation'), 'NewRes';
$echo->mock('ReserveAvailableSlotsForEvent', $reserve_mock);
};

subtest 'Bulky goods collection, cancelling' => sub {
# Collection time: 2023-07-01T:07:00:00
my $full_refund_time = '2023-06-30T05:59:59Z'; # 06:59:59 UK time
my $partial_refund_time = '2023-07-01T05:59:59Z'; # 06:59:59 UK time
Expand Down

0 comments on commit a92d737

Please sign in to comment.