Initial independent version of CrAPP

This commit is contained in:
Ivan Cacciari
2026-08-06 08:36:47 +02:00
commit 330669176d
167 changed files with 24096 additions and 0 deletions
@@ -0,0 +1,33 @@
CREATE TABLE public.turni_palloni (
evento_id text PRIMARY KEY,
giocatore_id text NOT NULL,
aggiornato_da text,
aggiornato_il timestamptz NOT NULL DEFAULT now()
);
GRANT SELECT, INSERT, UPDATE, DELETE ON public.turni_palloni TO anon, authenticated;
GRANT ALL ON public.turni_palloni TO service_role;
ALTER TABLE public.turni_palloni ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Chiunque puo leggere i turni palloni" ON public.turni_palloni FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY "Chiunque puo creare i turni palloni" ON public.turni_palloni FOR INSERT TO anon, authenticated WITH CHECK (true);
CREATE POLICY "Chiunque puo modificare i turni palloni" ON public.turni_palloni FOR UPDATE TO anon, authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Chiunque puo eliminare i turni palloni" ON public.turni_palloni FOR DELETE TO anon, authenticated USING (true);
CREATE TABLE public.push_subscriptions (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
giocatore_id text NOT NULL,
endpoint text NOT NULL UNIQUE,
p256dh text NOT NULL,
auth text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
GRANT SELECT, INSERT, UPDATE, DELETE ON public.push_subscriptions TO anon, authenticated;
GRANT ALL ON public.push_subscriptions TO service_role;
ALTER TABLE public.push_subscriptions ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Chiunque puo iscriversi alle notifiche" ON public.push_subscriptions FOR INSERT TO anon, authenticated WITH CHECK (true);
CREATE POLICY "Chiunque puo aggiornare la propria iscrizione" ON public.push_subscriptions FOR UPDATE TO anon, authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Chiunque puo cancellare la propria iscrizione" ON public.push_subscriptions FOR DELETE TO anon, authenticated USING (true);
CREATE POLICY "Chiunque puo verificare le iscrizioni" ON public.push_subscriptions FOR SELECT TO anon, authenticated USING (true);
@@ -0,0 +1,2 @@
CREATE EXTENSION IF NOT EXISTS pg_cron;
CREATE EXTENSION IF NOT EXISTS pg_net;
@@ -0,0 +1,14 @@
CREATE TABLE public.scout_sessioni (
evento_id text PRIMARY KEY,
giocatore_id text NOT NULL,
giocatore_nome text NOT NULL,
aggiornato_il timestamp with time zone NOT NULL DEFAULT now()
);
GRANT SELECT, INSERT, UPDATE, DELETE ON public.scout_sessioni TO anon;
GRANT SELECT, INSERT, UPDATE, DELETE ON public.scout_sessioni TO authenticated;
GRANT ALL ON public.scout_sessioni TO service_role;
ALTER TABLE public.scout_sessioni ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Chiunque puo leggere le sessioni scout" ON public.scout_sessioni FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY "Chiunque puo aprire una sessione scout" ON public.scout_sessioni FOR INSERT TO anon, authenticated WITH CHECK (true);
CREATE POLICY "Chiunque puo aggiornare la sessione scout" ON public.scout_sessioni FOR UPDATE TO anon, authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Chiunque puo chiudere la sessione scout" ON public.scout_sessioni FOR DELETE TO anon, authenticated USING (true);
@@ -0,0 +1,33 @@
CREATE TABLE public.mvp_voti (
id uuid NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
match_id text NOT NULL,
votante_id text NOT NULL,
votato_id text NOT NULL,
votato_nome text NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now(),
UNIQUE (match_id, votante_id)
);
GRANT SELECT, INSERT, UPDATE, DELETE ON public.mvp_voti TO anon;
GRANT SELECT, INSERT, UPDATE, DELETE ON public.mvp_voti TO authenticated;
GRANT ALL ON public.mvp_voti TO service_role;
ALTER TABLE public.mvp_voti ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Chiunque puo leggere i voti MVP" ON public.mvp_voti FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY "Chiunque puo votare l MVP" ON public.mvp_voti FOR INSERT TO anon, authenticated WITH CHECK (true);
CREATE POLICY "Chiunque puo cambiare il proprio voto" ON public.mvp_voti FOR UPDATE TO anon, authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Chiunque puo togliere il proprio voto" ON public.mvp_voti FOR DELETE TO anon, authenticated USING (true);
CREATE OR REPLACE FUNCTION public.update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql SET search_path = public;
CREATE TRIGGER update_mvp_voti_updated_at
BEFORE UPDATE ON public.mvp_voti
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
@@ -0,0 +1,161 @@
CREATE TYPE public.app_role AS ENUM ('admin', 'user');
CREATE TABLE public.user_roles (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
role public.app_role NOT NULL,
UNIQUE (user_id, role)
);
GRANT SELECT ON public.user_roles TO authenticated;
GRANT ALL ON public.user_roles TO service_role;
CREATE OR REPLACE FUNCTION public.has_role(_user_id uuid, _role public.app_role)
RETURNS boolean
LANGUAGE sql
STABLE
SECURITY DEFINER
SET search_path = public
AS $$
SELECT EXISTS (
SELECT 1
FROM public.user_roles
WHERE user_id = _user_id AND role = _role
);
$$;
ALTER TABLE public.user_roles ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can view own roles" ON public.user_roles
FOR SELECT TO authenticated USING (user_id = auth.uid());
CREATE POLICY "Admins can manage all roles" ON public.user_roles
FOR ALL TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role))
WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));
CREATE TABLE public.giocatori (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
nome text NOT NULL,
numero integer NOT NULL,
ruolo text NOT NULL,
nascita date NOT NULL,
foto text,
auth_user_id uuid REFERENCES auth.users(id) ON DELETE SET NULL UNIQUE,
creato_il timestamptz NOT NULL DEFAULT now(),
aggiornato_il timestamptz NOT NULL DEFAULT now()
);
GRANT SELECT, UPDATE ON public.giocatori TO authenticated;
GRANT ALL ON public.giocatori TO service_role;
ALTER TABLE public.giocatori ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Authenticated can view roster" ON public.giocatori
FOR SELECT TO authenticated USING (true);
CREATE POLICY "Users can update own profile" ON public.giocatori
FOR UPDATE TO authenticated USING (auth_user_id = auth.uid())
WITH CHECK (auth_user_id = auth.uid());
CREATE POLICY "Admins can manage roster" ON public.giocatori
FOR ALL TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role))
WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));
CREATE TABLE public.eventi (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
tipo text NOT NULL CHECK (tipo IN ('partita', 'allenamento', 'evento', 'compleanno')),
titolo text NOT NULL,
data date NOT NULL,
ora text NOT NULL,
luogo text NOT NULL,
avversario text,
casa boolean,
creato_da uuid REFERENCES auth.users(id) ON DELETE SET NULL,
creato_il timestamptz NOT NULL DEFAULT now(),
aggiornato_il timestamptz NOT NULL DEFAULT now()
);
GRANT SELECT ON public.eventi TO authenticated;
GRANT ALL ON public.eventi TO service_role;
ALTER TABLE public.eventi ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Authenticated can view events" ON public.eventi
FOR SELECT TO authenticated USING (true);
CREATE POLICY "Admins can manage events" ON public.eventi
FOR ALL TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role))
WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));
CREATE TABLE public.presenze (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
evento_id uuid REFERENCES public.eventi(id) ON DELETE CASCADE NOT NULL,
giocatore_id uuid REFERENCES public.giocatori(id) ON DELETE CASCADE NOT NULL,
stato text NOT NULL CHECK (stato IN ('presente', 'assente', 'forse', 'ritardo', 'infortunato')),
aggiornato_da uuid REFERENCES auth.users(id) ON DELETE SET NULL,
aggiornato_il timestamptz NOT NULL DEFAULT now(),
UNIQUE (evento_id, giocatore_id)
);
GRANT SELECT, INSERT, UPDATE, DELETE ON public.presenze TO authenticated;
GRANT ALL ON public.presenze TO service_role;
ALTER TABLE public.presenze ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Authenticated can view presences" ON public.presenze
FOR SELECT TO authenticated USING (true);
CREATE POLICY "Users can manage own presence" ON public.presenze
FOR ALL TO authenticated
USING (
EXISTS (
SELECT 1 FROM public.giocatori g
WHERE g.id = giocatore_id AND g.auth_user_id = auth.uid()
)
)
WITH CHECK (
EXISTS (
SELECT 1 FROM public.giocatori g
WHERE g.id = giocatore_id AND g.auth_user_id = auth.uid()
)
);
CREATE POLICY "Admins can manage all presences" ON public.presenze
FOR ALL TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role))
WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));
CREATE OR REPLACE FUNCTION public.update_aggiornato_il()
RETURNS TRIGGER AS $$
BEGIN
NEW.aggiornato_il = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql SET search_path = public;
CREATE TRIGGER update_giocatori_aggiornato_il BEFORE UPDATE ON public.giocatori
FOR EACH ROW EXECUTE FUNCTION public.update_aggiornato_il();
CREATE TRIGGER update_eventi_aggiornato_il BEFORE UPDATE ON public.eventi
FOR EACH ROW EXECUTE FUNCTION public.update_aggiornato_il();
CREATE TRIGGER update_presenze_aggiornato_il BEFORE UPDATE ON public.presenze
FOR EACH ROW EXECUTE FUNCTION public.update_aggiornato_il();
INSERT INTO public.giocatori (nome, numero, ruolo, nascita) VALUES
('Salvador Battistella', 1, 'Libero', '1997-08-30'),
('Mattias Bologna', 2, 'Centrale', '1996-12-07'),
('Alessandra Brunacci', 3, 'Palleggiatore', '2000-04-28'),
('Ivan Cacciari', 4, 'Schiacciatore laterale', '1995-05-01'),
('Mattia Catalano', 5, 'Palleggiatore', '1995-08-13'),
('Silvia Chilese', 6, 'Libero', '1996-02-01'),
('Alessio Cocco', 7, 'Centrale', '1997-07-04'),
('Carlo Di Castelnuovo', 8, 'Jolly', '1994-05-28'),
('Camilla Esposito', 9, 'Palleggiatore', '2003-04-04'),
('Davide Grilli', 10, 'Opposto', '1998-11-30'),
('Antonella Loverre', 11, 'Schiacciatore laterale', '2000-02-02'),
('Laura Passabì', 12, 'Schiacciatore laterale', '1999-10-03'),
('Nicola Pezzoli', 13, 'Centrale', '2000-09-16'),
('Iacopo Ricci', 14, 'Schiacciatore laterale', '1996-12-13'),
('Cristina Titone', 15, 'Libero', '1993-03-24'),
('Francesca Tucci', 16, 'Centrale', '2001-04-18'),
('Giada Valbonesi', 17, 'Opposto', '1994-05-20');
@@ -0,0 +1,32 @@
CREATE TABLE public.badge_social_voti (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
match_id text NOT NULL,
categoria text NOT NULL,
votante_id text NOT NULL,
votato_id text NOT NULL,
votato_nome text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (match_id, categoria, votante_id)
);
GRANT SELECT, INSERT, UPDATE, DELETE ON public.badge_social_voti TO anon;
GRANT SELECT, INSERT, UPDATE, DELETE ON public.badge_social_voti TO authenticated;
GRANT ALL ON public.badge_social_voti TO service_role;
ALTER TABLE public.badge_social_voti ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Chiunque puo leggere i voti social"
ON public.badge_social_voti FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY "Chiunque puo votare i badge social"
ON public.badge_social_voti FOR INSERT TO anon, authenticated WITH CHECK (true);
CREATE POLICY "Chiunque puo cambiare il proprio voto social"
ON public.badge_social_voti FOR UPDATE TO anon, authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Chiunque puo togliere il proprio voto social"
ON public.badge_social_voti FOR DELETE TO anon, authenticated USING (true);
CREATE TRIGGER update_badge_social_voti_updated_at
BEFORE UPDATE ON public.badge_social_voti
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
CREATE INDEX badge_social_voti_match_idx ON public.badge_social_voti (match_id);
@@ -0,0 +1,25 @@
CREATE TABLE public.risposte_presenze (
evento_id text NOT NULL,
giocatore_id text NOT NULL,
stato text NOT NULL,
aggiornato_il timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (evento_id, giocatore_id)
);
GRANT SELECT, INSERT, UPDATE, DELETE ON public.risposte_presenze TO anon, authenticated;
GRANT ALL ON public.risposte_presenze TO service_role;
ALTER TABLE public.risposte_presenze ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Chiunque puo leggere le presenze" ON public.risposte_presenze FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY "Chiunque puo salvare la propria presenza" ON public.risposte_presenze FOR INSERT TO anon, authenticated WITH CHECK (true);
CREATE POLICY "Chiunque puo aggiornare la propria presenza" ON public.risposte_presenze FOR UPDATE TO anon, authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Chiunque puo togliere la propria presenza" ON public.risposte_presenze FOR DELETE TO anon, authenticated USING (true);
CREATE TABLE public.promemoria_push (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
endpoint text NOT NULL,
titolo text NOT NULL,
testo text NOT NULL,
creato_il timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX promemoria_push_endpoint_idx ON public.promemoria_push (endpoint, creato_il DESC);
GRANT ALL ON public.promemoria_push TO service_role;
ALTER TABLE public.promemoria_push ENABLE ROW LEVEL SECURITY;
@@ -0,0 +1,16 @@
CREATE TABLE public.scout_live (
evento_id text PRIMARY KEY,
stato jsonb NOT NULL DEFAULT '{}'::jsonb,
aggiornato_il timestamptz NOT NULL DEFAULT now()
);
GRANT SELECT, INSERT, UPDATE, DELETE ON public.scout_live TO anon;
GRANT SELECT, INSERT, UPDATE, DELETE ON public.scout_live TO authenticated;
GRANT ALL ON public.scout_live TO service_role;
ALTER TABLE public.scout_live ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Chiunque puo leggere lo scout live" ON public.scout_live FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY "Chiunque puo creare lo scout live" ON public.scout_live FOR INSERT TO anon, authenticated WITH CHECK (true);
CREATE POLICY "Chiunque puo aggiornare lo scout live" ON public.scout_live FOR UPDATE TO anon, authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Chiunque puo eliminare lo scout live" ON public.scout_live FOR DELETE TO anon, authenticated USING (true);
@@ -0,0 +1,91 @@
-- 1) Eventi gestibili dagli amministratori
CREATE TABLE public.eventi_app (
id text PRIMARY KEY,
tipo text NOT NULL,
titolo text NOT NULL,
luogo text NOT NULL DEFAULT '',
data date NOT NULL,
ora text NOT NULL DEFAULT '20:30',
note text NOT NULL DEFAULT '',
convocati text[] NOT NULL DEFAULT '{}',
campionato boolean NOT NULL DEFAULT false,
pagelle_chiuse boolean NOT NULL DEFAULT false,
creato_il timestamptz NOT NULL DEFAULT now(),
aggiornato_il timestamptz NOT NULL DEFAULT now()
);
GRANT SELECT, INSERT, UPDATE, DELETE ON public.eventi_app TO anon, authenticated;
GRANT ALL ON public.eventi_app TO service_role;
ALTER TABLE public.eventi_app ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Chiunque puo leggere gli eventi" ON public.eventi_app FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY "Chiunque puo creare eventi" ON public.eventi_app FOR INSERT TO anon, authenticated WITH CHECK (true);
CREATE POLICY "Chiunque puo modificare eventi" ON public.eventi_app FOR UPDATE TO anon, authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Chiunque puo eliminare eventi" ON public.eventi_app FOR DELETE TO anon, authenticated USING (true);
CREATE TRIGGER update_eventi_app_aggiornato_il
BEFORE UPDATE ON public.eventi_app
FOR EACH ROW EXECUTE FUNCTION public.update_aggiornato_il();
INSERT INTO public.eventi_app (id, tipo, titolo, luogo, data, ora, campionato) VALUES
('e1', 'partita', 'CRAP Volley vs Aurora Nera', 'PalaCRAP, Via dei Tigli 4', '2026-08-04', '21:00', true),
('e2', 'allenamento', 'Allenamento tecnico', 'Palestra Comunale', '2026-08-06', '20:30', false),
('e3', 'allenamento', 'Atletica + fondamentali', 'Palestra Comunale', '2026-08-08', '19:00', false),
('e4', 'evento', 'Pizzata di squadra', 'Da Peppino', '2026-08-10', '21:30', false),
('e5', 'partita', 'Volley Bruzzano vs CRAP Volley', 'PalaBruzzano', '2026-08-13', '21:15', true),
('e6', 'allenamento', 'Rifinitura pre-gara', 'Palestra Comunale', '2026-08-20', '20:30', false);
-- 2) Pagelle: voto anonimo 1-10 tra compagni
CREATE TABLE public.pagelle_voti (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
match_id text NOT NULL,
votante_id text NOT NULL,
votato_id text NOT NULL,
voto smallint NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT pagelle_voto_range CHECK (voto >= 1 AND voto <= 10),
CONSTRAINT pagelle_no_autovoto CHECK (votante_id <> votato_id),
CONSTRAINT pagelle_unico UNIQUE (match_id, votante_id, votato_id)
);
GRANT SELECT, INSERT, UPDATE, DELETE ON public.pagelle_voti TO anon, authenticated;
GRANT ALL ON public.pagelle_voti TO service_role;
ALTER TABLE public.pagelle_voti ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Chiunque puo leggere le pagelle" ON public.pagelle_voti FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY "Chiunque puo votare le pagelle" ON public.pagelle_voti FOR INSERT TO anon, authenticated WITH CHECK (true);
CREATE POLICY "Chiunque puo cambiare il proprio voto pagella" ON public.pagelle_voti FOR UPDATE TO anon, authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Chiunque puo togliere il proprio voto pagella" ON public.pagelle_voti FOR DELETE TO anon, authenticated USING (true);
CREATE TRIGGER update_pagelle_voti_updated_at
BEFORE UPDATE ON public.pagelle_voti
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
-- 3) Sondaggio cacche pre-partita
CREATE TABLE public.cacche_partita (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
evento_id text NOT NULL,
giocatore_id text NOT NULL,
quantita smallint NOT NULL DEFAULT 0,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT cacche_range CHECK (quantita >= 0 AND quantita <= 10),
CONSTRAINT cacche_unico UNIQUE (evento_id, giocatore_id)
);
GRANT SELECT, INSERT, UPDATE, DELETE ON public.cacche_partita TO anon, authenticated;
GRANT ALL ON public.cacche_partita TO service_role;
ALTER TABLE public.cacche_partita ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Chiunque puo leggere le cacche" ON public.cacche_partita FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY "Chiunque puo dichiarare le cacche" ON public.cacche_partita FOR INSERT TO anon, authenticated WITH CHECK (true);
CREATE POLICY "Chiunque puo aggiornare le cacche" ON public.cacche_partita FOR UPDATE TO anon, authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Chiunque puo cancellare le cacche" ON public.cacche_partita FOR DELETE TO anon, authenticated USING (true);
CREATE TRIGGER update_cacche_partita_updated_at
BEFORE UPDATE ON public.cacche_partita
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
@@ -0,0 +1,4 @@
UPDATE public.giocatori SET ruolo = 'Banda' WHERE nome = 'Ivan Cacciari';
UPDATE public.giocatori SET ruolo = 'Banda' WHERE nome = 'Antonella Loverre';
UPDATE public.giocatori SET ruolo = 'Banda' WHERE nome = 'Laura Passabì';
UPDATE public.giocatori SET ruolo = 'Banda' WHERE nome = 'Iacopo Ricci';
@@ -0,0 +1 @@
ALTER TABLE public.eventi_app ADD COLUMN IF NOT EXISTS casa boolean NOT NULL DEFAULT true;