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
|
#!/usr/bin/env perl
use strict;
use warnings;
require 5.8.0;
use feature 'say';
BEGIN {
use File::Basename qw(dirname);
use File::Spec;
my $d = dirname(File::Spec->rel2abs($0));
require "$d/../setenv.pl";
}
use FixMyStreet::App;
my $moniker = $ARGV[0];
my $c = FixMyStreet::App->new();
my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($moniker)->new({ c => $c });
$c->stash->{cobrand} = $cobrand;
die "Not a staging site, bailing out" unless $c->config->{STAGING_SITE}; # TODO, allow override via --force
say "Applying contacts for $cobrand";
ensure_bodies();
setup_contacts();
=head2 setup_contacts, ensure_bodies
routines to update extra data for contacts. These can be called by
a script:
bin/setup-contacts zurich
=cut
sub ensure_bodies {
my @bodies = $cobrand->body_details_data;
my $bodies_rs = $c->model('DB::Body');
for my $body (@bodies) {
# following should work (having added Unique name/parent constraint, but doesn't)
# $bodies_rs->find_or_create( $body, { $parent ? ( key => 'body_name_parent_key' ) : () } );
# let's keep it simple and just allow unique names
next if $bodies_rs->search({ name => $body->{name} })->count;
if (my $area_id = delete $body->{area_id}) {
$body->{body_areas} = [ { area_id => $area_id } ];
}
my $parent = $body->{parent};
if ($parent and ! ref $parent) {
$body->{parent} = { name => $parent };
}
$bodies_rs->find_or_create( $body );
}
}
sub setup_contacts {
die "Not a staging site, bailing out" unless $c->config->{STAGING_SITE}; # TODO, allow override via --force
my @contact_details = $cobrand->contact_details_data;
for my $detail (@contact_details) {
update_contact( $detail, $description );
}
}
sub update_contact {
my ($contact_data, $description) = @_;
my $contact_rs = $c->model('DB::Contact');
my $category = $contact_data->{category} or die "No category provided";
$description ||= "Update contact";
my $contact = ensure_contact($contact_data, $description)
or return; # e.g. nothing returned if deleted
if (my $fields = $contact_data->{fields}) {
my @fields = map { get_field_extra($_) } @$fields;
my $note = sprintf 'Fields edited by automated script%s', $description ? " ($description)" : '';
$contact->set_extra_fields(@fields);
$contact->set_inflated_columns({
confirmed => 1,
deleted => 0,
editor => 'automated script',
whenedited => \'NOW()',
note => "Updated fields $description",
});
$contact->update;
}
}
sub ensure_contact {
my ($contact_data, $description) = @_;
my $category = $contact_data->{category} or die "No category provided";
$description ||= "Ensure contact exists $category";
my $email = temp_email_to_update(); # will only be set if newly created
my $body = get_body_for_contact($contact_data) or die "No body found for $category";
my $contact_rs = $c->model('DB::Contact');
my $category_details = $contact_data->{category_details} || {};
if (my $old_name = delete $contact_data->{rename_from}) {
if (my $old_category = $contact_rs->find({
category => $old_name,
, body => $body,
})) {
$old_category->update({
category => $category,
whenedited => \'NOW()',
note => "Renamed $description",
%{ $category_details || {} },
});
return $old_category;
}
}
if ($contact_data->{delete}) {
my $contact = $contact_rs->search({
body_id => $body->id,
category => $category,
deleted => 0
});
if ($contact->count) {
print sprintf "Deleting: %s\n", $category;
$contact->update({
deleted => 1,
editor => 'automated script',
whenedited => \'NOW()',
note => "Deleted by script $description",
});
}
return;
}
return $contact_rs->find_or_create(
{
body => $body,
category => $category,
confirmed => 1,
deleted => 0,
email => $email,
editor => 'automated script',
note => 'created by automated script',
send_method => '',
whenedited => \'NOW()',
%{ $category_details || {} },
},
{
key => 'contacts_body_id_category_idx'
}
);
}
sub get_field_extra {
my ($field) = @_;
my %default = (
variable => 'true',
order => '1',
required => 'no',
datatype => 'string',
datatype_description => 'a string',
);
if (($field->{datatype} || '') eq 'boolean') {
%default = (
%default,
datatype => 'singlevaluelist',
datatype_description => 'Yes or No',
values => { value => [
{ key => ['No'], name => ['No'] },
{ key => ['Yes'], name => ['Yes'] },
] },
);
}
return { %default, %$field };
}
sub temp_email_to_update { 'test@example.com' }
sub get_body_for_contact {
my ($contact_data) = @_;
if (my $body_name = $contact_data->{body_name}) {
return $c->model('DB::Body')->find({ name => $body_name });
}
if ($cobrand->can('contact_details_data_body_default')) {
return $cobrand->contact_details_data_body_default;
}
return;
# TODO: for UK Councils use
# $c->model('DB::Body')->find(id => $cobrand->council_id);
# # NB: (or better that's the area in BodyAreas)
}
|