Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bexley][WW] Cancel GGW subscription #5330

Open
wants to merge 1 commit into
base: bexley-ww-garden-subscription
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion perllib/FixMyStreet/App/Controller/Waste.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,9 @@ sub garden_cancel : Chained('garden_setup') : Args(0) {
$c->forward('check_if_staff_can_pay', [ $payment_method ]);

$c->stash->{first_page} = 'intro';
$c->stash->{form_class} = 'FixMyStreet::App::Form::Waste::Garden::Cancel';
$c->stash->{form_class}
= $c->cobrand->call_hook('waste_cancel_form_class')
|| 'FixMyStreet::App::Form::Waste::Garden::Cancel';
$c->forward('form');
}

Expand Down
1 change: 1 addition & 0 deletions perllib/FixMyStreet/App/Form/Waste/Garden/Cancel.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ has_field confirm => (
option_label => 'I confirm I wish to cancel my subscription',
required => 1,
label => "Confirm",
order => 998,
);

has_field submit => (
Expand Down
38 changes: 38 additions & 0 deletions perllib/FixMyStreet/App/Form/Waste/Garden/Cancel/Bexley.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package FixMyStreet::App::Form::Waste::Garden::Cancel::Bexley;

use utf8;
use HTML::FormHandler::Moose;
extends 'FixMyStreet::App::Form::Waste::Garden::Cancel';

has_field reason => (
type => 'Select',
widget => 'RadioGroup',
required => 1,
label => 'Reason for cancellation',
messages => { required => 'Please select a reason' },
);

sub options_reason {
my $form = shift;

my @options = (
'Price',
'Service Issues',
'Moving Out of Borough',
'Other',
);
return map { { label => $_, value => $_ } } @options;
}

has_field reason_further_details => (
required => 1,
type => 'Text',
widget => 'Textarea',
label =>
"If you selected 'Other', please provide further details (up to 250 characters)",
required_when => { reason => 'Other' },
maxlength => 250,
messages => { required => 'Please provide further details' },
);

1;
93 changes: 90 additions & 3 deletions perllib/FixMyStreet/Cobrand/Bexley/Garden.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,102 @@

package FixMyStreet::Cobrand::Bexley::Garden;

use DateTime::Format::Strptime;
use Integrations::Agile;
use FixMyStreet::App::Form::Waste::Garden::Cancel::Bexley;

use Moo::Role;
with 'FixMyStreet::Roles::Cobrand::SCP',
'FixMyStreet::Roles::Cobrand::Paye';

has agile => (
is => 'lazy',
default => sub {
my $self = shift;
my $cfg = $self->feature('agile');
return Integrations::Agile->new(%$cfg);
},
);

sub garden_service_name { 'garden waste collection service' }

# TODO No current subscription look up here
#
sub garden_current_subscription { undef }
sub garden_service_ids {
return [ 'GA-140', 'GA-240' ];
}

sub garden_current_subscription {
my $self = shift;

my $current = $self->{c}->stash->{property}{garden_current_subscription};
return $current if $current;

my $uprn = $self->{c}->stash->{property}{uprn};
return undef unless $uprn;

# TODO Fetch active subscription from DB for UPRN
# (get_original_sub() in Controller/Waste.pm needs to handle Bexley UPRN).
# Could be more than one customer, so match against email.
# Could be more than one contract, so match against reference.

my $results = $self->agile->CustomerSearch($uprn);
return undef unless $results && $results->{Customers};
my $customer = $results->{Customers}[0];
return undef unless $customer && $customer->{ServiceContracts};
my $contract = $customer->{ServiceContracts}[0];
return unless $contract;

my $parser
= DateTime::Format::Strptime->new( pattern => '%d/%m/%Y %H:%M' );
my $end_date = $parser->parse_datetime( $contract->{EndDate} );

# Agile says there is a subscription; now get service data from
# Whitespace
my $services = $self->{c}->stash->{services};
for ( @{ $self->garden_service_ids } ) {
if ( my $srv = $services->{$_} ) {
$srv->{customer_external_ref}
= $customer->{CustomerExternalReference};
$srv->{end_date} = $end_date;
return $srv;

Check warning on line 65 in perllib/FixMyStreet/Cobrand/Bexley/Garden.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Cobrand/Bexley/Garden.pm#L63-L65

Added lines #L63 - L65 were not covered by tests
}
}

return {
agile_only => 1,
customer_external_ref => $customer->{CustomerExternalReference},
end_date => $end_date,
};
}

# TODO This is a placeholder
sub get_current_garden_bins { 1 }

sub waste_cancel_asks_staff_for_user_details { 1 }

sub waste_cancel_form_class {
'FixMyStreet::App::Form::Waste::Garden::Cancel::Bexley';
}

sub waste_garden_sub_params {
my ( $self, $data, $type ) = @_;

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

if ( $data->{category} eq 'Cancel Garden Subscription' ) {
my $srv = $self->garden_current_subscription;

my $parser = DateTime::Format::Strptime->new( pattern => '%d/%m/%Y' );
my $due_date_str = $parser->format_datetime( $srv->{end_date} );

my $reason = $data->{reason};
$reason .= ': ' . $data->{reason_further_details}
if $data->{reason_further_details};

$c->set_param( 'customer_external_ref', $srv->{customer_external_ref} );
$c->set_param( 'due_date', $due_date_str );
$c->set_param( 'reason', $reason );
}
}

=item * You can order a maximum of five bins

Expand Down
5 changes: 4 additions & 1 deletion perllib/FixMyStreet/Cobrand/Bexley/Waste.pm
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ sub bin_services_for_address {
];
}

