refactor: 移除旧服务模型统一使用业务提供项
This commit is contained in:
1 parent
ca85a583ce
commit
844a3bd715
13 files changed
+92
-146
No files matched your search
@@ -13,7 +13,7 @@ import {
|
||||
} from "class-validator";
|
||||
|
||||
export class BookingItemDto {
|
||||
@IsString() serviceId!: string;
|
||||
@IsString() offeringId!: string;
|
||||
@IsInt() @Min(1) @Max(50) quantity!: number;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ export class CreateBookingDto {
|
||||
@IsOptional() @IsString() wechatId?: string;
|
||||
@IsIn(["wechat", "phone", "none"]) contactPreference!: "wechat" | "phone" | "none";
|
||||
@IsOptional() @IsIn(["pickup", "delivery"]) fulfillmentType?: "pickup" | "delivery";
|
||||
@IsOptional() @IsString() serviceId?: string;
|
||||
@IsOptional() @IsString() offeringId?: string;
|
||||
@IsOptional() @IsISO8601() requestedDate?: string;
|
||||
@IsOptional() @IsString() requestedTime?: string;
|
||||
@IsOptional() @IsString() deliveryAddress?: string;
|
||||
|
||||
@@ -12,13 +12,13 @@ export interface CreateBookingInput {
|
||||
wechatId?: string;
|
||||
contactPreference: "wechat" | "phone" | "none";
|
||||
fulfillmentType?: "pickup" | "delivery";
|
||||
serviceId?: string;
|
||||
offeringId?: string;
|
||||
requestedDate?: string;
|
||||
requestedTime?: string;
|
||||
deliveryAddress?: string;
|
||||
note?: string;
|
||||
extraData?: Prisma.InputJsonObject;
|
||||
items?: Array<{ serviceId: string; quantity: number }>;
|
||||
items?: Array<{ offeringId: string; quantity: number }>;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
@@ -52,27 +52,26 @@ export class BookingService {
|
||||
) {
|
||||
throw new BadRequestException("私厨需求必须填写日期、人数和地点");
|
||||
}
|
||||
const services = input.items?.length
|
||||
? await this.prisma.service.findMany({
|
||||
const offerings = input.items?.length
|
||||
? await this.prisma.offering.findMany({
|
||||
where: {
|
||||
id: { in: input.items.map((item) => item.serviceId) },
|
||||
id: { in: input.items.map((item) => item.offeringId) },
|
||||
brandId,
|
||||
isEnabled: true,
|
||||
},
|
||||
})
|
||||
: [];
|
||||
const serviceMap = new Map(services.map((service) => [service.id, service]));
|
||||
const offeringMap = new Map(offerings.map((offering) => [offering.id, offering]));
|
||||
const items = (input.items ?? []).map((item) => {
|
||||
const service = serviceMap.get(item.serviceId);
|
||||
if (!service || !service.bookingEnabled || service.priceAmount === null)
|
||||
const offering = offeringMap.get(item.offeringId);
|
||||
if (!offering || offering.priceAmount === null)
|
||||
throw new BadRequestException("套餐不存在或暂不可预订");
|
||||
return {
|
||||
serviceId: service.id,
|
||||
offeringId: service.offeringId,
|
||||
serviceNameSnapshot: service.name,
|
||||
unitPriceSnapshot: service.priceAmount,
|
||||
offeringId: offering.id,
|
||||
serviceNameSnapshot: offering.name,
|
||||
unitPriceSnapshot: offering.priceAmount,
|
||||
quantity: item.quantity,
|
||||
subtotalSnapshot: service.priceAmount * item.quantity,
|
||||
subtotalSnapshot: offering.priceAmount * item.quantity,
|
||||
};
|
||||
});
|
||||
const estimatedAmount = items.reduce((sum, item) => sum + item.subtotalSnapshot, 0) || null;
|
||||
@@ -89,8 +88,7 @@ export class BookingService {
|
||||
wechatId: input.wechatId?.trim() ?? null,
|
||||
contactPreference: input.contactPreference,
|
||||
fulfillmentType: input.fulfillmentType ?? null,
|
||||
serviceId: items[0]?.serviceId ?? input.serviceId ?? null,
|
||||
offeringId: items[0]?.offeringId ?? null,
|
||||
offeringId: items[0]?.offeringId ?? input.offeringId ?? null,
|
||||
requestedDate: input.requestedDate
|
||||
? new Date(`${input.requestedDate}T00:00:00.000Z`)
|
||||
: null,
|
||||
@@ -118,7 +116,7 @@ export class BookingService {
|
||||
return this.prisma.booking.findMany({
|
||||
where: { brandId, customerUserId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: { items: true, service: { select: { name: true } } },
|
||||
include: { items: true, offering: { select: { name: true } } },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -165,7 +163,7 @@ export class BookingService {
|
||||
async getForAdmin(brandId: string, id: string) {
|
||||
const booking = await this.prisma.booking.findFirst({
|
||||
where: { id, brandId },
|
||||
include: { items: true, tags: { include: { tag: true } }, service: true },
|
||||
include: { items: true, tags: { include: { tag: true } }, offering: true },
|
||||
});
|
||||
if (!booking) throw new NotFoundException("订单不存在");
|
||||
return booking;
|
||||
|
||||
+22
-57
@@ -82,12 +82,11 @@ export class PublicController {
|
||||
const offerings = await this.prisma.offering.findMany({
|
||||
where: { isEnabled: true },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
include: { services: { where: { isEnabled: true }, take: 1 } },
|
||||
});
|
||||
return offerings.map(({ services, ...offering }) => ({
|
||||
return offerings.map((offering) => ({
|
||||
...offering,
|
||||
bookingEnabled: services[0]?.bookingEnabled ?? false,
|
||||
categoryKey: services[0]?.categoryKey ?? offering.kind,
|
||||
bookingEnabled: true,
|
||||
categoryKey: offering.kind,
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -559,7 +558,7 @@ export class AdminConfigController {
|
||||
@Get("services")
|
||||
listServices(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.prisma.service.findMany({
|
||||
return this.prisma.offering.findMany({
|
||||
where: { brandId: this.brandId() },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
@@ -570,32 +569,15 @@ export class AdminConfigController {
|
||||
@Body() body: CreateServiceDto,
|
||||
): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
const { config, ...fields } = body;
|
||||
const { config, categoryKey, ...fields } = body;
|
||||
const brandId = this.brandId();
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
const offering = await tx.offering.create({
|
||||
data: {
|
||||
brandId,
|
||||
kind: "service",
|
||||
name: body.name,
|
||||
description: body.description,
|
||||
coverUrl: body.coverUrl ?? null,
|
||||
priceText: body.priceText ?? null,
|
||||
priceAmount: body.priceAmount ?? null,
|
||||
isEnabled: body.isEnabled ?? true,
|
||||
sortOrder: body.sortOrder ?? 0,
|
||||
...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }),
|
||||
},
|
||||
});
|
||||
return tx.service.create({
|
||||
data: {
|
||||
...fields,
|
||||
brandId,
|
||||
offeringId: offering.id,
|
||||
categoryKey: body.categoryKey ?? "standard",
|
||||
...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }),
|
||||
},
|
||||
});
|
||||
return this.prisma.offering.create({
|
||||
data: {
|
||||
...fields,
|
||||
brandId,
|
||||
kind: categoryKey ?? "service",
|
||||
...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }),
|
||||
},
|
||||
});
|
||||
}
|
||||
@Patch("services/:id")
|
||||
@@ -605,33 +587,16 @@ export class AdminConfigController {
|
||||
@Body() body: UpdateServiceDto,
|
||||
): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
const service = await this.prisma.service.findFirst({ where: { id, brandId: this.brandId() } });
|
||||
if (!service) throw new BadRequestException("服务不存在");
|
||||
const { config, ...fields } = body;
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
const updated = await tx.service.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...fields,
|
||||
...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }),
|
||||
},
|
||||
});
|
||||
if (service.offeringId) {
|
||||
await tx.offering.update({
|
||||
where: { id: service.offeringId },
|
||||
data: {
|
||||
name: updated.name,
|
||||
description: updated.description,
|
||||
coverUrl: updated.coverUrl,
|
||||
priceText: updated.priceText,
|
||||
priceAmount: updated.priceAmount,
|
||||
isEnabled: updated.isEnabled,
|
||||
sortOrder: updated.sortOrder,
|
||||
config: updated.config ?? Prisma.JsonNull,
|
||||
},
|
||||
});
|
||||
}
|
||||
return updated;
|
||||
const item = await this.prisma.offering.findFirst({ where: { id, brandId: this.brandId() } });
|
||||
if (!item) throw new BadRequestException("业务提供项不存在");
|
||||
const { config, categoryKey, ...fields } = body;
|
||||
return this.prisma.offering.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...fields,
|
||||
...(categoryKey === undefined ? {} : { kind: categoryKey }),
|
||||
...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user