@@ -11,9 +11,11 @@ DECLARE
11
11
occasionId bigint ;
12
12
is_open_bool BOOLEAN ;
13
13
occasion_user occasion_users%rowtype;
14
+ unit_user unit_users%rowtype;
14
15
is_admin_bool BOOLEAN ;
15
16
occasion_link text ;
16
17
version_recommended text ;
18
+ occasion_unit bigint ;
17
19
BEGIN
18
20
-- Log the request details in log_app_config table
19
21
INSERT INTO public .log_app_config (organization, platform)
55
57
-- If no link or form_link is provided
56
58
ELSE
57
59
-- Get the default occasion from the organization
58
- SELECT data- >> ' DEFAULT_OCCASION' INTO occasionId
60
+ SELECT ( data- >> ' DEFAULT_OCCASION' ):: bigint INTO occasionId
59
61
FROM organizations
60
62
WHERE id = org_id;
61
63
@@ -98,13 +100,29 @@ BEGIN
98
100
WHERE occasion = occasionId
99
101
AND " user" = auth .uid ();
100
102
103
+ -- Retrieve the unit ID from the occasions table
104
+ SELECT unit INTO occasion_unit
105
+ FROM occasions
106
+ WHERE id = occasionId;
107
+
108
+ -- Get the unit user record if it exists
109
+ SELECT * INTO unit_user
110
+ FROM unit_users
111
+ WHERE unit = occasion_unit
112
+ AND " user" = auth .uid ();
113
+
101
114
-- Check if the current user is an admin on the occasion
102
115
is_admin_bool := get_is_admin_on_occasion(occasionId);
103
116
104
117
-- If the occasion is not open, enforce access restrictions
105
118
IF is_open_bool = FALSE THEN
106
119
IF auth .uid () IS NULL OR (occasion_user IS NULL AND NOT is_admin_bool) THEN
107
- RETURN json_build_object(' code' , 403 , ' message' , ' Access forbidden' , ' link' , occasion_link, ' version_recommended' , version_recommended);
120
+ RETURN json_build_object(
121
+ ' code' , 403 ,
122
+ ' message' , ' Access forbidden' ,
123
+ ' link' , occasion_link,
124
+ ' version_recommended' , version_recommended
125
+ );
108
126
END IF;
109
127
END IF;
110
128
@@ -120,11 +138,20 @@ BEGIN
120
138
AND " user" = auth .uid ();
121
139
END IF;
122
140
141
+ -- Retrieve unit_user again in case the user was added to the occasion and now belongs to a unit
142
+ IF unit_user IS NULL AND occasion_unit IS NOT NULL AND auth .uid () IS NOT NULL THEN
143
+ SELECT * INTO unit_user
144
+ FROM unit_users
145
+ WHERE unit = occasion_unit
146
+ AND " user" = auth .uid ();
147
+ END IF;
148
+
123
149
-- Return final response with all data and status code 200 at the end
124
150
RETURN json_build_object(
125
151
' code' , 200 ,
126
152
' is_admin' , is_admin_bool,
127
153
' occasion_user' , COALESCE(row_to_json(occasion_user)::jsonb, NULL ),
154
+ ' unit_user' , COALESCE(row_to_json(unit_user)::jsonb, NULL ),
128
155
' link' , occasion_link,
129
156
' occasion' , occasionId,
130
157
' version_recommended' , version_recommended
0 commit comments