Repository navigation
Expand file tree
/
Copy pathcreate-notifications-table.sql
More file actions
43 lines (35 loc) · 1.86 KB
/
Copy pathcreate-notifications-table.sql
File metadata and controls
43 lines (35 loc) · 1.86 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
-- Create the notifications table that the useNotifications hook expects
CREATE TABLE IF NOT EXISTS notifications (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
type VARCHAR(50) NOT NULL CHECK (type IN ('submission_feedback', 'partner_activity', 'clarity_call', 'achievement', 'general')),
title VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
read BOOLEAN DEFAULT FALSE,
data JSONB,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_notifications_user_id ON notifications(user_id);
CREATE INDEX IF NOT EXISTS idx_notifications_read ON notifications(read);
CREATE INDEX IF NOT EXISTS idx_notifications_created_at ON notifications(created_at);
CREATE INDEX IF NOT EXISTS idx_notifications_user_read ON notifications(user_id, read);
-- Enable RLS
ALTER TABLE notifications ENABLE ROW LEVEL SECURITY;
-- RLS Policies for notifications
DROP POLICY IF EXISTS "Users can view their own notifications" ON notifications;
CREATE POLICY "Users can view their own notifications" ON notifications
FOR SELECT USING (auth.uid() = user_id);
DROP POLICY IF EXISTS "Users can update their own notifications" ON notifications;
CREATE POLICY "Users can update their own notifications" ON notifications
FOR UPDATE USING (auth.uid() = user_id);
DROP POLICY IF EXISTS "System can create notifications" ON notifications;
CREATE POLICY "System can create notifications" ON notifications
FOR INSERT WITH CHECK (true);
DROP POLICY IF EXISTS "Admins can manage all notifications" ON notifications;
CREATE POLICY "Admins can manage all notifications" ON notifications
FOR ALL USING (
auth.jwt() ->> 'email' LIKE '%admin%' OR
(auth.jwt() ->> 'user_metadata')::jsonb ->> 'role' = 'admin'
);
SELECT 'NOTIFICATIONS TABLE CREATED SUCCESSFULLY' as status;