690 files changed
+12045
-33943
No files matched your search
@@ -0,0 +1,51 @@
|
||||
"use server";
|
||||
|
||||
import { auth } from "@/auth";
|
||||
import { prisma } from "@/lib/db/prisma";
|
||||
import { getPrimaryBusiness } from "@/lib/business/get-primary-business";
|
||||
import { subscribeToPush, unsubscribeFromPush } from "@/services/notifications/push-subscription.service";
|
||||
|
||||
export interface PushActionResult {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registered users only (staff, owner, signed-in customer) — a guest has
|
||||
* no session to attribute the subscription to. A guest subscribing right
|
||||
* after a guest booking is a real, documented gap for a future pass (see
|
||||
* docs/phase-7-summary.md, "Known limitations"), not an oversight.
|
||||
*/
|
||||
export async function subscribeToPushAction(input: {
|
||||
endpoint: string;
|
||||
p256dhKey: string;
|
||||
authKey: string;
|
||||
deviceLabel?: string;
|
||||
}): Promise<PushActionResult> {
|
||||
const session = await auth();
|
||||
if (!session?.user) return { success: false, error: "You must be signed in." };
|
||||
|
||||
const [business, customerProfile] = await Promise.all([
|
||||
getPrimaryBusiness(),
|
||||
prisma.customerProfile.findUnique({ where: { userId: session.user.id } }),
|
||||
]);
|
||||
|
||||
await subscribeToPush(prisma, {
|
||||
businessId: business.id,
|
||||
userId: session.user.id,
|
||||
customerId: customerProfile?.id,
|
||||
endpoint: input.endpoint,
|
||||
p256dhKey: input.p256dhKey,
|
||||
authKey: input.authKey,
|
||||
deviceLabel: input.deviceLabel,
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function unsubscribeFromPushAction(endpoint: string): Promise<PushActionResult> {
|
||||
const session = await auth();
|
||||
if (!session?.user) return { success: false, error: "You must be signed in." };
|
||||
|
||||
await unsubscribeFromPush(prisma, { endpoint, ownerUserId: session.user.id });
|
||||
return { success: true };
|
||||
}
|
||||
Reference in new issue
Block a user