Spaces:
Sleeping
Sleeping
File size: 1,013 Bytes
aa916fd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
-- Add account management functions
-- Function to update an account
CREATE OR REPLACE FUNCTION update_account(
name TEXT,
account_id UUID
) RETURNS void
LANGUAGE plpgsql SECURITY DEFINER
AS $$
BEGIN
UPDATE public.accounts
SET
name = update_account.name,
updated_at = now()
WHERE id = update_account.account_id;
END;
$$;
-- Function to get all accounts for current user
CREATE OR REPLACE FUNCTION get_accounts()
RETURNS json
LANGUAGE plpgsql SECURITY DEFINER
AS $$
DECLARE
current_user_id uuid;
account_data json;
BEGIN
-- Get the current user's ID
current_user_id := auth.uid();
-- Query for accounts
SELECT json_agg(
json_build_object(
'id', a.id,
'name', a.name,
'slug', a.slug,
'personal_account', a.id = current_user_id
)
) INTO account_data
FROM public.accounts a
WHERE a.id = current_user_id;
-- Return empty array if no results
IF account_data IS NULL THEN
RETURN '[]'::json;
END IF;
RETURN account_data;
END;
$$; |