Aggiunge infrastruttura PocketBase locale (Docker, schema, hook)

Introduce PocketBase come sostituto di Supabase solo per l'ambiente di
sviluppo locale (docs/PORTABILITA.md): docker-compose.pocketbase.yml,
schema iniziale in pocketbase/pb_migrations/ (collection equivalenti alle
tabelle Supabase, con le stesse API rule dove esprimibili) e la logica
procedurale che in Postgres viveva in trigger/funzioni riscritta come
hook JS in pocketbase/pb_hooks/ (claim slot giocatore, blocco autovoto,
convocati/pagelle chiuse, immutabilità di risposto_il).

Il superuser e il provider Google OAuth2 si ricreano da soli a ogni
avvio da variabili d'ambiente (script scripts/pocketbase-reset.sh),
così un reset non richiede passaggi manuali da dashboard.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
davideandClaude Sonnet 5 committed 2026-09-11 14:57:55 +02:00
1 parent fcff950006
commit 15885f32c7
10 files changed
+953 -2

No files matched your search

@@ -0,0 +1,49 @@
/// <reference path="../pb_data/types.d.ts" />
//
// Equivalente del trigger enforce_giocatori_squadra_update (M1/M5/M8, DD-016/DD-018):
// un non-admin può aggiornare uno slot di giocatori_squadra solo per reclamarlo (slot
// libero, email dell'account uguale a quella registrata sulla riga), senza cambiare nessun
// altro campo. La updateRule della collection apre la porta solo quando auth_user_id è
// vuoto o l'utente è admin; qui si valida il resto.
onRecordUpdateRequest((e) => {
// Superuser (pbAdmin lato server, dashboard, script di migrazione dati) e admin
// applicativi (role="admin" sulla collection users) bypassano il vincolo di claim.
const isAdmin = e.hasSuperuserAuth() || (e.auth && e.auth.get("role") === "admin");
if (isAdmin) {
e.next();
return;
}
const originale = e.app.findRecordById("giocatori_squadra", e.record.id);
const campiBloccati = [
"nome",
"cognome",
"numero",
"ruolo",
"attivo",
"email",
"numero_tessera",
"data_tessera",
];
for (const campo of campiBloccati) {
if (String(e.record.get(campo)) !== String(originale.get(campo))) {
throw new BadRequestError("Aggiornamento non autorizzato su giocatori_squadra");
}
}
const utenteId = e.auth ? e.auth.id : "";
const emailAccount = (e.auth ? e.auth.get("email") || "" : "").toLowerCase();
const emailSlot = (originale.get("email") || "").toLowerCase();
const slotLibero = !originale.get("auth_user_id");
const claimSuAccountCorrente = e.record.get("auth_user_id") === utenteId;
const emailCombacia = emailSlot !== "" && emailSlot === emailAccount;
if (!slotLibero || !claimSuAccountCorrente || !emailCombacia) {
throw new BadRequestError("Aggiornamento non autorizzato su giocatori_squadra");
}
e.next();
}, "giocatori_squadra");
+23
View File
@@ -0,0 +1,23 @@
/// <reference path="../pb_data/types.d.ts" />
//
// Il Client ID/Secret di Google OAuth2 configurati da dashboard vivono in pb_data e
// andrebbero persi a ogni "npm run pocketbase:reset" (azzera pb_data). Questo hook li
// reimposta a ogni avvio da variabili d'ambiente (mai committate, solo in .env), così
// sopravvivono al reset senza reinserirli a mano ogni volta dalla dashboard.
onBootstrap((e) => {
e.next();
const clientId = $os.getenv("GOOGLE_OAUTH_CLIENT_ID");
const clientSecret = $os.getenv("GOOGLE_OAUTH_CLIENT_SECRET");
if (!clientId || !clientSecret) {
return;
}
const collection = e.app.findCollectionByNameOrId("users");
collection.oauth2.enabled = true;
collection.oauth2.providers = [
{ name: "google", clientId: clientId, clientSecret: clientSecret },
];
e.app.save(collection);
});
@@ -0,0 +1,16 @@
/// <reference path="../pb_data/types.d.ts" />
//
// Equivalente del trigger risposte_presenze_risposto_il_immutabile (M9): risposto_il è
// l'istante della PRIMA risposta, scritto una sola volta e mai più modificabile — serve
// per la serie "Conferme 24h" (confronto con eventi_app.created).
onRecordCreateRequest((e) => {
e.record.set("risposto_il", new Date().toISOString());
e.next();
}, "risposte_presenze");
onRecordUpdateRequest((e) => {
const originale = e.app.findRecordById("risposte_presenze", e.record.id);
e.record.set("risposto_il", originale.get("risposto_il"));
e.next();
}, "risposte_presenze");
+137
View File
@@ -0,0 +1,137 @@
/// <reference path="../pb_data/types.d.ts" />
//
// Equivalente di evento_permette_voto() + i CHECK "no autovoto" (M12/M13): applicato a
// mvp_voti, pagelle_voti, badge_social_voti su create/update. Gli admin non hanno questi
// vincoli (possono correggere un voto anche fuori convocazione/dopo la chiusura pagelle).
// convocati vuoto = "tutta la rosa" (stessa convenzione di convocatiEvento() in eventi.ts).
//
// La validazione è ripetuta in ciascun callback (non estratta in una funzione condivisa a
// livello di file): ogni hook di PocketBase viene eseguito nel proprio contesto JS isolato,
// che non vede le funzioni dichiarate fuori dal callback stesso (verificato empiricamente:
// una funzione condivisa causa "ReferenceError: ... is not defined" a runtime).
onRecordCreateRequest((e) => {
const isAdmin = e.hasSuperuserAuth() || (e.auth && e.auth.get("role") === "admin");
if (!isAdmin) {
const votanteId = e.record.get("votante");
const votatoId = e.record.get("votato");
if (votanteId === votatoId) {
throw new BadRequestError("Non puoi votare te stesso");
}
const evento = e.app.findRecordById("eventi_app", e.record.get("evento"));
const convocati = evento.get("convocati") || [];
if (
convocati.length > 0 &&
(convocati.indexOf(votanteId) === -1 || convocati.indexOf(votatoId) === -1)
) {
throw new BadRequestError("Votante e votato devono essere convocati all'evento");
}
}
e.next();
}, "mvp_voti");
onRecordUpdateRequest((e) => {
const isAdmin = e.hasSuperuserAuth() || (e.auth && e.auth.get("role") === "admin");
if (!isAdmin) {
const votanteId = e.record.get("votante");
const votatoId = e.record.get("votato");
if (votanteId === votatoId) {
throw new BadRequestError("Non puoi votare te stesso");
}
const evento = e.app.findRecordById("eventi_app", e.record.get("evento"));
const convocati = evento.get("convocati") || [];
if (
convocati.length > 0 &&
(convocati.indexOf(votanteId) === -1 || convocati.indexOf(votatoId) === -1)
) {
throw new BadRequestError("Votante e votato devono essere convocati all'evento");
}
}
e.next();
}, "mvp_voti");
onRecordCreateRequest((e) => {
const isAdmin = e.hasSuperuserAuth() || (e.auth && e.auth.get("role") === "admin");
if (!isAdmin) {
const votanteId = e.record.get("votante");
const votatoId = e.record.get("votato");
if (votanteId === votatoId) {
throw new BadRequestError("Non puoi votare te stesso");
}
const evento = e.app.findRecordById("eventi_app", e.record.get("evento"));
const convocati = evento.get("convocati") || [];
if (
convocati.length > 0 &&
(convocati.indexOf(votanteId) === -1 || convocati.indexOf(votatoId) === -1)
) {
throw new BadRequestError("Votante e votato devono essere convocati all'evento");
}
if (evento.get("pagelle_chiuse")) {
throw new BadRequestError("Le pagelle per questo evento sono chiuse");
}
}
e.next();
}, "pagelle_voti");
onRecordUpdateRequest((e) => {
const isAdmin = e.hasSuperuserAuth() || (e.auth && e.auth.get("role") === "admin");
if (!isAdmin) {
const votanteId = e.record.get("votante");
const votatoId = e.record.get("votato");
if (votanteId === votatoId) {
throw new BadRequestError("Non puoi votare te stesso");
}
const evento = e.app.findRecordById("eventi_app", e.record.get("evento"));
const convocati = evento.get("convocati") || [];
if (
convocati.length > 0 &&
(convocati.indexOf(votanteId) === -1 || convocati.indexOf(votatoId) === -1)
) {
throw new BadRequestError("Votante e votato devono essere convocati all'evento");
}
if (evento.get("pagelle_chiuse")) {
throw new BadRequestError("Le pagelle per questo evento sono chiuse");
}
}
e.next();
}, "pagelle_voti");
onRecordCreateRequest((e) => {
const isAdmin = e.hasSuperuserAuth() || (e.auth && e.auth.get("role") === "admin");
if (!isAdmin) {
const votanteId = e.record.get("votante");
const votatoId = e.record.get("votato");
if (votanteId === votatoId) {
throw new BadRequestError("Non puoi votare te stesso");
}
const evento = e.app.findRecordById("eventi_app", e.record.get("evento"));
const convocati = evento.get("convocati") || [];
if (
convocati.length > 0 &&
(convocati.indexOf(votanteId) === -1 || convocati.indexOf(votatoId) === -1)
) {
throw new BadRequestError("Votante e votato devono essere convocati all'evento");
}
}
e.next();
}, "badge_social_voti");
onRecordUpdateRequest((e) => {
const isAdmin = e.hasSuperuserAuth() || (e.auth && e.auth.get("role") === "admin");
if (!isAdmin) {
const votanteId = e.record.get("votante");
const votatoId = e.record.get("votato");
if (votanteId === votatoId) {
throw new BadRequestError("Non puoi votare te stesso");
}
const evento = e.app.findRecordById("eventi_app", e.record.get("evento"));
const convocati = evento.get("convocati") || [];
if (
convocati.length > 0 &&
(convocati.indexOf(votanteId) === -1 || convocati.indexOf(votatoId) === -1)
) {
throw new BadRequestError("Votante e votato devono essere convocati all'evento");
}
}
e.next();
}, "badge_social_voti");