feat: 增加业务提供项管理与详情接口
This commit is contained in:
1 parent
a73e377f91
commit
066f6595e4
4 files changed
+152
No files matched your search
@@ -86,3 +86,21 @@ export class InstallTemplateDto {
|
||||
@IsIn(["personal", "food"])
|
||||
templateKey!: "personal" | "food";
|
||||
}
|
||||
|
||||
export class CreateOfferingDto {
|
||||
@IsIn(["service", "product", "package", "subscription"]) kind!: string;
|
||||
@IsString() name!: string;
|
||||
@IsOptional() @IsString() summary?: string;
|
||||
@IsOptional() @IsString() description?: 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() isEnabled?: boolean;
|
||||
@IsOptional() config?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export class UpdateOfferingDto extends CreateOfferingDto {
|
||||
@IsOptional() @IsIn(["service", "product", "package", "subscription"]) kind?: string;
|
||||
@IsOptional() @IsString() name?: string;
|
||||
}
|
||||
@@ -33,6 +33,8 @@ import {
|
||||
UpdateServiceDto,
|
||||
UpdateTrustContentDto,
|
||||
InstallTemplateDto,
|
||||
CreateOfferingDto,
|
||||
UpdateOfferingDto,
|
||||
} from "./admin.dto.js";
|
||||
import { AdminAuthService } from "./admin-auth.service.js";
|
||||
import { AdminLoginDto } from "./admin-auth.dto.js";
|
||||
@@ -146,12 +148,73 @@ export class PublicWorkspaceController {
|
||||
}
|
||||
}
|
||||
|
||||
@Controller("public/offerings")
|
||||
export class PublicOfferingController {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
@Get(":id")
|
||||
async get(@Param("id") id: string): Promise<unknown> {
|
||||
const item = await this.prisma.offering.findFirst({
|
||||
where: {
|
||||
id,
|
||||
brandId: process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
isEnabled: true,
|
||||
},
|
||||
});
|
||||
if (!item) throw new BadRequestException("业务提供项不存在");
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
@Controller("admin/workspace")
|
||||
export class AdminWorkspaceController {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
private brandId(): string {
|
||||
return process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
|
||||
}
|
||||
|
||||
@Get("offerings")
|
||||
listOfferings(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.prisma.offering.findMany({
|
||||
where: { brandId: this.brandId() },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
}
|
||||
|
||||
@Post("offerings")
|
||||
createOffering(
|
||||
@Req() request: FastifyRequest,
|
||||
@Body() body: CreateOfferingDto,
|
||||
): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
const { config, ...fields } = body;
|
||||
return this.prisma.offering.create({
|
||||
data: {
|
||||
...fields,
|
||||
brandId: this.brandId(),
|
||||
...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@Patch("offerings/:id")
|
||||
async updateOffering(
|
||||
@Req() request: FastifyRequest,
|
||||
@Param("id") id: string,
|
||||
@Body() body: UpdateOfferingDto,
|
||||
): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
const item = await this.prisma.offering.findFirst({ where: { id, brandId: this.brandId() } });
|
||||
if (!item) throw new BadRequestException("业务提供项不存在");
|
||||
const { config, ...fields } = body;
|
||||
return this.prisma.offering.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...fields,
|
||||
...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }),
|
||||
},
|
||||
});
|
||||
}
|
||||
private requireAdmin(request: FastifyRequest): void {
|
||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
||||
|
||||
@@ -78,6 +78,45 @@ test("admin can install a template and public workspace exposes enabled configur
|
||||
}
|
||||
});
|
||||
|
||||
test("admin can manage offerings and public endpoint hides disabled offerings", 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/offerings", {
|
||||
method: "POST",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: {
|
||||
kind: "product",
|
||||
name: `测试提供项-${Date.now()}`,
|
||||
summary: "测试摘要",
|
||||
isEnabled: true,
|
||||
},
|
||||
});
|
||||
assert.equal(created.statusCode, 201);
|
||||
const id = JSON.parse(created.body).id;
|
||||
try {
|
||||
const listed = await request("/admin/workspace/offerings", {
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(listed.statusCode, 200);
|
||||
assert.ok(JSON.parse(listed.body).some((item) => item.id === id));
|
||||
const publicItem = await request(`/public/offerings/${id}`);
|
||||
assert.equal(publicItem.statusCode, 200);
|
||||
const updated = await request(`/admin/workspace/offerings/${id}`, {
|
||||
method: "PATCH",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: { isEnabled: false },
|
||||
});
|
||||
assert.equal(updated.statusCode, 200);
|
||||
const hidden = await request(`/public/offerings/${id}`);
|
||||
assert.equal(hidden.statusCode, 400);
|
||||
} finally {
|
||||
await prisma.offering.delete({ where: { id } });
|
||||
}
|
||||
});
|
||||
|
||||
test("admin booking endpoints reject missing or customer credentials", async () => {
|
||||
const missing = await request("/admin/bookings");
|
||||
assert.equal(missing.statusCode, 401);
|
||||
|
||||
@@ -108,6 +108,20 @@ export interface BrandTemplateDefinition {
|
||||
serviceCategories: string[];
|
||||
trustContentTypes: string[];
|
||||
}
|
||||
export interface Offering {
|
||||
id: string;
|
||||
brandId?: string;
|
||||
kind: "service" | "product" | "package" | "subscription";
|
||||
name: string;
|
||||
summary?: string | null;
|
||||
description?: string | null;
|
||||
coverUrl?: string | null;
|
||||
priceText?: string | null;
|
||||
priceAmount?: number | null;
|
||||
isEnabled: boolean;
|
||||
sortOrder: number;
|
||||
config?: Record<string, unknown> | null;
|
||||
}
|
||||
|
||||
export class ApiClientError extends Error {
|
||||
constructor(
|
||||
@@ -218,6 +232,24 @@ export class ApiClient {
|
||||
getPublicTemplate(key: "personal" | "food"): Promise<BrandTemplateDefinition> {
|
||||
return this.request(`/public/templates/${key}`);
|
||||
}
|
||||
getPublicOffering(id: string): Promise<Offering> {
|
||||
return this.request(`/public/offerings/${encodeURIComponent(id)}`);
|
||||
}
|
||||
listAdminOfferings(): Promise<Offering[]> {
|
||||
return this.request("/admin/workspace/offerings");
|
||||
}
|
||||
createAdminOffering(body: Omit<Offering, "id" | "brandId">): Promise<Offering> {
|
||||
return this.request("/admin/workspace/offerings", { method: "POST", body });
|
||||
}
|
||||
updateAdminOffering(
|
||||
id: string,
|
||||
body: Partial<Omit<Offering, "id" | "brandId">>,
|
||||
): Promise<Offering> {
|
||||
return this.request(`/admin/workspace/offerings/${encodeURIComponent(id)}`, {
|
||||
method: "PATCH",
|
||||
body,
|
||||
});
|
||||
}
|
||||
updateAdminService(id: string, body: ServiceUpdate): Promise<PublicService> {
|
||||
return this.request(`/admin/config/services/${encodeURIComponent(id)}`, {
|
||||
method: "PATCH",
|
||||
|
||||
Reference in new issue
Block a user