refactor: 拆分模板工作区服务并统一后台访问控制
This commit is contained in:
1 parent
5051839986
commit
b4e3e86dcd
13 files changed
+441
-329
No files matched your search
@@ -1,22 +1,12 @@
|
||||
import { AdminBrand } from "../../admin-auth.decorators.js";
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
ForbiddenException,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
UnauthorizedException,
|
||||
} from "@nestjs/common";
|
||||
import { Body, Controller, Get, Param, Post, Query, Req } from "@nestjs/common";
|
||||
import { Transform } from "class-transformer";
|
||||
import type { TransformFnParams } from "class-transformer";
|
||||
import { BookingStatus } from "@prisma/client";
|
||||
import { IsBoolean, IsEnum, IsOptional, IsString } from "class-validator";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { AdminAccessService } from "../../admin-access.service.js";
|
||||
import { BookingService } from "../../booking.service.js";
|
||||
import { readIdentity } from "../../auth.js";
|
||||
import { UpdateBookingStatusDto, UpdateBookingTagsDto, CreateTagDto } from "../../admin.dto.js";
|
||||
|
||||
class AdminBookingQueryDto {
|
||||
@@ -35,67 +25,67 @@ class AdminBookingQueryDto {
|
||||
@AdminBrand()
|
||||
@Controller("admin/bookings")
|
||||
export class AdminBookingController {
|
||||
constructor(private readonly bookings: BookingService) {}
|
||||
constructor(
|
||||
private readonly access: AdminAccessService,
|
||||
private readonly bookings: BookingService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
list(@Req() request: FastifyRequest, @Query() query: AdminBookingQueryDto): Promise<unknown> {
|
||||
const brandId = this.requireAdmin(request);
|
||||
async list(
|
||||
@Req() request: FastifyRequest,
|
||||
@Query() query: AdminBookingQueryDto,
|
||||
): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.listForAdmin(brandId, query);
|
||||
}
|
||||
|
||||
@Get("unread-count")
|
||||
unreadCount(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
return this.bookings.unreadCountForAdmin(this.requireAdmin(request));
|
||||
async unreadCount(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.unreadCountForAdmin(brandId);
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
detail(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
return this.bookings.getForAdmin(this.requireAdmin(request), id);
|
||||
async detail(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.getForAdmin(brandId, id);
|
||||
}
|
||||
|
||||
@Post(":id/status")
|
||||
update(
|
||||
async update(
|
||||
@Req() request: FastifyRequest,
|
||||
@Param("id") id: string,
|
||||
@Body() body: UpdateBookingStatusDto,
|
||||
): Promise<unknown> {
|
||||
return this.bookings.updateStatus(this.requireAdmin(request), id, body.status, body.adminNote);
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.updateStatus(brandId, id, body.status, body.adminNote);
|
||||
}
|
||||
|
||||
@Post(":id/read")
|
||||
markRead(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
return this.bookings.markRead(this.requireAdmin(request), id);
|
||||
async markRead(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.markRead(brandId, id);
|
||||
}
|
||||
|
||||
@Post(":id/tags")
|
||||
updateTags(
|
||||
async updateTags(
|
||||
@Req() request: FastifyRequest,
|
||||
@Param("id") id: string,
|
||||
@Body() body: UpdateBookingTagsDto,
|
||||
): Promise<unknown> {
|
||||
return this.bookings.updateTags(this.requireAdmin(request), id, body.tagIds);
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.updateTags(brandId, id, body.tagIds);
|
||||
}
|
||||
|
||||
@Get("tags")
|
||||
listTags(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
return this.bookings.listTags(this.requireAdmin(request));
|
||||
async listTags(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.listTags(brandId);
|
||||
}
|
||||
|
||||
@Post("tags")
|
||||
createTag(@Req() request: FastifyRequest, @Body() body: CreateTagDto): Promise<unknown> {
|
||||
return this.bookings.createTag(this.requireAdmin(request), body.name);
|
||||
}
|
||||
|
||||
private requireAdmin(request: FastifyRequest): string {
|
||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
||||
let identity;
|
||||
try {
|
||||
identity = readIdentity(token);
|
||||
} catch {
|
||||
throw new UnauthorizedException("管理员登录态无效或已过期");
|
||||
}
|
||||
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
|
||||
return identity.brandId;
|
||||
async createTag(@Req() request: FastifyRequest, @Body() body: CreateTagDto): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.createTag(brandId, body.name);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
UpdateBrandPresentationDto,
|
||||
UpdateTrustContentDto,
|
||||
} from "../../admin.dto.js";
|
||||
import { TemplateWorkspaceService } from "../../template-workspace.service.js";
|
||||
import { TrustContentService } from "../../template/trust-content.service.js";
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
import { AdminBrandConfigService } from "./admin-brand-config.service.js";
|
||||
|
||||
@@ -15,7 +15,7 @@ import { AdminBrandConfigService } from "./admin-brand-config.service.js";
|
||||
@Controller("admin/config")
|
||||
export class AdminConfigController {
|
||||
constructor(
|
||||
private readonly templateWorkspace: TemplateWorkspaceService,
|
||||
private readonly trustContent: TrustContentService,
|
||||
private readonly brandConfig: AdminBrandConfigService,
|
||||
) {}
|
||||
@Get("brand")
|
||||
@@ -42,7 +42,7 @@ export class AdminConfigController {
|
||||
@Get("trust-content")
|
||||
listTrustContent(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
const identity = requireAdminIdentity(request);
|
||||
return this.templateWorkspace.listTrustContent(identity.brandId);
|
||||
return this.trustContent.listTrustContent(identity.brandId);
|
||||
}
|
||||
@Post("trust-content")
|
||||
createTrustContent(
|
||||
@@ -50,7 +50,7 @@ export class AdminConfigController {
|
||||
@Body() body: CreateTrustContentDto,
|
||||
): Promise<unknown> {
|
||||
const identity = requireAdminIdentity(request);
|
||||
return this.templateWorkspace.createTrustContent(identity.brandId, body);
|
||||
return this.trustContent.createTrustContent(identity.brandId, body);
|
||||
}
|
||||
@Patch("trust-content/:id")
|
||||
updateTrustContent(
|
||||
@@ -59,7 +59,7 @@ export class AdminConfigController {
|
||||
@Body() body: UpdateTrustContentDto,
|
||||
): Promise<unknown> {
|
||||
const identity = requireAdminIdentity(request);
|
||||
return this.templateWorkspace.updateTrustContent(identity.brandId, id, body);
|
||||
return this.trustContent.updateTrustContent(identity.brandId, id, body);
|
||||
}
|
||||
@Delete("trust-content/:id")
|
||||
async deleteTrustContent(
|
||||
@@ -67,6 +67,6 @@ export class AdminConfigController {
|
||||
@Param("id") id: string,
|
||||
): Promise<unknown> {
|
||||
const identity = requireAdminIdentity(request);
|
||||
return this.templateWorkspace.deleteTrustContent(identity.brandId, id);
|
||||
return this.trustContent.deleteTrustContent(identity.brandId, id);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,9 @@ import { AdminMembershipGuard } from "../admin-membership.guard.js";
|
||||
import { BrandReleaseApiService } from "../brand-release-api.service.js";
|
||||
import { BrandReleaseService } from "../brand-release.service.js";
|
||||
import { PrismaService } from "../prisma.service.js";
|
||||
import { TemplateWorkspaceService } from "../template-workspace.service.js";
|
||||
import { PageSchemeService } from "../template/page-scheme.service.js";
|
||||
import { TemplateInstallationService } from "../template/template-installation.service.js";
|
||||
import { TrustContentService } from "../template/trust-content.service.js";
|
||||
import { WechatAuthService } from "../wechat-auth.js";
|
||||
|
||||
@Module({
|
||||
@@ -15,7 +17,9 @@ import { WechatAuthService } from "../wechat-auth.js";
|
||||
AdminMembershipGuard,
|
||||
BrandReleaseService,
|
||||
BrandReleaseApiService,
|
||||
TemplateWorkspaceService,
|
||||
PageSchemeService,
|
||||
TemplateInstallationService,
|
||||
TrustContentService,
|
||||
],
|
||||
exports: [
|
||||
PrismaService,
|
||||
@@ -24,7 +28,9 @@ import { WechatAuthService } from "../wechat-auth.js";
|
||||
AdminMembershipGuard,
|
||||
BrandReleaseService,
|
||||
BrandReleaseApiService,
|
||||
TemplateWorkspaceService,
|
||||
PageSchemeService,
|
||||
TemplateInstallationService,
|
||||
TrustContentService,
|
||||
],
|
||||
})
|
||||
export class InfrastructureModule {}
|
||||
@@ -14,7 +14,8 @@ import type {
|
||||
CreateBrandMemberDto,
|
||||
UpdateBrandMemberDto,
|
||||
} from "../../admin.dto.js";
|
||||
import { TemplateWorkspaceService } from "../../template-workspace.service.js";
|
||||
import { PageSchemeService } from "../../template/page-scheme.service.js";
|
||||
import { TemplateInstallationService } from "../../template/template-installation.service.js";
|
||||
|
||||
type AdminIdentity = { subject: string; brandId: string; tokenVersion?: number };
|
||||
|
||||
@@ -22,11 +23,12 @@ type AdminIdentity = { subject: string; brandId: string; tokenVersion?: number }
|
||||
export class TenantService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly templateWorkspace: TemplateWorkspaceService,
|
||||
private readonly pageSchemes: PageSchemeService,
|
||||
private readonly templateInstallations: TemplateInstallationService,
|
||||
) {}
|
||||
|
||||
async getCreationOptions() {
|
||||
const catalog = await this.templateWorkspace.getPageSchemeCatalog();
|
||||
const catalog = await this.pageSchemes.getPageSchemeCatalog();
|
||||
// 创建品牌只应从每个页面方案的最新已发布版本开始;旧版本仍可在页面方案页显式切换。
|
||||
return {
|
||||
...catalog,
|
||||
@@ -40,7 +42,7 @@ export class TenantService {
|
||||
const name = body.name.trim();
|
||||
const slug = body.slug.trim();
|
||||
if (!name || !slug) throw new BadRequestException("品牌名称和小程序地址标识不能为空");
|
||||
const packageVersion = await this.templateWorkspace.resolveInstallablePackageVersion(
|
||||
const packageVersion = await this.templateInstallations.resolveInstallablePackageVersion(
|
||||
body.businessType,
|
||||
body.templatePackageVersionId,
|
||||
);
|
||||
|
||||
@@ -2,24 +2,28 @@ import { AdminBrand, AdminRoles } from "../../admin-auth.decorators.js";
|
||||
import { Body, Controller, Get, Post, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { RollbackTemplateDto, UpgradeTemplateDto } from "../../admin.dto.js";
|
||||
import { TemplateWorkspaceService } from "../../template-workspace.service.js";
|
||||
import { PageSchemeService } from "../../template/page-scheme.service.js";
|
||||
import { TemplateInstallationService } from "../../template/template-installation.service.js";
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/workspace")
|
||||
class AdminTemplateController {
|
||||
constructor(private readonly templateWorkspace: TemplateWorkspaceService) {}
|
||||
constructor(
|
||||
private readonly pageSchemes: PageSchemeService,
|
||||
private readonly templateInstallations: TemplateInstallationService,
|
||||
) {}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Get("template/options")
|
||||
options(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
return this.templateWorkspace.getPageSchemeOptions(requireAdminIdentity(request).brandId);
|
||||
return this.pageSchemes.getPageSchemeOptions(requireAdminIdentity(request).brandId);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Get("template/preview")
|
||||
preview(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
return this.templateWorkspace.preview(requireAdminIdentity(request).brandId);
|
||||
return this.templateInstallations.preview(requireAdminIdentity(request).brandId);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@@ -30,7 +34,7 @@ class AdminTemplateController {
|
||||
): Promise<unknown> {
|
||||
const identity = requireAdminIdentity(request);
|
||||
const requestId = request.headers["x-request-id"]?.toString() ?? request.id;
|
||||
return this.templateWorkspace.upgrade(identity.brandId, body, identity.subject, requestId);
|
||||
return this.templateInstallations.upgrade(identity.brandId, body, identity.subject, requestId);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@@ -41,7 +45,7 @@ class AdminTemplateController {
|
||||
): Promise<unknown> {
|
||||
const identity = requireAdminIdentity(request);
|
||||
const requestId = request.headers["x-request-id"]?.toString() ?? request.id;
|
||||
return this.templateWorkspace.rollback(identity.brandId, identity.subject, body, requestId);
|
||||
return this.templateInstallations.rollback(identity.brandId, identity.subject, body, requestId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,19 +8,19 @@ import {
|
||||
validateCapabilityConfig,
|
||||
} from "@personal-brand/config";
|
||||
import { PrismaService } from "../../prisma.service.js";
|
||||
import { TemplateWorkspaceService } from "../../template-workspace.service.js";
|
||||
import { TemplateInstallationService } from "../../template/template-installation.service.js";
|
||||
import type { UpdateCapabilityInstallationDto } from "../../admin.dto.js";
|
||||
|
||||
@Injectable()
|
||||
export class AdminWorkspaceCapabilitiesService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly templateWorkspace: TemplateWorkspaceService,
|
||||
private readonly templateInstallations: TemplateInstallationService,
|
||||
) {}
|
||||
|
||||
async list(brandId: string) {
|
||||
const [templateKey, installations] = await Promise.all([
|
||||
this.templateWorkspace.getEffectiveTemplateKey(brandId),
|
||||
this.templateInstallations.getEffectiveTemplateKey(brandId),
|
||||
this.prisma.capabilityInstallation.findMany({ where: { brandId } }),
|
||||
]);
|
||||
const installed = new Map(installations.map((item) => [item.capabilityKey, item]));
|
||||
@@ -54,7 +54,7 @@ export class AdminWorkspaceCapabilitiesService {
|
||||
body: UpdateCapabilityInstallationDto,
|
||||
) {
|
||||
const template = getBrandTemplate(
|
||||
await this.templateWorkspace.getEffectiveTemplateKey(brandId),
|
||||
await this.templateInstallations.getEffectiveTemplateKey(brandId),
|
||||
);
|
||||
if (!template?.capabilities.includes(key as never) && !(key in capabilityPlugins))
|
||||
throw new BadRequestException("能力未被当前模板声明");
|
||||
|
||||
@@ -2,7 +2,7 @@ import { AdminBrand, AdminRoles } from "../../admin-auth.decorators.js";
|
||||
import { Controller, Get, Post, Body, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { InstallTemplateDto, SelectTemplateVersionDto } from "../../admin.dto.js";
|
||||
import { TemplateWorkspaceService } from "../../template-workspace.service.js";
|
||||
import { TemplateInstallationService } from "../../template/template-installation.service.js";
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
import { AdminWorkspaceService } from "./admin-workspace.service.js";
|
||||
|
||||
@@ -11,7 +11,7 @@ import { AdminWorkspaceService } from "./admin-workspace.service.js";
|
||||
class AdminWorkspacePreviewController {
|
||||
constructor(
|
||||
private readonly workspaceService: AdminWorkspaceService,
|
||||
private readonly templateWorkspace: TemplateWorkspaceService,
|
||||
private readonly templateInstallations: TemplateInstallationService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
@@ -32,7 +32,7 @@ class AdminWorkspacePreviewController {
|
||||
): Promise<unknown> {
|
||||
const identity = requireAdminIdentity(request);
|
||||
const requestId = request.headers["x-request-id"]?.toString() ?? request.id;
|
||||
return this.templateWorkspace.install(
|
||||
return this.templateInstallations.install(
|
||||
identity.brandId,
|
||||
body.templateKey,
|
||||
body.templatePackageVersionId,
|
||||
@@ -49,7 +49,7 @@ class AdminWorkspacePreviewController {
|
||||
): Promise<unknown> {
|
||||
const identity = requireAdminIdentity(request);
|
||||
const requestId = request.headers["x-request-id"]?.toString() ?? request.id;
|
||||
return this.templateWorkspace.selectVersion(
|
||||
return this.templateInstallations.selectVersion(
|
||||
identity.brandId,
|
||||
body,
|
||||
identity.subject,
|
||||
|
||||
Reference in new issue
Block a user