aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/Open311
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/Open311')
-rw-r--r--perllib/Open311/GetServiceRequestUpdates.pm132
-rw-r--r--perllib/Open311/PopulateServiceList.pm43
2 files changed, 169 insertions, 6 deletions
diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm
new file mode 100644
index 000000000..c30d05b22
--- /dev/null
+++ b/perllib/Open311/GetServiceRequestUpdates.pm
@@ -0,0 +1,132 @@
+package Open311::GetServiceRequestUpdates;
+
+use Moose;
+use Open311;
+use FixMyStreet::App;
+use DateTime::Format::W3CDTF;
+
+has council_list => ( is => 'ro' );
+has system_user => ( is => 'rw' );
+has start_date => ( is => 'ro', default => undef );
+has end_date => ( is => 'ro', default => undef );
+has suppress_alerts => ( is => 'rw', default => 0 );
+
+sub fetch {
+ my $self = shift;
+
+ my $councils = FixMyStreet::App->model('DB::Open311Conf')->search(
+ {
+ send_method => 'Open311',
+ send_comments => 1,
+ comment_user_id => { '!=', undef },
+ endpoint => { '!=', '' },
+ }
+ );
+
+ while ( my $council = $councils->next ) {
+
+ my $o = Open311->new(
+ endpoint => $council->endpoint,
+ api_key => $council->api_key,
+ jurisdiction => $council->jurisdiction,
+ );
+
+ $self->suppress_alerts( $council->suppress_alerts );
+ $self->system_user( $council->comment_user );
+ $self->update_comments( $o, { areaid => $council->area_id }, );
+ }
+}
+
+sub update_comments {
+ my ( $self, $open311, $council_details ) = @_;
+
+ my @args = ();
+
+ if ( $self->start_date || $self->end_date ) {
+ return 0 unless $self->start_date && $self->end_date;
+
+ push @args, $self->start_date;
+ push @args, $self->end_date;
+ }
+
+ my $requests = $open311->get_service_request_updates( @args );
+
+ unless ( $open311->success ) {
+ warn "Failed to fetch ServiceRequest Updates: " . $open311->error;
+ return 0;
+ }
+
+ for my $request (@$requests) {
+ my $request_id = $request->{service_request_id};
+
+ # If there's no request id then we can't work out
+ # what problem it belongs to so just skip
+ next unless $request_id;
+
+ my $problem =
+ FixMyStreet::App->model('DB::Problem')
+ ->search( {
+ external_id => $request_id,
+ council => { like => '%' . $council_details->{areaid} . '%' },
+ } );
+
+ if (my $p = $problem->first) {
+ my $c = $p->comments->search( { external_id => $request->{update_id} } );
+
+ if ( !$c->first ) {
+ my $comment_time = DateTime::Format::W3CDTF->parse_datetime( $request->{updated_datetime} );
+
+ my $comment = FixMyStreet::App->model('DB::Comment')->new(
+ {
+ problem => $p,
+ user => $self->system_user,
+ external_id => $request->{update_id},
+ text => $request->{description},
+ mark_fixed => 0,
+ mark_open => 0,
+ anonymous => 0,
+ name => $self->system_user->name,
+ confirmed => $comment_time,
+ created => $comment_time,
+ state => 'confirmed',
+ }
+ );
+
+ # if the comment is older than the last update
+ # do not change the status of the problem as it's
+ # tricky to determine the right thing to do.
+ if ( $comment->created_local > $p->lastupdate_local ) {
+ if ( $p->is_open and lc($request->{status}) eq 'closed' ) {
+ $p->state( 'fixed - council' );
+ $comment->problem_state( 'fixed - council' );
+ } elsif ( ( $p->is_closed || $p->is_fixed ) and lc($request->{status}) eq 'open' ) {
+ $p->state( 'confirmed' );
+ $comment->problem_state( 'confirmed' );
+ }
+ }
+
+ $p->lastupdate( $comment->created );
+ $p->update;
+ $comment->insert();
+
+ if ( $self->suppress_alerts ) {
+ my $alert = FixMyStreet::App->model('DB::Alert')->find( {
+ alert_type => 'new_updates',
+ parameter => $p->id,
+ confirmed => 1,
+ user_id => $p->user->id,
+ } );
+
+ my $alerts_sent = FixMyStreet::App->model('DB::AlertSent')->find_or_create( {
+ alert_id => $alert->id,
+ parameter => $comment->id,
+ } );
+ }
+ }
+ }
+ }
+
+ return 1;
+}
+
+1;
diff --git a/perllib/Open311/PopulateServiceList.pm b/perllib/Open311/PopulateServiceList.pm
index cfec9005d..7b5f4c7fe 100644
--- a/perllib/Open311/PopulateServiceList.pm
+++ b/perllib/Open311/PopulateServiceList.pm
@@ -8,6 +8,7 @@ use Open311;
has council_list => ( is => 'ro' );
has found_contacts => ( is => 'rw', default => sub { [] } );
+has verbose => ( is => 'ro', default => 0 );
has _current_council => ( is => 'rw' );
has _current_open311 => ( is => 'rw' );
@@ -81,7 +82,7 @@ sub process_service {
$self->_current_service->{description} :
$self->_current_service->{service_name};
- print $self->_current_service->{service_code} . ': ' . $category . "\n";
+ print $self->_current_service->{service_code} . ': ' . $category . "\n" if $self->verbose;
my $contacts = FixMyStreet::App->model( 'DB::Contact')->search(
{
area_id => $self->_current_council->area_id,
@@ -121,7 +122,7 @@ sub _handle_existing_contact {
my $service_name = $self->_normalize_service_name;
- print $self->_current_council->area_id . " already has a contact for service code " . $self->_current_service->{service_code} . "\n";
+ print $self->_current_council->area_id . " already has a contact for service code " . $self->_current_service->{service_code} . "\n" if $self->verbose;
if ( $contact->deleted || $service_name ne $contact->category || $self->_current_service->{service_code} ne $contact->email ) {
eval {
@@ -179,16 +180,22 @@ sub _create_contact {
if ( $contact ) {
push @{ $self->found_contacts }, $self->_current_service->{service_code};
- print "created contact for service code " . $self->_current_service->{service_code} . " for council @{[$self->_current_council->area_id]}\n";
+ print "created contact for service code " . $self->_current_service->{service_code} . " for council @{[$self->_current_council->area_id]}\n" if $self->verbose;
}
}
-sub _add_contact_to_meta {
+sub _add_meta_to_contact {
my ( $self, $contact ) = @_;
- print "Fetching meta data for $self->_current_service->{service_code}\n";
+ print "Fetching meta data for $self->_current_service->{service_code}\n" if $self->verbose;
my $meta_data = $self->_current_open311->get_service_meta_info( $self->_current_service->{service_code} );
+ if ( ref $meta_data->{ attributes }->{ attribute } eq 'HASH' ) {
+ $meta_data->{ attributes }->{ attribute } = [
+ $meta_data->{ attributes }->{ attribute }
+ ];
+ }
+
# turn the data into something a bit more friendly to use
my @meta =
# remove trailing colon as we add this when we display so we don't want 2
@@ -197,7 +204,31 @@ sub _add_contact_to_meta {
sort { $a->{order} <=> $b->{order} }
@{ $meta_data->{attributes}->{attribute} };
- $contact->extra( \@meta );
+ # we add these later on from bromley so don't list them here
+ # as we don't want to display them
+ if ( $self->_current_council->area_id == 2482 ) {
+ my %ignore = map { $_ => 1 } qw/
+ service_request_id_ext
+ requested_datetime
+ report_url
+ title
+ last_name
+ email
+ easting
+ northing
+ report_title
+ public_anonymity_required
+ email_alerts_requested
+ /;
+
+ @meta = grep { ! $ignore{ $_->{ code } } } @meta;
+ }
+
+ if ( @meta ) {
+ $contact->extra( \@meta );
+ } else {
+ $contact->extra( undef );
+ }
$contact->update;
}