feat: 收口多品牌与模板构建基础
This commit is contained in:
1 parent
0425a1c672
commit
77224ea51f
317 files changed
+11290
-1158
No files matched your search
@@ -2,6 +2,7 @@ import { BadRequestException, Injectable, NotFoundException } from "@nestjs/comm
|
||||
import { BookingStatus, Prisma } from "@prisma/client";
|
||||
import { PrismaService } from "./prisma.service.js";
|
||||
import { canTransitionWorkflow, workflowRegistry } from "@personal-brand/config";
|
||||
import { BrandReleaseService } from "./brand-release.service.js";
|
||||
|
||||
const BASIC_BOOKING_WORKFLOW = workflowRegistry["booking.basic"];
|
||||
|
||||
@@ -23,9 +24,32 @@ export interface CreateBookingInput {
|
||||
|
||||
@Injectable()
|
||||
export class BookingService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly releases: BrandReleaseService,
|
||||
) {}
|
||||
|
||||
async create(brandId: string, customerUserId: string, input: CreateBookingInput) {
|
||||
const snapshot = await this.releases.getPublishedSnapshot(brandId);
|
||||
if (!snapshot) throw new BadRequestException("品牌尚未发布");
|
||||
const publishedOfferingIds = this.releases.publishedOfferingIds(snapshot);
|
||||
if (
|
||||
(input.offeringId && !publishedOfferingIds.has(input.offeringId)) ||
|
||||
input.items?.some((item) => !publishedOfferingIds.has(item.offeringId))
|
||||
) {
|
||||
throw new BadRequestException("业务提供项不存在或暂不可预订");
|
||||
}
|
||||
const requestedOfferingIds = [
|
||||
...(input.offeringId ? [input.offeringId] : []),
|
||||
...(input.items ?? []).map((item) => item.offeringId),
|
||||
];
|
||||
if (requestedOfferingIds.length) {
|
||||
const enabledCount = await this.prisma.offering.count({
|
||||
where: { id: { in: requestedOfferingIds }, brandId, isEnabled: true },
|
||||
});
|
||||
if (enabledCount !== new Set(requestedOfferingIds).size)
|
||||
throw new BadRequestException("业务提供项不存在或暂不可预订");
|
||||
}
|
||||
if (!input.customerName.trim() || (!input.phone?.trim() && !input.wechatId?.trim())) {
|
||||
throw new BadRequestException("称呼和至少一种联系方式为必填项");
|
||||
}
|
||||
@@ -52,23 +76,22 @@ export class BookingService {
|
||||
) {
|
||||
throw new BadRequestException("私厨需求必须填写日期、人数和地点");
|
||||
}
|
||||
const offerings = input.items?.length
|
||||
? await this.prisma.offering.findMany({
|
||||
where: {
|
||||
id: { in: input.items.map((item) => item.offeringId) },
|
||||
brandId,
|
||||
isEnabled: true,
|
||||
},
|
||||
})
|
||||
: [];
|
||||
const offeringMap = new Map(offerings.map((offering) => [offering.id, offering]));
|
||||
const offeringMap = new Map(
|
||||
snapshot.offerings.flatMap((raw) => {
|
||||
if (!raw || typeof raw !== "object") return [];
|
||||
const item = raw as Record<string, unknown>;
|
||||
return typeof item.id === "string" && item.isEnabled === true
|
||||
? [[item.id, item] as const]
|
||||
: [];
|
||||
}),
|
||||
);
|
||||
const items = (input.items ?? []).map((item) => {
|
||||
const offering = offeringMap.get(item.offeringId);
|
||||
if (!offering || offering.priceAmount === null)
|
||||
if (!offering || typeof offering.priceAmount !== "number")
|
||||
throw new BadRequestException("套餐不存在或暂不可预订");
|
||||
return {
|
||||
offeringId: offering.id,
|
||||
serviceNameSnapshot: offering.name,
|
||||
offeringId: item.offeringId,
|
||||
serviceNameSnapshot: String(offering.name),
|
||||
unitPriceSnapshot: offering.priceAmount,
|
||||
quantity: item.quantity,
|
||||
subtotalSnapshot: offering.priceAmount * item.quantity,
|
||||
|
||||
Reference in new issue
Block a user