Le route server in src/routes/api/public/ (promemoria-palloni, sollecita-presenze, apri-sondaggio, push-subscribe, notifiche-attive) usano pbAdmin al posto di supabaseAdmin per leggere/scrivere push_subscriptions e i dati necessari a comporre gli avvisi. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
import { createFileRoute } from "@tanstack/react-router";
|
|
import { z } from "zod";
|
|
|
|
const schemaIscrizione = z.object({
|
|
endpoint: z.string().url().max(1000),
|
|
giocatoreId: z.string().min(1).max(50),
|
|
p256dh: z.string().min(1).max(500),
|
|
auth: z.string().min(1).max(500),
|
|
});
|
|
|
|
const schemaCancellazione = z.object({ endpoint: z.string().url().max(1000) });
|
|
|
|
export const Route = createFileRoute("/api/public/push-subscribe")({
|
|
server: {
|
|
handlers: {
|
|
POST: async ({ request }) => {
|
|
const parsed = schemaIscrizione.safeParse(await request.json());
|
|
if (!parsed.success) return new Response("Dati non validi", { status: 400 });
|
|
|
|
const { pbAdmin } = await import("@/integrations/pocketbase/client.server");
|
|
const { upsertByFilter } = await import("@/integrations/pocketbase/upsert");
|
|
const admin = await pbAdmin();
|
|
|
|
try {
|
|
await upsertByFilter(
|
|
"push_subscriptions",
|
|
"endpoint = {:endpoint}",
|
|
{ endpoint: parsed.data.endpoint },
|
|
{
|
|
endpoint: parsed.data.endpoint,
|
|
giocatore: parsed.data.giocatoreId,
|
|
p256dh: parsed.data.p256dh,
|
|
auth: parsed.data.auth,
|
|
},
|
|
admin,
|
|
);
|
|
} catch (error) {
|
|
console.error("push-subscribe", error);
|
|
return new Response("Errore salvataggio", { status: 500 });
|
|
}
|
|
return Response.json({ ok: true });
|
|
},
|
|
DELETE: async ({ request }) => {
|
|
const parsed = schemaCancellazione.safeParse(await request.json());
|
|
if (!parsed.success) return new Response("Dati non validi", { status: 400 });
|
|
|
|
const { pbAdmin } = await import("@/integrations/pocketbase/client.server");
|
|
const admin = await pbAdmin();
|
|
const esistente = await admin
|
|
.collection("push_subscriptions")
|
|
.getFirstListItem(
|
|
admin.filter("endpoint = {:endpoint}", { endpoint: parsed.data.endpoint }),
|
|
)
|
|
.catch(() => null);
|
|
if (esistente) await admin.collection("push_subscriptions").delete(esistente.id);
|
|
return Response.json({ ok: true });
|
|
},
|
|
},
|
|
},
|
|
});
|