2026-09-06 00:08:58 +02:00
|
|
|
import { createFileRoute } from "@tanstack/react-router";
|
2026-09-29 14:19:54 +02:00
|
|
|
import type { SupabaseClient } from "@supabase/supabase-js";
|
2026-09-06 00:08:58 +02:00
|
|
|
import { z } from "zod";
|
2026-09-06 12:05:39 +02:00
|
|
|
import { richiediAdmin } from "@/lib/auth-route.server";
|
2026-09-29 14:19:54 +02:00
|
|
|
import { avvisoSondaggio, destinatariSondaggio } from "@/lib/cacche";
|
2026-09-06 00:08:58 +02:00
|
|
|
import { leggiEventi } from "@/lib/eventi.server";
|
|
|
|
|
import { inviaPush } from "@/lib/webpush.server";
|
2026-09-24 14:54:35 +02:00
|
|
|
import { isAllenatore } from "@/lib/giocatori-squadra";
|
|
|
|
|
import { leggiGiocatoriSquadra } from "@/lib/giocatori-squadra.server";
|
2026-09-06 00:08:58 +02:00
|
|
|
|
|
|
|
|
const schema = z.object({ eventoId: z.string().min(1).max(50) });
|
|
|
|
|
|
|
|
|
|
/** Avviso "sondaggio pre-partita aperto": lo fa partire un admin dalla pagina partita. */
|
|
|
|
|
export const Route = createFileRoute("/api/public/apri-sondaggio")({
|
|
|
|
|
server: {
|
|
|
|
|
handlers: {
|
|
|
|
|
POST: async ({ request }) => {
|
2026-09-06 12:05:39 +02:00
|
|
|
const negato = await richiediAdmin(request);
|
|
|
|
|
if (negato) return negato;
|
|
|
|
|
|
2026-09-06 00:08:58 +02:00
|
|
|
const parsed = schema.safeParse(await request.json());
|
|
|
|
|
if (!parsed.success) return new Response("Dati non validi", { status: 400 });
|
|
|
|
|
|
|
|
|
|
const eventi = await leggiEventi();
|
|
|
|
|
const partita = eventi.find((e) => e.id === parsed.data.eventoId);
|
|
|
|
|
if (!partita) return new Response("Evento non trovato", { status: 404 });
|
|
|
|
|
|
|
|
|
|
const { supabaseAdmin } = await import("@/integrations/supabase/client.server");
|
2026-09-24 14:54:35 +02:00
|
|
|
const { data: tutte } = await supabaseAdmin
|
2026-09-06 00:08:58 +02:00
|
|
|
.from("push_subscriptions")
|
2026-09-24 14:54:35 +02:00
|
|
|
.select("endpoint, p256dh, auth, giocatore_id");
|
2026-09-29 14:19:54 +02:00
|
|
|
const squadra = await leggiGiocatoriSquadra();
|
2026-09-24 14:54:35 +02:00
|
|
|
// Il sondaggio cacche non riguarda l'allenatore, che non le vede (DD-034).
|
2026-09-29 14:19:54 +02:00
|
|
|
const allenatori = new Set(squadra.filter(isAllenatore).map((g) => g.id));
|
2026-09-24 14:54:35 +02:00
|
|
|
const iscrizioni = (tutte ?? []).filter((i) => !allenatori.has(i.giocatore_id));
|
2026-09-06 00:08:58 +02:00
|
|
|
|
2026-09-29 14:19:54 +02:00
|
|
|
const { titolo, testo } = avvisoSondaggio(partita);
|
|
|
|
|
|
|
|
|
|
// Storico in-app ed email (DD-038): indipendente dai dispositivi iscritti alla push. Un
|
|
|
|
|
// upsert perché ripremere il pulsante per la stessa partita deve riportare la notifica a
|
|
|
|
|
// non letta, non fallire per il vincolo UNIQUE; la mail riparte dal trigger di M22.
|
|
|
|
|
// `types.ts` non include ancora `notifiche_utente`, stesso aggiramento delle altre route.
|
|
|
|
|
const client = supabaseAdmin as unknown as SupabaseClient;
|
|
|
|
|
await client.from("notifiche_utente").upsert(
|
|
|
|
|
destinatariSondaggio(squadra).map((giocatoreId) => ({
|
|
|
|
|
giocatore_id: giocatoreId,
|
|
|
|
|
tipo: "sondaggio_cacche",
|
|
|
|
|
titolo,
|
|
|
|
|
corpo: testo,
|
|
|
|
|
evento_id: partita.id,
|
|
|
|
|
letta: false,
|
|
|
|
|
creato_il: new Date().toISOString(),
|
|
|
|
|
})),
|
|
|
|
|
{ onConflict: "giocatore_id,evento_id,tipo" },
|
|
|
|
|
);
|
2026-09-06 00:08:58 +02:00
|
|
|
|
|
|
|
|
let inviate = 0;
|
2026-09-24 14:54:35 +02:00
|
|
|
for (const iscrizione of iscrizioni) {
|
2026-09-06 00:08:58 +02:00
|
|
|
try {
|
2026-09-06 14:24:07 +02:00
|
|
|
const { stato } = await inviaPush(iscrizione, titolo, testo);
|
2026-09-06 00:08:58 +02:00
|
|
|
if (stato === 404 || stato === 410) {
|
|
|
|
|
await supabaseAdmin
|
|
|
|
|
.from("push_subscriptions")
|
|
|
|
|
.delete()
|
|
|
|
|
.eq("endpoint", iscrizione.endpoint);
|
|
|
|
|
} else if (stato >= 200 && stato < 300) {
|
|
|
|
|
inviate += 1;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("apri-sondaggio", error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 14:54:35 +02:00
|
|
|
return Response.json({ inviate, destinatari: iscrizioni.length });
|
2026-09-06 00:08:58 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|