buzzthedev

Untitled

Sep 30th, 2024 (edited)
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PostgreSQL 16.97 KB | Software | 0 0
  1. create table
  2.   public.panel_settings (
  3.     display_name text not null default 'User'::text,
  4.     terms text null default '<ul><li>No refunds given</li></ul>'::text,
  5.     id uuid not null default gen_random_uuid (),
  6.     discord text null default ''::text,
  7.     logo text not null default '/default.svg'::text,
  8.     avatar_url text null default '/logo.svg'::text,
  9.     created_at timestamp with time zone null default now(),
  10.     api_key uuid null default gen_random_uuid (),
  11.     api_uses_left smallint not null default '500'::smallint,
  12.     license_active boolean not null default false,
  13.     subscription_id text null,
  14.     settings JSONB DEFAULT '{"currency_prefix": "$"}',
  15.     psk TEXT,
  16.     sk TEXT,
  17.     constraint panel_settings_pkey primary key (id),
  18.     constraint panel_settings_api_key_key unique (api_key),
  19.     constraint panel_settings_discord_check check ((length(discord) < 30)),
  20.     constraint panel_settings_display_name_check check ((length(display_name) < 100)),
  21.     constraint panel_settings_logo_check check ((length(logo) < 500)),
  22.     constraint panel_settings_terms_check check ((length(terms) < 500)),
  23.     constraint panel_settings_uses_left_check check ((api_uses_left < 5000)),
  24.     constraint panel_settings_avatar_url_check check ((length(avatar_url) < 1000))
  25.   ) tablespace pg_default;
  26.  
  27. create table
  28.   public.panel_clients (
  29.     id uuid not null default gen_random_uuid (),
  30.     created_at timestamp with time zone not null default now(),
  31.     username text not null default ''::text,
  32.     discord text null,
  33.     email text null,
  34.     password text null,
  35.     avatar_url text null default ''::text,
  36.     panel_id uuid not null default auth.uid (),
  37.     stripe_id text null,
  38.     constraint panel_clients_pkey primary key (id),
  39.     constraint panel_clients_panel_id_fkey foreign key (panel_id) references panel_settings (id) on update cascade on delete cascade,
  40.     constraint panel_clients_avatar_url_check check ((length(avatar_url) < 10000)),
  41.     constraint panel_clients_discord_check check ((length(discord) < 30)),
  42.     constraint panel_clients_email_check check ((length(email) < 50)),
  43.     constraint panel_clients_username_check check ((length(username) < 30))
  44.   ) tablespace pg_default;
  45.  
  46. create table
  47.   public.notifications (
  48.     id bigint generated by default as identity,
  49.     created_at timestamp with time zone not null default now(),
  50.     panel_id uuid not null default auth.uid (),
  51.     title text null default 'New notification'::text,
  52.     message text null,
  53.     subject uuid null default gen_random_uuid (),
  54.     link text not null default '/notifications'::text,
  55.     read boolean null default false,
  56.     constraint notifications_pkey primary key (id),
  57.     constraint public_notifications_panel_id_fkey foreign key (panel_id) references panel_settings (id) on update cascade on delete cascade,
  58.     constraint public_notifications_subject_fkey foreign key (subject) references panel_clients (id) on update cascade on delete cascade
  59.   ) tablespace pg_default;
  60.  
  61. create table
  62.   public.products (
  63.     id bigint generated by default as identity,
  64.     creator uuid not null default auth.uid (),
  65.     description text null default ''::text,
  66.     date_created timestamp with time zone not null default now(),
  67.     constraint products_pkey primary key (id),
  68.     constraint products_creator_fkey foreign key (creator) references panel_settings (id) on update cascade,
  69.     constraint products_description_check check ((length(description) < 36))
  70.   ) tablespace pg_default;
  71.  
  72. create table
  73.   public.panel_commissions (
  74.     created_at timestamp with time zone not null default now(),
  75.     title text null default 'New commission'::text,
  76.     client uuid null,
  77.     total_value numeric null default 0.00,
  78.     total_paid numeric null default 0.00,
  79.     start_date timestamp with time zone null default now(),
  80.     deadline timestamp with time zone null default now(),
  81.     status text not null default 'not_started'::text,
  82.     id uuid not null default gen_random_uuid (),
  83.     panel_id uuid not null default auth.uid (),
  84.     tracking_id uuid not null default gen_random_uuid (),
  85.     product bigint null,
  86.     pinned boolean null default false,
  87.     notes text null,
  88.     constraint panel_commissions_pkey primary key (id),
  89.     constraint panel_commissions_tracking_id_key unique (tracking_id),
  90.     constraint panel_commissions_panel_id_fkey foreign key (panel_id) references panel_settings (id) on update cascade on delete cascade,
  91.     constraint panel_commissions_client_fkey foreign key (client) references panel_clients (id) on update cascade on delete cascade,
  92.     constraint panel_commissions_product_fkey foreign key (product) references products (id) on update cascade on delete restrict,
  93.     constraint panel_commissions_notes_check check ((length(notes) < 1500)),
  94.     constraint panel_commissions_status_check check ((length(status) < 20))
  95.   ) tablespace pg_default;
  96.  
  97. create table
  98.   public.panel_quotes (
  99.     created_at timestamp with time zone not null default now(),
  100.     title text null default 'New quote'::text,
  101.     client uuid null,
  102.     proposed_amount integer null default 0,
  103.     start_date timestamp with time zone not null default now(),
  104.     deadline timestamp with time zone not null default now(),
  105.     payment_terms text not null default 'custom'::text,
  106.     status text not null default 'pending'::text,
  107.     id uuid not null default gen_random_uuid (),
  108.     panel_id uuid not null default auth.uid (),
  109.     constraint panel_quotes_pkey primary key (id),
  110.     constraint panel_quotes_client_fkey foreign key (client) references panel_clients (id) on update cascade on delete cascade,
  111.     constraint panel_quotes_panel_id_fkey foreign key (panel_id) references panel_settings (id) on update cascade on delete cascade,
  112.     constraint panel_quotes_proposed_amount_check check ((proposed_amount < 10000000))
  113.   ) tablespace pg_default;
  114.  
  115. create table
  116.   public.panel_requests (
  117.     created_at timestamp with time zone not null default now(),
  118.     description text not null,
  119.     offered_amount numeric null default 0.00,
  120.     deadline timestamp with time zone null,
  121.     status text not null default 'requested'::text,
  122.     id uuid not null default gen_random_uuid (),
  123.     panel_id uuid not null default auth.uid (),
  124.     commission uuid null,
  125.     paid boolean not null default false,
  126.     constraint panel_requests_pkey primary key (id),
  127.     constraint panel_requests_commission_fkey foreign key (commission) references panel_commissions (id) on update cascade on delete cascade,
  128.     constraint panel_requests_panel_id_fkey foreign key (panel_id) references panel_settings (id) on update cascade on delete cascade,
  129.     constraint panel_requests_description_check check ((length(description) > 0)),
  130.     constraint panel_requests_status_check check ((length(status) < 20))
  131.   ) tablespace pg_default;
  132.  
  133. create table
  134.   public.panel_views (
  135.     id bigint generated by default as identity,
  136.     data json not null,
  137.     panel_id uuid null default auth.uid (),
  138.     name text not null default 'New view'::text,
  139.     constraint panel_views_pkey primary key (id),
  140.     constraint panel_views_panel_id_fkey foreign key (panel_id) references panel_settings (id) on update cascade on delete cascade,
  141.     constraint panel_views_name_check check ((length(name) < 30))
  142.   ) tablespace pg_default;
  143.  
  144. create table
  145.   public.panel_invoices (
  146.     created_at timestamp with time zone not null default now(),
  147.     amount real null default '0'::real,
  148.     link text null,
  149.     paid_at timestamp with time zone null,
  150.     client uuid null,
  151.     title text null,
  152.     pinned boolean null default false,
  153.     panel_id uuid null default auth.uid (),
  154.     id uuid not null default gen_random_uuid (),
  155.     due_date timestamp with time zone null,
  156.     status text null default 'unpaid'::text,
  157.     stripe_invoice_id text null,
  158.     memo text null,
  159.     type TEXT,
  160.     commission UUID,
  161.     constraint panel_invoices_pkey primary key (id),
  162.     constraint panel_invoices_id_key unique (id),
  163.     constraint panel_invoices_client_fkey foreign key (client) references panel_clients (id),
  164.     constraint panel_invoices_panel_id_fkey foreign key (panel_id) references panel_settings (id) on update cascade on delete cascade,
  165.     constraint fk_commission foreign key (commission) references panel_commissions (id)
  166.   ) tablespace pg_default;
  167.  
  168. ALTER TABLE public.panel_settings ENABLE ROW LEVEL SECURITY;
  169. ALTER TABLE public.panel_clients ENABLE ROW LEVEL SECURITY;
  170. ALTER TABLE public.panel_commissions ENABLE ROW LEVEL SECURITY;
  171. ALTER TABLE public.panel_quotes ENABLE ROW LEVEL SECURITY;
  172. ALTER TABLE public.panel_requests ENABLE ROW LEVEL SECURITY;
  173. ALTER TABLE public.panel_invoices ENABLE ROW LEVEL SECURITY;
  174. ALTER TABLE public.notifications ENABLE ROW LEVEL SECURITY;
  175. ALTER TABLE public.products ENABLE ROW LEVEL SECURITY;
  176. ALTER TABLE public.panel_views ENABLE ROW LEVEL SECURITY;
  177.  
  178. DROP POLICY IF EXISTS panel_settings_select ON public.panel_settings;
  179. DROP POLICY IF EXISTS panel_settings_insert ON public.panel_settings;
  180. DROP POLICY IF EXISTS panel_settings_update ON public.panel_settings;
  181.  
  182. CREATE POLICY panel_settings_select
  183. ON public.panel_settings
  184. FOR SELECT
  185. TO authenticated
  186. USING (id = (SELECT auth.uid()));
  187.  
  188. CREATE POLICY panel_settings_insert
  189. ON public.panel_settings
  190. FOR INSERT
  191. TO authenticated
  192. WITH CHECK (
  193.   id = (SELECT auth.uid())
  194.   AND NOT EXISTS (
  195.     SELECT 1
  196.     FROM public.panel_clients pc
  197.     WHERE pc.id = (SELECT auth.uid())
  198.   )
  199. );
  200.  
  201. CREATE POLICY panel_settings_update
  202. ON public.panel_settings
  203. FOR UPDATE
  204. TO authenticated
  205. USING (id = (SELECT auth.uid()))
  206. WITH CHECK (id = (SELECT auth.uid()));
  207.  
  208. DROP POLICY IF EXISTS panel_clients_select ON public.panel_clients;
  209. DROP POLICY IF EXISTS panel_clients_update ON public.panel_clients;
  210.  
  211. CREATE POLICY panel_clients_select
  212. ON public.panel_clients
  213. FOR SELECT
  214. TO authenticated
  215. USING (
  216.   panel_id = (SELECT auth.uid())
  217.   OR id = (SELECT auth.uid())
  218. );
  219.  
  220. CREATE POLICY panel_clients_update
  221. ON public.panel_clients
  222. FOR UPDATE
  223. TO authenticated
  224. USING (panel_id = (SELECT auth.uid()))
  225. WITH CHECK (panel_id = (SELECT auth.uid()));
  226.  
  227. DROP POLICY IF EXISTS panel_commissions_select ON public.panel_commissions;
  228. DROP POLICY IF EXISTS panel_commissions_insert ON public.panel_commissions;
  229. DROP POLICY IF EXISTS panel_commissions_update ON public.panel_commissions;
  230. DROP POLICY IF EXISTS panel_commissions_delete ON public.panel_commissions;
  231.  
  232. CREATE POLICY panel_commissions_select
  233. ON public.panel_commissions
  234. FOR SELECT
  235. TO authenticated
  236. USING (
  237.   panel_id = (SELECT auth.uid())
  238.   OR client = (SELECT auth.uid())
  239. );
  240.  
  241. CREATE POLICY panel_commissions_insert
  242. ON public.panel_commissions
  243. FOR INSERT
  244. TO authenticated
  245. WITH CHECK (
  246.   panel_id = (SELECT auth.uid())
  247.   AND (
  248.     client IS NULL
  249.     OR EXISTS (
  250.       SELECT 1
  251.       FROM public.panel_clients pc
  252.       WHERE pc.id = client
  253.         AND pc.panel_id = (SELECT auth.uid())
  254.     )
  255.   )
  256.   AND (
  257.     product IS NULL
  258.     OR EXISTS (
  259.       SELECT 1
  260.       FROM public.products p
  261.       WHERE p.id = product
  262.         AND p.creator = (SELECT auth.uid())
  263.     )
  264.   )
  265. );
  266.  
  267. CREATE POLICY panel_commissions_update
  268. ON public.panel_commissions
  269. FOR UPDATE
  270. TO authenticated
  271. USING (panel_id = (SELECT auth.uid()))
  272. WITH CHECK (
  273.   panel_id = (SELECT auth.uid())
  274.   AND (
  275.     client IS NULL
  276.     OR EXISTS (
  277.       SELECT 1
  278.       FROM public.panel_clients pc
  279.       WHERE pc.id = client
  280.         AND pc.panel_id = (SELECT auth.uid())
  281.     )
  282.   )
  283.   AND (
  284.     product IS NULL
  285.     OR EXISTS (
  286.       SELECT 1
  287.       FROM public.products p
  288.       WHERE p.id = product
  289.         AND p.creator = (SELECT auth.uid())
  290.     )
  291.   )
  292. );
  293.  
  294. CREATE POLICY panel_commissions_delete
  295. ON public.panel_commissions
  296. FOR DELETE
  297. TO authenticated
  298. USING (panel_id = (SELECT auth.uid()));
  299.  
  300. DROP POLICY IF EXISTS panel_quotes_select ON public.panel_quotes;
  301. DROP POLICY IF EXISTS panel_quotes_insert ON public.panel_quotes;
  302. DROP POLICY IF EXISTS panel_quotes_update ON public.panel_quotes;
  303. DROP POLICY IF EXISTS panel_quotes_delete ON public.panel_quotes;
  304.  
  305. CREATE POLICY panel_quotes_select
  306. ON public.panel_quotes
  307. FOR SELECT
  308. TO authenticated
  309. USING (
  310.   panel_id = (SELECT auth.uid())
  311.   OR client = (SELECT auth.uid())
  312. );
  313.  
  314. CREATE POLICY panel_quotes_insert
  315. ON public.panel_quotes
  316. FOR INSERT
  317. TO authenticated
  318. WITH CHECK (
  319.   panel_id = (SELECT auth.uid())
  320.   AND (
  321.     client IS NULL
  322.     OR EXISTS (
  323.       SELECT 1
  324.       FROM public.panel_clients pc
  325.       WHERE pc.id = client
  326.         AND pc.panel_id = (SELECT auth.uid())
  327.     )
  328.   )
  329. );
  330.  
  331. CREATE POLICY panel_quotes_update
  332. ON public.panel_quotes
  333. FOR UPDATE
  334. TO authenticated
  335. USING (panel_id = (SELECT auth.uid()))
  336. WITH CHECK (
  337.   panel_id = (SELECT auth.uid())
  338.   AND (
  339.     client IS NULL
  340.     OR EXISTS (
  341.       SELECT 1
  342.       FROM public.panel_clients pc
  343.       WHERE pc.id = client
  344.         AND pc.panel_id = (SELECT auth.uid())
  345.     )
  346.   )
  347. );
  348.  
  349. CREATE POLICY panel_quotes_delete
  350. ON public.panel_quotes
  351. FOR DELETE
  352. TO authenticated
  353. USING (panel_id = (SELECT auth.uid()));
  354.  
  355. DROP POLICY IF EXISTS panel_requests_select ON public.panel_requests;
  356. DROP POLICY IF EXISTS panel_requests_insert ON public.panel_requests;
  357. DROP POLICY IF EXISTS panel_requests_update ON public.panel_requests;
  358. DROP POLICY IF EXISTS panel_requests_delete ON public.panel_requests;
  359.  
  360. CREATE POLICY panel_requests_select
  361. ON public.panel_requests
  362. FOR SELECT
  363. TO authenticated
  364. USING (
  365.   panel_id = (SELECT auth.uid())
  366.   OR EXISTS (
  367.     SELECT 1
  368.     FROM public.panel_commissions c
  369.     WHERE c.id = commission
  370.       AND c.client = (SELECT auth.uid())
  371.   )
  372. );
  373.  
  374. CREATE POLICY panel_requests_insert
  375. ON public.panel_requests
  376. FOR INSERT
  377. TO authenticated
  378. WITH CHECK (
  379.   panel_id = (SELECT auth.uid())
  380.   AND (
  381.     commission IS NULL
  382.     OR EXISTS (
  383.       SELECT 1
  384.       FROM public.panel_commissions c
  385.       WHERE c.id = commission
  386.         AND c.panel_id = (SELECT auth.uid())
  387.     )
  388.   )
  389. );
  390.  
  391. CREATE POLICY panel_requests_update
  392. ON public.panel_requests
  393. FOR UPDATE
  394. TO authenticated
  395. USING (panel_id = (SELECT auth.uid()))
  396. WITH CHECK (
  397.   panel_id = (SELECT auth.uid())
  398.   AND (
  399.     commission IS NULL
  400.     OR EXISTS (
  401.       SELECT 1
  402.       FROM public.panel_commissions c
  403.       WHERE c.id = commission
  404.         AND c.panel_id = (SELECT auth.uid())
  405.     )
  406.   )
  407. );
  408.  
  409. CREATE POLICY panel_requests_delete
  410. ON public.panel_requests
  411. FOR DELETE
  412. TO authenticated
  413. USING (panel_id = (SELECT auth.uid()));
  414.  
  415. DROP POLICY IF EXISTS panel_invoices_select ON public.panel_invoices;
  416. DROP POLICY IF EXISTS panel_invoices_update ON public.panel_invoices;
  417. DROP POLICY IF EXISTS panel_invoices_delete ON public.panel_invoices;
  418.  
  419. CREATE POLICY panel_invoices_select
  420. ON public.panel_invoices
  421. FOR SELECT
  422. TO authenticated
  423. USING (
  424.   panel_id = (SELECT auth.uid())
  425.   OR client = (SELECT auth.uid())
  426. );
  427.  
  428. CREATE POLICY panel_invoices_update
  429. ON public.panel_invoices
  430. FOR UPDATE
  431. TO authenticated
  432. USING (panel_id = (SELECT auth.uid()))
  433. WITH CHECK (panel_id = (SELECT auth.uid()));
  434.  
  435. CREATE POLICY panel_invoices_delete
  436. ON public.panel_invoices
  437. FOR DELETE
  438. TO authenticated
  439. USING (panel_id = (SELECT auth.uid()));
  440.  
  441. DROP POLICY IF EXISTS notifications_select ON public.notifications;
  442. DROP POLICY IF EXISTS notifications_update ON public.notifications;
  443. DROP POLICY IF EXISTS notifications_delete ON public.notifications;
  444.  
  445. CREATE POLICY notifications_select
  446. ON public.notifications
  447. FOR SELECT
  448. TO authenticated
  449. USING (panel_id = (SELECT auth.uid()));
  450.  
  451. CREATE POLICY notifications_update
  452. ON public.notifications
  453. FOR UPDATE
  454. TO authenticated
  455. USING (panel_id = (SELECT auth.uid()))
  456. WITH CHECK (panel_id = (SELECT auth.uid()));
  457.  
  458. CREATE POLICY notifications_delete
  459. ON public.notifications
  460. FOR DELETE
  461. TO authenticated
  462. USING (panel_id = (SELECT auth.uid()));
  463.  
  464. DROP POLICY IF EXISTS products_select ON public.products;
  465. DROP POLICY IF EXISTS products_insert ON public.products;
  466. DROP POLICY IF EXISTS products_update ON public.products;
  467. DROP POLICY IF EXISTS products_delete ON public.products;
  468.  
  469. CREATE POLICY products_select
  470. ON public.products
  471. FOR SELECT
  472. TO authenticated
  473. USING (creator = (SELECT auth.uid()));
  474.  
  475. CREATE POLICY products_insert
  476. ON public.products
  477. FOR INSERT
  478. TO authenticated
  479. WITH CHECK (creator = (SELECT auth.uid()));
  480.  
  481. CREATE POLICY products_update
  482. ON public.products
  483. FOR UPDATE
  484. TO authenticated
  485. USING (creator = (SELECT auth.uid()))
  486. WITH CHECK (creator = (SELECT auth.uid()));
  487.  
  488. CREATE POLICY products_delete
  489. ON public.products
  490. FOR DELETE
  491. TO authenticated
  492. USING (creator = (SELECT auth.uid()));
  493.  
  494. DROP POLICY IF EXISTS panel_views_select ON public.panel_views;
  495. DROP POLICY IF EXISTS panel_views_insert ON public.panel_views;
  496. DROP POLICY IF EXISTS panel_views_delete ON public.panel_views;
  497.  
  498. CREATE POLICY panel_views_select
  499. ON public.panel_views
  500. FOR SELECT
  501. TO authenticated
  502. USING (panel_id = (SELECT auth.uid()));
  503.  
  504. CREATE POLICY panel_views_insert
  505. ON public.panel_views
  506. FOR INSERT
  507. TO authenticated
  508. WITH CHECK (panel_id = (SELECT auth.uid()));
  509.  
  510. CREATE POLICY panel_views_delete
  511. ON public.panel_views
  512. FOR DELETE
  513. TO authenticated
  514. USING (panel_id = (SELECT auth.uid()));
  515.  
Advertisement
Add Comment
Please, Sign In to add comment