From 4dcde449aeaf4af62d9823df7d635f097d004236 Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 13 Oct 2010 20:16:57 +0000 Subject: [PATCH] clean up duplicate system-controlled penalties during penalty calculation; perform penalty trigger event creation after the standalone commit occurs. todo, handle non-standalone post-commit penalty trigger event creation git-svn-id: svn://svn.open-ils.org/ILS/trunk@18322 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../src/perlmods/OpenILS/Utils/Penalty.pm | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/Open-ILS/src/perlmods/OpenILS/Utils/Penalty.pm b/Open-ILS/src/perlmods/OpenILS/Utils/Penalty.pm index 4edc51cfbb..f259345da4 100644 --- a/Open-ILS/src/perlmods/OpenILS/Utils/Penalty.pm +++ b/Open-ILS/src/perlmods/OpenILS/Utils/Penalty.pm @@ -26,41 +26,53 @@ sub calculate_penalties { my $penalties = $e->json_query({from => ['actor.calculate_system_penalties',$user_id, $context_org]}); my $user = $e->retrieve_actor_user( $user_id ); - my $ses = OpenSRF::AppSession->create('open-ils.trigger') if (@$penalties); + my @existing_penalties = grep { defined $_->{id} } @$penalties; + my @wanted_penalties = grep { !defined $_->{id} } @$penalties; + my @trigger_events; my %csp; - for my $pen_obj (@$penalties) { - - next if grep { # leave duplicate penalties in place - $_->{org_unit} == $pen_obj->{org_unit} and - $_->{standing_penalty} == $pen_obj->{standing_penalty} and - ($_->{id} || '') ne ($pen_obj->{id} || '') } @$penalties; + for my $pen_obj (@wanted_penalties) { my $pen = Fieldmapper::actor::user_standing_penalty->new; $pen->$_($pen_obj->{$_}) for keys %$pen_obj; - if(defined $pen_obj->{id}) { - $e->delete_actor_user_standing_penalty($pen) or return $e->die_event; + # let's see if this penalty is accounted for already + my ($existing) = grep { + $_->{org_unit} == $pen_obj->{org_unit} and + $_->{standing_penalty} == $pen_obj->{standing_penalty} + } @existing_penalties; + + if($existing) { + # we have one of these already. Leave it be, but remove it from the + # existing set so it's not deleted in the subsequent loop + @existing_penalties = grep { $_->{id} ne $existing->{id} } @existing_penalties; } else { + + # this is a new penalty $e->create_actor_user_standing_penalty($pen) or return $e->die_event; - my $csp_obj = $csp{$pen->standing_penalty} || + my $csp_obj = $csp{$pen->standing_penalty} || $e->retrieve_config_standing_penalty( $pen->standing_penalty ); # cache for later $csp{$pen->standing_penalty} = $csp_obj; - $ses->request( - 'open-ils.trigger.event.autocreate', - 'penalty.' . $csp_obj->name, - $pen, - $pen->org_unit - ); + push(@trigger_events, ['penalty.' . $csp_obj->name, $pen, $pen->org_unit]); } } + # at this point, any penalties remaining in the existing + # penalty set are unaccounted for and should be removed + for my $pen_obj (@existing_penalties) { + my $pen = Fieldmapper::actor::user_standing_penalty->new; + $pen->$_($pen_obj->{$_}) for keys %$pen_obj; + $e->delete_actor_user_standing_penalty($pen) or return $e->die_event; + } + $e->commit if $commit; + + $U->create_events_for_hook($$_[0], $$_[1], $$_[2]) for @trigger_events; return undef; } -- 2.43.2