]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Utils/BadContact.pm
Revert "LP#1635737 Use new OpenSRF interval_to_seconds() context"
[working/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         $usr_penalty->note($_->$contact_type);
102
103         $editor->create_actor_user_standing_penalty($usr_penalty) or
104             return $editor->die_event;
105
106         $_->$clear_meth;
107         $editor->update_actor_user($_) or return $editor->die_event;
108
109         my $updated = $editor->retrieve_actor_user($editor->data);
110         $last_xact_id_map->{$_->id} = $updated->last_xact_id;
111     }
112
113     $editor->commit or return $editor->die_event;
114
115     return new OpenILS::Event(
116         "SUCCESS", payload => {last_xact_id => $last_xact_id_map}
117     );
118 }
119
120 1;