budget-view-finance/supabase/migrations/20250615204657-2e288caf-c8a1-46b4-959a-c2ecfaa24986.sql
gpt-engineer-app[bot] 9cd928329b Run SQL migration
Apply the reviewed SQL changes to the database.
2025-06-15 20:47:37 +00:00

44 lines
1.2 KiB
PL/PgSQL

CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
DECLARE
nome_val TEXT;
empresa_val TEXT;
whatsapp_val TEXT;
BEGIN
-- Extract values from metadata, providing default placeholders if they are missing
nome_val := COALESCE(new.raw_user_meta_data->>'nome', 'Nome não informado');
empresa_val := new.raw_user_meta_data->>'empresa'; -- This field can be null
whatsapp_val := COALESCE(new.raw_user_meta_data->>'whatsapp', '00000000000');
-- This logic attempts to update an existing user record found by email.
-- This is key to reconnecting your old data to your new login.
UPDATE public.usuarios
SET
id = new.id,
nome = nome_val,
empresa = empresa_val,
whatsapp = whatsapp_val
WHERE email = new.email;
-- If no record was updated (because the email wasn't found),
-- it means this is a completely new user, so we insert a new record.
IF NOT FOUND THEN
INSERT INTO public.usuarios (id, email, nome, empresa, whatsapp)
VALUES (
new.id,
new.email,
nome_val,
empresa_val,
whatsapp_val
);
END IF;
RETURN new;
END;
$$;