aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/Open311/GetServiceRequestUpdates.pm
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2019-10-16 12:37:52 +0100
committerMatthew Somerville <matthew@mysociety.org>2020-04-02 17:14:20 +0100
commit1f0473c9844b6e32338d1c1af6e368f3387e8890 (patch)
treede1edb52f27957ad4c6c35be850f91389ca09f16 /perllib/Open311/GetServiceRequestUpdates.pm
parentd44c06c213bc73632893ab58421ab98b7058b913 (diff)
Combine and improve fetch-comments/reports scripts
Make them take start/end hour arguments, cope if only one given, optional body, and combine them together in one `fetch` script.
Diffstat (limited to 'perllib/Open311/GetServiceRequestUpdates.pm')
-rw-r--r--perllib/Open311/GetServiceRequestUpdates.pm59
1 files changed, 37 insertions, 22 deletions
diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm
index da3461f6e..a407d8e37 100644
--- a/perllib/Open311/GetServiceRequestUpdates.pm
+++ b/perllib/Open311/GetServiceRequestUpdates.pm
@@ -60,31 +60,39 @@ sub fetch {
}
}
-sub process_body {
+sub parse_dates {
my $self = shift;
-
- my $open311 = $self->current_open311;
my $body = $self->current_body;
my @args = ();
- if ( $self->start_date || $self->end_date ) {
- return 0 unless $self->start_date && $self->end_date;
+ my $dt = DateTime->now();
+ # Oxfordshire uses local time and not UTC for dates
+ FixMyStreet->set_time_zone($dt) if $body->areas->{$AREA_ID_OXFORDSHIRE};
- push @args, $self->start_date;
- push @args, $self->end_date;
# default to asking for last 2 hours worth if not Bromley
+ if ($self->start_date) {
+ push @args, DateTime::Format::W3CDTF->format_datetime( $self->start_date );
} elsif ( ! $body->areas->{$AREA_ID_BROMLEY} ) {
- my $end_dt = DateTime->now();
- # Oxfordshire uses local time and not UTC for dates
- FixMyStreet->set_time_zone($end_dt) if ( $body->areas->{$AREA_ID_OXFORDSHIRE} );
- my $start_dt = $end_dt->clone;
- $start_dt->add( hours => -2 );
-
+ my $start_dt = $dt->clone->add( hours => -2 );
push @args, DateTime::Format::W3CDTF->format_datetime( $start_dt );
- push @args, DateTime::Format::W3CDTF->format_datetime( $end_dt );
}
+ if ($self->end_date) {
+ push @args, DateTime::Format::W3CDTF->format_datetime( $self->end_date );
+ } elsif ( ! $body->areas->{$AREA_ID_BROMLEY} ) {
+ push @args, DateTime::Format::W3CDTF->format_datetime( $dt );
+ }
+
+ return @args;
+}
+
+sub process_body {
+ my $self = shift;
+
+ my $open311 = $self->current_open311;
+ my $body = $self->current_body;
+ my @args = $self->parse_dates;
my $requests = $open311->get_service_request_updates( @args );
unless ( $open311->success ) {
@@ -106,16 +114,9 @@ sub process_body {
return 1;
}
-sub find_problem {
+sub check_date {
my ($self, $request, @args) = @_;
- my $body = $self->current_body;
- 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
- return unless $request_id || $request->{fixmystreet_id};
-
my $comment_time = eval {
DateTime::Format::W3CDTF->parse_datetime( $request->{updated_datetime} || "" )
->set_time_zone(FixMyStreet->local_time_zone);
@@ -124,6 +125,20 @@ sub find_problem {
my $updated = DateTime::Format::W3CDTF->format_datetime($comment_time->clone->set_time_zone('UTC'));
return if @args && ($updated lt $args[0] || $updated gt $args[1]);
$request->{comment_time} = $comment_time;
+ return 1;
+}
+
+sub find_problem {
+ my ($self, $request, @args) = @_;
+
+ $self->check_date($request, @args) or return;
+
+ my $body = $self->current_body;
+ 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
+ return unless $request_id || $request->{fixmystreet_id};
my $problem;
my $criteria = {