2026-08-06 08:36:47 +02:00
|
|
|
import { createFileRoute } from "@tanstack/react-router";
|
|
|
|
|
import { z } from "zod";
|
2026-09-06 12:05:39 +02:00
|
|
|
import { richiediAdmin } from "@/lib/auth-route.server";
|
2026-09-03 10:16:52 +02:00
|
|
|
import { formatData } from "@/lib/crapp-data";
|
2026-08-06 08:36:47 +02:00
|
|
|
import { leggiEventi } from "@/lib/eventi.server";
|
2026-09-03 10:16:52 +02:00
|
|
|
import { leggiGiocatoriSquadra } from "@/lib/giocatori-squadra.server";
|
2026-09-06 19:30:49 +02:00
|
|
|
import { destinatariSollecito } from "@/lib/presenze";
|
2026-08-06 08:36:47 +02:00
|
|
|
import { inviaPush } from "@/lib/webpush.server";
|
|
|
|
|
|
|
|
|
|
const schema = z.object({
|
|
|
|
|
eventoId: z.string().min(1).max(50),
|
|
|
|
|
da: z.string().min(1).max(60).optional(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const Route = createFileRoute("/api/public/sollecita-presenze")({
|
|
|
|
|
server: {
|
|
|
|
|
handlers: {
|
|
|
|
|
POST: async ({ request }) => {
|
2026-09-06 12:05:39 +02:00
|
|
|
const negato = await richiediAdmin(request);
|
|
|
|
|
if (negato) return negato;
|
|
|
|
|
|
2026-08-06 08:36:47 +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 evento = eventi.find((e) => e.id === parsed.data.eventoId);
|
|
|
|
|
if (!evento) return new Response("Evento non trovato", { status: 404 });
|
|
|
|
|
|
|
|
|
|
const { supabaseAdmin } = await import("@/integrations/supabase/client.server");
|
|
|
|
|
|
|
|
|
|
const { data: righe } = await supabaseAdmin
|
|
|
|
|
.from("risposte_presenze")
|
|
|
|
|
.select("giocatore_id, stato")
|
|
|
|
|
.eq("evento_id", evento.id);
|
|
|
|
|
|
2026-09-03 10:16:52 +02:00
|
|
|
const squadra = await leggiGiocatoriSquadra();
|
2026-09-06 19:30:49 +02:00
|
|
|
const destinatari = destinatariSollecito(squadra, righe ?? []);
|
2026-08-06 08:36:47 +02:00
|
|
|
|
|
|
|
|
if (destinatari.length === 0) return Response.json({ inviate: 0, destinatari: 0 });
|
|
|
|
|
|
|
|
|
|
const { data: iscrizioni } = await supabaseAdmin
|
|
|
|
|
.from("push_subscriptions")
|
2026-09-06 14:24:07 +02:00
|
|
|
.select("endpoint, p256dh, auth")
|
2026-08-06 08:36:47 +02:00
|
|
|
.in("giocatore_id", destinatari);
|
|
|
|
|
|
|
|
|
|
const titolo = "Manca la tua risposta";
|
|
|
|
|
const testo = `${evento.titolo} · ${formatData(evento.data)} ore ${evento.ora}. ${
|
|
|
|
|
parsed.data.da ? `${parsed.data.da} chiede` : "Serve"
|
|
|
|
|
} una conferma: presente, assente o in ritardo?`;
|
|
|
|
|
|
|
|
|
|
let inviate = 0;
|
|
|
|
|
for (const iscrizione of iscrizioni ?? []) {
|
|
|
|
|
try {
|
2026-09-06 14:24:07 +02:00
|
|
|
const { stato } = await inviaPush(iscrizione, titolo, testo);
|
2026-08-06 08:36:47 +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("sollecita-presenze", error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Response.json({ inviate, destinatari: destinatari.length });
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|