]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_actor_usr_delete_function.sql
LP#1812733 - Repair actor.usr_delete function.
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / XXXX.schema.fix_actor_usr_delete_function.sql
1
2 BEGIN;
3
4 SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
5
6 CREATE OR REPLACE FUNCTION actor.usr_delete(
7         src_usr  IN INTEGER,
8         dest_usr IN INTEGER
9 ) RETURNS VOID AS $$
10 DECLARE
11         old_profile actor.usr.profile%type;
12         old_home_ou actor.usr.home_ou%type;
13         new_profile actor.usr.profile%type;
14         new_home_ou actor.usr.home_ou%type;
15         new_name    text;
16         new_dob     actor.usr.dob%type;
17 BEGIN
18         SELECT
19                 id || '-PURGED-' || now(),
20                 profile,
21                 home_ou,
22                 dob
23         INTO
24                 new_name,
25                 old_profile,
26                 old_home_ou,
27                 new_dob
28         FROM
29                 actor.usr
30         WHERE
31                 id = src_usr;
32         --
33         -- Quit if no such user
34         --
35         IF old_profile IS NULL THEN
36                 RETURN;
37         END IF;
38         --
39         perform actor.usr_purge_data( src_usr, dest_usr );
40         --
41         -- Find the root grp_tree and the root org_unit.  This would be simpler if we 
42         -- could assume that there is only one root.  Theoretically, someday, maybe,
43         -- there could be multiple roots, so we take extra trouble to get the right ones.
44         --
45         SELECT
46                 id
47         INTO
48                 new_profile
49         FROM
50                 permission.grp_ancestors( old_profile )
51         WHERE
52                 parent is null;
53         --
54         SELECT
55                 id
56         INTO
57                 new_home_ou
58         FROM
59                 actor.org_unit_ancestors( old_home_ou )
60         WHERE
61                 parent_ou is null;
62         --
63         -- Truncate date of birth
64         --
65         IF new_dob IS NOT NULL THEN
66                 new_dob := date_trunc( 'year', new_dob );
67         END IF;
68         --
69         UPDATE
70                 actor.usr
71                 SET
72                         card = NULL,
73                         profile = new_profile,
74                         usrname = new_name,
75                         email = NULL,
76                         passwd = random()::text,
77                         standing = DEFAULT,
78                         ident_type = 
79                         (
80                                 SELECT MIN( id )
81                                 FROM config.identification_type
82                         ),
83                         ident_value = NULL,
84                         ident_type2 = NULL,
85                         ident_value2 = NULL,
86                         net_access_level = DEFAULT,
87                         photo_url = NULL,
88                         prefix = NULL,
89                         first_given_name = new_name,
90                         guardian = NULL,
91                         family_name = new_name,
92                         suffix = NULL,
93                         alias = NULL,
94                         day_phone = NULL,
95                         evening_phone = NULL,
96                         other_phone = NULL,
97                         mailing_address = NULL,
98                         billing_address = NULL,
99                         home_ou = new_home_ou,
100                         dob = new_dob,
101                         active = FALSE,
102                         master_account = DEFAULT, 
103                         super_user = DEFAULT,
104                         barred = FALSE,
105                         deleted = TRUE,
106                         juvenile = DEFAULT,
107                         usrgroup = 0,
108                         claims_returned_count = DEFAULT,
109                         credit_forward_balance = DEFAULT,
110                         last_xact_id = DEFAULT,
111                         alert_message = NULL,
112                         create_date = now(),
113                         expire_date = now()
114         WHERE
115                 id = src_usr;
116 END;
117 $$ LANGUAGE plpgsql;
118
119 COMMIT;