-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsupabase-setup.sql
More file actions
51 lines (44 loc) · 1.82 KB
/
supabase-setup.sql
File metadata and controls
51 lines (44 loc) · 1.82 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
-- Create orders table
CREATE TABLE IF NOT EXISTS orders (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_wallet TEXT NOT NULL,
user_email TEXT,
items JSONB NOT NULL,
total_amount DECIMAL(20, 6) NOT NULL,
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'processing', 'shipped', 'delivered', 'cancelled')),
transaction_signature TEXT,
tracking_number TEXT,
estimated_delivery TEXT,
shipping_address JSONB,
notes TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create index for faster queries
CREATE INDEX IF NOT EXISTS idx_orders_user_wallet ON orders(user_wallet);
CREATE INDEX IF NOT EXISTS idx_orders_status ON orders(status);
CREATE INDEX IF NOT EXISTS idx_orders_created_at ON orders(created_at);
-- Enable Row Level Security (RLS)
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
-- Create policy to allow users to view their own orders
CREATE POLICY "Users can view their own orders" ON orders
FOR SELECT USING (auth.jwt() ->> 'wallet_address' = user_wallet);
-- Create policy to allow users to create their own orders
CREATE POLICY "Users can create their own orders" ON orders
FOR INSERT WITH CHECK (auth.jwt() ->> 'wallet_address' = user_wallet);
-- Create policy to allow admins to view all orders (you can modify this based on your admin logic)
CREATE POLICY "Admins can view all orders" ON orders
FOR ALL USING (auth.jwt() ->> 'role' = 'admin');
-- Create function to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
-- Create trigger to automatically update updated_at
CREATE TRIGGER update_orders_updated_at
BEFORE UPDATE ON orders
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();