-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.sql
89 lines (80 loc) · 2.31 KB
/
.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
-- FUNCTION: code_src.update_task_takenby(numeric, text)
-- DROP FUNCTION code_src.update_task_takenby(numeric, text);
CREATE OR REPLACE FUNCTION code_src.update_task_takenby(
v_task_id numeric,
v_user_name text)
RETURNS text
LANGUAGE 'plpgsql'
COST 100
VOLATILE SECURITY DEFINER
AS $BODY$
DECLARE
o_message text;
o_status text;
o_json text;
ret_json text;
ret_msg text;
v_old_user_name text;
BEGIN
/*
Summary:
Update task taken by value
Testing:
SELECT update_task_takenby(67092,'slee5')
*/
o_status = 'OK';
o_message = '';
SELECT taken_by INTO v_old_user_name FROM ige_task WHERE task_id = v_task_id;
-- Remove active task from old user
IF NOT is_blank_string(v_old_user_name) AND UPPER(v_old_user_name) <> UPPER(v_user_name) THEN
UPDATE ige_task_active
SET task_id = null,
date_modified = current_timestamp
WHERE UPPER(username) = UPPER(v_old_user_name);
END IF;
-- Update taken_by
UPDATE ige_task
SET taken_by = UPPER(v_user_name)
WHERE task_id = v_task_id;
-- Update active task for new taken_by user
IF EXISTS (SELECT * FROM ige_task_active WHERE UPPER(username) = UPPER(v_user_name)) THEN
UPDATE ige_task_active
SET task_id = v_task_id
WHERE UPPER(username) = UPPER(v_user_name);
ELSE
IF EXISTS
(
SELECT *
FROM ige_task
WHERE task_id = v_task_id
AND task_type IN ('LINEARNAME', 'AMA') -- For phase 1 only and this should be removed in phase 2
) THEN
INSERT INTO ige_task_active (username, task_id, date_modified)
VALUES (UPPER(v_user_name), v_task_id, current_timestamp);
END IF;
END IF;
SELECT row_to_json(c) INTO o_json
FROM
(
SELECT o_status status,
o_message message
) c;
RETURN o_json;
EXCEPTION
WHEN OTHERS THEN
o_status = SQLSTATE;
o_message = SQLERRM;
SELECT row_to_json(c) INTO o_json
FROM
(
SELECT o_status status,
format_string(o_message) message
) c;
RETURN o_json;
END;
$BODY$;
ALTER FUNCTION code_src.update_task_takenby(numeric, text)
OWNER TO network;
GRANT EXECUTE ON FUNCTION code_src.update_task_takenby(numeric, text) TO anchorto_run;
GRANT EXECUTE ON FUNCTION code_src.update_task_takenby(numeric, text) TO network;
REVOKE ALL ON FUNCTION code_src.update_task_takenby(numeric, text) FROM PUBLIC;