Skip to content

Commit

Permalink
Handle strings better in adding notifications (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
jongrim authored Feb 27, 2024
1 parent 9ce6c73 commit d467333
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.add_rsvp_to_notifications(user_id uuid, user_name text, email text, game_name text, related_url text, message text)
RETURNS void
LANGUAGE plpgsql
AS $function$BEGIN
INSERT INTO notifications (type, user_id, user_name, email, message, related_url, custom_fields)
VALUES ('rsvp', user_id, user_name, email, message, related_url,
to_jsonb(json_build_object('game_name', game_name)));
END;$function$
;

CREATE OR REPLACE FUNCTION public.leave_session(user_id text, session_id bigint)
RETURNS record
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
declare
results "sessions"%rowtype;
length integer;
count integer;
cur_rsvp uuid;
new_rsvp uuid;
cur_game_id integer;
game_title text;
user_email text;
user_name text;
begin
if not exists (select * from sessions where id = session_id and user_id = ANY(rsvps)) then
raise exception 'User not in session';
end if;

select array_length(rsvps, 1)
into length
from sessions
where id = session_id
and user_id = ANY(rsvps);
select participant_count into count from sessions
where id = session_id;

if length - 1 < count then
update sessions set has_openings = true, rsvps = array_remove(rsvps, "user_id")
where id = session_id
returning * into results;
else
select rsvps[count]::uuid into cur_rsvp from sessions
where id = session_id;
update sessions set has_openings = false, rsvps = array_remove(rsvps, "user_id")
where id = session_id
returning * into results;
select rsvps[count]::uuid, game_id
into new_rsvp, cur_game_id from sessions
where id = session_id;
if cur_rsvp != new_rsvp then
select title into game_title
from games where id = cur_game_id;
select email, username into user_email, user_name
from profiles where id = new_rsvp;
PERFORM add_rsvp_to_notifications(
new_rsvp,
user_name,
user_email,
game_title,
format('https://app.playabl.io/games/%s', cur_game_id),
format('Congrats! You are in for %L!', quote_literal(game_title))
);
end if;
end if;

return results;
end;
$function$
;


0 comments on commit d467333

Please sign in to comment.