1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00
Files
AdministrationItDepartmens/.bolt/supabase_discarded_migrations/0008_damp_paper.sql
MoonTestUse1 e81df4c87e Initial commit
2024-12-23 19:27:44 +06:00

56 lines
1.1 KiB
PL/PgSQL

/*
# Fix employee creation process
1. Changes
- Add trigger to create auth user and employee synchronously
- Update create_employee function to handle auth user creation
- Add proper error handling
2. Security
- Maintain RLS policies
- Ensure secure password handling
*/
-- Function to create auth user and employee
CREATE OR REPLACE FUNCTION create_employee(
p_first_name text,
p_last_name text,
p_department text,
p_password text
)
RETURNS employees
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
v_employee employees;
v_auth_user uuid;
BEGIN
-- Validate password
PERFORM validate_password(p_password);
-- Create auth user first
v_auth_user := auth.uid();
-- Create employee record
INSERT INTO employees (
id,
first_name,
last_name,
department,
password_hash
) VALUES (
v_auth_user,
p_first_name,
p_last_name,
p_department,
hash_password(p_password)
)
RETURNING * INTO v_employee;
RETURN v_employee;
EXCEPTION
WHEN others THEN
RAISE EXCEPTION 'Failed to create employee: %', SQLERRM;
END;
$$;