aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/SendReport.pm
blob: b869299a21fc7de42792ed9846785419a39976bb (plain)
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
package FixMyStreet::SendReport;

use Moo;
use MooX::Types::MooseLike::Base qw(:all);

use Module::Pluggable
    sub_name    => 'senders',
    search_path => __PACKAGE__,
    except => 'FixMyStreet::SendReport::Email::SingleBodyOnly',
    require     => 1;

has 'body_config' => ( is => 'rw', isa => HashRef, default => sub { {} } );
has 'bodies' => ( is => 'rw', isa => ArrayRef, default => sub { [] } );
has 'to' => ( is => 'rw', isa => ArrayRef, default => sub { [] } );
has 'bcc' => ( is => 'rw', isa => ArrayRef, default => sub { [] } );
has 'success' => ( is => 'rw', isa => Bool, default => 0 );
has 'error' => ( is => 'rw', isa => Str, default => '' );
has 'unconfirmed_counts' => ( 'is' => 'rw', isa => HashRef, default => sub { {} } );
has 'unconfirmed_notes' => ( 'is' => 'rw', isa => HashRef, default => sub { {} } );


sub should_skip {
    my $self  = shift;
    my $row   = shift;
    my $debug = shift;

    return 0 unless $row->send_fail_count;
    return 0 if $debug;

    my $now = DateTime->now( time_zone => FixMyStreet->local_time_zone );
    my $diff = $now - $row->send_fail_timestamp;

    my $backoff = $row->send_fail_count > 1 ? 30 : 5;
    return $diff->in_units( 'minutes' ) < $backoff;
}

sub get_senders {
    my $self = shift;

    my %senders = map { $_ => 1 } $self->senders;

    return \%senders;
}

sub reset {
    my $self = shift;

    $self->bodies( [] );
    $self->body_config( {} );
    $self->to( [] );
    $self->bcc( [] );
}

sub add_body {
    my $self = shift;
    my $body = shift;
    my $config = shift;

    push @{$self->bodies}, $body;
    $self->body_config->{ $body->id } = $config;
}

sub fetch_category {
    my ($self, $body, $row, $category_override) = @_;

    my $contact = $row->result_source->schema->resultset("Contact")->not_deleted->find( {
        body_id => $body->id,
        category => $category_override || $row->category,
    } );

    unless ($contact) {
        my $error = "Category " . $row->category . " does not exist for body " . $body->id . " and report " . $row->id . "\n";
        $self->error( "Failed to send over Open311\n" ) unless $self->error;
        $self->error( $self->error . "\n" . $error );
    }

    return $contact;
}

1;