diff options
author | pezholio <pezholio@gmail.com> | 2017-05-05 15:20:31 +0100 |
---|---|---|
committer | pezholio <pezholio@gmail.com> | 2017-05-05 15:24:12 +0100 |
commit | 77ff26ef363a54f0c550649d2a02bad0f449b926 (patch) | |
tree | 4092f1d31577fdef3cc303bc6836042129466b91 | |
parent | 1c07eca3d6e8cea5d70c043de02d30bd7cdc5630 (diff) |
Move alert signup logic out of controller
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/Update.pm | 23 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/User.pm | 21 | ||||
-rw-r--r-- | t/app/model/user.t | 29 |
3 files changed, 58 insertions, 15 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm index 4c2d92d5e..6e94547f3 100644 --- a/perllib/FixMyStreet/App/Controller/Report/Update.pm +++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm @@ -548,24 +548,17 @@ sub signup_for_alerts : Private { my ( $self, $c ) = @_; my $update = $c->stash->{update}; + my $user = $update->user; + my $problem_id = $update->problem_id; + if ( $c->stash->{add_alert} ) { my $options = { - user => $update->user, - alert_type => 'new_updates', - parameter => $update->problem_id, + cobrand => $update->cobrand, + cobrand_data => $update->cobrand_data, + lang => $update->lang, }; - my $alert = $c->model('DB::Alert')->find($options); - unless ($alert) { - $alert = $c->model('DB::Alert')->create({ - %$options, - cobrand => $update->cobrand, - cobrand_data => $update->cobrand_data, - lang => $update->lang, - }); - } - $alert->confirm(); - - } elsif ( my $alert = $update->user->alert_for_problem($update->problem_id) ) { + $user->create_alert($problem_id, $options); + } elsif ( my $alert = $user->alert_for_problem($problem_id) ) { $alert->disable(); } diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm index cf6de9a76..8385d2eea 100644 --- a/perllib/FixMyStreet/DB/Result/User.pm +++ b/perllib/FixMyStreet/DB/Result/User.pm @@ -204,6 +204,27 @@ sub alert_for_problem { } ); } +=head2 create_alert + +Sign a user up to receive alerts on a given problem + +=cut + +sub create_alert { + my ( $self, $id, $options ) = @_; + my $alert = $self->alert_for_problem($id); + + unless ( $alert ) { + $alert = $self->alerts->create({ + %$options, + alert_type => 'new_updates', + parameter => $id, + }); + } + + $alert->confirm(); +} + sub body { my $self = shift; return '' unless $self->from_body; diff --git a/t/app/model/user.t b/t/app/model/user.t index d4115d586..dd8b8ca5d 100644 --- a/t/app/model/user.t +++ b/t/app/model/user.t @@ -30,6 +30,35 @@ is $problem->user->latest_anonymity, 0, "User's last update was not anonyous"; create_update($problem, anonymous => 't'); is $problem->user->latest_anonymity, 1, "User's last update was anonymous"; +subtest "Sign user up for alerts" => sub { + my $user = $problem->user; + + my $alert_exists = $user->alert_for_problem( $problem->id ); + is !defined( $alert_exists ), 1, "No current alerts exist"; + + my $options = { + cobrand => 'default', + lang => 'en-gb', + }; + $user->create_alert($problem->id, $options); + my $alert = $user->alert_for_problem( $problem->id ); + + is defined( $alert ), 1, "User is signed up for alerts"; + is $alert->confirmed, 1, "Alert is confirmed"; + + $alert->delete(); + + $user->alerts->create({ + alert_type => 'new_updates', + parameter => $problem->id, + }); + + $user->create_alert($problem->id, $options); + + my $new_alert = $user->alert_for_problem( $problem->id ); + is $alert->confirmed, 1, "Already created alert is confirmed"; +}; + FixMyStreet::override_config { ALLOWED_COBRANDS => [ { fixmystreet => '.' } ], MAPIT_URL => 'http://mapit.uk/', |