]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CircNotify.pm
LP#1661688: Add a link and other tweaks to alternate hold pickup feature
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Circ / CircNotify.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2016  Equinox Software, Inc.
3 # Mike Rylander <mrylander@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
15
16
17 package OpenILS::Application::Circ::CircNotify;
18 use base qw/OpenILS::Application/;
19 use strict; use warnings;
20 use OpenSRF::EX qw(:try);
21 use vars q/$AUTOLOAD/;
22 use OpenILS::Event;
23 use OpenSRF::Utils::JSON;
24 use OpenSRF::Utils::Logger qw(:logger);
25 use OpenILS::Utils::CStoreEditor q/:funcs/;
26 use OpenSRF::Utils::SettingsClient;
27 use OpenILS::Application::AppUtils;
28 use OpenILS::Const qw/:const/;
29 use OpenILS::Utils::Fieldmapper;
30 use OpenSRF::MultiSession;
31 use Email::Send;
32 use Data::Dumper;
33 use OpenSRF::EX qw/:try/;
34 my $U = 'OpenILS::Application::AppUtils';
35
36 use open ':utf8';
37
38 sub circ_batch_notify {
39     my ($self, $client, $auth, $patronid, $circlist) = @_;
40     my $e = new_editor(authtoken => $auth);
41     return $e->event unless $e->checkauth;
42     return $e->event unless $e->allowed('STAFF_LOGIN');
43
44     my $circs = $e->search_action_circulation({ id => $circlist });
45     return $e->event if $e->event;
46
47     my $hook = 'circ.checkout.batch_notify';
48     $hook .= '.session' if $self->api_name =~ /session/;
49
50     for my $circ (@$circs) {
51         # WISHLIST: This may become more sophisticated and check "friend" permissions
52         # in the future, at lease in the non-session variant.
53         return OpenILS::Event->new('PATRON_CIRC_MISMATCH') if $circ->usr != $patronid;
54         return $e->event unless $e->allowed('VIEW_CIRCULATIONS', $circ->circ_lib);
55     }
56
57     my %events;
58     my $multi = OpenSRF::MultiSession->new(
59         app                 => 'open-ils.trigger',
60         cap                 => 3,
61         success_handler     => sub {
62             my $self = shift;
63             my $req = shift;
64
65             return unless $req->{response}->[0];
66             my $event = $req->{response}->[0]->content;
67             return unless $event;
68             $event = $e->retrieve_action_trigger_event($event);
69
70             return unless $event;
71             $events{$event->event_def} ||= [];
72             push @{$events{$event->event_def}}, $event->id;
73         },
74     );
75
76     $multi->request(
77         'open-ils.trigger.event.autocreate.ignore_opt_in',
78         $hook => $_ => $e->requestor->ws_ou
79     ) for ( @$circs );
80     $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
81
82     $multi->session_wait(1);
83     $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
84
85     if (!keys(%events)) {
86         return $client->respond_complete;
87     }
88
89     $multi = OpenSRF::MultiSession->new(
90         app                 => 'open-ils.trigger',
91         cap                 => 3,
92         success_handler     => sub {
93             my $self = shift;
94             my $req = shift;
95
96             return unless $req->{response}->[0];
97             $client->respond( $req->{response}->[0]->content );
98         },
99     );
100
101     $multi->request(
102         'open-ils.trigger.event_group.fire',
103         $events{$_}
104     ) for ( sort keys %events );
105
106     $multi->session_wait(1);
107     return $client->respond_complete;
108 }
109 __PACKAGE__->register_method(
110     method   => 'circ_batch_notify',
111     api_name => 'open-ils.circ.checkout.batch_notify',
112     stream   => 1,
113     signature => {
114         desc   => 'Creates and fires grouped events for a set of circulation IDs',
115         params => [
116             { name => 'authtoken', desc => 'Staff auth token',   type => 'string' },
117             { name => 'patronid', desc => 'actor.usr.id of patron which must own the circulations', type => 'number' },
118             { name => 'circlist', desc => 'Arrayref of circulation IDs to bundle into the event group', type => 'array' }
119         ],
120         return => {
121             desc => 'Event on error, stream of zero or more event group firing results '.
122                     'otherwise. See: open-ils.trigger.event_group.fire'
123         }
124     }
125 );
126 __PACKAGE__->register_method(
127     method   => 'circ_batch_notify',
128     api_name => 'open-ils.circ.checkout.batch_notify.session',
129     stream   => 1,
130     signature => {
131         desc   => 'Creates and fires grouped events for a set of circulation IDs.  '.
132                   'For use by session-specific actions such as self-checkout or circ desk checkout.',
133         params => [
134             { name => 'authtoken', desc => 'Staff auth token',   type => 'string' },
135             { name => 'patronid', desc => 'actor.usr.id of patron which must own the circulations', type => 'number' },
136             { name => 'circlist', desc => 'Arrayref of circulation IDs to bundle into the event group', type => 'array' }
137         ],
138         return => {
139             desc => 'Event on error, stream of zero or more event group firing results '.
140                     'otherwise. See: open-ils.trigger.event_group.fire'
141         }
142     }
143 );
144
145 1;