aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/SendReport/Angus.pm
blob: 4373c3c37125d6b5b3363c804e5049f45ec1f749 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package FixMyStreet::SendReport::Angus;

use Moo;

BEGIN { extends 'FixMyStreet::SendReport'; }

use Try::Tiny;
use Encode;
use XML::Simple;
use mySociety::Web qw(ent);

sub get_auth_token {
    my ($self, $authxml) = @_;

    my $xml = new XML::Simple;
    my $obj;

    eval {
        $obj = $xml->parse_string( $authxml );
    };

    my $success = $obj->{success};
    $success =~ s/^\s+|\s+$//g if defined $success;
    my $token = $obj->{AuthenticateResult};
    $token =~ s/^\s+|\s+$//g if defined $token;

    if (defined $success && $success eq 'True' && defined $token) {
        return $token;
    } else {
        $self->error("Couldn't authenticate against Angus endpoint.");
    }
}

sub get_external_id {
    my ($self, $resultxml) = @_;

    my $xml = new XML::Simple;
    my $obj;

    eval {
        $obj = $xml->parse_string( $resultxml );
    };

    my $success = $obj->{success};
    $success =~ s/^\s+|\s+$//g if defined $success;
    my $external_id = $obj->{CreateRequestResult}->{RequestId};

    if (defined $success && $success eq 'True' && defined $external_id) {
        return $external_id;
    } else {
        $self->error("Couldn't find external id in response from Angus endpoint.");
        return undef;
    }
}

sub crm_request_type {
    my ($self, $row, $h) = @_;
    return 'StLight'; # TODO: Set this according to report category
}

sub jadu_form_fields {
    my ($self, $row, $h) = @_;
    my $xml = XML::Simple->new(
        NoAttr=> 1,
        KeepRoot => 1,
        SuppressEmpty => 0,
    );
    my $metas = $row->get_extra_fields();
    my %extras;
    foreach my $field (@$metas) {
        $extras{$field->{name}} = $field->{value};
    }
    my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new();
    my $output = $xml->XMLout({
        formfields => {
            formfield => [
                {
                    name => 'RequestTitle',
                    value => $h->{title}
                },
                {
                    name => 'RequestDetails',
                    value => $h->{detail}
                },
                {
                    name => 'ReporterName',
                    value => $h->{name}
                },
                {
                    name => 'ReporterEmail',
                    value => $h->{email}
                },
                {
                    name => 'ReporterAnonymity',
                    value => $row->anonymous ? 'True' : 'False'
                },
                {
                    name => 'ReportedDateTime',
                    value => $h->{confirmed}
                },
                {
                    name => 'ColumnId',
                    value => $extras{'column_id'} || ''
                },
                {
                    name => 'ReportId',
                    value => $h->{id}
                },
                {
                    name => 'ReportedNorthing',
                    value => $h->{northing}
                },
                {
                    name => 'ReportedEasting',
                    value => $h->{easting}
                },
                {
                    name => 'Imageurl1',
                    value => $row->photos->[0] ? ($cobrand->base_url . $row->photos->[0]->{url_full}) : ''
                },
                {
                    name => 'Imageurl2',
                    value => $row->photos->[1] ? ($cobrand->base_url . $row->photos->[1]->{url_full}) : ''
                },
                {
                    name => 'Imageurl3',
                    value => $row->photos->[2] ? ($cobrand->base_url . $row->photos->[2]->{url_full}) : ''
                }
            ]
        }
    });
    # The endpoint crashes if the JADUFormFields string has whitespace between XML elements, so strip it out...
    $output =~ s/>[\s\n]+</></g;
    return $output;
}

sub send {
    my ( $self, $row, $h ) = @_;

    # FIXME: should not recreate this each time
    my $angus_service;

    require Integrations::AngusSOAP;

    my $return = 1;
    $angus_service ||= Integrations::AngusSOAP->on_fault(sub { my($soap, $res) = @_; die ref $res ? $res->faultstring : $soap->transport->status, "\n"; });
    try {
        my $authresult = $angus_service->AuthenticateJADU();
        my $authtoken = $self->get_auth_token( $authresult );
        # authenticationtoken, CallerId, CallerAddressId, DeliveryId, DeliveryAddressId, CRMRequestType, JADUXFormRef, PaymentRef, JADUFormFields
        my $message = ent(encode_utf8($h->{message}));
        my $name = ent(encode_utf8($h->{name}));
        my $result = $angus_service->CreateServiceRequest(
            $authtoken, '1', '1', '1', '1', $self->crm_request_type($row, $h),
            'FMS', '', $self->jadu_form_fields($row, $h)
        );
        my $external_id = $self->get_external_id( $result );
        if ( $external_id ) {
            $row->external_id( $external_id );
            $row->send_method_used('Angus');
            $return = 0;
        }
    } catch {
        my $e = $_;
        print "Caught an error: $e\n";
        $self->error( "Error sending to Angus: $e" );
    };
    $self->success( !$return );
    return $return;
}

1;