$property->{garden_current_subscription}
= $self->garden_current_subscription;

@site_services_filtered = $self->_remove_service_if_assisted_exists(@site_services_filtered);

@site_services_filtered = $self->service_sort(@site_services_filtered);
Expand Down Expand Up @@ -1246,7 +1249,7 @@ sub in_cab_logs_reason_prefixes {
'Clear Sacks' => ['MDR-SACK', 'CW-SACK'],
'Paper & Card' => ['PA-1100', 'PA-1280', 'PA-140', 'PA-240', 'PA-55', 'PA-660', 'PA-940', 'PC-180', 'PC-55'],
'Food' => ['FO-140', 'FO-23'],
'Garden' => ['GA-140', 'GA-240'],
'Garden' => ['GA-140', 'GA-240'], # TODO Call Garden.pm->garden_service_ids to make sure these IDs are consistent
'Plastics & Glass' => ['PG-1100', 'PG-1280', 'PG-240', 'PG-360', 'PG-55', 'PG-660', 'PG-940', 'PL-1100', 'PL-1280', 'PL-140', 'PL-55', 'PL-660', 'PL-940'],
'Glass' => ['GL-1100', 'GL-1280', 'GL-55', 'GL-660'],
'Refuse' => ['RES-1100', 'RES-1280', 'RES-140', 'RES-180', 'RES-240', 'RES-660', 'RES-720', 'RES-940', 'RES-CHAM', 'RES-DBIN', 'RES-SACK'],
Expand Down
78 changes: 78 additions & 0 deletions perllib/Integrations/Agile.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
=head1 NAME

Integrations::Agile - Agile Applications API integration

=head1 DESCRIPTION

This module provides an interface to the Agile Applications API

=cut

package Integrations::Agile;

use strict;
use warnings;

use HTTP::Request;
use JSON::MaybeXS;
use LWP::UserAgent;
use Moo;
use URI;

has url => ( is => 'ro' );

# TODO Logging

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

Check warning on line 27 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L27

Added line #L27 was not covered by tests

my $action = $args{action};
my $controller = $args{controller};
my $data = $args{data};
my $method = 'POST';

Check warning on line 32 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L29-L32

Added lines #L29 - L32 were not covered by tests

my $body = {

Check warning on line 34 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L34

Added line #L34 was not covered by tests
Method => $method,
Controller => $controller,
Action => $action,
Data => $data,
};
my $body_json = encode_json($body);

Check warning on line 40 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L40

Added line #L40 was not covered by tests

my $uri = URI->new( $self->{url} );

Check warning on line 42 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L42

Added line #L42 was not covered by tests

my $req = HTTP::Request->new( $method, $uri );
$req->content_type('application/json; charset=UTF-8');
$req->content($body_json);

Check warning on line 46 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L44-L46

Added lines #L44 - L46 were not covered by tests

my $ua = LWP::UserAgent->new;
my $res = $ua->request($req);

Check warning on line 49 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L48-L49

Added lines #L48 - L49 were not covered by tests

if ( $res->is_success ) {
return decode_json( $res->content );

Check warning on line 52 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L52

Added line #L52 was not covered by tests
} else {
die $res->content;

Check warning on line 54 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L54

Added line #L54 was not covered by tests
}
}

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

