feat: 支持视觉模板历史回滚
This commit is contained in:
1 parent
a951504563
commit
a30c939f90
4 files changed
+86
No files matched your search
@@ -702,6 +702,53 @@ export class AdminConfigController {
|
||||
: [],
|
||||
}));
|
||||
}
|
||||
@Post("visual-template/rollback")
|
||||
async rollbackVisualTemplate(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
const brandId = this.brandId();
|
||||
const [brand, installation] = await Promise.all([
|
||||
this.prisma.brandProfile.findUnique({ where: { id: brandId }, select: { config: true } }),
|
||||
this.prisma.brandTemplateInstallation.findFirst({ where: { brandId } }),
|
||||
]);
|
||||
const config =
|
||||
brand?.config && typeof brand.config === "object" && !Array.isArray(brand.config)
|
||||
? (brand.config as Record<string, unknown>)
|
||||
: {};
|
||||
const history = Array.isArray(installation?.previousSnapshots)
|
||||
? installation.previousSnapshots
|
||||
: [];
|
||||
const index = [...history]
|
||||
.map((item, i) => ({ item, i }))
|
||||
.reverse()
|
||||
.find(
|
||||
({ item }) =>
|
||||
item && typeof item === "object" && !Array.isArray(item) && "visualTemplateKey" in item,
|
||||
)?.i;
|
||||
const previous =
|
||||
index === undefined ? undefined : (history[index] as { visualTemplateKey?: unknown });
|
||||
if (!previous || typeof previous.visualTemplateKey !== "string")
|
||||
throw new BadRequestException("没有可回滚的视觉模板版本");
|
||||
await this.prisma.$transaction([
|
||||
this.prisma.brandProfile.update({
|
||||
where: { id: brandId },
|
||||
data: {
|
||||
config: {
|
||||
...config,
|
||||
visualTemplateKey: previous.visualTemplateKey,
|
||||
} as Prisma.InputJsonValue,
|
||||
},
|
||||
}),
|
||||
...(installation
|
||||
? [
|
||||
this.prisma.brandTemplateInstallation.update({
|
||||
where: { id: installation.id },
|
||||
data: { previousSnapshots: history.slice(0, index) },
|
||||
}),
|
||||
]
|
||||
: []),
|
||||
]);
|
||||
return { visualTemplateKey: previous.visualTemplateKey, rolledBack: true };
|
||||
}
|
||||
@Get("visual-template/preview")
|
||||
getVisualTemplatePreview(@Req() request: FastifyRequest): unknown {
|
||||
this.requireAdmin(request);
|
||||
|
||||
Reference in new issue
Block a user