1 -- Evergreen DB patch 0553.unnest_action_hold_request_permit_test.sql
3 -- Replace usage of custom explode_array() function with native unnest()
7 -- check whether patch can be applied
8 SELECT evergreen.upgrade_deps_block_check('0553', :eg_version);
10 CREATE OR REPLACE FUNCTION action.hold_request_permit_test( pickup_ou INT, request_ou INT, match_item BIGINT, match_user INT, match_requestor INT, retargetting BOOL ) RETURNS SETOF action.matrix_test_result AS $func$
13 user_object actor.usr%ROWTYPE;
14 age_protect_object config.rule_age_hold_protect%ROWTYPE;
15 standing_penalty config.standing_penalty%ROWTYPE;
16 transit_range_ou_type actor.org_unit_type%ROWTYPE;
17 transit_source actor.org_unit%ROWTYPE;
18 item_object asset.copy%ROWTYPE;
19 item_cn_object asset.call_number%ROWTYPE;
20 ou_skip actor.org_unit_setting%ROWTYPE;
21 result action.matrix_test_result;
22 hold_test config.hold_matrix_matchpoint%ROWTYPE;
24 hold_transit_prox INT;
25 frozen_hold_count INT;
26 context_org_list INT[];
29 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
30 SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( pickup_ou );
32 result.success := TRUE;
34 -- Fail if we couldn't find a user
35 IF user_object.id IS NULL THEN
36 result.fail_part := 'no_user';
37 result.success := FALSE;
43 SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
45 -- Fail if we couldn't find a copy
46 IF item_object.id IS NULL THEN
47 result.fail_part := 'no_item';
48 result.success := FALSE;
54 SELECT INTO matchpoint_id action.find_hold_matrix_matchpoint(pickup_ou, request_ou, match_item, match_user, match_requestor);
55 result.matchpoint := matchpoint_id;
57 SELECT INTO ou_skip * FROM actor.org_unit_setting WHERE name = 'circ.holds.target_skip_me' AND org_unit = item_object.circ_lib;
59 -- Fail if the circ_lib for the item has circ.holds.target_skip_me set to true
60 IF ou_skip.id IS NOT NULL AND ou_skip.value = 'true' THEN
61 result.fail_part := 'circ.holds.target_skip_me';
62 result.success := FALSE;
68 -- Fail if user is barred
69 IF user_object.barred IS TRUE THEN
70 result.fail_part := 'actor.usr.barred';
71 result.success := FALSE;
77 -- Fail if we couldn't find any matchpoint (requires a default)
78 IF matchpoint_id IS NULL THEN
79 result.fail_part := 'no_matchpoint';
80 result.success := FALSE;
86 SELECT INTO hold_test * FROM config.hold_matrix_matchpoint WHERE id = matchpoint_id;
88 IF hold_test.holdable IS FALSE THEN
89 result.fail_part := 'config.hold_matrix_test.holdable';
90 result.success := FALSE;
95 IF hold_test.transit_range IS NOT NULL THEN
96 SELECT INTO transit_range_ou_type * FROM actor.org_unit_type WHERE id = hold_test.transit_range;
97 IF hold_test.distance_is_from_owner THEN
98 SELECT INTO transit_source ou.* FROM actor.org_unit ou JOIN asset.call_number cn ON (cn.owning_lib = ou.id) WHERE cn.id = item_object.call_number;
100 SELECT INTO transit_source * FROM actor.org_unit WHERE id = item_object.circ_lib;
103 PERFORM * FROM actor.org_unit_descendants( transit_source.id, transit_range_ou_type.depth ) WHERE id = pickup_ou;
106 result.fail_part := 'transit_range';
107 result.success := FALSE;
113 FOR standing_penalty IN
114 SELECT DISTINCT csp.*
115 FROM actor.usr_standing_penalty usp
116 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
117 WHERE usr = match_user
118 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
119 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
120 AND csp.block_list LIKE '%HOLD%' LOOP
122 result.fail_part := standing_penalty.name;
123 result.success := FALSE;
128 IF hold_test.stop_blocked_user IS TRUE THEN
129 FOR standing_penalty IN
130 SELECT DISTINCT csp.*
131 FROM actor.usr_standing_penalty usp
132 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
133 WHERE usr = match_user
134 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
135 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
136 AND csp.block_list LIKE '%CIRC%' LOOP
138 result.fail_part := standing_penalty.name;
139 result.success := FALSE;
145 IF hold_test.max_holds IS NOT NULL AND NOT retargetting THEN
146 SELECT INTO hold_count COUNT(*)
147 FROM action.hold_request
148 WHERE usr = match_user
149 AND fulfillment_time IS NULL
150 AND cancel_time IS NULL
151 AND CASE WHEN hold_test.include_frozen_holds THEN TRUE ELSE frozen IS FALSE END;
153 IF hold_count >= hold_test.max_holds THEN
154 result.fail_part := 'config.hold_matrix_test.max_holds';
155 result.success := FALSE;
161 IF item_object.age_protect IS NOT NULL THEN
162 SELECT INTO age_protect_object * FROM config.rule_age_hold_protect WHERE id = item_object.age_protect;
164 IF item_object.create_date + age_protect_object.age > NOW() THEN
165 IF hold_test.distance_is_from_owner THEN
166 SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
167 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_cn_object.owning_lib AND to_org = pickup_ou;
169 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_object.circ_lib AND to_org = pickup_ou;
172 IF hold_transit_prox > age_protect_object.prox THEN
173 result.fail_part := 'config.rule_age_hold_protect.prox';
174 result.success := FALSE;
187 $func$ LANGUAGE plpgsql;