Skip to content

Commit

Permalink
Merge branch 'bexley-ww-ggw-cancel' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
davea committed Feb 12, 2025
2 parents 4cd649c + 1ff272e commit ad4480c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
11 changes: 11 additions & 0 deletions perllib/Integrations/Agile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use HTTP::Request;
use JSON::MaybeXS;
use LWP::UserAgent;
use Moo;
use URI;

with 'Role::Config';
with 'Role::Logger';
Expand Down Expand Up @@ -69,6 +70,16 @@ sub api_call {
}
}

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

return $self->api_call(
action => 'cancel',
controller => 'servicecontract',
data => $params,
);
}

sub IsAddressFree {
my ( $self, $uprn ) = @_;

Expand Down
48 changes: 48 additions & 0 deletions perllib/Open311/Endpoint/Integration/Agile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ has agile => (
},
);

use constant SERVICE_TO_SUB_MAPPING => {
garden_subscription => \&_garden_subscription,
garden_subscription_cancel => \&_garden_subscription_cancel,
};

use constant PAYMENT_METHOD_MAPPING => {
credit_card => 'CREDITCARD',
direct_debit => 'DIRECTDEBIT',
Expand Down Expand Up @@ -87,6 +92,16 @@ sub post_service_request {
$self->logger->debug(
"post_service_request arguments: " . encode_json($args) );

my $sub = SERVICE_TO_SUB_MAPPING->{$service->service_code};

die 'Service \'' . $service->service_code . '\' not handled' unless $sub;

return &$sub( $self, $args );
}

sub _garden_subscription {
my ( $self, $args) = @_;

my $integration = $self->get_integration;

my $is_free = $integration->IsAddressFree( $args->{attributes}{uprn} );
Expand Down Expand Up @@ -133,4 +148,37 @@ sub post_service_request {
}
}

sub _garden_subscription_cancel {
my ( $self, $args ) = @_;

my $integration = $self->get_integration;

my $is_free = $integration->IsAddressFree( $args->{attributes}{uprn} );

if ( $is_free->{IsFree} eq 'False' ) {
my $res = $integration->Cancel( {
CustomerExternalReference => $args->{attributes}{customer_external_ref},
ServiceContractUPRN => $args->{attributes}{uprn},
Reason => $args->{attributes}{reason},
DueDate => $args->{attributes}{due_date},
} );

# Expected response:
# {
# Reference: string,
# Status: string,
# }
my $request = $self->new_request(
service_request_id => $res->{Reference},
);

return $request;

} else {
die 'UPRN '
. $args->{attributes}{uprn}
. ' does not have a subscription to be cancelled, or is invalid';
}
}

1;
64 changes: 62 additions & 2 deletions t/open311/endpoint/agile.t
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ $integration->mock( api_call => sub {
ServiceContractReference => 'GW-SERV-001',
};
}
} elsif ( $action eq 'cancel' ) {
return {
Reference => 'GWIT2025-001-001',
Status => 'Hold',
};
}
} );

Expand All @@ -69,7 +74,10 @@ subtest 'GET services' => sub {
ok $res->is_success, 'valid request'
or diag $res->content;

is_deeply decode_json($res->content), [
my @got = sort { $a->{service_code} cmp $b->{service_code} }
@{ decode_json( $res->content ) };

is_deeply \@got, [
{
group => "Waste",
service_code => "garden_subscription",
Expand All @@ -78,7 +86,16 @@ subtest 'GET services' => sub {
type => "realtime",
service_name => "Garden Subscription",
metadata => "true"
}
},
{
group => "Waste",
service_code => "garden_subscription_cancel",
description => "Cancel Garden Subscription",
keywords => "waste_only",
type => "realtime",
service_name => "Cancel Garden Subscription",
metadata => "true"
},
], 'correct json returned';
};

Expand Down Expand Up @@ -206,4 +223,47 @@ subtest 'handle unknown error' => sub {
'Dies with error msg';
};

subtest 'successfully cancel a garden subscription' => sub {
my $res = $endpoint->run_test_request(
POST => '/requests.json',
api_key => 'test',
service_code => 'garden_subscription_cancel',
lat => 51,
long => -1,
'attribute[fixmystreet_id]' => 2000002,
'attribute[customer_external_ref]' => 'customer_ABC',
'attribute[uprn]' => '234_has_sub',
'attribute[reason]' => 'I used all my garden waste for a bonfire',
'attribute[due_date]' => '01/01/2025',
);

ok $res->is_success, 'valid request'
or diag $res->content;

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

subtest 'try to cancel when no subscription' => sub {
my $res = $endpoint->run_test_request(
POST => '/requests.json',
api_key => 'test',
service_code => 'garden_subscription_cancel',
lat => 51,
long => -1,
'attribute[fixmystreet_id]' => 2000002,
'attribute[customer_external_ref]' => 'customer_ABC',
'attribute[uprn]' => '123_no_sub',
'attribute[reason]' => 'I used all my garden waste for a bonfire',
'attribute[due_date]' => '01/01/2025',
);

my $content = decode_json($res->content);
is $content->[0]{code}, 500;
like $content->[0]{description},
qr/UPRN 123_no_sub does not have a subscription to be cancelled/,
'Dies with error msg';
};

done_testing;
3 changes: 3 additions & 0 deletions t/open311/endpoint/agile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ category_mapping:
garden_subscription:
name: Garden Subscription
group: Waste
garden_subscription_cancel:
name: Cancel Garden Subscription
group: Waste

0 comments on commit ad4480c

Please sign in to comment.