feat: 增加管理员品牌归属关系

This commit is contained in:
que01 committed 2026-09-23 02:10:19 +08:00
1 parent fef0a890ad
commit ee3bd9051b
4 files changed
+52 -2

No files matched your search

@@ -0,0 +1,24 @@
-- CreateTable
CREATE TABLE "BrandMembership" (
"id" TEXT NOT NULL,
"adminUserId" TEXT NOT NULL,
"brandId" TEXT NOT NULL,
"role" TEXT NOT NULL DEFAULT 'admin',
"isEnabled" BOOLEAN NOT NULL DEFAULT true,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "BrandMembership_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "BrandMembership_brandId_isEnabled_idx" ON "BrandMembership"("brandId", "isEnabled");
-- CreateIndex
CREATE UNIQUE INDEX "BrandMembership_adminUserId_brandId_key" ON "BrandMembership"("adminUserId", "brandId");
-- AddForeignKey
ALTER TABLE "BrandMembership" ADD CONSTRAINT "BrandMembership_adminUserId_fkey" FOREIGN KEY ("adminUserId") REFERENCES "AdminUser"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "BrandMembership" ADD CONSTRAINT "BrandMembership_brandId_fkey" FOREIGN KEY ("brandId") REFERENCES "BrandProfile"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+16
View File
@@ -60,6 +60,7 @@ model BrandProfile {
inquirySubmissions InquirySubmission[]
capabilityInstallations CapabilityInstallation[]
capabilityAuditLogs CapabilityAuditLog[]
adminMemberships BrandMembership[]
}
model CapabilityInstallation {
@@ -327,6 +328,21 @@ model AdminUser {
lastLoginAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
memberships BrandMembership[]
}
model BrandMembership {
id String @id @default(uuid())
adminUserId String
brandId String
role String @default("admin")
isEnabled Boolean @default(true)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
adminUser AdminUser @relation(fields: [adminUserId], references: [id], onDelete: Cascade)
brand BrandProfile @relation(fields: [brandId], references: [id], onDelete: Cascade)
@@unique([adminUserId, brandId])
@@index([brandId, isEnabled])
}
model BookingTag {
+6 -1
View File
@@ -59,7 +59,7 @@ async function main(): Promise<void> {
create: { brandId: brand.id, name, isPreset: true },
});
}
await prisma.adminUser.upsert({
const admin = await prisma.adminUser.upsert({
where: { username: "admin" },
update: {},
create: {
@@ -68,6 +68,11 @@ async function main(): Promise<void> {
displayName: "品牌管理员",
},
});
await prisma.brandMembership.upsert({
where: { adminUserId_brandId: { adminUserId: admin.id, brandId: brand.id } },
update: { isEnabled: true, role: "owner" },
create: { adminUserId: admin.id, brandId: brand.id, role: "owner" },
});
}
main().finally(() => prisma.$disconnect());
+6 -1
View File
@@ -16,10 +16,15 @@ export class AdminAuthService {
where: { id: user.id },
data: { lastLoginAt: new Date() },
});
const membership = await this.prisma.brandMembership.findFirst({
where: { adminUserId: user.id, isEnabled: true },
orderBy: { createdAt: "asc" },
});
if (!membership) throw new UnauthorizedException("账号尚未配置品牌权限");
return {
token: signIdentity({
subject: user.id,
brandId: process.env.DEFAULT_BRAND_ID ?? "",
brandId: membership.brandId,
role: "admin",
}),
displayName: user.displayName,