-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAgile.pm
234 lines (181 loc) · 6.92 KB
/
Agile.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
=head1 NAME
Open311::Endpoint::Integration::Agile - An integration with the Agile backend
=head1 SYNOPSIS
This integration lets us fetch & post Green Garden Waste data to and from
Agile
=cut
package Open311::Endpoint::Integration::Agile;
use v5.14;
use Moo;
use Integrations::Agile;
use Open311::Endpoint::Service::UKCouncil::Agile;
use JSON::MaybeXS;
extends 'Open311::Endpoint';
with 'Open311::Endpoint::Role::mySociety';
with 'Open311::Endpoint::Role::ConfigFile';
with 'Role::EndpointConfig';
with 'Role::Logger';
has jurisdiction_id => ( is => 'ro' );
has category_mapping => (
is => 'lazy',
default => sub { $_[0]->endpoint_config->{category_mapping} }
);
has service_class => (
is => 'ro',
default => 'Open311::Endpoint::Service::UKCouncil::Agile',
);
has integration_class => ( is => 'ro', default => 'Integrations::Agile' );
has agile => (
is => 'lazy',
default => sub {
$_[0]->integration_class->new(
config_filename => $_[0]->jurisdiction_id );
},
);
use constant SERVICE_TO_SUB_MAPPING => {
garden_subscription => \&_garden_subscription,
garden_subscription_renew => \&_garden_subscription_renew,
garden_subscription_cancel => \&_garden_subscription_cancel,
};
use constant PAYMENT_METHOD_MAPPING => {
credit_card => 'CREDITCARD',
direct_debit => 'DIRECTDEBIT',
csc => 'CREDITCARD',
};
sub get_integration {
$_[0]->log_identifier( $_[0]->jurisdiction_id );
return $_[0]->agile;
}
sub services {
my ($self) = @_;
my $services = $self->category_mapping;
my @services = map {
my $service = $services->{$_};
my $name = $service->{name};
$self->service_class->new(
service_name => $name,
service_code => $_,
description => $name,
keywords => ['waste_only'],
$service->{group} ? ( group => $service->{group} ) : (),
$service->{groups} ? ( groups => $service->{groups} ) : (),
);
} keys %$services;
return @services;
}
sub post_service_request {
my ( $self, $service, $args ) = @_;
$self->logger->info(
"post_service_request(" . $service->service_code . ")" );
$self->logger->debug(
"post_service_request arguments: " . encode_json($args) );
# Garden Subscription may be a 'renew' type
my $lookup = $service->service_code;
$lookup .= "_$args->{attributes}{type}" if $args->{attributes}{type};
my $sub = SERVICE_TO_SUB_MAPPING->{$lookup};
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} );
if ( $is_free->{IsFree} eq 'True' ) {
my $res = $integration->SignUp( {
Firstname => $args->{first_name},
Surname => $args->{last_name},
Email => $args->{email},
TelNumber => $args->{phone} || '',
TitleCode => 'Default',
CustomerExternalReference => '',
ServiceContractUPRN => $args->{attributes}{uprn},
WasteContainerQuantity => int( $args->{attributes}{total_containers} ) || 1,
AlreadyHasBinQuantity => int( $args->{attributes}{current_containers} ) || 0,
PaymentReference => $args->{attributes}{PaymentCode},
PaymentMethodCode =>
PAYMENT_METHOD_MAPPING->{ $args->{attributes}{payment_method} },
# Used for FMS report ID
ActionReference => $args->{attributes}{fixmystreet_id},
DirectDebitDate => $args->{attributes}{direct_debit_start_date} // '',
DirectDebitReference => $args->{attributes}{direct_debit_reference} // '',
} );
# Expected response:
# {
# CustomerExternalReference: string,
# CustomerReference: string,
# ServiceContractReference: string
# }
my $request = $self->new_request(
service_request_id => $res->{ServiceContractReference},
);
return $request;
} else {
die 'UPRN '
. $args->{attributes}{uprn}
. ' already has a subscription or is invalid';
}
}
sub _garden_subscription_renew {
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->Renew( {
CustomerExternalReference => $args->{attributes}{customer_external_ref},
ServiceContractUPRN => $args->{attributes}{uprn},
WasteContainerQuantity => int( $args->{attributes}{total_containers} ) || 1,
AlreadyHasBinQuantity => int( $args->{attributes}{current_containers} ) || 0,
PaymentReference => $args->{attributes}{PaymentCode},
PaymentMethodCode =>
PAYMENT_METHOD_MAPPING->{ $args->{attributes}{payment_method} },
DirectDebitDate => $args->{attributes}{direct_debit_start_date} // '',
DirectDebitReference => $args->{attributes}{direct_debit_reference} // '',
} );
# Expected response:
# {
# Id: int,
# Address: string,
# ServiceContractStatus: string,
# WasteContainerType: string,
# WasteContainerQuantity: int,
# StartDate: string,
# EndDate: string,
# ReminderDate: string,
# }
my $request = $self->new_request(
service_request_id => $res->{Id}, # TODO Is this correct?
);
return $request;
} else {
die 'UPRN '
. $args->{attributes}{uprn}
. ' does not have a subscription to be renewed, or is invalid';
}
}
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;