Skip to content

Commit bbd4298

Browse files
authored
Merge pull request #8 from aasaru/add_body_to_add_sub_delegate
PH-621 add logic to add_sub_delegate funciton
2 parents d9550f9 + d31e0cf commit bbd4298

3 files changed

Lines changed: 88 additions & 7 deletions

File tree

tests/pg_data/01_init_tables.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ CREATE TABLE person (
1212

1313
CREATE TABLE mandate (
1414
id SERIAL PRIMARY KEY,
15-
delegate_id INTEGER NOT NULL REFERENCES person(id),
1615
representee_id INTEGER NOT NULL REFERENCES person(id),
16+
delegate_id INTEGER NOT NULL REFERENCES person(id),
1717
role TEXT,
1818
validity_period_from DATE,
1919
validity_period_through DATE,

tests/pg_data/03a_function_create_mandate.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,21 @@ DECLARE
3131
v_delegate_code TEXT := SUBSTRING(p_delegate_identifier FROM 3);
3232
v_delegate_country TEXT := SUBSTRING(p_delegate_identifier FROM 1 FOR 2);
3333

34+
rec_role_conf roles_view%ROWTYPE;
35+
3436
BEGIN
3537

38+
SELECT * INTO rec_role_conf FROM roles_view WHERE code = p_role;
39+
40+
IF rec_role_conf.code IS NULL THEN
41+
RAISE 'There is no role with code=% defined in roles_view', p_role USING ERRCODE = '23010';
42+
end if;
43+
44+
45+
IF p_can_sub_delegate = TRUE AND rec_role_conf.can_sub_delegate = FALSE THEN
46+
RAISE 'The role with code=% is defined with can_sub_delegate=FALSE but mandate to be added has it true', p_role USING ERRCODE = '23011';
47+
end if;
48+
3649
-- Insert or update person record corresponding to representee
3750
INSERT INTO person (type, personal_company_code, personal_company_code_country, first_name, surname, legal_name)
3851
VALUES (p_representee_type, v_representee_code, v_representee_country, p_representee_first_name, p_representee_surname, p_representee_legal_name)
Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,92 @@
11
CREATE OR REPLACE FUNCTION function_insert_mandate_subdelegate(
2-
32
p_representee_id TEXT,
43
p_delegate_id TEXT,
54
p_mandate_id TEXT,
6-
75
p_sub_delegate_identifier TEXT,
86
p_sub_delegate_first_name TEXT,
97
p_sub_delegate_surname TEXT,
108
p_sub_delegate_legal_name TEXT,
119
p_sub_delegate_type TEXT,
12-
1310
p_validity_period_from DATE,
1411
p_validity_period_through DATE,
15-
1612
p_created_by TEXT,
1713
p_created_by_represented_person TEXT,
1814
p_document_uuid TEXT,
1915
p_can_display_document_to_delegate BOOLEAN
20-
) RETURNS BOOLEAN AS $$
16+
) RETURNS BOOLEAN AS
17+
$$
18+
DECLARE
19+
rec_existing_mandate mandate%ROWTYPE;
20+
v_sub_delegate_code TEXT := SUBSTRING(p_sub_delegate_identifier FROM 3);
21+
v_sub_delegate_country TEXT := SUBSTRING(p_sub_delegate_identifier FROM 1 FOR 2);
22+
23+
v_sub_delegate_id person.id%TYPE;
24+
25+
v_representee_id person.id%TYPE := p_representee_id::integer;
26+
v_delegate_id person.id%TYPE := p_delegate_id::integer;
27+
v_mandate_id mandate.id%TYPE := p_mandate_id::integer;
28+
v_validity_period_from mandate.validity_period_from%TYPE := p_validity_period_from;
29+
2130
BEGIN
22-
RETURN TRUE;
31+
32+
33+
34+
35+
SELECT *
36+
INTO rec_existing_mandate
37+
FROM mandate
38+
WHERE representee_id = v_representee_id
39+
AND delegate_id = v_delegate_id
40+
AND id = v_mandate_id;
41+
42+
IF rec_existing_mandate.id IS NULL THEN
43+
RAISE 'There is no mandate where id=% AND representee_id=% AND delegate_id=%', p_mandate_id, v_representee_id, v_delegate_id USING ERRCODE = '23002';
44+
end if;
45+
46+
IF rec_existing_mandate.can_sub_delegate = FALSE THEN
47+
RAISE 'Mandate to sub-delegate (id=%) has can_sub_delegate=FALSE', p_mandate_id USING ERRCODE = '23002';
48+
end if;
49+
50+
IF p_validity_period_from IS NULL THEN
51+
v_validity_period_from := NOW();
52+
end if;
53+
54+
IF p_validity_period_through IS NOT NULL AND v_validity_period_from > p_validity_period_through THEN
55+
RAISE 'Mandate to sub-delegate (id=%) has validity_period_through before validity_period_from', p_mandate_id USING ERRCODE = '23003';
56+
end if;
57+
58+
IF rec_existing_mandate.validity_period_from > v_validity_period_from THEN
59+
RAISE 'Mandate to sub-delegate (id=%) has validity_period_from=% BUT new mandate is set to start=% (which is earlier)',
60+
p_mandate_id,
61+
rec_existing_mandate.validity_period_from,
62+
v_validity_period_from
63+
USING ERRCODE = '23004';
64+
end if;
65+
66+
IF p_validity_period_through IS NULL AND rec_existing_mandate.validity_period_through IS NOT NULL THEN
67+
RAISE 'Mandate to sub-delegate (id=%) has validity_period_through=% BUT new mandate is set to be valid indefinitely (which is longer)',
68+
p_mandate_id,
69+
rec_existing_mandate.validity_period_through
70+
USING ERRCODE = '23005';
71+
end if;
72+
73+
-- Insert or update person record corresponding to new delegate
74+
INSERT INTO person (type, personal_company_code, personal_company_code_country, first_name, surname, legal_name)
75+
VALUES (p_sub_delegate_type, v_sub_delegate_code, v_sub_delegate_country, p_sub_delegate_first_name, p_sub_delegate_surname, p_sub_delegate_legal_name)
76+
ON CONFLICT (personal_company_code, personal_company_code_country)
77+
DO UPDATE SET first_name = EXCLUDED.first_name,
78+
surname = EXCLUDED.surname,
79+
legal_name = EXCLUDED.legal_name
80+
RETURNING id INTO v_sub_delegate_id;
81+
82+
83+
INSERT INTO mandate (representee_id, delegate_id, role, validity_period_from, validity_period_through,
84+
can_sub_delegate, created_by, created_by_represented_person,
85+
original_mandate_id, document_uuid, can_display_document_to_delegate)
86+
VALUES (v_representee_id, v_sub_delegate_id, rec_existing_mandate.role, v_validity_period_from::DATE, p_validity_period_through::DATE,
87+
FALSE, p_created_by, p_created_by_represented_person,
88+
rec_existing_mandate.id, p_document_uuid, p_can_display_document_to_delegate);
89+
90+
RETURN TRUE;
2391
END;
2492
$$ LANGUAGE plpgsql;

0 commit comments

Comments
 (0)