feat: implement pwa push notifications

This commit is contained in:
2026-01-13 09:03:46 +01:00
parent e7291951d8
commit 14430b275e
16 changed files with 547 additions and 50 deletions

View File

@@ -0,0 +1,45 @@
"use server";
import prisma from "@/lib/prisma";
export async function subscribeUser(planId: string, subscription: any) {
if (!subscription || !subscription.endpoint || !subscription.keys) {
throw new Error("Invalid subscription object");
}
const { endpoint, keys: { p256dh, auth } } = subscription;
// Use upsert to handle updates gracefully
// If endpoint exists, update the planId. A device follows the last plan logged into (or explicitly subscribed to).
// Check existence first to decide logic (or just upsert)
// Actually, if we want to allow multiple plans per device, we need a different model.
// For now, simple model: One device -> One Plan.
await prisma.pushSubscription.upsert({
where: { endpoint },
create: {
planId,
endpoint,
p256dh,
auth,
},
update: {
planId,
p256dh,
auth,
}
});
return { success: true };
}
export async function unsubscribeUser(endpoint: string) {
if (!endpoint) return;
await prisma.pushSubscription.deleteMany({
where: { endpoint },
});
return { success: true };
}