feat: 增加能力插件启停与审计
This commit is contained in:
1 parent
0fb96bdea3
commit
8a02d09e2d
6 files changed
+147
-1
No files matched your search
@@ -25,7 +25,9 @@ import {
|
||||
CreateInquiryFormDto,
|
||||
UpdateInquiryFormDto,
|
||||
UpdateInquirySubmissionDto,
|
||||
UpdateCapabilityInstallationDto,
|
||||
} from "./admin.dto.js";
|
||||
import { capabilityPlugins, getBrandTemplate } from "@personal-brand/config";
|
||||
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
||||
import { validateFieldSnapshot, validateOfferingConfig } from "@personal-brand/validation";
|
||||
|
||||
@@ -130,7 +132,11 @@ class AdminWorkspaceController {
|
||||
await this.prisma.offering.delete({ where: { id } });
|
||||
return { deleted: true };
|
||||
}
|
||||
private requireAdmin(request: FastifyRequest): void {
|
||||
private requireAdmin(request: FastifyRequest): {
|
||||
subject: string;
|
||||
brandId: string;
|
||||
role: "admin";
|
||||
} {
|
||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
||||
let identity;
|
||||
@@ -140,6 +146,7 @@ class AdminWorkspaceController {
|
||||
throw new UnauthorizedException("管理员登录态无效或已过期");
|
||||
}
|
||||
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
|
||||
return identity as { subject: string; brandId: string; role: "admin" };
|
||||
}
|
||||
@Get()
|
||||
async get(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
@@ -167,6 +174,58 @@ class AdminWorkspaceController {
|
||||
return this.templateWorkspace.install(this.brandId(), body.templateKey);
|
||||
}
|
||||
|
||||
@Get("capabilities")
|
||||
async listCapabilities(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.prisma.capabilityInstallation.findMany({ where: { brandId: this.brandId() } });
|
||||
}
|
||||
|
||||
@Patch("capabilities/:key")
|
||||
async updateCapability(
|
||||
@Req() request: FastifyRequest,
|
||||
@Param("key") key: string,
|
||||
@Body() body: UpdateCapabilityInstallationDto,
|
||||
): Promise<unknown> {
|
||||
const identity = this.requireAdmin(request);
|
||||
const brandId = this.brandId();
|
||||
const brand = await this.prisma.brandProfile.findUnique({ where: { id: brandId } });
|
||||
const template = getBrandTemplate(brand?.templateKey ?? "personal");
|
||||
if (!template?.capabilities.includes(key as never) && !(key in capabilityPlugins))
|
||||
throw new BadRequestException("能力未被当前模板声明");
|
||||
const existing = await this.prisma.capabilityInstallation.findUnique({
|
||||
where: { brandId_capabilityKey: { brandId, capabilityKey: key } },
|
||||
});
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
const installation = await tx.capabilityInstallation.upsert({
|
||||
where: { brandId_capabilityKey: { brandId, capabilityKey: key } },
|
||||
create: {
|
||||
brandId,
|
||||
capabilityKey: key,
|
||||
capabilityVersion: body.capabilityVersion,
|
||||
status: body.status,
|
||||
config: (body.config ?? {}) as Prisma.InputJsonValue,
|
||||
},
|
||||
update: {
|
||||
capabilityVersion: body.capabilityVersion,
|
||||
status: body.status,
|
||||
...(body.config === undefined ? {} : { config: body.config as Prisma.InputJsonValue }),
|
||||
},
|
||||
});
|
||||
await tx.capabilityAuditLog.create({
|
||||
data: {
|
||||
brandId,
|
||||
capabilityKey: key,
|
||||
actorId: identity.subject,
|
||||
action: existing ? "updated" : "installed",
|
||||
fromStatus: existing?.status ?? null,
|
||||
toStatus: body.status,
|
||||
config: (body.config ?? existing?.config ?? {}) as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
return installation;
|
||||
});
|
||||
}
|
||||
|
||||
@Get("template/preview")
|
||||
async previewTemplate(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
|
||||
Reference in new issue
Block a user