feat: 收口多品牌与模板构建基础

This commit is contained in:
que01 committed 2026-09-25 02:55:46 +08:00
1 parent 0425a1c672
commit 77224ea51f
317 files changed
+11290 -1158

No files matched your search

@@ -1,8 +1,12 @@
import {
Controller,
BadRequestException,
Body,
ConflictException,
ForbiddenException,
Get,
Param,
Patch,
Post,
Req,
UnauthorizedException,
@@ -10,10 +14,105 @@ import {
import type { FastifyRequest } from "fastify";
import { PrismaService } from "../../prisma.service.js";
import { readIdentity, signIdentity } from "../../auth.js";
import { AdminAccessService } from "../../admin-access.service.js";
import { CreateBrandMemberDto, UpdateBrandMemberDto } from "../../admin.dto.js";
import bcrypt from "bcryptjs";
@Controller("admin/brands")
export class TenantController {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly access: AdminAccessService,
) {}
@Get("members")
async listMembers(@Req() request: FastifyRequest): Promise<unknown> {
await this.access.requireRole(request, ["owner", "admin", "platform-admin"]);
const current = this.adminIdentity(request);
return this.prisma.brandMembership.findMany({
where: { brandId: current.brandId },
include: {
adminUser: { select: { id: true, username: true, displayName: true, isEnabled: true } },
},
orderBy: { createdAt: "asc" },
});
}
@Post("members")
async createMember(
@Req() request: FastifyRequest,
@Body() body: CreateBrandMemberDto,
): Promise<unknown> {
await this.access.requireRole(request, ["owner", "admin", "platform-admin"]);
const actor = this.adminIdentity(request);
const username = body.username.trim();
const displayName = body.displayName.trim();
if (!username || !displayName) throw new BadRequestException("账号和显示名称不能为空");
const existing = await this.prisma.adminUser.findUnique({ where: { username } });
if (existing) throw new ConflictException("账号已存在,请使用其他账号");
const passwordHash = await bcrypt.hash(body.password, 12);
const member = await this.prisma.$transaction(async (tx) => {
const adminUser = await tx.adminUser.create({
data: {
username,
displayName,
passwordHash,
role: "admin",
isEnabled: true,
mustChangePassword: true,
},
});
return tx.brandMembership.create({
data: {
adminUserId: adminUser.id,
brandId: actor.brandId,
role: body.role,
isEnabled: true,
},
include: {
adminUser: { select: { id: true, username: true, displayName: true, isEnabled: true } },
},
});
});
return member;
}
@Patch("members/:id")
async updateMember(
@Req() request: FastifyRequest,
@Param("id") id: string,
@Body() body: UpdateBrandMemberDto,
): Promise<unknown> {
await this.access.requireRole(request, ["owner", "admin", "platform-admin"]);
const actor = this.adminIdentity(request);
const membership = await this.prisma.brandMembership.findFirst({
where: { id, brandId: actor.brandId },
});
if (!membership) throw new BadRequestException("成员不存在");
if (membership.adminUserId === actor.subject) {
if (body.isEnabled === false || (body.role && body.role !== membership.role)) {
throw new BadRequestException("不能修改自己的角色或停用自己");
}
}
const nextRole = body.role ?? membership.role;
const nextEnabled = body.isEnabled ?? membership.isEnabled;
if (membership.role === "owner" && (nextRole !== "owner" || !nextEnabled)) {
const ownerCount = await this.prisma.brandMembership.count({
where: { brandId: actor.brandId, role: "owner", isEnabled: true },
});
if (ownerCount <= 1) throw new BadRequestException("品牌至少需要保留一位启用的负责人");
}
return this.prisma.brandMembership.update({
where: { id: membership.id },
data: {
...(body.role ? { role: body.role } : {}),
...(body.isEnabled === undefined ? {} : { isEnabled: body.isEnabled }),
},
include: {
adminUser: { select: { id: true, username: true, displayName: true, isEnabled: true } },
},
});
}
@Get()
async list(@Req() request: FastifyRequest): Promise<unknown> {