diff options
author | Dave Arter <davea@mysociety.org> | 2018-11-07 12:43:39 +0000 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2018-11-07 12:45:49 +0000 |
commit | 49e30befced6b929facbcdc79e81a36ab570d0a7 (patch) | |
tree | 7f420f18e94b3f8942a8151458b6a90f1c342134 /perllib/Open311/PopulateServiceList.pm | |
parent | 702d0747e13882b46186578291dcd2f4ce17d38a (diff) |
Fix Open311 category group bugs
- Group wasn’t being set correctly by open311-populate-service-list as cobrand attribute
not being updated for each body.
- Extra metadata was being persisted to the DB every time even if nothing had changed,
causing lots of duplicate entries in contacts_history.
Diffstat (limited to 'perllib/Open311/PopulateServiceList.pm')
-rw-r--r-- | perllib/Open311/PopulateServiceList.pm | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/perllib/Open311/PopulateServiceList.pm b/perllib/Open311/PopulateServiceList.pm index d3f30d60c..8101c609d 100644 --- a/perllib/Open311/PopulateServiceList.pm +++ b/perllib/Open311/PopulateServiceList.pm @@ -8,12 +8,13 @@ has found_contacts => ( is => 'rw', default => sub { [] } ); has verbose => ( is => 'ro', default => 0 ); has schema => ( is => 'ro', lazy => 1, default => sub { FixMyStreet::DB->schema->connect } ); -has _current_body => ( is => 'rw' ); +has _current_body => ( is => 'rw', trigger => sub { + my ($self, $body) = @_; + $self->_current_body_cobrand($body->get_cobrand_handler); +} ); +has _current_body_cobrand => ( is => 'rw' ); has _current_open311 => ( is => 'rw' ); has _current_service => ( is => 'rw' ); -has _current_body_cobrand => ( is => 'ro', lazy => 1, default => sub { - return shift->_current_body->get_cobrand_handler; -} ); sub process_bodies { my $self = shift; @@ -309,14 +310,26 @@ sub _normalize_service_name { sub _set_contact_group { my ($self, $contact) = @_; - if ($self->_current_body_cobrand && $self->_current_body_cobrand->call_hook('enable_category_groups')) { - if (my $group = $self->_current_service->{group}) { - $contact->set_extra_metadata(group => $group); - $contact->update; + my $groups_enabled = $self->_current_body_cobrand && $self->_current_body_cobrand->call_hook('enable_category_groups'); + my $old_group = $contact->get_extra_metadata('group') || ''; + my $new_group = $groups_enabled ? $self->_current_service->{group} || '' : ''; + + if ($old_group ne $new_group) { + if ($new_group) { + $contact->set_extra_metadata(group => $new_group); + $contact->update({ + editor => $0, + whenedited => \'current_timestamp', + note => 'group updated automatically by script', + }); + } else { + $contact->unset_extra_metadata('group'); + $contact->update({ + editor => $0, + whenedited => \'current_timestamp', + note => 'group removed automatically by script', + }); } - } else { - $contact->unset_extra_metadata('group'); - $contact->update; } } |