import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common"; import { Prisma } from "@prisma/client"; import { PrismaService } from "../../prisma.service.js"; import { BrandReleaseService } from "../../brand-release.service.js"; import type { CreateInquirySubmissionDto } from "../../booking.dto.js"; @Injectable() export class CustomerInquirySubmissionService { constructor( private readonly prisma: PrismaService, private readonly releases: BrandReleaseService, ) {} listForCustomer(brandId: string, customerSubject: string) { return this.prisma.inquirySubmission.findMany({ where: { brandId, customerSubject }, orderBy: { createdAt: "desc" }, include: { inquiryForm: { select: { name: true, purpose: true } }, offering: { select: { id: true, name: true } }, statusHistory: { select: { fromStatus: true, toStatus: true, createdAt: true }, orderBy: { createdAt: "asc" }, }, }, }); } async getForCustomer(brandId: string, customerSubject: string, id: string) { const submission = await this.prisma.inquirySubmission.findFirst({ where: { id, brandId, customerSubject }, include: { inquiryForm: { select: { name: true, purpose: true } }, offering: { select: { id: true, name: true } }, statusHistory: { select: { fromStatus: true, toStatus: true, createdAt: true }, orderBy: { createdAt: "asc" }, }, }, }); if (!submission) throw new NotFoundException("咨询记录不存在"); return submission; } async create(brandId: string, customerSubject: string, body: CreateInquirySubmissionDto) { const snapshot = await this.releases.getPublishedSnapshot(brandId); const publishedForm = snapshot && this.releases.publishedForm(snapshot, body.formId); if (!publishedForm) throw new BadRequestException("咨询表单不存在或品牌尚未发布"); const form = await this.prisma.inquiryForm.findFirst({ where: { id: body.formId, brandId, isEnabled: true }, }); if (!form) throw new BadRequestException("咨询表单不存在"); const offeringId = typeof publishedForm.offeringId === "string" ? publishedForm.offeringId : null; if (offeringId) { const offering = await this.prisma.offering.findFirst({ where: { id: offeringId, brandId, isEnabled: true }, select: { id: true }, }); if (!offering) throw new BadRequestException("当前业务提供项不可用"); } const capabilityKey = publishedForm.purpose === "quote-request" ? "quote-request" : "inquiry"; const capability = await this.prisma.capabilityInstallation.findUnique({ where: { brandId_capabilityKey: { brandId, capabilityKey } }, }); if (capability && capability.status !== "enabled") throw new BadRequestException("当前咨询能力不可用"); const fields = Array.isArray(publishedForm.fieldsSnapshot) ? (publishedForm.fieldsSnapshot as Array>) : []; for (const field of fields) { const key = typeof field.key === "string" ? field.key : ""; const label = typeof field.label === "string" ? field.label : key; const value = body.values[key]; const textValue = typeof value === "string" || typeof value === "number" ? String(value) : ""; const empty = value === undefined || value === null || value === ""; if (field.required === true && empty) throw new BadRequestException(`${label}不能为空`); if (empty) continue; if ((field.type === "phone" || field.type === "tel") && !/^1\d{10}$/.test(textValue)) throw new BadRequestException(`${label}格式不正确`); if (field.type === "number" && !Number.isFinite(Number(value))) throw new BadRequestException(`${label}必须是数字`); } const visibleFields = fields.filter((field) => this.isVisibleField(field, body.values)); const allowedKeys = new Set(visibleFields.map((field) => String(field.key))); const values = Object.fromEntries( Object.entries(body.values).filter(([key]) => allowedKeys.has(key)), ); return this.prisma.inquirySubmission.create({ data: { brandId, customerSubject, inquiryFormId: form.id, offeringId, formVersion: Number(publishedForm.version), fieldsSnapshot: publishedForm.fieldsSnapshot as Prisma.InputJsonValue, values: values as Prisma.InputJsonValue, activeFields: Object.fromEntries(Object.keys(values).map((key) => [key, true])), }, }); } private visibilityRuleMatches(rule: unknown, values: Record): boolean { if (!rule || typeof rule !== "object") return true; const item = rule as Record; if (typeof item.field !== "string") return true; const value = values[item.field]; switch (item.operator) { case "exists": return Array.isArray(value) ? value.length > 0 : Boolean(value); case "equals": return value === item.value; case "not-equals": return value !== item.value; case "in": return Array.isArray(item.value) && typeof value === "string" && item.value.includes(value); default: return true; } } private isVisibleField(field: Record, values: Record): boolean { return (Array.isArray(field.visibleWhen) ? field.visibleWhen : []).every((rule) => this.visibilityRuleMatches(rule, values), ); } }