Improve invites

This commit is contained in:
Arthur Belleville 2025-10-28 12:00:45 +01:00
parent 0d0abaf945
commit d9a54be4c8
No known key found for this signature in database
6 changed files with 679 additions and 535 deletions

View file

@ -7,9 +7,8 @@ export const useInviteUser = () => {
const api = useAuthedApi();
const { mutate, isPending } = useMutation({
mutationFn: async ({ email, tablo_id }: { email: string; tablo_id: string }) => {
const { data } = await api.post("/api/v1/tablos/invite", {
const { data } = await api.post(`/api/v1/tablos/invite/${tablo_id}`, {
email,
tablo_id,
});
return data;
},

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,10 @@
-- Add is_pending column to tablo_invites table
ALTER TABLE tablo_invites
ADD COLUMN IF NOT EXISTS is_pending BOOLEAN DEFAULT FALSE NOT NULL;
-- Add comment to document the column
COMMENT ON COLUMN tablo_invites.is_pending IS
'When TRUE, the invite is pending acceptance. When FALSE, the invite has been accepted or rejected.';
-- Create index for performance when querying pending invites
CREATE INDEX IF NOT EXISTS idx_tablo_invites_is_pending ON tablo_invites(is_pending);

49
sql/28_modify_trigger.sql Normal file
View file

@ -0,0 +1,49 @@
-- Modify the handle_new_user trigger to set is_temporary based on app_metadata.role
CREATE OR REPLACE FUNCTION
public.handle_new_user()
RETURNS TRIGGER AS
$$
DECLARE
name TEXT;
first_name TEXT;
last_name TEXT;
is_temp BOOLEAN;
BEGIN
-- Extract first_name and last_name from metadata
first_name = new.raw_user_meta_data ->> 'first_name';
last_name = new.raw_user_meta_data ->> 'last_name';
-- Determine the full name
IF new.raw_user_meta_data ->> 'name' IS NOT NULL
THEN
name = new.raw_user_meta_data ->> 'name';
-- If name is provided but not first/last, try to split it
IF first_name IS NULL AND last_name IS NULL AND name IS NOT NULL THEN
first_name = SPLIT_PART(name, ' ', 1);
IF ARRAY_LENGTH(STRING_TO_ARRAY(name, ' '), 1) > 1 THEN
last_name = SUBSTRING(name FROM LENGTH(SPLIT_PART(name, ' ', 1)) + 2);
END IF;
END IF;
ELSE
name = CONCAT(first_name, ' ', last_name);
END IF;
-- Check if the role is 'invited_user' in app_metadata
IF COALESCE(new.raw_user_meta_data->>'role', '') = 'invited_user'
THEN
is_temp = TRUE;
ELSE
is_temp = FALSE;
END IF;
INSERT INTO public.profiles (id, name, email, avatar_url, first_name, last_name, is_temporary)
VALUES (new.id, name, new.email, new.raw_user_meta_data ->> 'avatar_url', first_name, last_name, is_temp);
RETURN new;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Add comment to document the change
COMMENT ON FUNCTION public.handle_new_user() IS
'Trigger function that creates a profile when a new user is created. Sets is_temporary=true for users with app_metadata.role=invited_user';

View file

@ -0,0 +1,10 @@
-- Add created_at column to tablo_invites table
ALTER TABLE tablo_invites
ADD COLUMN IF NOT EXISTS created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL;
-- Add comment to document the column
COMMENT ON COLUMN tablo_invites.created_at IS
'Timestamp when the invite was created';
-- Create index for performance when querying by creation date
CREATE INDEX IF NOT EXISTS idx_tablo_invites_created_at ON tablo_invites(created_at);

View file

@ -0,0 +1,59 @@
-- Add last_signed_in column to profiles table
ALTER TABLE profiles
ADD COLUMN IF NOT EXISTS last_signed_in TIMESTAMP WITH TIME ZONE;
-- Add comment to document the column
COMMENT ON COLUMN profiles.last_signed_in IS
'Timestamp when the user last signed in, updated from auth.users.last_sign_in_at';
-- Create function to update last_signed_in column on profiles table
CREATE OR REPLACE FUNCTION public.create_last_signed_in_on_profiles()
RETURNS TRIGGER AS $$
BEGIN
IF (NEW.last_sign_in_at is null) THEN
RETURN NULL;
ELSE
UPDATE public.profiles
SET last_signed_in = NEW.last_sign_in_at
WHERE id = (NEW.id)::uuid;
RETURN NEW;
END IF;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Create trigger to update last_signed_in column on profiles table
CREATE TRIGGER trigger_on_last_signed_in
AFTER UPDATE ON auth.users
FOR EACH ROW
EXECUTE FUNCTION public.create_last_signed_in_on_profiles();
-- Create function to update tablo_invites is_pending for temporary users
CREATE OR REPLACE FUNCTION public.update_tablo_invites_on_login()
RETURNS TRIGGER AS $$
BEGIN
-- Check if the user is temporary and update pending invites
UPDATE public.tablo_invites
SET is_pending = FALSE
WHERE invited_email = NEW.email
AND is_pending = TRUE
AND EXISTS (
SELECT 1 FROM public.profiles
WHERE id = (NEW.id)::uuid
AND is_temporary = TRUE
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Create trigger to update tablo_invites on user login
CREATE TRIGGER trigger_update_tablo_invites_on_login
AFTER UPDATE ON auth.users
FOR EACH ROW
EXECUTE FUNCTION public.update_tablo_invites_on_login();
-- Add comment to document the trigger
COMMENT ON TRIGGER trigger_update_tablo_invites_on_login ON auth.users IS
'Automatically sets is_pending=false for tablo_invites when a temporary user signs in';
-- Trigger after login: https://github.com/orgs/supabase/discussions/7463