feat: 完成动态咨询表单公开读取与提交
This commit is contained in:
1 parent
4470302b3f
commit
1503f042e0
3 files changed
+70
-1
No files matched your search
@@ -5,6 +5,7 @@ import {
|
|||||||
IsInt,
|
IsInt,
|
||||||
IsISO8601,
|
IsISO8601,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
|
IsObject,
|
||||||
IsString,
|
IsString,
|
||||||
Max,
|
Max,
|
||||||
Min,
|
Min,
|
||||||
@@ -33,3 +34,8 @@ export class CreateBookingDto {
|
|||||||
@IsArray() @ValidateNested({ each: true }) @Type(() => BookingItemDto) items: BookingItemDto[] =
|
@IsArray() @ValidateNested({ each: true }) @Type(() => BookingItemDto) items: BookingItemDto[] =
|
||||||
[];
|
[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class CreateInquirySubmissionDto {
|
||||||
|
@IsString() formId!: string;
|
||||||
|
@IsObject() values!: Record<string, unknown>;
|
||||||
|
}
|
||||||
+54
-1
@@ -22,7 +22,7 @@ import type { NestFastifyApplication } from "@nestjs/platform-fastify";
|
|||||||
import { PrismaService } from "./prisma.service.js";
|
import { PrismaService } from "./prisma.service.js";
|
||||||
import { WechatAuthService } from "./wechat-auth.js";
|
import { WechatAuthService } from "./wechat-auth.js";
|
||||||
import { BookingService } from "./booking.service.js";
|
import { BookingService } from "./booking.service.js";
|
||||||
import { CreateBookingDto } from "./booking.dto.js";
|
import { CreateBookingDto, CreateInquirySubmissionDto } from "./booking.dto.js";
|
||||||
import {
|
import {
|
||||||
CreateServiceDto,
|
CreateServiceDto,
|
||||||
CreateTagDto,
|
CreateTagDto,
|
||||||
@@ -176,6 +176,57 @@ export class PublicOfferingController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Controller("public/inquiry-forms")
|
||||||
|
export class PublicInquiryFormController {
|
||||||
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
|
@Get(":id")
|
||||||
|
async get(@Param("id") id: string): Promise<unknown> {
|
||||||
|
const form = await this.prisma.inquiryForm.findFirst({
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
brandId: process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||||
|
isEnabled: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!form) throw new BadRequestException("咨询表单不存在");
|
||||||
|
return form;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Controller("customer/inquiry-submissions")
|
||||||
|
export class CustomerInquirySubmissionController {
|
||||||
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
|
@Post()
|
||||||
|
async create(
|
||||||
|
@Req() request: FastifyRequest,
|
||||||
|
@Body() body: CreateInquirySubmissionDto,
|
||||||
|
): Promise<unknown> {
|
||||||
|
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||||
|
if (!token) throw new UnauthorizedException("缺少客户登录态");
|
||||||
|
let identity;
|
||||||
|
try {
|
||||||
|
identity = readIdentity(token);
|
||||||
|
} catch {
|
||||||
|
throw new UnauthorizedException("客户登录态无效或已过期");
|
||||||
|
}
|
||||||
|
if (identity.role !== "customer") throw new ForbiddenException("无客户权限");
|
||||||
|
const form = await this.prisma.inquiryForm.findFirst({
|
||||||
|
where: { id: body.formId, brandId: identity.brandId, isEnabled: true },
|
||||||
|
});
|
||||||
|
if (!form) throw new BadRequestException("咨询表单不存在");
|
||||||
|
return this.prisma.inquirySubmission.create({
|
||||||
|
data: {
|
||||||
|
brandId: identity.brandId,
|
||||||
|
inquiryFormId: form.id,
|
||||||
|
formVersion: form.version,
|
||||||
|
fieldsSnapshot: form.fieldsSnapshot as Prisma.InputJsonValue,
|
||||||
|
values: body.values as Prisma.InputJsonValue,
|
||||||
|
activeFields: Object.fromEntries(Object.keys(body.values).map((key) => [key, true])),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Controller("admin/workspace")
|
@Controller("admin/workspace")
|
||||||
export class AdminWorkspaceController {
|
export class AdminWorkspaceController {
|
||||||
constructor(private readonly prisma: PrismaService) {}
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
@@ -789,6 +840,8 @@ export class AdminConfigController {
|
|||||||
PublicTemplateController,
|
PublicTemplateController,
|
||||||
PublicWorkspaceController,
|
PublicWorkspaceController,
|
||||||
PublicOfferingController,
|
PublicOfferingController,
|
||||||
|
PublicInquiryFormController,
|
||||||
|
CustomerInquirySubmissionController,
|
||||||
AdminWorkspaceController,
|
AdminWorkspaceController,
|
||||||
CustomerSessionController,
|
CustomerSessionController,
|
||||||
AdminSessionController,
|
AdminSessionController,
|
||||||
|
|||||||
@@ -162,7 +162,17 @@ test("admin can create and version an inquiry form", async () => {
|
|||||||
});
|
});
|
||||||
assert.equal(updated.statusCode, 200);
|
assert.equal(updated.statusCode, 200);
|
||||||
assert.equal(JSON.parse(updated.body).version, 2);
|
assert.equal(JSON.parse(updated.body).version, 2);
|
||||||
|
const publicForm = await request(`/public/inquiry-forms/${id}`);
|
||||||
|
assert.equal(publicForm.statusCode, 200);
|
||||||
|
const submission = await request("/customer/inquiry-submissions", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { authorization: `Bearer ${customerToken}` },
|
||||||
|
payload: { formId: id, values: { project: "品牌升级" } },
|
||||||
|
});
|
||||||
|
assert.equal(submission.statusCode, 201);
|
||||||
|
assert.equal(JSON.parse(submission.body).formVersion, 2);
|
||||||
} finally {
|
} finally {
|
||||||
|
await prisma.inquirySubmission.deleteMany({ where: { inquiryFormId: id } });
|
||||||
await prisma.inquiryForm.delete({ where: { id } });
|
await prisma.inquiryForm.delete({ where: { id } });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user