1 package OpenILS::Utils::BadContact;
7 use OpenILS::Utils::CStoreEditor;
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Application::AppUtils;
11 my $U = "OpenILS::Application::AppUtils";
13 our $PENALTY_NAME_MAP = {
14 email => "INVALID_PATRON_EMAIL_ADDRESS",
15 day_phone => "INVALID_PATRON_DAY_PHONE",
16 evening_phone => "INVALID_PATRON_EVENING_PHONE",
17 other_phone => "INVALID_PATRON_OTHER_PHONE"
20 sub mark_users_contact_invalid {
22 $class, $editor, $contact_type, $howfind,
23 $addl_note, $penalty_ou, $staff_id
26 if (not ref $howfind eq "HASH") {
27 return new OpenILS::Event(
28 "BAD_PARAMS", note => "howfind argument must be hash"
32 if (not exists $PENALTY_NAME_MAP->{$contact_type}) {
33 return new OpenILS::Event(
34 "BAD_PARAMS", note => "contact_type argument invalid"
38 my $penalty_name = $PENALTY_NAME_MAP->{$contact_type};
40 # we can find either user-by-id, or user(s)-by-contact-info
43 if (exists $howfind->{usr}) {
44 # just the specified patron
46 $users = $editor->search_actor_user({
47 id => $howfind->{usr}, deleted => "f"
48 }) or return $editor->die_event;
50 } elsif (exists $howfind->{$contact_type}) {
51 # all users with matching contact data
53 $users = $editor->search_actor_user({
54 $contact_type => $howfind->{$contact_type}, deleted => "f"
55 }) or return $editor->die_event;
58 # waste no more time if no users collected
59 return $editor->die_event(new OpenILS::Event("ACTOR_USER_NOT_FOUND"))
60 unless $users and @$users;
62 # we'll need this to apply user penalties
63 my $penalty = $editor->search_config_standing_penalty({
67 return $editor->die_event unless $penalty and @$penalty;
68 $penalty = $penalty->[0];
71 $penalty_ou = $U->org_unit_ancestor_at_depth(
72 $penalty_ou, $penalty->org_depth)
73 if defined $penalty->org_depth;
76 # Fallback to using top of org tree if no penalty_ou provided. This
77 # possibly makes sense in most cases anyway.
79 my $results = $editor->json_query({
80 "select" => {"aou" => ["id"]},
81 "from" => {"aou" => {}},
82 "where" => {"parent_ou" => undef}
83 }) or return $editor->die_event;
85 $penalty_ou = $results->[0]->{"id"};
88 my $last_xact_id_map = {};
89 my $clear_meth = "clear_$contact_type";
92 if ($editor->requestor) {
93 next unless $editor->allowed("UPDATE_USER", $_->home_ou);
96 my $usr_penalty = new Fieldmapper::actor::user_standing_penalty;
97 $usr_penalty->usr($_->id);
98 $usr_penalty->org_unit($penalty_ou);
99 $usr_penalty->standing_penalty($penalty->id);
100 $usr_penalty->staff($staff_id);
102 my $message = $_->$contact_type;
103 if (defined($addl_note) && $addl_note !~ /^\s*$/) {
104 $message .= ' ' . $addl_note;
107 my ($result) = $U->simplereq('open-ils.actor', 'open-ils.actor.user.penalty.apply',
110 { message => $message }
113 # FIXME: this perpetuates a bug; the patron editor UI doesn't handle these error states well
114 if ($result && ref $result eq 'HASH') {
120 $editor->update_actor_user($_) or return $editor->die_event;
122 my $updated = $editor->retrieve_actor_user($editor->data);
123 $last_xact_id_map->{$_->id} = $updated->last_xact_id;
126 $editor->commit or return $editor->die_event;
128 return new OpenILS::Event(
129 "SUCCESS", payload => {last_xact_id => $last_xact_id_map}