feat: 完善通用服务后台配置能力
This commit is contained in:
1 parent
60ca897574
commit
d167b58a05
6 files changed
+46
-3
No files matched your search
@@ -0,0 +1,2 @@
|
|||||||
|
ALTER TABLE "Service" ADD COLUMN "categoryKey" TEXT NOT NULL DEFAULT 'standard';
|
||||||
|
ALTER TABLE "Service" ADD COLUMN "config" JSONB;
|
||||||
@@ -59,6 +59,8 @@ model Service {
|
|||||||
brand BrandProfile @relation(fields: [brandId], references: [id])
|
brand BrandProfile @relation(fields: [brandId], references: [id])
|
||||||
name String
|
name String
|
||||||
description String
|
description String
|
||||||
|
categoryKey String @default("standard")
|
||||||
|
config Json?
|
||||||
coverUrl String?
|
coverUrl String?
|
||||||
priceText String?
|
priceText String?
|
||||||
priceAmount Int?
|
priceAmount Int?
|
||||||
|
|||||||
@@ -23,6 +23,21 @@ export class UpdateServiceDto {
|
|||||||
@IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number;
|
@IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number;
|
||||||
@IsOptional() @IsBoolean() bookingEnabled?: boolean;
|
@IsOptional() @IsBoolean() bookingEnabled?: boolean;
|
||||||
@IsOptional() @IsBoolean() isEnabled?: boolean;
|
@IsOptional() @IsBoolean() isEnabled?: boolean;
|
||||||
|
@IsOptional() @IsString() categoryKey?: string;
|
||||||
|
@IsOptional() config?: Record<string, unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CreateServiceDto {
|
||||||
|
@IsString() name!: string;
|
||||||
|
@IsString() description!: string;
|
||||||
|
@IsOptional() @IsString() categoryKey?: string;
|
||||||
|
@IsOptional() @IsString() coverUrl?: string;
|
||||||
|
@IsOptional() @IsString() priceText?: string;
|
||||||
|
@IsOptional() @IsInt() @Min(0) priceAmount?: number;
|
||||||
|
@IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number;
|
||||||
|
@IsOptional() @IsBoolean() bookingEnabled?: boolean;
|
||||||
|
@IsOptional() @IsBoolean() isEnabled?: boolean;
|
||||||
|
@IsOptional() config?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UpdateBookingStatusDto {
|
export class UpdateBookingStatusDto {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ 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 } from "./booking.dto.js";
|
||||||
import { CreateTagDto, UpdateBookingStatusDto, UpdateBookingTagsDto, UpdateBrandDto, UpdateServiceDto } from "./admin.dto.js";
|
import { CreateServiceDto, CreateTagDto, UpdateBookingStatusDto, UpdateBookingTagsDto, UpdateBrandDto, UpdateServiceDto } from "./admin.dto.js";
|
||||||
import { AdminAuthService } from "./admin-auth.service.js";
|
import { AdminAuthService } from "./admin-auth.service.js";
|
||||||
import { AdminLoginDto } from "./admin-auth.dto.js";
|
import { AdminLoginDto } from "./admin-auth.dto.js";
|
||||||
import type { FastifyRequest } from "fastify";
|
import type { FastifyRequest } from "fastify";
|
||||||
@@ -75,6 +75,8 @@ export class PublicController {
|
|||||||
priceText: true,
|
priceText: true,
|
||||||
priceAmount: true,
|
priceAmount: true,
|
||||||
bookingEnabled: true,
|
bookingEnabled: true,
|
||||||
|
categoryKey: true,
|
||||||
|
config: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -294,12 +296,17 @@ export class AdminConfigController {
|
|||||||
getBrand(@Req() request: FastifyRequest): Promise<unknown> { this.requireAdmin(request); return this.prisma.brandProfile.findUnique({ where: { id: this.brandId() } }); }
|
getBrand(@Req() request: FastifyRequest): Promise<unknown> { this.requireAdmin(request); return this.prisma.brandProfile.findUnique({ where: { id: this.brandId() } }); }
|
||||||
@Patch("brand")
|
@Patch("brand")
|
||||||
updateBrand(@Req() request: FastifyRequest, @Body() body: UpdateBrandDto): Promise<unknown> { this.requireAdmin(request); return this.prisma.brandProfile.update({ where: { id: this.brandId() }, data: body }); }
|
updateBrand(@Req() request: FastifyRequest, @Body() body: UpdateBrandDto): Promise<unknown> { this.requireAdmin(request); return this.prisma.brandProfile.update({ where: { id: this.brandId() }, data: body }); }
|
||||||
|
@Get("services")
|
||||||
|
listServices(@Req() request: FastifyRequest): Promise<unknown> { this.requireAdmin(request); return this.prisma.service.findMany({ where: { brandId: this.brandId() }, orderBy: { sortOrder: "asc" } }); }
|
||||||
|
@Post("services")
|
||||||
|
createService(@Req() request: FastifyRequest, @Body() body: CreateServiceDto): Promise<unknown> { this.requireAdmin(request); const { config, ...fields } = body; return this.prisma.service.create({ data: { ...fields, brandId: this.brandId(), categoryKey: body.categoryKey ?? "standard", ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }) } }); }
|
||||||
@Patch("services/:id")
|
@Patch("services/:id")
|
||||||
async updateService(@Req() request: FastifyRequest, @Param("id") id: string, @Body() body: UpdateServiceDto): Promise<unknown> {
|
async updateService(@Req() request: FastifyRequest, @Param("id") id: string, @Body() body: UpdateServiceDto): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
this.requireAdmin(request);
|
||||||
const service = await this.prisma.service.findFirst({ where: { id, brandId: this.brandId() } });
|
const service = await this.prisma.service.findFirst({ where: { id, brandId: this.brandId() } });
|
||||||
if (!service) throw new BadRequestException("服务不存在");
|
if (!service) throw new BadRequestException("服务不存在");
|
||||||
return this.prisma.service.update({ where: { id }, data: body });
|
const { config, ...fields } = body;
|
||||||
|
return this.prisma.service.update({ where: { id }, data: { ...fields, ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }) } });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -98,6 +98,7 @@ test("admin can read and update brand and service configuration", async () => {
|
|||||||
assert.ok(brandBefore);
|
assert.ok(brandBefore);
|
||||||
const serviceBefore = await prisma.service.findFirst({ where: { brandId, isEnabled: true } });
|
const serviceBefore = await prisma.service.findFirst({ where: { brandId, isEnabled: true } });
|
||||||
assert.ok(serviceBefore);
|
assert.ok(serviceBefore);
|
||||||
|
let createdServiceId;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const brand = await request("/admin/config/brand", { headers: { authorization: `Bearer ${adminToken}` } });
|
const brand = await request("/admin/config/brand", { headers: { authorization: `Bearer ${adminToken}` } });
|
||||||
@@ -121,9 +122,23 @@ test("admin can read and update brand and service configuration", async () => {
|
|||||||
assert.equal(updatedService.statusCode, 200);
|
assert.equal(updatedService.statusCode, 200);
|
||||||
assert.equal(JSON.parse(updatedService.body).priceText, "集成测试价格");
|
assert.equal(JSON.parse(updatedService.body).priceText, "集成测试价格");
|
||||||
assert.equal(JSON.parse(updatedService.body).sortOrder, 99);
|
assert.equal(JSON.parse(updatedService.body).sortOrder, 99);
|
||||||
|
|
||||||
|
const listedServices = await request("/admin/config/services", { headers: { authorization: `Bearer ${adminToken}` } });
|
||||||
|
assert.equal(listedServices.statusCode, 200);
|
||||||
|
assert.ok(JSON.parse(listedServices.body).some((item) => item.id === serviceBefore.id));
|
||||||
|
|
||||||
|
const createdService = await request("/admin/config/services", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { authorization: `Bearer ${adminToken}` },
|
||||||
|
payload: { name: "集成测试通用服务", description: "通用品牌服务", categoryKey: "consulting", config: { duration: 60 } },
|
||||||
|
});
|
||||||
|
assert.equal(createdService.statusCode, 201);
|
||||||
|
createdServiceId = JSON.parse(createdService.body).id;
|
||||||
|
assert.equal(JSON.parse(createdService.body).categoryKey, "consulting");
|
||||||
} finally {
|
} finally {
|
||||||
await prisma.brandProfile.update({ where: { id: brandId }, data: { description: brandBefore.description, contactPhone: brandBefore.contactPhone, templateKey: brandBefore.templateKey } });
|
await prisma.brandProfile.update({ where: { id: brandId }, data: { description: brandBefore.description, contactPhone: brandBefore.contactPhone, templateKey: brandBefore.templateKey } });
|
||||||
await prisma.service.update({ where: { id: serviceBefore.id }, data: { priceText: serviceBefore.priceText, sortOrder: serviceBefore.sortOrder } });
|
await prisma.service.update({ where: { id: serviceBefore.id }, data: { priceText: serviceBefore.priceText, sortOrder: serviceBefore.sortOrder } });
|
||||||
|
if (createdServiceId) await prisma.service.delete({ where: { id: createdServiceId } });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export interface BrandProfile {
|
|||||||
contactWechat: string | null;
|
contactWechat: string | null;
|
||||||
}
|
}
|
||||||
export type BrandUpdate = Partial<Omit<BrandProfile, "id">>;
|
export type BrandUpdate = Partial<Omit<BrandProfile, "id">>;
|
||||||
export interface ServiceUpdate { name?: string; description?: string; coverUrl?: string; priceText?: string; priceAmount?: number; sortOrder?: number; bookingEnabled?: boolean; isEnabled?: boolean }
|
export interface ServiceUpdate { name?: string; description?: string; coverUrl?: string; priceText?: string; priceAmount?: number; sortOrder?: number; bookingEnabled?: boolean; isEnabled?: boolean; categoryKey?: string; config?: Record<string, unknown> }
|
||||||
|
|
||||||
export interface BookingItemInput { serviceId: string; quantity: number }
|
export interface BookingItemInput { serviceId: string; quantity: number }
|
||||||
export interface CreateBookingInput {
|
export interface CreateBookingInput {
|
||||||
@@ -109,6 +109,8 @@ export class ApiClient {
|
|||||||
getAdminBrand(): Promise<BrandProfile> { return this.request("/admin/config/brand"); }
|
getAdminBrand(): Promise<BrandProfile> { return this.request("/admin/config/brand"); }
|
||||||
updateAdminBrand(body: BrandUpdate): Promise<BrandProfile> { return this.request("/admin/config/brand", { method: "PATCH", body }); }
|
updateAdminBrand(body: BrandUpdate): Promise<BrandProfile> { return this.request("/admin/config/brand", { method: "PATCH", body }); }
|
||||||
updateAdminService(id: string, body: ServiceUpdate): Promise<PublicService> { return this.request(`/admin/config/services/${encodeURIComponent(id)}`, { method: "PATCH", body }); }
|
updateAdminService(id: string, body: ServiceUpdate): Promise<PublicService> { return this.request(`/admin/config/services/${encodeURIComponent(id)}`, { method: "PATCH", body }); }
|
||||||
|
listAdminServices(): Promise<PublicService[]> { return this.request("/admin/config/services"); }
|
||||||
|
createAdminService(body: ServiceUpdate & { name: string; description: string }): Promise<PublicService> { return this.request("/admin/config/services", { method: "POST", body }); }
|
||||||
private async request<T>(path: string, options: { method?: string; body?: unknown } = {}): Promise<T> {
|
private async request<T>(path: string, options: { method?: string; body?: unknown } = {}): Promise<T> {
|
||||||
const headers: Record<string, string> = { accept: "application/json" };
|
const headers: Record<string, string> = { accept: "application/json" };
|
||||||
if (options.body !== undefined) headers["content-type"] = "application/json";
|
if (options.body !== undefined) headers["content-type"] = "application/json";
|
||||||
|
|||||||
Reference in new issue
Block a user