aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/Open311/GetServiceRequestUpdates.pm
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2012-03-19 18:10:47 +0000
committerStruan Donald <struan@exo.org.uk>2012-03-19 18:10:47 +0000
commit3fce129f4aa4ae25ebaad559d8da9c30cad484c6 (patch)
tree9c2ab6c8521536d205823f961d421e50c6eae209 /perllib/Open311/GetServiceRequestUpdates.pm
parent246303b1e234fbde17775054859af4d6fe7e4a80 (diff)
add first pass of update comments method and tests
Diffstat (limited to 'perllib/Open311/GetServiceRequestUpdates.pm')
-rw-r--r--perllib/Open311/GetServiceRequestUpdates.pm77
1 files changed, 77 insertions, 0 deletions
diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm
new file mode 100644
index 000000000..d4e323cc3
--- /dev/null
+++ b/perllib/Open311/GetServiceRequestUpdates.pm
@@ -0,0 +1,77 @@
+package Open311::GetServiceRequestUpdates;
+
+use Moose;
+use Open311;
+use FixMyStreet::App;
+
+has council_list => ( is => 'ro' );
+has system_user => ( is => 'ro' );
+
+sub update_comments {
+ my ( $self, $open311, $council_details ) = @_;
+
+ my $service_requests = $open311->get_service_request_updates( );
+
+ my $requests;
+
+ # XML::Simple is a bit inconsistent in how it structures
+ # things depending on the number of children an element has :(
+ if ( ref $service_requests->{request_update } eq 'ARRAY' ) {
+ $requests = $service_requests->{requesti_update};
+ }
+ else {
+ $requests = [ $service_requests->{request_update} ];
+ }
+
+ for my $request (@$requests) {
+ # if it's a ref that means it's an empty element
+ # however, if there's no updated date then we can't
+ # tell if it's newer that what we have so we should skip it
+ next if ref $request->{updated_datetime} || ! exists $request->{updated_datetime};
+
+ my $request_id = $request->{service_request_id};
+
+ my $problem =
+ FixMyStreet::App->model('DB::Problem')
+ ->search( { external_id => $request_id, } );
+
+ if (my $p = $problem->first) {
+ my $c = FixMyStreet::App->model('DB::Comment')
+ ->search( { external_id => $request->{update_id} } );
+
+ if ( !$c->first ) {
+ 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
+ }
+ );
+ $comment->confirm;
+
+ if ( $p->is_open and $request->{status} eq 'closed' ) {
+ $p->state( 'closed' );
+ $p->update;
+
+ $comment->mark_fixed( 1 );
+ } elsif ( $p->is_closed and $request->{status} eq 'open' ) {
+ $p->state( 'open' );
+ $p->update;
+
+ $comment->mark_open( 1 );
+ }
+
+ $comment->insert();
+ }
+ }
+ }
+
+ return 1;
+}
+
+1;