]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Utils/BadContact.pm
edacc92b8664100c63114340a9fe3ec05abcdecd
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Utils / BadContact.pm
1 package OpenILS::Utils::BadContact;
2
3 use warnings;
4 use strict;
5
6 use OpenILS::Event;
7 use OpenILS::Utils::CStoreEditor;
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Application::AppUtils;
10
11 my $U = "OpenILS::Application::AppUtils";
12
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"
18 };
19
20 sub mark_users_contact_invalid {
21     my (
22         $class, $editor, $contact_type, $howfind,
23         $addl_note, $penalty_ou, $staff_id
24     ) = @_;
25
26     if (not ref $howfind eq "HASH") {
27         return new OpenILS::Event(
28             "BAD_PARAMS", note => "howfind argument must be hash"
29         );
30     }
31
32     if (not exists $PENALTY_NAME_MAP->{$contact_type}) {
33         return new OpenILS::Event(
34             "BAD_PARAMS", note => "contact_type argument invalid"
35         );
36     }
37
38     my $penalty_name = $PENALTY_NAME_MAP->{$contact_type};
39
40     # we can find either user-by-id, or user(s)-by-contact-info
41     my $users;
42
43     if (exists $howfind->{usr}) {
44         # just the specified patron
45
46         $users = $editor->search_actor_user({
47             id => $howfind->{usr}, deleted => "f"
48         }) or return $editor->die_event;
49
50     } elsif (exists $howfind->{$contact_type}) {
51         # all users with matching contact data
52
53         $users = $editor->search_actor_user({
54             $contact_type => $howfind->{$contact_type}, deleted => "f"
55         }) or return $editor->die_event;
56     }
57
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;
61
62     # we'll need this to apply user penalties
63     my $penalty = $editor->search_config_standing_penalty({
64         name => $penalty_name
65     });
66
67     return $editor->die_event unless $penalty and @$penalty;
68     $penalty = $penalty->[0];
69
70     if ($penalty_ou) {
71         $penalty_ou = $U->org_unit_ancestor_at_depth(
72             $penalty_ou, $penalty->org_depth) 
73             if defined $penalty->org_depth;
74
75     } else {
76         # Fallback to using top of org tree if no penalty_ou provided. This
77         # possibly makes sense in most cases anyway.
78
79         my $results = $editor->json_query({
80             "select" => {"aou" => ["id"]},
81             "from" => {"aou" => {}},
82             "where" => {"parent_ou" => undef}
83         }) or return $editor->die_event;
84
85         $penalty_ou = $results->[0]->{"id"};
86     }
87
88     my $last_xact_id_map = {};
89     my $clear_meth = "clear_$contact_type";
90
91     foreach (@$users) {
92         if ($editor->requestor) {
93             next unless $editor->allowed("UPDATE_USER", $_->home_ou);
94         }
95
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);
101
102         my $message = $_->$contact_type;
103         if (defined($addl_note) && $addl_note !~ /^\s*$/) {
104             $message .= ' ' . $addl_note;
105         }
106
107         my ($result) = $U->simplereq('open-ils.actor', 'open-ils.actor.user.penalty.apply',
108             $editor->authtoken,
109             $usr_penalty,
110             { message => $message }
111         );
112
113         # FIXME: this perpetuates a bug; the patron editor UI doesn't handle these error states well
114         if ($result && ref $result eq 'HASH') {
115             $editor->rollback;
116             return $result;
117         }
118
119         $_->$clear_meth;
120         $editor->update_actor_user($_) or return $editor->die_event;
121
122         my $updated = $editor->retrieve_actor_user($editor->data);
123         $last_xact_id_map->{$_->id} = $updated->last_xact_id;
124     }
125
126     $editor->commit or return $editor->die_event;
127
128     return new OpenILS::Event(
129         "SUCCESS", payload => {last_xact_id => $last_xact_id_map}
130     );
131 }
132
133 1;