You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.5 KiB

import { cookies } from "next/headers";
import { fail, ok } from "@/lib/response";
import {
EMBED_DEMO_COOKIE,
verifyEmbedDemoToken,
} from "@/modules/embed-demo/auth";
import { isKnownCustomer } from "@/modules/auth/service-token-env";
import { isDbCustomerActive } from "@/modules/customer/customer-service";
import {
defaultMarkupDto,
serializeMarkupConfig,
} from "@/modules/pricing/markup-service";
import { prisma } from "@/lib/prisma";
async function isEmbedCustomerAllowed(customerId: string): Promise<boolean> {
if (isKnownCustomer(customerId)) {
return true;
}
return isDbCustomerActive(customerId);
}
/** GET /api/embed-demo/me — 当前演示会话与加价配置 */
export async function GET() {
const cookieStore = await cookies();
const raw = cookieStore.get(EMBED_DEMO_COOKIE)?.value;
if (!raw) {
return fail("UNAUTHORIZED", "未登录", 401);
}
let customerId: string;
try {
const payload = await verifyEmbedDemoToken(raw);
customerId = payload.sub;
} catch {
return fail("UNAUTHORIZED", "演示会话无效或已过期", 401);
}
if (!(await isEmbedCustomerAllowed(customerId))) {
return fail("FORBIDDEN", "客户已停用", 403);
}
const markupRow = await prisma.markupConfig.findFirst({
where: { customerId, isDeleted: false },
});
const markup = markupRow
? serializeMarkupConfig(markupRow)
: defaultMarkupDto(customerId);
return ok({
customer_id: customerId,
markup,
});
}