Skip to content

Commit

Permalink
squash! [Merton] Initial setup.
Browse files Browse the repository at this point in the history
Add bulky handling bits (for bulky confirmation/cancellation).
And garden/bulky tests.
  • Loading branch information
dracos committed Apr 24, 2024
1 parent f9c6e88 commit 6405cb2
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 38 deletions.
32 changes: 32 additions & 0 deletions perllib/Open311/Endpoint/Integration/UK/Merton/Echo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Merton specifics for its Echo backend

package Open311::Endpoint::Integration::UK::Merton::Echo;

use utf8;
use DateTime;
use Moo;
extends 'Open311::Endpoint::Integration::Echo';
Expand Down Expand Up @@ -55,4 +56,35 @@ around check_for_data_value => sub {
return $class->$orig($name, $args, $request, $parent_name);
};

around post_service_request_update => sub {
my ($orig, $class, $args) = @_;
return $class->$orig($args) unless $args->{description};

if ($args->{description} =~ /Payment confirmed, reference (.*), amount (.*)/) {
my ($ref, $amount) = ($1, $2);
$amount =~ s/£//;
my $integ = $class->get_integration;
my $event = $integ->GetEvent($args->{service_request_id});
# Could GetEventType and loop through it all to find these IDs out but for just this seemed okay
my $data = {
id => 27409,
childdata => [
{ id => 27410, value => $ref },
{ id => 27411, value => $amount },
],
};
$integ->UpdateEvent({ id => $event->{Id}, data => [ $data ] });
$args->{description} = ''; # Blank out so nothing sent to Echo now
}

if ($args->{description} eq 'Booking cancelled by customer') {
$args->{actiontype_id} = 8;
$args->{datatype_id} = 0;
}

my $result = $class->$orig($args);

return $result;
};

1;
225 changes: 187 additions & 38 deletions t/open311/endpoint/merton_echo.t
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ use Test::MockModule;
use JSON::MaybeXS;

use constant EVENT_TYPE_MISSED => 'missed';
use constant EVENT_TYPE_MISSED_REFUSE => 1566;
use constant EVENT_TYPE_MISSED_RECYCLING => 1568;
use constant EVENT_TYPE_SUBSCRIBE => 1638;
use constant EVENT_TYPE_BULKY => 1636;

