refactor: 统一服务兼容接口的数据来源
This commit is contained in:
1 parent
537838532a
commit
ca85a583ce
2 files changed
+32
-13
No files matched your search
+26
-13
@@ -79,21 +79,16 @@ export class PublicController {
|
|||||||
|
|
||||||
@Get("services")
|
@Get("services")
|
||||||
async services(): Promise<unknown> {
|
async services(): Promise<unknown> {
|
||||||
return this.prisma.service.findMany({
|
const offerings = await this.prisma.offering.findMany({
|
||||||
where: { isEnabled: true },
|
where: { isEnabled: true },
|
||||||
orderBy: { sortOrder: "asc" },
|
orderBy: { sortOrder: "asc" },
|
||||||
select: {
|
include: { services: { where: { isEnabled: true }, take: 1 } },
|
||||||
id: true,
|
|
||||||
name: true,
|
|
||||||
description: true,
|
|
||||||
coverUrl: true,
|
|
||||||
priceText: true,
|
|
||||||
priceAmount: true,
|
|
||||||
bookingEnabled: true,
|
|
||||||
categoryKey: true,
|
|
||||||
config: true,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
return offerings.map(({ services, ...offering }) => ({
|
||||||
|
...offering,
|
||||||
|
bookingEnabled: services[0]?.bookingEnabled ?? false,
|
||||||
|
categoryKey: services[0]?.categoryKey ?? offering.kind,
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -613,13 +608,31 @@ export class AdminConfigController {
|
|||||||
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("服务不存在");
|
||||||
const { config, ...fields } = body;
|
const { config, ...fields } = body;
|
||||||
return this.prisma.service.update({
|
return this.prisma.$transaction(async (tx) => {
|
||||||
|
const updated = await tx.service.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
...fields,
|
...fields,
|
||||||
...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }),
|
...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
if (service.offeringId) {
|
||||||
|
await tx.offering.update({
|
||||||
|
where: { id: service.offeringId },
|
||||||
|
data: {
|
||||||
|
name: updated.name,
|
||||||
|
description: updated.description,
|
||||||
|
coverUrl: updated.coverUrl,
|
||||||
|
priceText: updated.priceText,
|
||||||
|
priceAmount: updated.priceAmount,
|
||||||
|
isEnabled: updated.isEnabled,
|
||||||
|
sortOrder: updated.sortOrder,
|
||||||
|
config: updated.config ?? Prisma.JsonNull,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return updated;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -378,6 +378,12 @@ 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 publicServices = await request("/public/services");
|
||||||
|
assert.equal(publicServices.statusCode, 200);
|
||||||
|
const projectedService = JSON.parse(publicServices.body).find(
|
||||||
|
(item) => item.id === serviceBefore.offeringId,
|
||||||
|
);
|
||||||
|
assert.equal(projectedService.priceText, "集成测试价格");
|
||||||
|
|
||||||
const listedServices = await request("/admin/config/services", {
|
const listedServices = await request("/admin/config/services", {
|
||||||
headers: { authorization: `Bearer ${adminToken}` },
|
headers: { authorization: `Bearer ${adminToken}` },
|
||||||
|
|||||||
Reference in new issue
Block a user