feat: 增加动态咨询表单配置接口
This commit is contained in:
1 parent
46c3e5504e
commit
32d3db90de
4 files changed
+124
No files matched your search
@@ -162,3 +162,17 @@ export class UpdateFieldDefinitionDto {
|
||||
@IsOptional() options?: unknown;
|
||||
@IsOptional() visibleWhen?: unknown;
|
||||
}
|
||||
|
||||
export class CreateInquiryFormDto {
|
||||
@IsString() name!: string;
|
||||
@IsOptional() @IsString() offeringId?: string;
|
||||
@IsArray() fieldsSnapshot!: unknown[];
|
||||
@IsOptional() @IsBoolean() isEnabled?: boolean;
|
||||
}
|
||||
|
||||
export class UpdateInquiryFormDto {
|
||||
@IsOptional() @IsString() name?: string;
|
||||
@IsOptional() @IsString() offeringId?: string;
|
||||
@IsOptional() @IsArray() fieldsSnapshot?: unknown[];
|
||||
@IsOptional() @IsBoolean() isEnabled?: boolean;
|
||||
}
|
||||
@@ -38,6 +38,8 @@ import {
|
||||
UpdateModuleDto,
|
||||
CreateFieldDefinitionDto,
|
||||
UpdateFieldDefinitionDto,
|
||||
CreateInquiryFormDto,
|
||||
UpdateInquiryFormDto,
|
||||
} from "./admin.dto.js";
|
||||
import { AdminAuthService } from "./admin-auth.service.js";
|
||||
import { AdminLoginDto } from "./admin-auth.dto.js";
|
||||
@@ -393,6 +395,52 @@ export class AdminWorkspaceController {
|
||||
await this.prisma.brandFieldDefinition.delete({ where: { id } });
|
||||
return { deleted: true };
|
||||
}
|
||||
|
||||
@Get("forms")
|
||||
listForms(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.prisma.inquiryForm.findMany({
|
||||
where: { brandId: this.brandId() },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
});
|
||||
}
|
||||
|
||||
@Post("forms")
|
||||
createForm(@Req() request: FastifyRequest, @Body() body: CreateInquiryFormDto): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.prisma.inquiryForm.create({
|
||||
data: {
|
||||
...body,
|
||||
brandId: this.brandId(),
|
||||
version: 1,
|
||||
fieldsSnapshot: body.fieldsSnapshot as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@Patch("forms/:id")
|
||||
async updateForm(
|
||||
@Req() request: FastifyRequest,
|
||||
@Param("id") id: string,
|
||||
@Body() body: UpdateInquiryFormDto,
|
||||
): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
const item = await this.prisma.inquiryForm.findFirst({
|
||||
where: { id, brandId: this.brandId() },
|
||||
});
|
||||
if (!item) throw new BadRequestException("咨询表单不存在");
|
||||
const { fieldsSnapshot, ...fields } = body;
|
||||
return this.prisma.inquiryForm.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...fields,
|
||||
...(fieldsSnapshot === undefined
|
||||
? {}
|
||||
: { fieldsSnapshot: fieldsSnapshot as Prisma.InputJsonValue }),
|
||||
...(fieldsSnapshot === undefined ? {} : { version: { increment: 1 } }),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Controller("customer/session")
|
||||
|
||||
@@ -132,6 +132,35 @@ test("admin can customize workspace modules and fields with brand isolation", as
|
||||
}
|
||||
});
|
||||
|
||||
test("admin can create and version an inquiry form", async () => {
|
||||
const login = await request("/admin/session/login", {
|
||||
method: "POST",
|
||||
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
||||
});
|
||||
const token = JSON.parse(login.body).token;
|
||||
const created = await request("/admin/workspace/forms", {
|
||||
method: "POST",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: {
|
||||
name: "品牌合作咨询",
|
||||
fieldsSnapshot: [{ key: "project", type: "textarea", label: "项目背景", required: true }],
|
||||
},
|
||||
});
|
||||
assert.equal(created.statusCode, 201);
|
||||
const id = JSON.parse(created.body).id;
|
||||
try {
|
||||
const updated = await request(`/admin/workspace/forms/${id}`, {
|
||||
method: "PATCH",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { fieldsSnapshot: [{ key: "budget", type: "number", label: "预算" }] },
|
||||
});
|
||||
assert.equal(updated.statusCode, 200);
|
||||
assert.equal(JSON.parse(updated.body).version, 2);
|
||||
} finally {
|
||||
await prisma.inquiryForm.delete({ where: { id } });
|
||||
}
|
||||
});
|
||||
|
||||
test("admin can manage offerings and public endpoint hides disabled offerings", async () => {
|
||||
const login = await request("/admin/session/login", {
|
||||
method: "POST",
|
||||
|
||||
@@ -144,6 +144,14 @@ export interface BrandFieldDefinition {
|
||||
sortOrder: number;
|
||||
source: string;
|
||||
}
|
||||
export interface InquiryForm {
|
||||
id: string;
|
||||
name: string;
|
||||
offeringId?: string | null;
|
||||
version: number;
|
||||
isEnabled: boolean;
|
||||
fieldsSnapshot: unknown[];
|
||||
}
|
||||
export interface Offering {
|
||||
id: string;
|
||||
brandId?: string;
|
||||
@@ -301,6 +309,31 @@ export class ApiClient {
|
||||
deleteAdminField(id: string): Promise<{ deleted: boolean }> {
|
||||
return this.request(`/admin/workspace/fields/${encodeURIComponent(id)}`, { method: "DELETE" });
|
||||
}
|
||||
listAdminInquiryForms(): Promise<InquiryForm[]> {
|
||||
return this.request("/admin/workspace/forms");
|
||||
}
|
||||
createAdminInquiryForm(body: {
|
||||
name: string;
|
||||
offeringId?: string;
|
||||
fieldsSnapshot: unknown[];
|
||||
isEnabled?: boolean;
|
||||
}): Promise<InquiryForm> {
|
||||
return this.request("/admin/workspace/forms", { method: "POST", body });
|
||||
}
|
||||
updateAdminInquiryForm(
|
||||
id: string,
|
||||
body: Partial<{
|
||||
name: string;
|
||||
offeringId: string;
|
||||
fieldsSnapshot: unknown[];
|
||||
isEnabled: boolean;
|
||||
}>,
|
||||
): Promise<InquiryForm> {
|
||||
return this.request(`/admin/workspace/forms/${encodeURIComponent(id)}`, {
|
||||
method: "PATCH",
|
||||
body,
|
||||
});
|
||||
}
|
||||
listAdminOfferings(): Promise<Offering[]> {
|
||||
return this.request("/admin/workspace/offerings");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user