aboutsummaryrefslogtreecommitdiffstats
path: root/bin/fixmystreet.com/fixture
blob: 2f882381ae7381d77d511b68f527114470ad6173 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env perl
#
# This script will create a test body and its categories, covering the area
# provided, and users associated with that body, which should help testing
# of report interactions.

use strict;
use warnings;
use v5.14;

BEGIN {
    use File::Basename qw(dirname);
    use File::Spec;
    my $d = dirname(File::Spec->rel2abs($0));
    require "$d/../../setenv.pl";
}

use List::Util qw(shuffle);
use Path::Tiny;
use FixMyStreet;
use FixMyStreet::Cobrand;
use FixMyStreet::PhotoStorage;
use FixMyStreet::DB::Factories;
use Getopt::Long::Descriptive;

my ($opt, $usage) = describe_options(
    '%c %o',
    [ 'area-id=i', "MapIt area ID to create body for", { required => 1 } ],
    [ 'name:s', "Name of body to use (defaults to MapIt area name)" ],
    [ 'empty', "Empty all tables of the database first" ],
    [ 'commit', "Actually commit changes to the database" ],
    [ 'coords=s', "Co-ordinates to use instead of example postcode" ],
    [ 'help', "print usage message and exit", { shortcircuit => 1 } ],
);
print($usage->text), exit if $opt->help;

FixMyStreet::DB::Factories->setup($opt);

# Body + categories
my $categories = ['Potholes', 'Street lighting', 'Graffiti', 'Other'];
my $body = FixMyStreet::DB::Factory::Body->find_or_create({
    area_id => $opt->area_id,
    categories => $categories,
    $opt->name ? (name => $opt->name) : (),
});
say "Created body " . $body->name . " for MapIt area ID " . $opt->area_id . ', categories ' . join(', ', @$categories);

FixMyStreet::DB::Factory::ResponseTemplate->create({
    body => $body, title => 'Generic',
    text => 'Thank you for your report, we will be in touch with an update soon.' });
FixMyStreet::DB::Factory::ResponseTemplate->create({
    body => $body, title => 'Fixed', state => 'fixed - council',
    text => 'Thank you for reporting that issue, it has now been resolved.' });
FixMyStreet::DB::Factory::ResponseTemplate->create({
    body => $body, title => 'Monitoring', state => 'unable to fix',
    text => 'Thank you for your enquiry. I have visited the site and assessed the problem that you have identified. Unfortunately it doesn’t meet our current intervention criteria so I cannot arrange a repair. We will continue to monitor this during our routine inspections.' });
FixMyStreet::DB::Factory::ResponseTemplate->create({
    body => $body, title => 'Taxed in date', state => 'unable to fix',
    text => 'We have investigated this report and found the vehicle to be taxed therefore no further action will be taken.  Please note that from October 2014 it is no longer a legal requirement to display a tax disc.' });
my $template = FixMyStreet::DB::Factory::ResponseTemplate->create({
    body => $body, title => 'Not responsible', state => 'not responsible',
    text => 'This report is not the responsibility of the council and will be passed to the relevant organisation.' });
$template->add_to_contacts($body->contacts->first);
my $priority = FixMyStreet::DB::Factory::ResponsePriority->create({ body => $body, description => '4 hour fix' });
FixMyStreet::DB::Factory::ResponsePriority->create({ body => $body, description => '28 day fix' });
FixMyStreet::DB::Factory::ResponsePriority->create({ body => $body, description => 'For information only' });
$priority->add_to_contacts($body->contacts->first);

# Users
say "Created users, all with password 'password':";
my %users;
my $perms_inspector = ['report_inspect', 'planned_reports'];
my $perms_cs = [
    'contribute_as_body', 'contribute_as_another_user',
    'moderate', 'view_body_contribute_details',
];
foreach (
    { name => 'Inspector Gadget', email => 'inspector@example.org', email_verified => 1, body => $body, permissions => $perms_inspector },
    { name => 'Harriet Helpful', email_verified => 1, email => 'cs@example.org', body => $body, permissions => $perms_cs },
    { name => 'Super User', email_verified => 1, email => 'super@example.org', body => $body, permissions => [
        @$perms_cs, @$perms_inspector, 'report_edit',
        'category_edit', 'template_edit', 'responsepriority_edit',
        'user_assign_body', 'user_manage_permissions', 'user_edit',
    ] },
    { name => 'Wizard of Oz', email_verified => 1, email => 'admin@example.org', is_superuser => 't' },
    { name => "Norma User", email_verified => 1, email => 'user@example.org' },
) {
    $users{$_->{email}} = FixMyStreet::DB::Factory::User->find_or_create($_);
    my $perms = $_->{permissions} ? " (" . join(', ', @{$_->{permissions} || []}) . ")" : "";
    my $su = $_->{is_superuser} ? " (superuser)" : "";
    say "* $_->{email}$perms$su";
}