my $soap_lite = Test::MockModule->new('SOAP::Lite');
$soap_lite->mock(call => sub {
Expand All @@ -42,50 +46,118 @@ $soap_lite->mock(call => sub {
my $method = $args[0]->name;
if ($method eq 'PostEvent') {
my @params = ${$args[3]->value}->value;
my $offset = 0;
my $guid;
if ($params[0]->name eq 'Guid') {
$offset = 1;
$guid = $params[0]->value;
}
my $client_ref = $params[1+$offset]->value;
my $event_type = $params[3+$offset]->value;
my $service_id = $params[4+$offset]->value;
like $client_ref, qr/MRT-200012[4-5]/;
if ($client_ref eq 'MRT-2000124') {
is $event_type, 1566;
is $service_id, 405;
my @data = ${$params[$offset]->value}->value->value;
my @bin = ${$data[0]->value}->value;
is $bin[0]->value, 2000;
is $bin[1]->value, 1;
} elsif ($client_ref eq 'MRT-2000125') {
is $event_type, 1568;
is $service_id, 408;
is $guid, undef;
my @data = ${$params[$offset]->value}->value->value;
my @paper = ${$data[0]->value}->value;
is $paper[0]->value, 2002;
is $paper[1]->value, 1;
if (@params == 5) {
my $client_ref = $params[1]->value;
my $event_type = $params[3]->value;
my $service_id = $params[4]->value;
like $client_ref, qr/MRT-200012[4-7]|bulky-cc/;
if ($client_ref eq 'MRT-2000124') {
is $event_type, EVENT_TYPE_MISSED_REFUSE;
is $service_id, 405;
my @data = ${$params[0]->value}->value->value;
my @bin = ${$data[0]->value}->value;
is $bin[0]->value, 2000;
is $bin[1]->value, 1;
} elsif ($client_ref eq 'MRT-2000125') {
is $event_type, EVENT_TYPE_MISSED_RECYCLING;
is $service_id, 408;
my @data = ${$params[0]->value}->value->value;
my @paper = ${$data[0]->value}->value;
is $paper[0]->value, 2002;
is $paper[1]->value, 1;
} elsif ($event_type == 1636) {
use Data::Dumper;
my @data = ${$params[0]->value}->value->value;
#print STDERR Dumper \@params;
my @payment = ${$data[0]->value}->value;
is $payment[0]->value, 1011;
is $payment[1]->value, 1;
my $val = $client_ref eq 'bulky-cc' ? 2 : 1;
@payment = ${$data[1]->value}->value;
is $payment[0]->value, 1013;
is $payment[1]->value, $val;
if ($client_ref eq 'bulky-cc') { # Also check items
is @data, 3, 'Has item present in the data';
}
}
} elsif (@params == 2) {
is $params[0]->value, '123pay';
my @data = ${$params[1]->value}->value->value;
my @payment = ${$data[0]->value}->value;
is $payment[1]->value, 27409;
my @child = ${$payment[0]->value}->value->value;
my @ref = ${$child[0]->value}->value;
is $ref[0]->value, 27410;
is $ref[1]->value, 'ABC';
@ref = ${$child[1]->value}->value;
is $ref[0]->value, 27411;
is $ref[1]->value, '34.56';
} else {
is @params, 'UNKNOWN';
}
return SOAP::Result->new(result => {
EventGuid => '1234',
});
} elsif ($method eq 'GetEventType') {
} elsif ($method eq 'GetEvent') {
return SOAP::Result->new(result => {
Datatypes => { ExtensibleDatatype => [
{ Id => 1004, Name => "Container Stuff",
ChildDatatypes => { ExtensibleDatatype => [
{ Id => 1005, Name => "Quantity" },
{ Id => 1007, Name => "Containers" },
] },
},
{ Id => 1008, Name => "Notes" },
{ Id => 2000, Name => "Refuse Bin" },
{ Id => 2001, Name => "Container Mix" },
{ Id => 2002, Name => "Paper" },
] },
Id => '123pay',
EventTypeId => EVENT_TYPE_BULKY,
EventStateId => 4002,
});
} elsif ($method eq 'GetEventType') {
my @params = ${$args[3]->value}->value;
my $id = ${$params[2]->value}->value->value->value;
if ($id == EVENT_TYPE_BULKY) {
return SOAP::Result->new(result => {
Datatypes => { ExtensibleDatatype => [
{ Id => 1011, Name => "Payment Type" },
{ Id => 1012, Name => "Payment Taken By" },
{ Id => 1013, Name => "Payment Method" },
{ Id => 1020, Name => "Bulky Collection",
ChildDatatypes => { ExtensibleDatatype => [
{ Id => 1021, Name => "Bulky Items" },
{ Id => 1022, Name => "Notes" },
] },
},
] },
});
} elsif ($id == EVENT_TYPE_SUBSCRIBE) {
return SOAP::Result->new(result => {
Datatypes => { ExtensibleDatatype => [
{ Id => 1004, Name => "Subscription Details",
ChildDatatypes => { ExtensibleDatatype => [
{ Id => 1005, Name => "Quantity" },
{ Id => 1007, Name => "Containers" },
] },
},
] },
});
} elsif ($id == EVENT_TYPE_MISSED_REFUSE) {
return SOAP::Result->new(result => {
Datatypes => { ExtensibleDatatype => [
{ Id => 1008, Name => "Notes" },
{ Id => 2000, Name => "Refuse Bin" },
{ Id => 2001, Name => "Container Mix" },
{ Id => 2002, Name => "Paper" },
] },
});
} elsif ($id == EVENT_TYPE_MISSED_RECYCLING) {
return SOAP::Result->new(result => {
Datatypes => { ExtensibleDatatype => [
{ Id => 1008, Name => "Notes" },
{ Id => 2000, Name => "Refuse Bin" },
{ Id => 2001, Name => "Container Mix" },
{ Id => 2002, Name => "Paper" },
] },
});
}
} elsif ($method eq 'PerformEventAction') {
my @params = ${$args[3]->value}->value;
is @params, 2, 'No notes';
my $ref = ${(${$params[1]->value}->value)[2]->value}->value->value->value;
my $actiontype_id = $params[0]->value;
is $actiontype_id, 8;
return SOAP::Result->new(result => { EventActionGuid => 'ABC' });
} else {
is $method, 'UNKNOWN';
}
Expand Down Expand Up @@ -136,4 +208,81 @@ subtest "POST missed mixed+paper OK" => sub {
} ], 'correct json returned';
};

subtest "POST subscription request OK" => sub {
my $res = $endpoint->run_test_request(@params,
service_code => EVENT_TYPE_SUBSCRIBE,
'attribute[fixmystreet_id]' => 2000126,
'attribute[Subscription_Details_Containers]' => 26, # Garden Bin
'attribute[Subscription_Details_Quantity]' => 1,
'attribute[Request_Type]' => 1,
);
ok $res->is_success, 'valid request'
or diag $res->content;

is_deeply decode_json($res->content),
[ {
"service_request_id" => '1234',
} ], 'correct json returned';
};

subtest "POST bulky request card payment OK" => sub {
my $res = $endpoint->run_test_request(@params,
service_code => EVENT_TYPE_BULKY,
'attribute[fixmystreet_id]' => 2000127,
'attribute[payment_method]' => 'credit_card',
'attribute[client_reference]' => 'bulky-cc',
'attribute[Bulky_Collection_Bulky_Items]' => "11",
'attribute[Bulky_Collection_Notes]' => "Vanity dressing table",
);
ok $res->is_success, 'valid request'
or diag $res->content;

is_deeply decode_json($res->content),
[ {
"service_request_id" => '1234',
} ], 'correct json returned';
};

subtest "POST a successful payment" => sub {
my $res = $endpoint->run_test_request(
POST => '/servicerequestupdates.json',
api_key => 'test',
updated_datetime => '2023-09-01T19:00:00+01:00',
service_request_id => '123pay',
update_id => 456,
status => 'OPEN',
description => 'Payment confirmed, reference ABC, amount £34.56',
first_name => 'Bob',
last_name => 'Mould',
);
ok $res->is_success, 'valid request'
or diag $res->content;

is_deeply decode_json($res->content),
[ {
"update_id" => 'BLANK',
} ], 'correct json returned';
};

subtest "POST a cancellation" => sub {
my $res = $endpoint->run_test_request(
POST => '/servicerequestupdates.json',
api_key => 'test',
updated_datetime => '2023-09-01T19:00:00+01:00',
service_request_id => '123cancel',
update_id => 456,
status => 'OPEN',
description => 'Booking cancelled by customer',
first_name => 'Bob',
last_name => 'Mould',
);
ok $res->is_success, 'valid request'
or diag $res->content;

is_deeply decode_json($res->content),
[ {
"update_id" => 'ABC',
} ], 'correct json returned';
};

done_testing;

0 comments on commit 6405cb2

Please sign in to comment.