Check warning on line 59 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L59

Added line #L59 was not covered by tests

return $self->call(

Check warning on line 61 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L61

Added line #L61 was not covered by tests
action => 'isaddressfree',
controller => 'customer',
data => { UPRN => $uprn },
);
}

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

Check warning on line 69 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L69

Added line #L69 was not covered by tests

return $self->call(

Check warning on line 71 in perllib/Integrations/Agile.pm

View check run for this annotation

Codecov / codecov/patch

perllib/Integrations/Agile.pm#L71

Added line #L71 was not covered by tests
action => 'search',
controller => 'customer',
data => { ServiceContractUPRN => $uprn },
);
}

1;
9 changes: 6 additions & 3 deletions t/app/controller/waste_bexley.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ my $mock_waste = Test::MockModule->new('BexleyAddresses');
# We don't actually read from the file, so just put anything that is a valid path
$mock_waste->mock( 'database_file', '/' );

my $agile_mock = Test::MockModule->new('Integrations::Agile');
$agile_mock->mock( 'CustomerSearch', sub { {} } );

my $dbi_mock = Test::MockModule->new('DBI');
$dbi_mock->mock( 'connect', sub {
my $dbh = Test::MockObject->new;
Expand Down Expand Up @@ -268,9 +271,9 @@ $existing_missed_collection_report2->add_to_comments(
FixMyStreet::override_config {
ALLOWED_COBRANDS => 'bexley',
MAPIT_URL => 'http://mapit.uk/',
COBRAND_FEATURES => { whitespace => { bexley => {
url => 'http://example.org/',
} },
COBRAND_FEATURES => {
whitespace => { bexley => { url => 'http://example.org/' } },
agile => { bexley => { url => 'test' } },
waste => { bexley => 1 },
waste_calendar_links => { bexley => { 'Wk-1' => 'PDF 1', 'Wk-2' => 'PDF 2'} },
},
Expand Down
4 changes: 4 additions & 0 deletions t/app/controller/waste_bexley_container_requests.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ $dbi_mock->mock( 'connect', sub {
return $dbh;
} );

my $agile_mock = Test::MockModule->new('Integrations::Agile');
$agile_mock->mock( 'CustomerSearch', sub { {} } );

my $mech = FixMyStreet::TestMech->new;

my $cobrand = FixMyStreet::Cobrand::Bexley->new;
Expand Down Expand Up @@ -674,6 +677,7 @@ FixMyStreet::override_config {
COBRAND_FEATURES => {
waste => { bexley => 1 },
whitespace => { bexley => { url => 'http://example.org/' } },
agile => { bexley => { url => 'test' } },
},
}, sub {
my $whitespace_mock = Test::MockModule->new('Integrations::Whitespace');
Expand Down
Loading