Skip to content

Commit

Permalink
fix: messages when recipient doesn't have auth_id (#4152)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariojsnunes authored Mar 7, 2025
1 parent dcd00f8 commit 25e4848
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/routes/api.messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {

const from = userProfile.data!.at(0)!.id
const to = recipientProfile.data!.at(0)!.id
const toAuthId = recipientProfile.data!.at(0)!.auth_id

const today = new Date()
const yesterday = new Date(today.getTime() - 1000 * 60 * 60 * 24)
Expand Down Expand Up @@ -81,9 +82,12 @@ export const action = async ({ request }: ActionFunctionArgs) => {
throw messageResult.error
}

const emailResult = await client.rpc('get_user_email_by_id', {
id: recipientProfile.data![0].auth_id,
})
// TODO: use get_user_email_by_id only after removing firebase completely and all profiles have an auth_id
const emailResult = toAuthId
? await client.rpc('get_user_email_by_id', { id: toAuthId })
: await client.rpc('get_user_email_by_username', {
username: data.to,
})
const receiverEmail = emailResult.data[0].email
const fromUsername = userProfile.data![0].username
const emailTemplate = (
Expand Down
10 changes: 10 additions & 0 deletions supabase/migrations/20250307061534_messages_temp_fix.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE OR REPLACE FUNCTION public.get_user_email_by_username(username text)
RETURNS TABLE(email character varying)
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
BEGIN
RETURN QUERY SELECT au.email FROM auth.users au WHERE au.raw_user_meta_data->>'username' = $1;
END;
$function$
;

0 comments on commit 25e4848

Please sign in to comment.