From ee3bd9051b0e6c89698160f98519ca0d6bbbcc3d Mon Sep 17 00:00:00 2001 From: que01 Date: Wed, 23 Sep 2026 02:10:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=91=98=E5=93=81=E7=89=8C=E5=BD=92=E5=B1=9E=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migration.sql | 24 +++++++++++++++++++ apps/api/prisma/schema.prisma | 16 +++++++++++++ apps/api/prisma/seed.ts | 7 +++++- apps/api/src/admin-auth.service.ts | 7 +++++- 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 apps/api/prisma/migrations/20260922180928_add_brand_memberships/migration.sql diff --git a/apps/api/prisma/migrations/20260922180928_add_brand_memberships/migration.sql b/apps/api/prisma/migrations/20260922180928_add_brand_memberships/migration.sql new file mode 100644 index 0000000..b646f43 --- /dev/null +++ b/apps/api/prisma/migrations/20260922180928_add_brand_memberships/migration.sql @@ -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; diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index 903b8fa..41a807c 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -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 { diff --git a/apps/api/prisma/seed.ts b/apps/api/prisma/seed.ts index cc712f8..271bbcf 100644 --- a/apps/api/prisma/seed.ts +++ b/apps/api/prisma/seed.ts @@ -59,7 +59,7 @@ async function main(): Promise { 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 { 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()); diff --git a/apps/api/src/admin-auth.service.ts b/apps/api/src/admin-auth.service.ts index 3a33631..c247411 100644 --- a/apps/api/src/admin-auth.service.ts +++ b/apps/api/src/admin-auth.service.ts @@ -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,