# Problems

my ($location, $lat, $lon);
if ($opt->coords) {
    $location = $opt->coords;
    ($lat, $lon) = split ',', $location;
} else {
    my $postcode = mySociety::MaPit::call('area/example_postcode', $opt->area_id);
    $postcode = mySociety::MaPit::call('postcode', $postcode);
    ($location, $lat, $lon) = map { $postcode->{$_} } qw/postcode wgs84_lat wgs84_lon/;
}

my $cobrand = 'default';
foreach (FixMyStreet::Cobrand->available_cobrand_classes) {
    my $sub = $_->{class} && $_->{class}->can('council_area_id');
    if ($sub && &$sub == $opt->area_id) {
        $cobrand = $_->{class}->moniker;
        last;
    }
}

FixMyStreet::PhotoStorage::backend->init()

my $user = $users{'user@example.org'};
my $num = 20;
say "Created $num problems around '$location' in cobrand '$cobrand'";
my $confirmed = DateTime->today->subtract(days => 1)->add(hours => 8);
my $problems = [];
for (1..$num) {
    $confirmed->add(seconds => rand(7000));
    my $category = $categories->[int(rand(@$categories))];
    push @$problems, FixMyStreet::DB::Factory::Problem->create_problem({
        body => $body,
        areas => ',' . $opt->area_id . ',',
        user => $user,
        postcode => $location,
        latitude => $lat,
        longitude => $lon,
        category => $category,
        cobrand => $cobrand,
        confirmed => $confirmed,
    });
}

for (1..3) {
    my $p = $problems->[int(rand(@$problems))];
    $users{'inspector@example.org'}->add_to_planned_reports($p);
}

# Flow comments
my @fixed_user = (
    'Brilliant! Seen to within two days and no more problems, thanks.',
    'Thank you for getting this sorted out so quickly.',
    'Walked past last Tuesday and it seems to have been fixed, hooray.',
    'Well it took a few emails back and forth to understand the exact location etc but pleased to say it’s all ok now.',
    'Yep, all good.',
    'The council deserve every penny of their salaries, honestly can’t give them higher praise for the way they’ve handled this issue. Bravo.',
    'All seems ok now, fingers crossed.',
    'Bish bash bosh. Sorted. Thanks so much.',
);

my @problems = shuffle(@$problems);

for (1..3) {
    my $problem = $problems[$_];
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        user => $users{'cs@example.org'},
        text => 'Could you please provide a better picture so we can locate this issue more easily.',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        user => $user,
        text => 'Here you go!',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        problem_state => 'investigating',
        user => $users{'cs@example.org'},
        text => 'Thanks, we are now looking into this.',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        problem_state => 'action scheduled',
        user => $users{'inspector@example.org'},
        text => 'Confirmed and scheduled for fixing.',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        problem_state => 'fixed - council',
        user => $users{'inspector@example.org'},
        text => 'Fixed',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $problem->update({ state => 'fixed - council' });
    $confirmed->add(seconds => rand(10000));
    FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        user => $user,
        text => $fixed_user[int(rand(@fixed_user))],
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
}

# Some 'still open' updates
my @open_user = (
    'I’m afraid this problem is recurrent. Could it be prioritised please?',
    'Ongoing issue.',
    'Council rang to say they’re aware and it’s on their list.',
    'Still awaiting news on this one.',
    'Council let me know it’s not a top priority, which TBH I  do understand now they’ve talked it through.',
);

my $updates = [];
for (5..9) {
    $confirmed->add(seconds => rand(10000));
    push @$updates, FixMyStreet::DB::Factory::Comment->create({
        problem => $problems[$_],
        user => $user,
        text => $open_user[int(rand(@open_user))],
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
}

# Some not responsible updates
for (11..13) {
    my $problem = $problems[$_];
    $confirmed->add(seconds => rand(10000));
    push @$updates, FixMyStreet::DB::Factory::Comment->create({
        problem => $problem,
        problem_state => 'not responsible',
        user => $users{'cs@example.org'},,
        text => 'This issue is outside our responsibility.',
        confirmed => DateTime::Format::Pg->format_datetime($confirmed),
    });
    $problem->update({ state => 'not responsible' });
}
say "Created updates on problems " . join(', ', map { $_->problem_id } @$updates);