diff --git a/app/background/clinician-info-form.tsx b/app/background/clinician-info-form.tsx new file mode 100644 index 0000000..fd0adea --- /dev/null +++ b/app/background/clinician-info-form.tsx @@ -0,0 +1,99 @@ +"use client"; + +import { toast } from "@/components/ui/use-toast"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { createClientComponentClient } from "@supabase/auth-helpers-nextjs"; +import { useRouter } from "next/navigation"; +import { type BaseSyntheticEvent } from "react"; +import { useForm } from "react-hook-form"; + +import { type Database } from "@/lib/schema"; +import { clinicianSchema, type ClinicianData } from "./info-schemas"; +type Clinician = Database["public"]["Tables"]["clinicians"]["Row"]; + +export default function ClinicianInfoForm(clinician: Clinician) { + const router = useRouter(); + + const defaultValues: Partial = { + first_name: clinician.first_name ?? undefined, + last_name: clinician.last_name ?? undefined, + employer: clinician.employer ?? undefined, + state: clinician.state ?? undefined, + city: clinician.city ?? undefined, + zip: clinician.zip ?? undefined, + }; + + const form = useForm({ + resolver: zodResolver(clinicianSchema), + defaultValues, + mode: "onChange", + }); + + const onSubmit = async (input: ClinicianData) => { + const supabase = createClientComponentClient(); + const { error } = await supabase + .from("clinicians") + .update({ + user_id: clinician.user_id, + ...input, + }) + .eq("id", clinician.id); + + if (error) { + return toast({ + title: "Something went wrong.", + description: error.message, + variant: "destructive", + }); + } + + form.reset(input); + router.refresh(); + return toast({ + title: "Success!", + description: "Your information has been updated.", + variant: "default", + }); + }; + + return ( +
void form.handleSubmit(onSubmit)(e)}> +
+ + + {form.formState.errors.first_name && (Error: {form.formState.errors.first_name?.message})} +
+
+ + + {form.formState.errors.last_name && (Error: {form.formState.errors.last_name?.message})} +
+
+ + + {form.formState.errors.employer && (Error: {form.formState.errors.employer?.message})} +
+
+ + + {form.formState.errors.state && (Error: {form.formState.errors.state?.message})} +
+
+ + + {form.formState.errors.city && (Error: {form.formState.errors.city?.message})} +
+
+ + + {form.formState.errors.zip && (Error: {form.formState.errors.zip?.message})} +
+
+
+ +
+
+ ); +} diff --git a/app/background/info-schemas.ts b/app/background/info-schemas.ts new file mode 100644 index 0000000..041f287 --- /dev/null +++ b/app/background/info-schemas.ts @@ -0,0 +1,62 @@ +import { z } from "zod"; + +const patientSchema = z.object({ + first_name: z + .string() + .nullable() + .transform((val) => (val?.trim() === "" ? null : val?.trim())), + last_name: z + .string() + .nullable() + .transform((val) => (val?.trim() === "" ? null : val?.trim())), + age: z.coerce + .number() + .int() + .gte(0) + .nullable() + .transform((val) => (val == 0 ? null : val)), + state: z + .string() + .nullable() + .transform((val) => (val?.trim() === "" ? null : val?.trim())), + city: z + .string() + .nullable() + .transform((val) => (val?.trim() === "" ? null : val?.trim())), + zip: z + .string() + .nullable() + .transform((val) => (val?.trim() === "" ? null : val?.trim())), +}); + +const clinicianSchema = z.object({ + first_name: z + .string() + .nullable() + .transform((val) => (val?.trim() === "" ? null : val?.trim())), + last_name: z + .string() + .nullable() + .transform((val) => (val?.trim() === "" ? null : val?.trim())), + employer: z + .string() + .nullable() + .transform((val) => (val?.trim() === "" ? null : val?.trim())), + state: z + .string() + .nullable() + .transform((val) => (val?.trim() === "" ? null : val?.trim())), + city: z + .string() + .nullable() + .transform((val) => (val?.trim() === "" ? null : val?.trim())), + zip: z + .string() + .nullable() + .transform((val) => (val?.trim() === "" ? null : val?.trim())), +}); + +type PatientData = z.infer; +type ClinicianData = z.infer; + +export { clinicianSchema, patientSchema, type ClinicianData, type PatientData }; diff --git a/app/background/page.tsx b/app/background/page.tsx new file mode 100644 index 0000000..0c421df --- /dev/null +++ b/app/background/page.tsx @@ -0,0 +1,32 @@ +import { createServerSupabaseClient } from "@/lib/server-utils"; +import { redirect } from "next/navigation"; +import ClinicianInfoForm from "./clinician-info-form"; +import PatientInfoForm from "./patient-info-form"; + +export default async function BackgroundInfo() { + const supabase = createServerSupabaseClient(); + const { + data: { session }, + } = await supabase.auth.getSession(); + + if (!session) { + redirect("/"); + } + + // Retrieve user information + const userid = session.user.id; + const { data: patients } = await supabase.from("patients").select().eq("user_id", userid); + const { data: clinicians } = await supabase.from("clinicians").select().eq("user_id", userid); + + // Allow size > 1 for now + if (!patients?.length && !clinicians?.length) { + return <>User data not found.; + } + + return ( + <> + {patients?.map((patient) => )} + {clinicians?.map((clinician) => )} + + ); +} diff --git a/app/background/patient-info-form.tsx b/app/background/patient-info-form.tsx new file mode 100644 index 0000000..48e87f4 --- /dev/null +++ b/app/background/patient-info-form.tsx @@ -0,0 +1,94 @@ +"use client"; + +import { toast } from "@/components/ui/use-toast"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { createClientComponentClient } from "@supabase/auth-helpers-nextjs"; +import { useRouter } from "next/navigation"; +import { type BaseSyntheticEvent } from "react"; +import { useForm } from "react-hook-form"; + +import { type Database } from "@/lib/schema"; +import { patientSchema, type PatientData } from "./info-schemas"; +type Patient = Database["public"]["Tables"]["patients"]["Row"]; + +export default function PatientInfoForm(patient: Patient) { + const router = useRouter(); + + const defaultValues: Partial = { + first_name: patient.first_name ?? undefined, + last_name: patient.last_name ?? undefined, + age: patient.age ?? undefined, + state: patient.state ?? undefined, + city: patient.city ?? undefined, + zip: patient.zip ?? undefined, + }; + + const form = useForm({ + resolver: zodResolver(patientSchema), + defaultValues, + mode: "onChange", + }); + + const onSubmit = async (input: PatientData) => { + const supabase = createClientComponentClient(); + const { error } = await supabase + .from("patients") + .update({ + user_id: patient.user_id, + ...input, + }) + .eq("id", patient.id); + + if (error) { + return toast({ + title: "Something went wrong.", + description: error.message, + variant: "destructive", + }); + } + + form.reset(input); + router.refresh(); + }; + + return ( +
void form.handleSubmit(onSubmit)(e)}> +
+ + + {form.formState.errors.first_name && (Error: {form.formState.errors.first_name?.message})} +
+
+ + + {form.formState.errors.last_name && (Error: {form.formState.errors.last_name?.message})} +
+
+ + + {form.formState.errors.age && (Error: {form.formState.errors.age?.message})} +
+
+ + + {form.formState.errors.state && (Error: {form.formState.errors.state?.message})} +
+
+ + + {form.formState.errors.city && (Error: {form.formState.errors.city?.message})} +
+
+ + + {form.formState.errors.zip && (Error: {form.formState.errors.zip?.message})} +
+
+
+ +
+
+ ); +} diff --git a/lib/schema.ts b/lib/schema.ts index 9e2a222..7e341d1 100644 --- a/lib/schema.ts +++ b/lib/schema.ts @@ -3,24 +3,388 @@ export type Json = string | number | boolean | null | { [key: string]: Json | un export interface Database { public: { Tables: { + admins: { + Row: { + id: string; + user_id: string; + }; + Insert: { + id: string; + user_id: string; + }; + Update: { + id?: string; + user_id?: string; + }; + Relationships: [ + { + foreignKeyName: "admins_user_id_fkey"; + columns: ["user_id"]; + referencedRelation: "users"; + referencedColumns: ["id"]; + }, + ]; + }; + clinic_members: { + Row: { + clinic_id: string | null; + diagnosis: string; + id: string; + join_date: string; + patient_id: string | null; + }; + Insert: { + clinic_id?: string | null; + diagnosis: string; + id: string; + join_date: string; + patient_id?: string | null; + }; + Update: { + clinic_id?: string | null; + diagnosis?: string; + id?: string; + join_date?: string; + patient_id?: string | null; + }; + Relationships: [ + { + foreignKeyName: "clinic_members_clinic_id_fkey"; + columns: ["clinic_id"]; + referencedRelation: "clinics"; + referencedColumns: ["id"]; + }, + { + foreignKeyName: "clinic_members_patient_id_fkey"; + columns: ["patient_id"]; + referencedRelation: "patients"; + referencedColumns: ["id"]; + }, + ]; + }; + clinicians: { + Row: { + city: string | null; + employer: string | null; + first_name: string | null; + id: string; + last_name: string | null; + state: string | null; + user_id: string | null; + zip: string | null; + }; + Insert: { + city?: string | null; + employer?: string | null; + first_name?: string | null; + id?: string; + last_name?: string | null; + state?: string | null; + user_id?: string | null; + zip?: string | null; + }; + Update: { + city?: string | null; + employer?: string | null; + first_name?: string | null; + id?: string; + last_name?: string | null; + state?: string | null; + user_id?: string | null; + zip?: string | null; + }; + Relationships: [ + { + foreignKeyName: "clinicians_user_id_fkey"; + columns: ["user_id"]; + referencedRelation: "users"; + referencedColumns: ["id"]; + }, + ]; + }; + clinics: { + Row: { + code: string; + id: string; + name: string; + owner: string; + }; + Insert: { + code: string; + id?: string; + name: string; + owner: string; + }; + Update: { + code?: string; + id?: string; + name?: string; + owner?: string; + }; + Relationships: [ + { + foreignKeyName: "clinics_owner_fkey"; + columns: ["owner"]; + referencedRelation: "clinicians"; + referencedColumns: ["id"]; + }, + ]; + }; + data: { + Row: { + aud: string | null; + banned_until: string | null; + confirmation_sent_at: string | null; + confirmation_token: string | null; + confirmed_at: string | null; + created_at: string | null; + deleted_at: string | null; + email: string | null; + email_change: string | null; + email_change_confirm_status: number | null; + email_change_sent_at: string | null; + email_change_token_current: string | null; + email_change_token_new: string | null; + email_confirmed_at: string | null; + encrypted_password: string | null; + id: string | null; + instance_id: string | null; + invited_at: string | null; + is_sso_user: boolean | null; + is_super_admin: boolean | null; + last_sign_in_at: string | null; + phone: string | null; + phone_change: string | null; + phone_change_sent_at: string | null; + phone_change_token: string | null; + phone_confirmed_at: string | null; + raw_app_meta_data: Json | null; + raw_user_meta_data: Json | null; + reauthentication_sent_at: string | null; + reauthentication_token: string | null; + recovery_sent_at: string | null; + recovery_token: string | null; + role: string | null; + updated_at: string | null; + }; + Insert: { + aud?: string | null; + banned_until?: string | null; + confirmation_sent_at?: string | null; + confirmation_token?: string | null; + confirmed_at?: string | null; + created_at?: string | null; + deleted_at?: string | null; + email?: string | null; + email_change?: string | null; + email_change_confirm_status?: number | null; + email_change_sent_at?: string | null; + email_change_token_current?: string | null; + email_change_token_new?: string | null; + email_confirmed_at?: string | null; + encrypted_password?: string | null; + id?: string | null; + instance_id?: string | null; + invited_at?: string | null; + is_sso_user?: boolean | null; + is_super_admin?: boolean | null; + last_sign_in_at?: string | null; + phone?: string | null; + phone_change?: string | null; + phone_change_sent_at?: string | null; + phone_change_token?: string | null; + phone_confirmed_at?: string | null; + raw_app_meta_data?: Json | null; + raw_user_meta_data?: Json | null; + reauthentication_sent_at?: string | null; + reauthentication_token?: string | null; + recovery_sent_at?: string | null; + recovery_token?: string | null; + role?: string | null; + updated_at?: string | null; + }; + Update: { + aud?: string | null; + banned_until?: string | null; + confirmation_sent_at?: string | null; + confirmation_token?: string | null; + confirmed_at?: string | null; + created_at?: string | null; + deleted_at?: string | null; + email?: string | null; + email_change?: string | null; + email_change_confirm_status?: number | null; + email_change_sent_at?: string | null; + email_change_token_current?: string | null; + email_change_token_new?: string | null; + email_confirmed_at?: string | null; + encrypted_password?: string | null; + id?: string | null; + instance_id?: string | null; + invited_at?: string | null; + is_sso_user?: boolean | null; + is_super_admin?: boolean | null; + last_sign_in_at?: string | null; + phone?: string | null; + phone_change?: string | null; + phone_change_sent_at?: string | null; + phone_change_token?: string | null; + phone_confirmed_at?: string | null; + raw_app_meta_data?: Json | null; + raw_user_meta_data?: Json | null; + reauthentication_sent_at?: string | null; + reauthentication_token?: string | null; + recovery_sent_at?: string | null; + recovery_token?: string | null; + role?: string | null; + updated_at?: string | null; + }; + Relationships: []; + }; + messages: { + Row: { + id: string; + media: string | null; + message: string; + receiver: string | null; + sender: string | null; + }; + Insert: { + id: string; + media?: string | null; + message: string; + receiver?: string | null; + sender?: string | null; + }; + Update: { + id?: string; + media?: string | null; + message?: string; + receiver?: string | null; + sender?: string | null; + }; + Relationships: [ + { + foreignKeyName: "messages_receiver_fkey"; + columns: ["receiver"]; + referencedRelation: "users"; + referencedColumns: ["id"]; + }, + { + foreignKeyName: "messages_sender_fkey"; + columns: ["sender"]; + referencedRelation: "users"; + referencedColumns: ["id"]; + }, + ]; + }; + milestones: { + Row: { + assigner: string | null; + clinic_id: string | null; + description: string | null; + id: string; + name: string; + patient: string | null; + }; + Insert: { + assigner?: string | null; + clinic_id?: string | null; + description?: string | null; + id: string; + name: string; + patient?: string | null; + }; + Update: { + assigner?: string | null; + clinic_id?: string | null; + description?: string | null; + id?: string; + name?: string; + patient?: string | null; + }; + Relationships: [ + { + foreignKeyName: "milestones_assigner_fkey"; + columns: ["assigner"]; + referencedRelation: "clinicians"; + referencedColumns: ["id"]; + }, + { + foreignKeyName: "milestones_clinic_id_fkey"; + columns: ["clinic_id"]; + referencedRelation: "clinics"; + referencedColumns: ["id"]; + }, + { + foreignKeyName: "milestones_patient_fkey"; + columns: ["patient"]; + referencedRelation: "patients"; + referencedColumns: ["id"]; + }, + ]; + }; + patients: { + Row: { + age: number | null; + city: string | null; + first_name: string | null; + id: string; + last_name: string | null; + state: string | null; + user_id: string; + zip: string | null; + }; + Insert: { + age?: number | null; + city?: string | null; + first_name?: string | null; + id?: string; + last_name?: string | null; + state?: string | null; + user_id: string; + zip?: string | null; + }; + Update: { + age?: number | null; + city?: string | null; + first_name?: string | null; + id?: string; + last_name?: string | null; + state?: string | null; + user_id?: string; + zip?: string | null; + }; + Relationships: [ + { + foreignKeyName: "patients_user_id_fkey"; + columns: ["user_id"]; + referencedRelation: "users"; + referencedColumns: ["id"]; + }, + ]; + }; profiles: { Row: { biography: string | null; display_name: string; email: string; id: string; + timestamp: string | null; }; Insert: { biography?: string | null; display_name: string; email: string; id: string; + timestamp?: string | null; }; Update: { biography?: string | null; display_name?: string; email?: string; id?: string; + timestamp?: string | null; }; Relationships: [ { @@ -31,12 +395,67 @@ export interface Database { }, ]; }; + tasks: { + Row: { + assign_date: string; + assigner: string | null; + complete_date: string; + completed: boolean; + description: string | null; + due_date: string | null; + id: string; + media: string | null; + name: string; + patient: string | null; + }; + Insert: { + assign_date: string; + assigner?: string | null; + complete_date: string; + completed: boolean; + description?: string | null; + due_date?: string | null; + id: string; + media?: string | null; + name: string; + patient?: string | null; + }; + Update: { + assign_date?: string; + assigner?: string | null; + complete_date?: string; + completed?: boolean; + description?: string | null; + due_date?: string | null; + id?: string; + media?: string | null; + name?: string; + patient?: string | null; + }; + Relationships: [ + { + foreignKeyName: "tasks_assigner_fkey"; + columns: ["assigner"]; + referencedRelation: "clinicians"; + referencedColumns: ["id"]; + }, + { + foreignKeyName: "tasks_patient_fkey"; + columns: ["patient"]; + referencedRelation: "patients"; + referencedColumns: ["id"]; + }, + ]; + }; }; Views: { [_ in never]: never; }; Functions: { - [_ in never]: never; + test_call: { + Args: Record; + Returns: unknown; + }; }; Enums: { [_ in never]: never; diff --git a/setup.sql b/setup.sql index 586d712..427a6e2 100644 --- a/setup.sql +++ b/setup.sql @@ -1,129 +1,129 @@ --- Create a table for patients -create table patients ( - id uuid not null primary key, - user_id uuid not null references auth.users(id), - first_name not null text, - last_name not null text, - age integer, - state text, - city text, - zip text -); - --- Create a table for clinicians -create table clinicians ( - id uuid not null primary key, - user_id uuid references auth.users(id), - first_name not null text, - last_name not null text, - employer text, - state text, - city text, - zip text -); - --- Create a table for admins -create table admins ( - id uuid not null primary key, - user_id uuid not null references auth.users(id) -); - --- Create a table for clinics - this was being problematic when putting it into supabse -create table clinics ( - id uuid not null primary key, - owner uuid references clinicians(id) not null, - code text not null, - name text not null -); - --- Create a table for messages -create table messages ( - id uuid not null primary key, - sender uuid references auth.users(id), - receiver uuid references auth.users(id), - message text not null, - media text -); - --- Create a table for tasks -create table tasks ( - id uuid not null primary key, - assigner uuid references clinicians(id), - patient uuid references patients(id), - name text not null, - description text, - media text, - assign_date timestamp not null, - due_date timestamp, - completed boolean not null, - complete_date timestamp not null -); - --- Create a table for clinic members -create table clinic_members ( - id uuid not null primary key, - patient_id uuid references patients(id), - clinic_id uuid references clinics(id), - diagnosis text not null, - join_date timestamp not null -); - --- Create a table for milestones -create table milestones ( - id uuid not null primary key, - assigner uuid references clinicians(id), - patient uuid references patients(id), - clinic_id uuid references clinics(id), - name text not null, - description text -); - --- -- Set up Row Level Security (RLS) --- -- See https://supabase.com/docs/guides/auth/row-level-security for more details. - --- Enable Row Level Security for patients, clinicians, admins, clinics, messages, tasks, clinicMembers, and milestones tables --- ALTER TABLE patients ENABLE ROW LEVEL SECURITY; --- ALTER TABLE clinicians ENABLE ROW LEVEL SECURITY; --- ALTER TABLE admins ENABLE ROW LEVEL SECURITY; --- ALTER TABLE clinics ENABLE ROW LEVEL SECURITY; --- ALTER TABLE messages ENABLE ROW LEVEL SECURITY; --- ALTER TABLE tasks ENABLE ROW LEVEL SECURITY; --- ALTER TABLE clinicMembers ENABLE ROW LEVEL SECURITY; --- ALTER TABLE milestones ENABLE ROW LEVEL SECURITY; - --- -- Allow users to view/edit their own data in the patients, clinicians, and admins tables --- create policy "Users can view their own data." on users --- for select using (auth.uid() = userId); - --- create policy "Patients can view their own data." on patients --- for select using (auth.uid() = userId); - --- create policy "Clinicians can view their own data." on patients --- for select using (auth.uid() = clinicianId); - --- This trigger automatically creates a profile entry when a new user signs up via Supabase Auth. --- See https://supabase.com/docs/guides/auth/managing-user-data#using-triggers for more details. -create function public.handle_new_user() -returns trigger as $$ -DECLARE - username text; -BEGIN - SELECT substring(NEW.email from '(.*)@') INTO username; - - INSERT INTO public.profiles (id, email, display_name, biography) - VALUES (NEW.id, NEW.id, username, ''); - - IF NEW.raw_user_meta_data->>'type' = 'patient' THEN - INSERT INTO public.patients (user_id, first_name, last_name, age, state, city, zip) - VALUES (NEW.id, NEW.raw_user_meta_data ->>'first_name', NEW.raw_user_meta_data->>'last_name', (NEW.raw_user_meta_data->>'age')::integer, NEW.raw_user_meta_data->>'state', NEW.raw_user_meta_data->>'city', NEW.raw_user_meta_data->>'zipcode'); - ELSIF NEW.raw_user_meta_data->>'type' = 'clinician' THEN - INSERT INTO public.clinicians (user_id, first_name, last_name, employer, state, city, zip) - VALUES (NEW.id, NEW.raw_user_meta_data->>'first_name', NEW.raw_user_meta_data->>'last_name', NEW.raw_user_meta_data->>'employer', NEW.raw_user_meta_data->>'state', NEW.raw_user_meta_data->>'city', NEW.raw_user_meta_data->>'zipcode'); - END IF; - - RETURN NEW; -END; -$$ language plpgsql security definer; -create trigger on_auth_user_created - after insert on auth.users - for each row execute procedure public.handle_new_user(); +-- Create a table for patients +create table patients ( + id uuid not null primary key, + user_id uuid not null references auth.users(id), + first_name not null text, + last_name not null text, + age integer, + state text, + city text, + zip text +); + +-- Create a table for clinicians +create table clinicians ( + id uuid not null primary key, + user_id uuid references auth.users(id), + first_name not null text, + last_name not null text, + employer text, + state text, + city text, + zip text +); + +-- Create a table for admins +create table admins ( + id uuid not null primary key, + user_id uuid not null references auth.users(id) +); + +-- Create a table for clinics - this was being problematic when putting it into supabse +create table clinics ( + id uuid not null primary key, + owner uuid references clinicians(id) not null, + code text not null, + name text not null +); + +-- Create a table for messages +create table messages ( + id uuid not null primary key, + sender uuid references auth.users(id), + receiver uuid references auth.users(id), + message text not null, + media text +); + +-- Create a table for tasks +create table tasks ( + id uuid not null primary key, + assigner uuid references clinicians(id), + patient uuid references patients(id), + name text not null, + description text, + media text, + assign_date timestamp not null, + due_date timestamp, + completed boolean not null, + complete_date timestamp not null +); + +-- Create a table for clinic members +create table clinic_members ( + id uuid not null primary key, + patient_id uuid references patients(id), + clinic_id uuid references clinics(id), + diagnosis text not null, + join_date timestamp not null +); + +-- Create a table for milestones +create table milestones ( + id uuid not null primary key, + assigner uuid references clinicians(id), + patient uuid references patients(id), + clinic_id uuid references clinics(id), + name text not null, + description text +); + +-- -- Set up Row Level Security (RLS) +-- -- See https://supabase.com/docs/guides/auth/row-level-security for more details. + +-- Enable Row Level Security for patients, clinicians, admins, clinics, messages, tasks, clinicMembers, and milestones tables +-- ALTER TABLE patients ENABLE ROW LEVEL SECURITY; +-- ALTER TABLE clinicians ENABLE ROW LEVEL SECURITY; +-- ALTER TABLE admins ENABLE ROW LEVEL SECURITY; +-- ALTER TABLE clinics ENABLE ROW LEVEL SECURITY; +-- ALTER TABLE messages ENABLE ROW LEVEL SECURITY; +-- ALTER TABLE tasks ENABLE ROW LEVEL SECURITY; +-- ALTER TABLE clinicMembers ENABLE ROW LEVEL SECURITY; +-- ALTER TABLE milestones ENABLE ROW LEVEL SECURITY; + +-- -- Allow users to view/edit their own data in the patients, clinicians, and admins tables +-- create policy "Users can view their own data." on users +-- for select using (auth.uid() = userId); + +-- create policy "Patients can view their own data." on patients +-- for select using (auth.uid() = userId); + +-- create policy "Clinicians can view their own data." on patients +-- for select using (auth.uid() = clinicianId); + +-- This trigger automatically creates a profile entry when a new user signs up via Supabase Auth. +-- See https://supabase.com/docs/guides/auth/managing-user-data#using-triggers for more details. +create function public.handle_new_user() +returns trigger as $$ +DECLARE + username text; +BEGIN + SELECT substring(NEW.email from '(.*)@') INTO username; + + INSERT INTO public.profiles (id, email, display_name, biography) + VALUES (NEW.id, NEW.id, username, ''); + + IF NEW.raw_user_meta_data->>'type' = 'patient' THEN + INSERT INTO public.patients (user_id, first_name, last_name, age, state, city, zip) + VALUES (NEW.id, NEW.raw_user_meta_data ->>'first_name', NEW.raw_user_meta_data->>'last_name', (NEW.raw_user_meta_data->>'age')::integer, NEW.raw_user_meta_data->>'state', NEW.raw_user_meta_data->>'city', NEW.raw_user_meta_data->>'zipcode'); + ELSIF NEW.raw_user_meta_data->>'type' = 'clinician' THEN + INSERT INTO public.clinicians (user_id, first_name, last_name, employer, state, city, zip) + VALUES (NEW.id, NEW.raw_user_meta_data->>'first_name', NEW.raw_user_meta_data->>'last_name', NEW.raw_user_meta_data->>'employer', NEW.raw_user_meta_data->>'state', NEW.raw_user_meta_data->>'city', NEW.raw_user_meta_data->>'zipcode'); + END IF; + + RETURN NEW; +END; +$$ language plpgsql security definer; +create trigger on_auth_user_created + after insert on auth.users + for each row execute procedure public.handle_new_user(); \ No newline at end of file