refactor: 将数据库访问从控制器下沉到服务层
This commit is contained in:
1 parent
f158f906d5
commit
3ae7b462f5
21 files changed
+1046
-857
No files matched your search
@@ -0,0 +1,114 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
} from "@nestjs/common";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { PrismaService } from "../../prisma.service.js";
|
||||
import { readIdentity, signIdentity } from "../../auth.js";
|
||||
import type { CreateBrandMemberDto, UpdateBrandMemberDto } from "../../admin.dto.js";
|
||||
|
||||
type AdminIdentity = ReturnType<typeof readIdentity>;
|
||||
|
||||
@Injectable()
|
||||
export class TenantService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
listMembers(brandId: string) {
|
||||
return this.prisma.brandMembership.findMany({
|
||||
where: { brandId },
|
||||
include: {
|
||||
adminUser: { select: { id: true, username: true, displayName: true, isEnabled: true } },
|
||||
},
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
}
|
||||
|
||||
async createMember(brandId: string, body: CreateBrandMemberDto) {
|
||||
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);
|
||||
return 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,
|
||||
role: body.role,
|
||||
isEnabled: true,
|
||||
},
|
||||
include: {
|
||||
adminUser: { select: { id: true, username: true, displayName: true, isEnabled: true } },
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async updateMember(
|
||||
brandId: string,
|
||||
actorSubject: string,
|
||||
id: string,
|
||||
body: UpdateBrandMemberDto,
|
||||
) {
|
||||
const membership = await this.prisma.brandMembership.findFirst({
|
||||
where: { id, brandId },
|
||||
});
|
||||
if (!membership) throw new BadRequestException("成员不存在");
|
||||
if (membership.adminUserId === actorSubject) {
|
||||
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, 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 } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
listMemberships(subject: string) {
|
||||
return this.prisma.brandMembership.findMany({
|
||||
where: { adminUserId: subject, isEnabled: true },
|
||||
include: { brand: { select: { id: true, slug: true, name: true, templateKey: true } } },
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
}
|
||||
|
||||
async switchBrand(identity: AdminIdentity, brandId: string) {
|
||||
const membership = await this.prisma.brandMembership.findFirst({
|
||||
where: { adminUserId: identity.subject, brandId, isEnabled: true },
|
||||
include: { brand: { select: { id: true, slug: true, name: true, templateKey: true } } },
|
||||
});
|
||||
if (!membership) throw new ForbiddenException("没有该品牌的管理权限");
|
||||
return {
|
||||
token: signIdentity({ subject: identity.subject, brandId, role: "admin" }),
|
||||
brand: membership.brand,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user