aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2016-10-06 14:10:23 +0100
committerMatthew Somerville <matthew-github@dracos.co.uk>2016-10-11 16:17:32 +0100
commit662b96c2539ec4f61d7ae84b07bb8cebdae02741 (patch)
tree1519e4a912fdc839a0246020d9651ba3561ad026
parente1038dac8f5ba0fe2f20781468b3cbe763b7ed68 (diff)
Adding report to shortlist removes from others.
-rw-r--r--perllib/FixMyStreet/DB/Result/User.pm15
-rw-r--r--perllib/FixMyStreet/DB/ResultSet/UserPlannedReport.pm23
-rw-r--r--t/app/model/user_planned_report.t14
3 files changed, 47 insertions, 5 deletions
diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm
index 7aa7a3272..7ec49b074 100644
--- a/perllib/FixMyStreet/DB/Result/User.pm
+++ b/perllib/FixMyStreet/DB/Result/User.pm
@@ -351,11 +351,18 @@ sub adopt {
# Planned reports / shortlist
-# Override the default auto-created function as we only want one live entry per user
+# Override the default auto-created function as we only want one live entry so
+# we need to delete it anywhere else and return an existing one if present.
around add_to_planned_reports => sub {
my ( $orig, $self ) = ( shift, shift );
my ( $report_col ) = @_;
- my $existing = $self->user_planned_reports->search_rs({ report_id => $report_col->id, removed => undef })->first;
+
+ $self->result_source->schema->resultset("UserPlannedReport")
+ ->active
+ ->for_report($report_col->id)
+ ->search_rs({ user_id => { '!=', $self->id } })
+ ->remove();
+ my $existing = $self->user_planned_reports->active->for_report($report_col->id)->first;
return $existing if $existing;
return $self->$orig(@_);
};
@@ -363,9 +370,7 @@ around add_to_planned_reports => sub {
# Override the default auto-created function as we don't want to ever delete anything
around remove_from_planned_reports => sub {
my ($orig, $self, $report) = @_;
- $self->user_planned_reports
- ->search_rs({ report_id => $report->id, removed => undef })
- ->update({ removed => \'current_timestamp' });
+ $self->user_planned_reports->active->for_report($report->id)->remove();
};
sub active_planned_reports {
diff --git a/perllib/FixMyStreet/DB/ResultSet/UserPlannedReport.pm b/perllib/FixMyStreet/DB/ResultSet/UserPlannedReport.pm
new file mode 100644
index 000000000..7e16e2dd3
--- /dev/null
+++ b/perllib/FixMyStreet/DB/ResultSet/UserPlannedReport.pm
@@ -0,0 +1,23 @@
+package FixMyStreet::DB::ResultSet::UserPlannedReport;
+use base 'DBIx::Class::ResultSet';
+
+use strict;
+use warnings;
+
+sub active {
+ my $rs = shift;
+ $rs->search({ removed => undef });
+}
+
+sub for_report {
+ my $rs = shift;
+ my $problem_id = shift;
+ $rs->search({ report_id => $problem_id });
+}
+
+sub remove {
+ my $rs = shift;
+ $rs->update({ removed => \'current_timestamp' });
+}
+
+1;
diff --git a/t/app/model/user_planned_report.t b/t/app/model/user_planned_report.t
index a9c2ee9b5..95a76615e 100644
--- a/t/app/model/user_planned_report.t
+++ b/t/app/model/user_planned_report.t
@@ -11,6 +11,7 @@ my $mech = FixMyStreet::TestMech->new();
my @problems = $mech->create_problems_for_body(1, 2237, 'Title');
my $problem = $problems[0];
my $user = $problem->user;
+my $user2 = $mech->create_user_ok('other@example.net');
is $user->active_planned_reports, 0;
is $user->planned_reports, 0;
@@ -31,8 +32,21 @@ $user->add_to_planned_reports($problem);
is $user->active_planned_reports, 1;
is $user->planned_reports, 2;
+$user2->add_to_planned_reports($problem);
+is $user->active_planned_reports, 0;
+is $user->planned_reports, 2;
+is $user2->active_planned_reports, 1;
+is $user2->planned_reports, 1;
+
+$user->add_to_planned_reports($problem);
+is $user->active_planned_reports, 1;
+is $user->planned_reports, 3;
+is $user2->active_planned_reports, 0;
+is $user2->planned_reports, 1;
+
done_testing();
END {
$mech->delete_user($user);
+ $mech->delete_user($user2);
}