a7ecb3eedb09f9c5212b5d79259745dbadbf924b
[evergreen-equinox.git] / docs / modules / admin / pages / actiontriggers.adoc
1 = Notifications / Action Triggers =
2 :toc:
3
4
5 == Introduction ==
6
7 indexterm:[action triggers, event definitions, notifications]
8
9 Action Triggers give administrators the ability to set up actions for
10 specific events. They are useful for notification events such as hold notifications.
11
12 To access the Action Triggers module, select *Administration* -> *Local Administration* ->  *Notifications / Action triggers*.
13
14 [NOTE]
15 ==========
16 You must have Local Administrator permissions to access the Action Triggers module.
17 ==========
18
19 You will notice four tabs on this page: <<event_definitions, Event Definitions>>, <<hooks, Hooks>>, <<reactors, Reactors>> and <<validators, Validators>>.
20
21
22 [#event_definitions]
23
24 == Event Definitions ==
25
26 Event Definitions is the main tab and contains the key fields when working with action triggers. These fields include:
27
28 === Table 1: Action Trigger Event Definitions ===
29
30
31 |==============================================
32 |*Field*                         |*Description*
33 | Owning Library                 |The shortname of the library for which the action / trigger / hook is defined.
34 | Name                           |The name of the trigger event, that links to a trigger event environment containing a set of fields that will be returned to the <<validators, Validators>> and/or <<reactors, Reactors>> for processing.
35 | <<hooks, Hook>>                |The name of the trigger for the trigger event. The underlying action_trigger.hook table defines the Fieldmapper class in the core_type column off of which the rest of the field definitions ``hang''.
36 | Enabled                        |Sets the given trigger as enabled or disabled. This must be set to enabled for the Action trigger to run.
37 | Processing Delay               |Defines how long after a given trigger / hook event has occurred before the associated action (``Reactor'') will be taken.
38 | Processing Delay Context Field |Defines the field associated with the event on which the processing delay is calculated. For example, the processing delay context field on the hold.capture hook (which has a core_type of ahr) is _capture_time_.
39 | Processing Group Context Field |Used to batch actions based on its associated group.
40 | <<reactors, Reactor>>          |Links the action trigger to the Reactor.
41 | <<validators, Validator>>      |The subroutines receive the trigger environment as an argument (see the linked Name for the environment definition) and returns either _1_ if the validator is _true_ or _0_ if the validator returns _false_.
42 | Event Repeatability Delay      |Allows events to be repeated after this delay interval.
43 | Failure Cleanup                |After an event is reacted to and if there is a failure a cleanup module can be run to clean up after the event.
44 | Granularity                    |Used to group events by how often they should be run. Options are Hourly, Daily, Weekly, Monthly, Yearly, but you may also create new values.
45 | Max Event Validity Delay       |Allows events to have a range of time that they are valid.  This value works with the *Processing Delay* to define a time range.
46 | Message Library Path           |Defines the org_unit object for a Patron Message Center message.
47 | Message Template               |A Template Toolkit template that can be used to generate output for a Patron Message Center message.  The output may or may not be used by the reactor or another external process.
48 | Message Title                  |The title that will display on a Patron Message Center message.
49 | Message User Path              |Defines the user object for a Patron Message Center message.
50 | Opt-In Settings Type           |Choose which User Setting Type will decide if this event will be valid for a certain user.  Use this to allow users to Opt-In or Opt-Out of certain events.
51 | Opt-In User Field              |Set to the name of the field in the selected hook's core type that will link the core type to the actor.usr table. 
52 | Success Cleanup                |After an event is reacted to successfully a cleanup module can be run to clean up after the event.
53 | Template                       |A Template Toolkit template that can be used to generate output.  The output may or may not be used by the reactor or another external process.
54 |==============================================
55
56
57 == Creating Action Triggers ==
58
59 . From the top menu, select *Administration* -> *Local Administration* ->  *Notifications / Action triggers*.
60 . Click on the _New_ button.
61 +
62 image::media/new_event_def.png[New Event Definition]
63 +
64 . Select an _Owning Library_.
65 . Create a unique _Name_ for your new action trigger.
66 . Select the _Hook_.
67 . Check the _Enabled_ check box.
68 . Set the _Processing Delay_ in the appropriate format. E.g. _7 days_ to run 7 days from the trigger event or _00:01:00_ to run 1 hour after the _Processing Delay Context Field_.
69 . Set the _Processing Delay Context Field_ and _Processing Group Context Field_.
70 . Select the _Reactor_ and _Validator_.
71 . Set the _Event Repeatability Delay_.
72 . Select the _Failure Cleanup_ and _Granularity_.
73 . Set the _Max Event Validity Delay_.
74 +
75 image::media/event_def_details.png[Event Definition Details]
76 +
77 . If you wish to send a User Message through the Message Center, set a _Message Library Path_.  Enter text in the _Message Template_.  Enter a title for this message in _Message Title_, and set a value in _Message User Path_.
78 . Select the _Opt-In Setting Type_.
79 . Set the _Opt-In User Field_.
80 . Select the _Success Cleanup_.
81 +
82 image::media/event_def_details_2.png[Event Definition Details]
83 +
84 . Enter text in the _Template_ text box if required. These are for email messages. Here is a sample template for sending 90 day overdue notices:
85
86
87   [%- USE date -%]
88   [%- user = target.0.usr -%]
89   To: [%- params.recipient_email || user.email %]
90   From: [%- helpers.get_org_setting(target.home_ou.id, 'org.bounced_emails') || lib.email || params.sender_email || default_sender %]
91   Subject: Overdue Items Marked Lost
92   Auto-Submitted: auto-generated
93
94   Dear [% user.family_name %], [% user.first_given_name %]
95   The following items are 90 days overdue and have been marked LOST.
96   [%- params.recipient_email || user.email %][%- params.sender_email || default_sender %]
97   [% FOR circ IN target %]
98     Title: [% circ.target_copy.call_number.record.simple_record.title %]
99     Barcode: [% circ.target_copy.barcode %]
100     Due: [% date.format(helpers.format_date(circ.due_date), '%Y-%m-%d') %]
101     Item Cost: [% helpers.get_copy_price(circ.target_copy) %]
102     Total Owed For Transaction: [% circ.billable_transaction.summary.total_owed %]
103     Library: [% circ.circ_lib.name %]
104   [% END %]
105
106   [% FOR circ IN target %]
107     Title: [% circ.target_copy.call_number.record.simple_record.title %]
108     Barcode: [% circ.target_copy.barcode %]
109     Due: [% date.format(helpers.format_date(circ.due_date), '%Y-%m-%d') %]
110     Item Cost: [% helpers.get_copy_price(circ.target_copy) %]
111     Total Owed For Transaction: [% circ.billable_transaction.summary.total_owed %]
112     Library: [% circ.circ_lib.name %]
113   [% END %]
114
115 . Once you are satisfied with your new event trigger, click the _Save_ button located at the bottom of the form.
116
117
118 [TIP]
119 =========
120 A quick and easy way to create new action triggers is to clone an existing action trigger.
121 =========
122
123 === Cloning Existing Action Triggers ===
124
125 . Check the check box next to the action trigger you wish to clone.
126 . Click _Clone Selected_ on the top left of the page.
127 . An editing window will open. Notice that the fields will be populated with content from the cloned action trigger. Edit as necessary and give the new action trigger a unique Name.
128 . Click _Save_.
129
130 === Editing Action Triggers ===
131
132 . Double-click on the action trigger you wish to edit.
133 . The edit screen will appear. When you are finished editing, click _Save_ at the bottom of the form. Or click _Cancel_ to exit the screen without saving.
134
135 [NOTE]
136 ============
137 Before deleting an action trigger, you should consider disabling it through the editing form. This way you can keep it for future use or cloning.
138 ============
139
140 === Deleting Action Triggers ===
141
142 . Check the check box next to the action trigger you wish to delete
143 . Click _Delete Selected_ on the top-right of the page.
144
145 === Testing Action Triggers ===
146
147 . Go to the list of action triggers.
148 . Click on the blue link text for the action trigger you'd like to test.
149 +
150 image::media/test_event_def.png[Blue Link Text]
151 +
152 . Go to the Test tab.
153 . If there is a test available, fill in the required information.
154 . View the output of the test.
155
156 image::media/test_event_def_output.png[Test Output]
157
158 WARNING: If you are testing an email or SMS notification, use a test account and email as an example. Using the Test feature will actually result in the notification being sent if configured correctly.  Similarly, use a test item or barcode when testing a circulation-based event like Mark Lost since the test will mark the item as lost.
159
160 [#hooks]
161
162 === Hooks ===
163
164 Hooks define the Fieldmapper class in the core_type column off of which the rest of the field definitions ``hang''.
165
166
167 ==== Table 2. Hooks ====
168
169
170 |=======================
171 | *Field*        | *Description*
172 | Hook Key       | A unique name given to the hook.
173 | Core Type      | Used to link the action trigger to the IDL class in fm_IDL.xml
174 | Description    | Text to describe the purpose of the hook.
175 | Passive        | Indicates whether or not an event is created by direct user action or is circumstantial.
176 |=======================
177
178 You may also create, edit and delete Hooks but the Core Type must refer to an IDL class in the fm_IDL.xml file.
179
180
181 [#reactors]
182
183 === Reactors ===
184
185 Reactors link the trigger definition to the action to be carried out.
186
187 ==== Table 3. Action Trigger Reactors ====
188
189
190 |=======================
191 | Field        | Description
192 | Module Name  | The name of the Module to run if the action trigger is validated. It must be defined as a subroutine in `/openils/lib/perl5/OpenILS/Application/Trigger/Reactor.pm` or as a module in `/openils/lib/perl5/OpenILS/Application/Trigger/Reactor/*.pm`.
193 | Description  | Description of the Action to be carried out.
194 |=======================
195
196 You may also create, edit and delete Reactors. Just remember that there must be an associated subroutine or module in the Reactor Perl module.
197
198 ==== CallHTTP Reactor ====
199
200 This Action/Trigger reactor module allows an Evergreen administrator to
201 create event defintions that use HTTP (or HTTPS) to contact external services
202 and let them know that something has happened in Evergreen.
203
204 For instance, a discovery layer can be informed when a bib record is updated
205 or when a user's barcode changes.
206
207 ===== CallHTTP Reactor Template Syntax =====
208
209 The new reactor module uses a template to define its behavior.  While the
210 template is processed by Template Toolkit, as with any A/T templates, its
211 output format is new to Evergreen.
212
213 The template should output data that can be parsed by the Config::General Perl
214 module.  See: https://metacpan.org/pod/Config::General
215
216 Top level settings should include the HTTP *method* and the *url*.
217
218 A block called *Headers* can be used to supply arbitrary HTTP headers.
219
220 A block called *Parameters* can be used to append CGI parameters to the URL,
221 most useful for GET form submission.  Repeated parameters are allowed.  If
222 this block is used, the URL should /not/ contain any parameters, use one or
223 the other.
224
225 A HEREDOC called *content* can be used with POST or PUT to send an arbitrary block
226 of content to the remote server.
227
228 If the requested URL requires Basic or Digest authentication, the template can
229 include top level configuration parameters to supply a *user*, *password*, *realm*,
230 and hostname:port *location*.
231
232 A default user agent string of "EvergreenReactor/1.0" is used when sending requests.
233 This can be overridden using the top level *agent* setting.
234
235 Here is an example template that could be used by a definition attached to the
236 *bib.edit* hook:
237
238 [source,conf]
239 ----
240 method   post # Valid values are post, get, put, delete, head
241 url      https://example.com/api/incoming-update
242 agent    MySpecialAgent/0.1
243
244 user     updater
245 password uPd4t3StufF
246 realm    "Secret area"
247 location example.com:443
248
249 <Headers>
250   Accept-Language en
251 </Headers>
252
253 <Parameters>
254   type bib
255   id   [% target.id %]
256 </Parameters>
257
258 content <<MARC
259 [% target.marc %]
260 MARC
261 ----
262
263
264
265
266 [#validators]
267
268 === Validators ===
269
270 Validators set the validation test to be preformed to determine whether the action trigger is executed.
271
272 ==== Table 4. Action Trigger Validators ====
273
274
275 |=======================
276 | Field         | Description
277 | Module Name   | The name of the subroutine in `/openils/lib/perl5/OpenILS/Application/Trigger/Reactor.pm` to validate the action trigger.
278 | Description   | Description of validation test to run.
279 |=======================
280
281 You may also create, edit and delete Validators. Just remember that their must be an associated subroutine in the Reactor.pm Perl module.
282
283 [#processing_action_triggers]
284 == Processing Action Triggers ==
285
286 To run action triggers, an Evergreen administrator will need to run the trigger processing script. This should be set up as a cron job to run periodically. To run the script, use this command:
287
288 ----
289 /openils/bin/action_trigger_runner.pl --process-hooks --run-pending
290 ----
291
292 You have several options when running the script:
293
294 * --run-pending: Run pending events to send emails or take other actions as
295 specified by the reactor in the event definition.
296
297 * --process-hooks: Create hook events
298
299 * --osrf-config=[config_file]: OpenSRF core config file. Defaults to:
300 /openils/conf/opensrf_core.xml
301
302 * --custom-filters=[filter_file]: File containing a JSON Object which describes any hooks
303 that should use a user-defined filter to find their target objects. Defaults to:
304 /openils/conf/action_trigger_filters.json
305
306 * --max-sleep=[seconds]: When in process-hooks mode, wait up to [seconds] for the lock file to go
307 away. Defaults to 3600 (1 hour).
308
309 * --hooks=hook1[,hook2,hook3,...]: Define which hooks to create events for. If none are defined, it
310 defaults to the list of hooks defined in the --custom-filters option.
311 Requires --process-hooks.
312
313 * --granularity=[label]: Limit creating events and running pending events to
314 those only with [label] granularity setting.
315
316 * --debug-stdout: Print server responses to STDOUT (as JSON) for debugging.
317
318 * --lock-file=[file_name]: Sets the lock file for the process.
319
320 * --verbose: Show details of script processing.
321
322 * --help: Show help information.
323
324 Examples:
325
326 * Run all pending events that have no granularity set. This is what you tell
327 CRON to run at regular intervals.
328 +
329 ----
330 perl action_trigger_runner.pl --run-pending
331 ----
332
333 * Batch create all "checkout.due" events
334 +
335 ----
336 perl action_trigger_runner.pl --hooks=checkout.due --process-hooks
337 ----
338
339 * Batch create all events for a specific granularity and to send notices for all
340 pending events with that same granularity.
341 +
342 ----
343 perl action_trigger_runner.pl --run-pending --granularity=Hourly --process-hooks
344 ----
345
346 [[configure_opac_email_print]]
347 == Configure OPAC Record Email and Print ==
348
349 The information displayed in the printout and email is defined and generated by two new Notification/Action Triggers named “biblio.record_entry.print” and biblio.record_entry.email”.  Notification/Action Triggers are found under *Admin -> Local Administration.*
350
351 The printout and email will include the following bibliographic information by default:
352
353 * Bibliographic Record ID 
354 * Title statement 
355 * Author 
356 * Item Type
357 * Publisher 
358 * Publication date 
359 * ISBN 
360 * ISSN
361 * UPC
362
363 If *Full display* is selected by the OPAC user, the following holdings information is included in the printout or email, if relevant:
364
365 * Circulating Library
366 * Item Location
367 * Call Number (including prefix and suffix)
368 * Monograph Parts
369 * Item Status
370 * Item Barcode
371
372 The bibliographic and item information included in the printout or email can be configured by modifying the respective Action Trigger templates.  
373
374 For the Full display, the maximum number of copies to be displayed per record can also be configured in the Action Trigger Event Parameter field.  To set the maximum number of copies for display, go to *Admin -> Local Administration -> Notifications / Action Triggers* and find the print or email notification/action trigger to modify.  
375
376 . Select the hyperlinked name of the Notification/Action Trigger to view the Trigger Event Environment.
377 +
378 image::media/ope_event_defs.png[Email and Print Event Definitions]
379 +
380 . Select the *Event Parameters* tab and in the upper right-hand corner, select *New* to create a new Trigger Event Parameter.
381 . In the _Parameter Name_ field enter holdings_limit.  
382 . In the _Parameter Value_ field enter the maximum number of copies to be displayed per record.
383 . Select *Save* to save your changes.
384 +
385 image::media/ope_event_params.png[Email and Print Event Parameters]
386 +