-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpansion_schema.sql
More file actions
44 lines (36 loc) · 1.79 KB
/
Copy pathexpansion_schema.sql
File metadata and controls
44 lines (36 loc) · 1.79 KB
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
-- Expansion Infrastructure: Support Tickets & Metadata
-- 1. Tickets Table
CREATE TABLE IF NOT EXISTS public.tickets (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
client_id UUID REFERENCES public.profiles(id) ON DELETE CASCADE NOT NULL,
subject TEXT NOT NULL,
description TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'Open' CHECK (status IN ('Open', 'In Progress', 'Resolved', 'Closed')),
priority TEXT NOT NULL DEFAULT 'Medium' CHECK (priority IN ('Low', 'Medium', 'High', 'Critical')),
created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- 2. Metadata for Profiles (Optional expansion)
ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS phone TEXT;
ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS last_login TIMESTAMP WITH TIME ZONE;
-- --- ROW LEVEL SECURITY (RLS) ---
ALTER TABLE public.tickets ENABLE ROW LEVEL SECURITY;
-- Tickets Policies
CREATE POLICY "Clients can view their own tickets." ON public.tickets
FOR SELECT USING (client_id = auth.uid() OR (EXISTS (SELECT 1 FROM public.profiles WHERE id = auth.uid() AND role = 'ADMIN')));
CREATE POLICY "Clients can create their own tickets." ON public.tickets
FOR INSERT WITH CHECK (client_id = auth.uid());
CREATE POLICY "Admins can manage all tickets." ON public.tickets
FOR ALL USING (EXISTS (SELECT 1 FROM public.profiles WHERE id = auth.uid() AND role = 'ADMIN'));
-- Trigger for updated_at
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_tickets_updated_at
BEFORE UPDATE ON public.tickets
FOR EACH ROW
EXECUTE PROCEDURE update_updated_at_column();