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
|
package FixMyStreet::SendReport::Open311;
use Moose;
use namespace::autoclean;
BEGIN { extends 'FixMyStreet::SendReport'; }
use FixMyStreet::App;
use mySociety::Config;
use DateTime::Format::W3CDTF;
use Open311;
sub should_skip {
my $self = shift;
my $row = shift;
if ( $row->send_fail_count > 0 ) {
if ( bromley_retry_timeout($row) ) {
return 1;
}
}
}
sub send {
my $self = shift;
my ( $row, $h ) = @_;
my $result = -1;
foreach my $council ( keys %{ $self->councils } ) {
my $conf = $self->councils->{$council}->{config};
my $always_send_latlong = 1;
my $send_notpinpointed = 0;
my $use_service_as_deviceid = 0;
my $basic_desc = 0;
# Extra bromley fields
if ( $row->council =~ /2482/ ) {
my $extra = $row->extra;
if ( $row->used_map || ( !$row->used_map && !$row->postcode ) ) {
push @$extra, { name => 'northing', value => $h->{northing} };
push @$extra, { name => 'easting', value => $h->{easting} };
}
push @$extra, { name => 'report_url', value => $h->{url} };
push @$extra, { name => 'service_request_id_ext', value => $row->id };
push @$extra, { name => 'report_title', value => $row->title };
push @$extra, { name => 'public_anonymity_required', value => $row->anonymous ? 'TRUE' : 'FALSE' };
push @$extra, { name => 'email_alerts_requested', value => 'FALSE' }; # always false as can never request them
push @$extra, { name => 'requested_datetime', value => DateTime::Format::W3CDTF->format_datetime($row->confirmed_local->set_nanosecond(0)) };
push @$extra, { name => 'email', value => $row->user->email };
$row->extra( $extra );
$always_send_latlong = 0;
$send_notpinpointed = 1;
$use_service_as_deviceid = 0;
# make sure we have last_name attribute present in row's extra, so
# it is passed correctly to Bromley as attribute[]
if ( $row->cobrand ne 'bromley' ) {
my ( $firstname, $lastname ) = ( $row->user->name =~ /(\w+)\.?\s+(.+)/ );
push @$extra, { name => 'last_name', value => $lastname };
}
$basic_desc = 1;
}
# FIXME: we've already looked this up before
my $contact = FixMyStreet::App->model("DB::Contact")->find( {
deleted => 0,
area_id => $conf->area_id,
category => $row->category
} );
my $open311 = Open311->new(
jurisdiction => $conf->jurisdiction,
endpoint => $conf->endpoint,
api_key => $conf->api_key,
always_send_latlong => $always_send_latlong,
send_notpinpointed => $send_notpinpointed,
use_service_as_deviceid => $use_service_as_deviceid,
basic_description => $basic_desc,
);
# non standard west berks end points
if ( $row->council =~ /2619/ ) {
$open311->endpoints( { services => 'Services', requests => 'Requests' } );
}
# required to get round issues with CRM constraints
if ( $row->council =~ /2218/ ) {
$row->user->name( $row->user->id . ' ' . $row->user->name );
}
if ($row->cobrand eq 'fixmybarangay') {
# FixMyBarangay endpoints expect external_id as an attribute
$row->extra( [ { 'name' => 'external_id', 'value' => $row->id } ] );
}
my $resp = $open311->send_service_request( $row, $h, $contact->email );
# make sure we don't save user changes from above
if ( $row->council =~ /2218/ || $row->council =~ /2482/ || $row->cobrand eq 'fixmybarangay') {
$row->discard_changes();
}
if ( $resp ) {
$row->external_id( $resp );
$row->send_method_used('Open311');
$result *= 0;
$self->success( 1 );
} else {
$result *= 1;
# temporary fix to resolve some issues with west berks
if ( $row->council =~ /2619/ ) {
$result *= 0;
}
}
}
$self->error( 'Failed to send over Open311' ) unless $self->success;
return $result;
}
sub bromley_retry_timeout {
my $row = shift;
my $tz = DateTime::TimeZone->new( name => 'local' );
my $now = DateTime->now( time_zone => $tz );
my $diff = $now - $row->send_fail_timestamp;
if ( $diff->in_units( 'minutes' ) < 30 ) {
return 1;
}
return 0;
}
1;
|