feat: 完成多品牌隔离专项验收

This commit is contained in:
que01 committed 2026-09-23 13:22:28 +08:00
1 parent 1a2f179e15
commit dcb40f629b
16 files changed
+197 -51

No files matched your search

+2
View File
@@ -8,6 +8,7 @@ import { AdminAuthService } from "./admin-auth.service.js";
import { ApiExceptionFilter, RequestIdMiddleware } from "./http.js";
import { AdminWorkspaceController } from "./admin-workspace.controller.js";
import { PublicWorkspaceController } from "./modules/public/public-workspace.controller.js";
import { PublicBrandContextService } from "./modules/public/public-brand-context.service.js";
import { AdminConfigController } from "./modules/admin/admin-config.controller.js";
import { PublicOfferingController } from "./modules/public/public-offering.controller.js";
import { PublicInquiryFormController } from "./modules/public/public-inquiry-form.controller.js";
@@ -62,6 +63,7 @@ export class HealthController {
TemplateWorkspaceService,
AdminAccessService,
AdminMembershipGuard,
PublicBrandContextService,
],
exports: [PrismaService, WechatAuthService],
})
@@ -0,0 +1,19 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { PrismaService } from "../../prisma.service.js";
const FALLBACK_BRAND_ID = "00000000-0000-0000-0000-000000000001";
@Injectable()
export class PublicBrandContextService {
constructor(private readonly prisma: PrismaService) {}
async resolveId(brandSlug?: string): Promise<string> {
if (!brandSlug) return process.env.DEFAULT_BRAND_ID ?? FALLBACK_BRAND_ID;
const brand = await this.prisma.brandProfile.findUnique({
where: { slug: brandSlug },
select: { id: true },
});
if (!brand) throw new NotFoundException("品牌不存在");
return brand.id;
}
}
@@ -1,15 +1,19 @@
import { BadRequestException, Controller, Get, Param } from "@nestjs/common";
import { BadRequestException, Controller, Get, Param, Query } from "@nestjs/common";
import { PrismaService } from "../../prisma.service.js";
import { PublicBrandContextService } from "./public-brand-context.service.js";
@Controller("public/inquiry-forms")
export class PublicInquiryFormController {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly brandContext: PublicBrandContextService,
) {}
@Get(":id")
async get(@Param("id") id: string): Promise<unknown> {
async get(@Param("id") id: string, @Query("brandSlug") brandSlug?: string): Promise<unknown> {
const form = await this.prisma.inquiryForm.findFirst({
where: {
id,
brandId: process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
brandId: await this.brandContext.resolveId(brandSlug),
isEnabled: true,
OR: [{ offeringId: null }, { offering: { isEnabled: true } }],
},
@@ -1,13 +1,17 @@
import { BadRequestException, Controller, Get, Param } from "@nestjs/common";
import { BadRequestException, Controller, Get, Param, Query } from "@nestjs/common";
import { Prisma } from "@prisma/client";
import { PrismaService } from "../../prisma.service.js";
import { PublicBrandContextService } from "./public-brand-context.service.js";
@Controller("public/offerings")
export class PublicOfferingController {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly brandContext: PublicBrandContextService,
) {}
@Get()
async list(): Promise<unknown> {
const brandId = process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
async list(@Query("brandSlug") brandSlug?: string): Promise<unknown> {
const brandId = await this.brandContext.resolveId(brandSlug);
const items = await this.prisma.offering.findMany({
where: { brandId, isEnabled: true },
orderBy: { sortOrder: "asc" },
@@ -22,11 +26,12 @@ export class PublicOfferingController {
return items.map((item) => this.present(item));
}
@Get(":id")
async get(@Param("id") id: string): Promise<unknown> {
async get(@Param("id") id: string, @Query("brandSlug") brandSlug?: string): Promise<unknown> {
const brandId = await this.brandContext.resolveId(brandSlug);
const item = await this.prisma.offering.findFirst({
where: {
id,
brandId: process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
brandId,
isEnabled: true,
},
include: {
@@ -1,6 +1,7 @@
import { Controller, Get, NotFoundException, Query } from "@nestjs/common";
import { Controller, Get, Query } from "@nestjs/common";
import { Prisma } from "@prisma/client";
import { PrismaService } from "../../prisma.service.js";
import { PublicBrandContextService } from "./public-brand-context.service.js";
import {
getBrandTemplate,
getVisualTemplate,
@@ -16,19 +17,14 @@ import type {
@Controller("public/workspace")
export class PublicWorkspaceController {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly brandContext: PublicBrandContextService,
) {}
private readonly cache = new Map<string, { expiresAt: number; value: unknown }>();
@Get()
async get(@Query("brandSlug") brandSlug?: string): Promise<unknown> {
const configuredId = process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
const selected = brandSlug
? await this.prisma.brandProfile.findUnique({
where: { slug: brandSlug },
select: { id: true },
})
: null;
if (brandSlug && !selected) throw new NotFoundException("品牌不存在");
const brandId = selected?.id ?? configuredId;
const brandId = await this.brandContext.resolveId(brandSlug);
const cached = process.env.NODE_ENV === "test" ? undefined : this.cache.get(brandId);
if (cached && cached.expiresAt > Date.now()) return cached.value;
const [
@@ -1,5 +1,6 @@
import { BadRequestException, Controller, Get, Param, Query } from "@nestjs/common";
import { PrismaService } from "../../prisma.service.js";
import { PublicBrandContextService } from "./public-brand-context.service.js";
import {
brandTemplates,
getBrandTemplate,
@@ -10,13 +11,16 @@ import {
@Controller("public")
export class PublicController {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly brandContext: PublicBrandContextService,
) {}
@Get("brand")
async brand(): Promise<unknown> {
async brand(@Query("brandSlug") brandSlug?: string): Promise<unknown> {
return this.prisma.brandProfile.findUnique({
where: {
id: process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
id: await this.brandContext.resolveId(brandSlug),
},
select: {
id: true,
@@ -38,13 +42,16 @@ export class PublicController {
@Controller("public/trust-content")
export class PublicTrustContentController {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly brandContext: PublicBrandContextService,
) {}
@Get()
list(): Promise<unknown> {
async list(@Query("brandSlug") brandSlug?: string): Promise<unknown> {
return this.prisma.trustContent.findMany({
where: {
brandId: process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
brandId: await this.brandContext.resolveId(brandSlug),
isPublished: true,
},
orderBy: [{ type: "asc" }, { sortOrder: "asc" }],
+55
View File
@@ -790,6 +790,61 @@ test("admin can list and switch only enabled brand memberships", async () => {
assert.equal(denied.statusCode, 403);
});
test("brand switching isolates admin configuration, offerings, and public workspace", async () => {
const login = await request("/admin/session/login", {
method: "POST",
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
});
assert.equal(login.statusCode, 201);
const defaultToken = JSON.parse(login.body).token;
const memberships = JSON.parse(
(
await request("/admin/brands", {
headers: { authorization: `Bearer ${defaultToken}` },
})
).body,
);
const doorsBrand = memberships.find((item) => item.brand.slug === "linjing-doors")?.brand;
assert.ok(doorsBrand);
const switched = await request(`/admin/brands/${doorsBrand.id}/switch`, {
method: "POST",
headers: { authorization: `Bearer ${defaultToken}` },
});
assert.equal(switched.statusCode, 201);
const doorsToken = JSON.parse(switched.body).token;
const [defaultConfig, doorsConfig, defaultOfferings, doorsOfferings, doorsWorkspace] =
await Promise.all([
request("/admin/config/brand", {
headers: { authorization: `Bearer ${defaultToken}` },
}),
request("/admin/config/brand", {
headers: { authorization: `Bearer ${doorsToken}` },
}),
request("/admin/workspace/offerings", {
headers: { authorization: `Bearer ${defaultToken}` },
}),
request("/admin/workspace/offerings", {
headers: { authorization: `Bearer ${doorsToken}` },
}),
request("/public/workspace?brandSlug=linjing-doors"),
]);
assert.equal(defaultConfig.statusCode, 200);
assert.equal(doorsConfig.statusCode, 200);
assert.equal(JSON.parse(defaultConfig.body).id, brandId);
assert.equal(JSON.parse(doorsConfig.body).id, doorsBrand.id);
assert.equal(defaultOfferings.statusCode, 200);
assert.equal(doorsOfferings.statusCode, 200);
assert.notDeepEqual(
JSON.parse(defaultOfferings.body).map((item) => item.id),
JSON.parse(doorsOfferings.body).map((item) => item.id),
);
assert.equal(doorsWorkspace.statusCode, 200);
assert.equal(JSON.parse(doorsWorkspace.body).brand.id, doorsBrand.id);
assert.equal(JSON.parse(doorsWorkspace.body).template.key, "doors");
});
test("disabled brand membership blocks existing admin token", async () => {
const login = await request("/admin/session/login", {
method: "POST",