52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
"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 };
|
|
}
|