aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/Open311/Endpoint/Role/mySociety.pm
blob: 3669c585bf0fc2e6d64c2407c9718cf4f201d38f (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
package Open311::Endpoint::Role::mySociety;

=head1 NAME

Open311::Endpoint::Role::mySociety - mySociety's proposed Open311 extensions

=head1 SYNOPSIS

See mySociety's 
L<blog post|https://www.mysociety.org/2013/02/20/open311-extended/>
and 
L<proposal|https://github.com/mysociety/FixMyStreet/wiki/Open311-FMS---Proposed-differences-to-Open311>
for a full explanation of the spec extension.

You can use the extensions as follows:

    package My::Open311::Endpoint;
    use Web::Simple;
    extends 'Open311::Endpoint';
    with 'Open311::Endpoint::Role::mySociety';

You will have to provide implementations of

    get_service_request_updates
    post_service_request_update

You will need to return L<Open311::Endpoint::Service::Request::Update>
objects.  However, the root L<Open311::Endpoint::Service::Request> is not
aware of updates, so you may may find it easier to ensure that the ::Service
objects you create (with get_service_request etc.) return
L<Open311::Endpoint::Service::Request::mySociety> objects.

=cut

use Moo::Role;
no warnings 'illegalproto';

use Open311::Endpoint::Service::Request::mySociety;
has '+request_class' => (
    is => 'ro',
    default => 'Open311::Endpoint::Service::Request::mySociety',
);

around dispatch_request => sub {
    my ($orig, $self, @args) = @_;
    my @dispatch = $self->$orig(@args);
    return (
        @dispatch,

        sub (GET + /servicerequestupdates + ?*) {
            my ($self, $args) = @_;
            $self->call_api( GET_Service_Request_Updates => $args );
        },

        sub (POST + /servicerequestupdates + ?*) {
            my ($self, $args) = @_;
            $self->call_api( POST_Service_Request_Update => $args );
        },

    );
};

sub GET_Service_Request_Updates_input_schema {
    my $self = shift;
    return {
        type => '//rec',
        required => {
            $self->get_jurisdiction_id_required_clause,
        },
        optional => {
            $self->get_jurisdiction_id_optional_clause,,
            start_date => '/open311/datetime',
            end_date   => '/open311/datetime',
        }
    };
}

sub GET_Service_Request_Updates_output_schema {
    my $self = shift;
    return {
        type => '//rec',
        required => {
            service_request_updates => {
                type => '//arr',
                contents => '/open311/service_request_update',
            },
        },
    };
}

sub GET_Service_Request_Updates {
    my ($self, $args) = @_;

    my @updates = $self->get_service_request_updates({
        jurisdiction_id => $args->{jurisdiction_id},
        start_date => $args->{start_date},
        end_date => $args->{end_date},
    });

    $self->format_updates(@updates);
}

sub format_updates {
    my ($self, @updates) = @_;
    return {
        service_request_updates => [
            map {
                my $update = $_;
                +{
                    (
                        map {
                            $_ => $update->$_,
                        }
                        qw/
                            update_id
                            service_request_id
                            status
                            description
                            media_url
                            / 
                    ),
                    (
                        map {
                            $_ => $self->w3_dt->format_datetime( $update->$_ ), 
                        }
                        qw/
                            updated_datetime
                        /
                    ),
                }
            } @updates
        ]
    };
}

sub get_service_request_updates {
    my ($self, $args) = @_;
    die "abstract method get_service_request_updates not overridden";
}

sub learn_additional_types {
    my ($self, $schema) = @_;
    $schema->learn_type( 'tag:wiki.open311.org,GeoReport_v2:rx/service_request_update',
        {
            type => '//rec',
            required => {
                service_request_id => $self->get_identifier_type('service_request_id'),
                update_id => $self->get_identifier_type('update_id'),
                status => '/open311/status',
                updated_datetime => '/open311/datetime',
                description => '//str',
                media_url => '//str',
            },
        }
    );
}

1;