aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/Open311
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/Open311')
-rw-r--r--perllib/Open311/GetServiceRequestUpdates.pm94
-rw-r--r--perllib/Open311/GetUpdates.pm24
-rw-r--r--perllib/Open311/PopulateServiceList.pm67
3 files changed, 110 insertions, 75 deletions
diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm
index 0b4e037fd..f7b758137 100644
--- a/perllib/Open311/GetServiceRequestUpdates.pm
+++ b/perllib/Open311/GetServiceRequestUpdates.pm
@@ -5,17 +5,19 @@ 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 );
has verbose => ( is => 'ro', default => 0 );
+Readonly::Scalar my $AREA_ID_BROMLEY => 2482;
+Readonly::Scalar my $AREA_ID_OXFORDSHIRE => 2237;
+
sub fetch {
my $self = shift;
- my $councils = FixMyStreet::App->model('DB::Open311Conf')->search(
+ my $bodies = FixMyStreet::App->model('DB::Body')->search(
{
send_method => 'Open311',
send_comments => 1,
@@ -24,29 +26,34 @@ sub fetch {
}
);
- while ( my $council = $councils->next ) {
+ while ( my $body = $bodies->next ) {
my $o = Open311->new(
- endpoint => $council->endpoint,
- api_key => $council->api_key,
- jurisdiction => $council->jurisdiction,
+ endpoint => $body->endpoint,
+ api_key => $body->api_key,
+ jurisdiction => $body->jurisdiction,
);
- if ( $council->area_id =~ /2482/ ) {
+ # custom endpoint URLs because these councils have non-standard paths
+ if ( $body->areas->{$AREA_ID_BROMLEY} ) {
my $endpoints = $o->endpoints;
$endpoints->{update} = 'update.xml';
$endpoints->{service_request_updates} = 'update.xml';
$o->endpoints( $endpoints );
+ } elsif ( $body->areas->{$AREA_ID_OXFORDSHIRE} ) {
+ my $endpoints = $o->endpoints;
+ $endpoints->{service_request_updates} = 'open311_service_request_update.cgi';
+ $o->endpoints( $endpoints );
}
- $self->suppress_alerts( $council->suppress_alerts );
- $self->system_user( $council->comment_user );
- $self->update_comments( $o, { areaid => $council->area_id }, );
+ $self->suppress_alerts( $body->suppress_alerts );
+ $self->system_user( $body->comment_user );
+ $self->update_comments( $o, { areas => $body->areas }, );
}
}
sub update_comments {
- my ( $self, $open311, $council_details ) = @_;
+ my ( $self, $open311, $body_details ) = @_;
my @args = ();
@@ -56,7 +63,7 @@ sub update_comments {
push @args, $self->start_date;
push @args, $self->end_date;
# default to asking for last 2 hours worth if not Bromley
- } elsif ( $council_details->{areaid} != 2482 ) {
+ } elsif ( ! $body_details->{areas}->{$AREA_ID_BROMLEY} ) {
my $end_dt = DateTime->now();
my $start_dt = $end_dt->clone;
$start_dt->add( hours => -2 );
@@ -68,7 +75,7 @@ sub update_comments {
my $requests = $open311->get_service_request_updates( @args );
unless ( $open311->success ) {
- warn "Failed to fetch ServiceRequest Updates for " . $council_details->{areaid} . ":\n" . $open311->error
+ warn "Failed to fetch ServiceRequest Updates for " . join(",", keys %{$body_details->{areas}}) . ":\n" . $open311->error
if $self->verbose;
return 0;
}
@@ -80,12 +87,13 @@ sub update_comments {
# 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} . '%' },
- } );
+ my $problem;
+ my $criteria = {
+ external_id => $request_id,
+ # XXX This assumes that areas will actually only be one area.
+ bodies_str => { like => '%' . join(",", keys %{$body_details->{areas}}) . '%' },
+ };
+ $problem = FixMyStreet::App->model('DB::Problem')->search( $criteria );
if (my $p = $problem->first) {
my $c = $p->comments->search( { external_id => $request->{update_id} } );
@@ -112,13 +120,17 @@ sub update_comments {
# 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' );
+ if ( $comment->created > $p->lastupdate ) {
+ my $state = $self->map_state( $request->{status} );
+
+ # don't update state unless it's an allowed state and it's
+ # actually changing the state of the problem
+ if ( FixMyStreet::DB::Result::Problem->council_states()->{$state} && $p->state ne $state &&
+ !( $p->is_fixed && FixMyStreet::DB::Result::Problem->fixed_states()->{$state} ) ) {
+ if ($p->is_visible) {
+ $p->state($state);
+ }
+ $comment->problem_state($state);
}
}
@@ -127,17 +139,19 @@ sub update_comments {
$comment->insert();
if ( $self->suppress_alerts ) {
- my $alert = FixMyStreet::App->model('DB::Alert')->find( {
+ my @alerts = FixMyStreet::App->model('DB::Alert')->search( {
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,
- } );
+ for my $alert (@alerts) {
+ my $alerts_sent = FixMyStreet::App->model('DB::AlertSent')->find_or_create( {
+ alert_id => $alert->id,
+ parameter => $comment->id,
+ } );
+ }
}
}
}
@@ -146,4 +160,22 @@ sub update_comments {
return 1;
}
+sub map_state {
+ my $self = shift;
+ my $incoming_state = shift;
+
+ $incoming_state = lc($incoming_state);
+ $incoming_state =~ s/_/ /g;
+
+ my %state_map = (
+ fixed => 'fixed - council',
+ 'not councils responsibility' => 'not responsible',
+ 'no further action' => 'unable to fix',
+ open => 'confirmed',
+ closed => 'fixed - council'
+ );
+
+ return $state_map{$incoming_state} || $incoming_state;
+}
+
1;
diff --git a/perllib/Open311/GetUpdates.pm b/perllib/Open311/GetUpdates.pm
index 5d5291d47..5007a1f82 100644
--- a/perllib/Open311/GetUpdates.pm
+++ b/perllib/Open311/GetUpdates.pm
@@ -4,26 +4,22 @@ use Moose;
use Open311;
use FixMyStreet::App;
-has council_list => ( is => 'ro' );
+has body_list => ( is => 'ro' );
has system_user => ( is => 'ro' );
sub get_updates {
my $self = shift;
- while ( my $council = $self->council_list->next ) {
+ while ( my $body = $self->body_list->next ) {
my $open311 = Open311->new(
- endpoint => $council->endpoint,
- jurisdiction => $council->jurisdiction,
- api_key => $council->api_key
+ endpoint => $body->endpoint,
+ jurisdiction => $body->jurisdiction,
+ api_key => $body->api_key
);
- my $area_id = $council->area_id;
-
- my $council_details = mySociety::MaPit::call( 'area', $area_id );
-
my $reports = FixMyStreet::App->model('DB::Problem')->search(
{
- council => { like => "\%$area_id\%" },
+ bodies_str => { like => "\%" . $body->id . "\%" },
state => { 'IN', [qw/confirmed fixed/] },
-and => [
external_id => { '!=', undef },
@@ -39,12 +35,12 @@ sub get_updates {
next unless @report_ids;
- $self->update_reports( \@report_ids, $open311, $council_details );
+ $self->update_reports( \@report_ids, $open311, $body );
}
}
sub update_reports {
- my ( $self, $report_ids, $open311, $council_details ) = @_;
+ my ( $self, $report_ids, $open311, $body ) = @_;
my $service_requests = $open311->get_service_requests( $report_ids );
@@ -72,7 +68,9 @@ sub update_reports {
->search( { external_id => $request_id, } );
if (my $p = $problem->first) {
- $p->update_from_open311_service_request( $request, $council_details, $self->system_user );
+ my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($p->cobrand)->new();
+ $cobrand->set_lang_and_domain($p->lang, 1, FixMyStreet->path_to('locale')->stringify );
+ $p->update_from_open311_service_request( $request, $body, $self->system_user );
}
}
diff --git a/perllib/Open311/PopulateServiceList.pm b/perllib/Open311/PopulateServiceList.pm
index e8ae56833..c5fc4a506 100644
--- a/perllib/Open311/PopulateServiceList.pm
+++ b/perllib/Open311/PopulateServiceList.pm
@@ -6,34 +6,34 @@ use XML::Simple;
use FixMyStreet::App;
use Open311;
-has council_list => ( is => 'ro' );
+has bodies => ( is => 'ro' );
has found_contacts => ( is => 'rw', default => sub { [] } );
has verbose => ( is => 'ro', default => 0 );
-has _current_council => ( is => 'rw' );
+has _current_body => ( is => 'rw' );
has _current_open311 => ( is => 'rw' );
has _current_service => ( is => 'rw' );
-my $council_list = FixMyStreet::App->model('DB::Open311conf');
+my $bodies = FixMyStreet::App->model('DB::Body');
-sub process_councils {
+sub process_bodies {
my $self = shift;
- while ( my $council = $self->council_list->next ) {
- next unless $council->endpoint;
- next unless lc($council->send_method) eq 'open311';
- $self->_current_council( $council );
- $self->process_council;
+ while ( my $body = $self->bodies->next ) {
+ next unless $body->endpoint;
+ next unless lc($body->send_method) eq 'open311';
+ next if $body->jurisdiction =~ /^fixmybarangay_\w+$/; # FMB depts. not using service discovery yet
+ $self->_current_body( $body );
+ $self->process_body;
}
}
-sub process_council {
+sub process_body {
my $self = shift;
-
my $open311 = Open311->new(
- endpoint => $self->_current_council->endpoint,
- jurisdiction => $self->_current_council->jurisdiction,
- api_key => $self->_current_council->api_key
+ endpoint => $self->_current_body->endpoint,
+ jurisdiction => $self->_current_body->jurisdiction,
+ api_key => $self->_current_body->api_key
);
$self->_current_open311( $open311 );
@@ -41,8 +41,10 @@ sub process_council {
my $list = $open311->get_service_list;
unless ( $list ) {
- my $id = $self->_current_council->area_id;
- warn "Council $id - http://mapit.mysociety.org/area/$id.html - did not return a service list\n"
+ my $id = $self->_current_body->id;
+ my $mapit_url = mySociety::Config::get('MAPIT_URL');
+ my $areas = join( ",", keys %{$self->_current_body->areas} );
+ warn "Body $id for areas $areas - $mapit_url/areas/$areas.html - did not return a service list\n"
if $self->verbose >= 1;
return;
}
@@ -55,7 +57,7 @@ sub _check_endpoints {
my $self = shift;
# west berks end point not standard
- if ( $self->_current_council->area_id == 2619 ) {
+ if ( $self->_current_body->areas->{2619} ) {
$self->_current_open311->endpoints(
{
services => 'Services',
@@ -71,7 +73,10 @@ sub process_services {
my $list = shift;
$self->found_contacts( [] );
- foreach my $service ( @{ $list->{service} } ) {
+ my $services = $list->{service};
+ # XML might only have one result and then squashed the 'array'-ness
+ $services = [ $services ] unless ref $services eq 'ARRAY';
+ foreach my $service ( @$services ) {
$self->_current_service( $service );
$self->process_service;
}
@@ -81,14 +86,14 @@ sub process_services {
sub process_service {
my $self = shift;
- my $category = $self->_current_council->area_id == 2218 ?
- $self->_current_service->{description} :
+ my $category = $self->_current_body->areas->{2218} ?
+ $self->_current_service->{description} :
$self->_current_service->{service_name};
print $self->_current_service->{service_code} . ': ' . $category . "\n" if $self->verbose >= 2;
my $contacts = FixMyStreet::App->model( 'DB::Contact')->search(
{
- area_id => $self->_current_council->area_id,
+ body_id => $self->_current_body->id,
-OR => [
email => $self->_current_service->{service_code},
category => $category,
@@ -125,7 +130,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" if $self->verbose >= 2;
+ print $self->_current_body->id . " already has a contact for service code " . $self->_current_service->{service_code} . "\n" if $self->verbose >= 2;
if ( $contact->deleted || $service_name ne $contact->category || $self->_current_service->{service_code} ne $contact->email ) {
eval {
@@ -143,7 +148,7 @@ sub _handle_existing_contact {
};
if ( $@ ) {
- warn "Failed to update contact for service code " . $self->_current_service->{service_code} . " for council @{[$self->_current_council->area_id]}: $@\n"
+ warn "Failed to update contact for service code " . $self->_current_service->{service_code} . " for body @{[$self->_current_body->id]}: $@\n"
if $self->verbose >= 1;
return;
}
@@ -168,7 +173,7 @@ sub _create_contact {
$contact = FixMyStreet::App->model( 'DB::Contact')->create(
{
email => $self->_current_service->{service_code},
- area_id => $self->_current_council->area_id,
+ body_id => $self->_current_body->id,
category => $service_name,
confirmed => 1,
deleted => 0,
@@ -180,7 +185,7 @@ sub _create_contact {
};
if ( $@ ) {
- warn "Failed to create contact for service code " . $self->_current_service->{service_code} . " for council @{[$self->_current_council->area_id]}: $@\n"
+ warn "Failed to create contact for service code " . $self->_current_service->{service_code} . " for body @{[$self->_current_body->id]}: $@\n"
if $self->verbose >= 1;
return;
}
@@ -191,7 +196,7 @@ 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" if $self->verbose >= 2;
+ print "created contact for service code " . $self->_current_service->{service_code} . " for body @{[$self->_current_body->id]}\n" if $self->verbose >= 2;
}
}
@@ -210,7 +215,7 @@ sub _add_meta_to_contact {
if ( ! $meta_data->{attributes}->{attribute} ) {
warn sprintf( "Empty meta data for %s at %s",
$self->_current_service->{service_code},
- $self->_current_council->endpoint )
+ $self->_current_body->endpoint )
if $self->verbose;
return;
}
@@ -225,7 +230,7 @@ sub _add_meta_to_contact {
# 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 ) {
+ if ( $self->_current_body->areas->{2482} ) {
my %ignore = map { $_ => 1 } qw/
service_request_id_ext
requested_datetime
@@ -256,8 +261,8 @@ sub _normalize_service_name {
# FIXME - at the moment it makes more sense to use the description
# for cambridgeshire but need a more flexible way to set this
- my $service_name = $self->_current_council->area_id == 2218 ?
- $self->_current_service->{description} :
+ my $service_name = $self->_current_body->areas->{2218} ?
+ $self->_current_service->{description} :
$self->_current_service->{service_name};
# remove trailing whitespace as it upsets db queries
# to look up contact details when creating problem
@@ -272,7 +277,7 @@ sub _delete_contacts_not_in_service_list {
my $found_contacts = FixMyStreet::App->model( 'DB::Contact')->search(
{
email => { -not_in => $self->found_contacts },
- area_id => $self->_current_council->area_id,
+ body_id => $self->_current_body->id,
deleted => 0,
}
);