-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_schema.sql
More file actions
97 lines (83 loc) · 2.96 KB
/
supabase_schema.sql
File metadata and controls
97 lines (83 loc) · 2.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
-- Create users table for storing user stats (if not exists)
CREATE TABLE IF NOT EXISTS user_stats (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
total_points INTEGER DEFAULT 0,
items_saved INTEGER DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id)
);
-- Create food_items table for storing user's food items (if not exists)
CREATE TABLE IF NOT EXISTS food_items (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
category TEXT DEFAULT 'User Added',
quantity TEXT DEFAULT '1 item',
expiry_date DATE,
status TEXT DEFAULT 'fresh' CHECK (status IN ('fresh', 'expiring', 'expired')),
ai_suggestion TEXT,
points INTEGER DEFAULT 50,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Enable Row Level Security
ALTER TABLE user_stats ENABLE ROW LEVEL SECURITY;
ALTER TABLE food_items ENABLE ROW LEVEL SECURITY;
-- Create policies for user_stats
CREATE POLICY "Users can view own stats" ON user_stats
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own stats" ON user_stats
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own stats" ON user_stats
FOR UPDATE USING (auth.uid() = user_id);
-- Create policies for food_items
CREATE POLICY "Users can view own food items" ON food_items
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own food items" ON food_items
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own food items" ON food_items
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own food items" ON food_items
FOR DELETE USING (auth.uid() = user_id);
-- Create a view for public rankings (anonymized)
DROP VIEW IF EXISTS public_rankings;
CREATE VIEW public_rankings AS
SELECT
ROW_NUMBER() OVER (ORDER BY total_points DESC) AS rank,
total_points,
items_saved,
CASE
WHEN ROW_NUMBER() OVER (ORDER BY total_points DESC) <= 10 THEN 'Top Player'
ELSE 'Anonymous Player'
END AS display_name
FROM user_stats
WHERE total_points > 0
ORDER BY total_points DESC;
-- Enable public access to rankings view
GRANT SELECT ON public_rankings TO anon, authenticated;
-- Create function to get user rank
CREATE OR REPLACE FUNCTION get_user_rank(user_uuid UUID)
RETURNS TEXT AS $$
DECLARE
user_rank INTEGER;
BEGIN
SELECT rank INTO user_rank
FROM (
SELECT
user_id,
ROW_NUMBER() OVER (ORDER BY total_points DESC) AS rank
FROM user_stats
WHERE total_points > 0
) ranked_users
WHERE user_id = user_uuid;
IF user_rank IS NULL THEN
RETURN 'Unranked';
ELSE
RETURN user_rank::TEXT;
END IF;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Grant execute permission
GRANT EXECUTE ON FUNCTION get_user_rank(UUID) TO authenticated;