Spaces:
Runtime error
Runtime error
File size: 13,257 Bytes
cd86722 | 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | -- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Create categories table
CREATE TABLE IF NOT EXISTS public.categories (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
name text NOT NULL,
type text DEFAULT 'delivery'::text,
created_at timestamp with time zone DEFAULT now(),
display_order integer DEFAULT 0,
CONSTRAINT categories_pkey PRIMARY KEY (id),
UNIQUE(name, type)
);
-- Create products table
CREATE TABLE IF NOT EXISTS public.products (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
category_id uuid,
name text NOT NULL,
description text,
image_url text,
is_in_stock boolean DEFAULT true,
type text DEFAULT 'delivery'::text,
created_at timestamp with time zone DEFAULT now(),
display_order integer DEFAULT 0,
fictitious_likes integer DEFAULT 0,
real_likes_count integer DEFAULT 0,
CONSTRAINT products_pkey PRIMARY KEY (id),
CONSTRAINT products_category_id_fkey FOREIGN KEY (category_id) REFERENCES public.categories(id) ON DELETE CASCADE
);
-- Create product_likes table to track real likes by user
CREATE TABLE IF NOT EXISTS public.product_likes (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
product_id uuid,
user_telegram_id text NOT NULL,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT product_likes_pkey PRIMARY KEY (id),
CONSTRAINT product_likes_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE,
UNIQUE(product_id, user_telegram_id)
);
-- Enable RLS on product_likes
ALTER TABLE public.product_likes ENABLE ROW LEVEL SECURITY;
-- Policies for product_likes
DROP POLICY IF EXISTS "Allow public read on product_likes" ON public.product_likes;
CREATE POLICY "Allow public read on product_likes" ON public.product_likes FOR SELECT USING (true);
DROP POLICY IF EXISTS "Allow all for admin on product_likes" ON public.product_likes;
CREATE POLICY "Allow all for admin on product_likes" ON public.product_likes FOR ALL USING (true) WITH CHECK (true);
-- Function to update real_likes_count on products table
CREATE OR REPLACE FUNCTION public.update_product_likes_count()
RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'INSERT') THEN
UPDATE public.products SET real_likes_count = real_likes_count + 1 WHERE id = NEW.product_id;
ELSIF (TG_OP = 'DELETE') THEN
UPDATE public.products SET real_likes_count = real_likes_count - 1 WHERE id = OLD.product_id;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public;
-- Trigger to call the function on insert or delete
DROP TRIGGER IF EXISTS on_product_like ON public.product_likes;
CREATE TRIGGER on_product_like
AFTER INSERT OR DELETE ON public.product_likes
FOR EACH ROW EXECUTE FUNCTION public.update_product_likes_count();
-- Create pricing table
CREATE TABLE IF NOT EXISTS public.product_prices (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
product_id uuid,
quantity text NOT NULL,
price numeric NOT NULL,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT product_prices_pkey PRIMARY KEY (id),
CONSTRAINT product_prices_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE
);
-- Create shop settings table
CREATE TABLE IF NOT EXISTS public.shop_settings (
id integer NOT NULL,
name text DEFAULT 'Ma Boutique'::text,
description text DEFAULT ''::text,
logo_url text DEFAULT ''::text,
banner_url text DEFAULT ''::text,
order_manager text DEFAULT ''::text,
channel_url text DEFAULT 'https://t.me/hftfrance'::text,
manager_chat_id text DEFAULT ''::text,
is_open boolean DEFAULT true,
is_meetup_enabled boolean DEFAULT false,
is_verification_enabled boolean DEFAULT false,
is_auto_broadcast_enabled boolean DEFAULT true,
order_dispatch_mode text DEFAULT 'private'::text,
delivery_group_id text DEFAULT ''::text,
meetup_group_id text DEFAULT ''::text,
last_opened_at timestamp with time zone DEFAULT now(),
welcome_message text DEFAULT ''::text,
info_text text DEFAULT ''::text,
accent_color text DEFAULT '#10B981'::text,
admin_id text DEFAULT ''::text,
CONSTRAINT shop_settings_pkey PRIMARY KEY (id)
);
-- Insert default settings if not exists
INSERT INTO public.shop_settings (id, name, accent_color)
VALUES (1, 'Ma Boutique', '#10B981')
ON CONFLICT (id) DO NOTHING;
-- Create bot_users table
CREATE TABLE IF NOT EXISTS public.bot_users (
id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
telegram_id text NOT NULL UNIQUE,
username text,
first_name text,
phone text,
is_verified boolean DEFAULT false,
verification_status text DEFAULT 'none'::text,
referral_info text,
last_seen timestamp with time zone DEFAULT now(),
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT bot_users_pkey PRIMARY KEY (id)
);
-- Create drivers table
CREATE TABLE IF NOT EXISTS public.drivers (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
telegram_id text NOT NULL UNIQUE,
username text,
first_name text,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT drivers_pkey PRIMARY KEY (id)
);
-- Create meetup_workers table
CREATE TABLE IF NOT EXISTS public.meetup_workers (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
telegram_id text NOT NULL UNIQUE,
username text,
first_name text,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT meetup_workers_pkey PRIMARY KEY (id)
);
-- Create orders table
CREATE TABLE IF NOT EXISTS public.orders (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
client_telegram_id text,
client_name text,
total numeric,
address text,
items jsonb,
status text DEFAULT 'pending'::text,
type text DEFAULT 'delivery'::text,
driver_id text,
completed_by_name text,
collected_amount numeric,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT orders_pkey PRIMARY KEY (id)
);
-- Create order_notifications table
CREATE TABLE IF NOT EXISTS public.order_notifications (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
order_id uuid,
driver_id text,
message_id integer,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT order_notifications_pkey PRIMARY KEY (id),
CONSTRAINT order_notifications_order_id_fkey FOREIGN KEY (order_id) REFERENCES public.orders(id) ON DELETE CASCADE
);
-- Create integrated_chats table
CREATE TABLE IF NOT EXISTS public.integrated_chats (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
chat_id text NOT NULL UNIQUE,
chat_title text,
chat_type text,
added_at timestamp with time zone DEFAULT now(),
CONSTRAINT integrated_chats_pkey PRIMARY KEY (id)
);
-- ==========================================
-- SECURITY CONFIGURATION (RLS & POLICIES)
-- ==========================================
-- 1. ENABLE ROW LEVEL SECURITY ON ALL TABLES
ALTER TABLE IF EXISTS public.categories ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS public.products ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS public.product_prices ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS public.shop_settings ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS public.bot_users ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS public.drivers ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS public.meetup_workers ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS public.orders ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS public.order_notifications ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS public.integrated_chats ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS public.product_likes ENABLE ROW LEVEL SECURITY;
-- 2. RADICAL CLEANUP: Remove all existing policies to start fresh
DO $$
DECLARE
pol RECORD;
BEGIN
FOR pol IN (
SELECT policyname, tablename
FROM pg_policies
WHERE schemaname = 'public'
AND tablename IN (
'bot_users', 'orders', 'product_likes', 'shop_settings',
'categories', 'products', 'product_prices', 'integrated_chats',
'order_notifications', 'drivers', 'meetup_workers'
)
) LOOP
EXECUTE format('DROP POLICY IF EXISTS %I ON public.%I', pol.policyname, pol.tablename);
END LOOP;
END $$;
-- 3. CREATE SECURE POLICIES (Strict Security: No public SELECT on sensitive data)
-- Public Read Access (Non-sensitive data only)
CREATE POLICY "webapp_select_categories" ON public.categories FOR SELECT TO anon, authenticated, service_role USING (id IS NOT NULL);
CREATE POLICY "webapp_select_products" ON public.products FOR SELECT TO anon, authenticated, service_role USING (id IS NOT NULL);
CREATE POLICY "webapp_select_prices" ON public.product_prices FOR SELECT TO anon, authenticated, service_role USING (id IS NOT NULL);
CREATE POLICY "webapp_select_settings" ON public.shop_settings FOR SELECT TO anon, authenticated, service_role USING (id IS NOT NULL);
-- Shop Settings Update (Allow Mini App for Admin/Manager identification)
CREATE POLICY "webapp_update_settings" ON public.shop_settings FOR UPDATE TO anon, authenticated, service_role USING (id IS NOT NULL);
-- Sensitive Tables: NO SELECT for anon (Public)
-- Only service_role can SELECT. anon can only INSERT/UPDATE where necessary.
-- Bot Users: anon can only register or update their own last_seen (via checkAuth)
CREATE POLICY "webapp_insert_users" ON public.bot_users FOR INSERT TO anon, authenticated, service_role WITH CHECK (id IS NOT NULL);
CREATE POLICY "webapp_update_users" ON public.bot_users FOR UPDATE TO anon, authenticated, service_role USING (id IS NOT NULL);
-- No SELECT policy for anon on bot_users -> Dump impossible.
-- Orders: anon can only create orders
CREATE POLICY "webapp_insert_orders" ON public.orders FOR INSERT TO anon, authenticated, service_role WITH CHECK (id IS NOT NULL);
-- No SELECT policy for anon on orders -> Dump impossible.
-- Product Likes Management (Publicly visible counts, but restricted management)
CREATE POLICY "webapp_select_likes" ON public.product_likes FOR SELECT TO anon, authenticated, service_role USING (id IS NOT NULL);
CREATE POLICY "webapp_insert_likes" ON public.product_likes FOR INSERT TO anon, authenticated, service_role WITH CHECK (id IS NOT NULL);
CREATE POLICY "webapp_delete_likes" ON public.product_likes FOR DELETE TO anon, authenticated, service_role USING (id IS NOT NULL);
-- Service Tables: Strictly locked to service_role (Resolves "RLS Enabled No Policy" suggestions)
-- No policies for anon -> Total protection.
CREATE POLICY "service_role_only_drivers" ON public.drivers FOR ALL TO service_role USING (true);
CREATE POLICY "service_role_only_workers" ON public.meetup_workers FOR ALL TO service_role USING (true);
CREATE POLICY "service_role_only_chats" ON public.integrated_chats FOR ALL TO service_role USING (true);
CREATE POLICY "service_role_only_notifs" ON public.order_notifications FOR ALL TO service_role USING (true);
-- 4. GRANT PERMISSIONS
-- service_role (The Bot) bypasses RLS but needs explicit grants for schema usage
GRANT USAGE ON SCHEMA public TO service_role;
GRANT ALL ON ALL TABLES IN SCHEMA public TO service_role;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO service_role;
GRANT ALL ON ALL FUNCTIONS IN SCHEMA public TO service_role;
-- authenticated role
GRANT USAGE ON SCHEMA public TO authenticated;
GRANT ALL ON ALL TABLES IN SCHEMA public TO authenticated;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO authenticated;
-- anon role (Mini App) - Strictly limited to functional needs
GRANT USAGE ON SCHEMA public TO anon;
GRANT INSERT, UPDATE ON public.bot_users TO anon; -- No SELECT
GRANT SELECT ON public.categories TO anon;
GRANT SELECT ON public.products TO anon;
GRANT SELECT ON public.product_prices TO anon;
GRANT SELECT, UPDATE ON public.shop_settings TO anon;
GRANT INSERT ON public.orders TO anon; -- No SELECT
GRANT SELECT, INSERT, DELETE ON public.product_likes TO anon;
-- Ensure future tables also get these permissions for service_role
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO service_role;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO service_role;
-- ==========================================
-- STORAGE CONFIGURATION (Buckets & Policies)
-- ==========================================
-- Create the bucket for images if it doesn't exist
INSERT INTO storage.buckets (id, name, public)
VALUES ('product-images', 'product-images', true)
ON CONFLICT (id) DO NOTHING;
-- Storage Policies
DROP POLICY IF EXISTS "Public Read Access" ON storage.objects;
CREATE POLICY "Public Read Access" ON storage.objects FOR SELECT TO anon, authenticated, service_role USING (bucket_id = 'product-images');
DROP POLICY IF EXISTS "Public Upload Access" ON storage.objects;
CREATE POLICY "Public Upload Access" ON storage.objects FOR INSERT TO anon, authenticated, service_role WITH CHECK (bucket_id = 'product-images');
DROP POLICY IF EXISTS "Public Update Access" ON storage.objects;
CREATE POLICY "Public Update Access" ON storage.objects FOR UPDATE TO anon, authenticated, service_role USING (bucket_id = 'product-images');
DROP POLICY IF EXISTS "Public Delete Access" ON storage.objects;
CREATE POLICY "Public Delete Access" ON storage.objects FOR DELETE TO anon, authenticated, service_role USING (bucket_id = 'product-images');
|