-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSETUP_FEATURES.sql
More file actions
48 lines (41 loc) · 1.96 KB
/
Copy pathSETUP_FEATURES.sql
File metadata and controls
48 lines (41 loc) · 1.96 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
45
46
47
48
-- 1. Rentals Table
CREATE TABLE IF NOT EXISTS public.rentals (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
item_id uuid REFERENCES public.items(id),
renter_id uuid REFERENCES auth.users(id),
owner_id uuid REFERENCES auth.users(id),
status text DEFAULT 'pending', -- pending, approved, active, completed, cancelled
total_price numeric,
start_date timestamp with time zone,
end_date timestamp with time zone,
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- 2. Messages Table
CREATE TABLE IF NOT EXISTS public.messages (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
sender_id uuid REFERENCES auth.users(id),
receiver_id uuid REFERENCES auth.users(id),
content text NOT NULL,
is_read boolean DEFAULT false,
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- 3. Enable RLS
ALTER TABLE public.rentals ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.messages ENABLE ROW LEVEL SECURITY;
-- 4. Rental Policies
DROP POLICY IF EXISTS "Users can view own rentals" ON public.rentals;
CREATE POLICY "Users can view own rentals" ON public.rentals
FOR SELECT USING (auth.uid() = renter_id OR auth.uid() = owner_id);
DROP POLICY IF EXISTS "Users can insert rentals" ON public.rentals;
CREATE POLICY "Users can insert rentals" ON public.rentals
FOR INSERT WITH CHECK (auth.uid() = renter_id);
DROP POLICY IF EXISTS "Users can update own rentals" ON public.rentals;
CREATE POLICY "Users can update own rentals" ON public.rentals
FOR UPDATE USING (auth.uid() = renter_id OR auth.uid() = owner_id);
-- 5. Message Policies
DROP POLICY IF EXISTS "Users can view own messages" ON public.messages;
CREATE POLICY "Users can view own messages" ON public.messages
FOR SELECT USING (auth.uid() = sender_id OR auth.uid() = receiver_id);
DROP POLICY IF EXISTS "Users can insert messages" ON public.messages;
CREATE POLICY "Users can insert messages" ON public.messages
FOR INSERT WITH CHECK (auth.uid() = sender_id);