2026-08-06 08:36:47 +02:00
|
|
|
import { createFileRoute } from "@tanstack/react-router";
|
2026-09-15 11:19:07 +02:00
|
|
|
import type { SupabaseClient } from "@supabase/supabase-js";
|
2026-08-06 08:36:47 +02:00
|
|
|
import { z } from "zod";
|
2026-09-24 14:54:35 +02:00
|
|
|
import { richiediGestoreEventi } 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-24 14:54:35 +02:00
|
|
|
const negato = await richiediGestoreEventi(request);
|
2026-09-06 12:05:39 +02:00
|
|
|
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?`;
|
|
|
|
|
|
2026-09-15 11:19:07 +02:00
|
|
|
// Storico in-app (M17): un upsert perché ripremere il pulsante deve riportare la
|
|
|
|
|
// notifica a non letta, non fallire per il vincolo UNIQUE. `types.ts` non include
|
|
|
|
|
// ancora `notifiche_utente`, stesso aggiramento di `giocatori-squadra.server.ts`.
|
|
|
|
|
const client = supabaseAdmin as unknown as SupabaseClient;
|
|
|
|
|
await client.from("notifiche_utente").upsert(
|
|
|
|
|
destinatari.map((giocatoreId) => ({
|
|
|
|
|
giocatore_id: giocatoreId,
|
|
|
|
|
tipo: "sollecita_presenze",
|
|
|
|
|
titolo,
|
|
|
|
|
corpo: testo,
|
|
|
|
|
evento_id: evento.id,
|
|
|
|
|
letta: false,
|
|
|
|
|
creato_il: new Date().toISOString(),
|
|
|
|
|
})),
|
|
|
|
|
{ onConflict: "giocatore_id,evento_id,tipo" },
|
|
|
|
|
);
|
|
|
|
|
|
2026-08-06 08:36:47 +02:00
|
|
|
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 });
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|