refactor: 将数据库访问从控制器下沉到服务层
This commit is contained in:
1 parent
f158f906d5
commit
3ae7b462f5
21 files changed
+1046
-857
No files matched your search
@@ -17,7 +17,6 @@ import { resolve } from "node:path";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { getTemplatePackageManifest, templatePackageRegistry } from "@personal-brand/config";
|
||||
import { AdminAccessService } from "../../admin-access.service.js";
|
||||
import { PrismaService } from "../../prisma.service.js";
|
||||
import { AdminMiniAppReleaseService } from "./admin-miniapp-release.service.js";
|
||||
import { UpdateMiniAppSettingsDto } from "./admin-miniapp-settings.dto.js";
|
||||
import { miniappArtifactRoot, miniappProjectPath, REPOSITORY_ROOT } from "./miniapp-artifact.js";
|
||||
@@ -59,27 +58,19 @@ export class AdminMiniAppReleaseController {
|
||||
constructor(
|
||||
private readonly access: AdminAccessService,
|
||||
private readonly releases: AdminMiniAppReleaseService,
|
||||
private readonly prisma: PrismaService,
|
||||
) {}
|
||||
|
||||
@Get("latest")
|
||||
async latest(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
const identity = await this.identity(request);
|
||||
const task = await this.prisma.miniAppReleaseTask.findFirst({
|
||||
where: { brandId: identity.brandId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
const task = await this.releases.latest(identity.brandId);
|
||||
return task ?? null;
|
||||
}
|
||||
|
||||
@Get("settings")
|
||||
async settings(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
const identity = await this.identity(request);
|
||||
const brand = await this.prisma.brandProfile.findUnique({
|
||||
where: { id: identity.brandId },
|
||||
select: { miniAppSettings: true },
|
||||
});
|
||||
const settings = this.jsonObject(brand?.miniAppSettings);
|
||||
const settings = this.jsonObject(await this.releases.settings(identity.brandId));
|
||||
const buildConfig = this.buildConfigSummary(settings.apiBaseUrl, settings.appId);
|
||||
return {
|
||||
appId: "",
|
||||
@@ -138,11 +129,7 @@ export class AdminMiniAppReleaseController {
|
||||
`上传私钥无法解析(${inspection.reason ?? "PEM 格式错误"})。请粘贴微信开发者工具生成的完整私钥,注意保留换行。`,
|
||||
);
|
||||
}
|
||||
const brand = await this.prisma.brandProfile.findUniqueOrThrow({
|
||||
where: { id: identity.brandId },
|
||||
select: { miniAppSettings: true },
|
||||
});
|
||||
const current = this.jsonObject(brand.miniAppSettings);
|
||||
const current = this.jsonObject(await this.releases.settings(identity.brandId));
|
||||
let apiBaseUrl: string | undefined;
|
||||
if (body.apiBaseUrl !== undefined) {
|
||||
const trimmed = body.apiBaseUrl.trim();
|
||||
@@ -170,10 +157,7 @@ export class AdminMiniAppReleaseController {
|
||||
...(credential ? { credential } : {}),
|
||||
templateKey: current.templateKey ?? null,
|
||||
} satisfies Prisma.InputJsonObject;
|
||||
await this.prisma.brandProfile.update({
|
||||
where: { id: identity.brandId },
|
||||
data: { miniAppSettings: settings },
|
||||
});
|
||||
await this.releases.saveSettings(identity.brandId, settings);
|
||||
return {
|
||||
appId: settings.appId,
|
||||
apiBaseUrl: this.effectiveApiBaseUrl(settings.apiBaseUrl),
|
||||
@@ -204,19 +188,12 @@ export class AdminMiniAppReleaseController {
|
||||
@Body() body: UploadMiniAppReleaseDto,
|
||||
): Promise<unknown> {
|
||||
const identity = await this.identity(request);
|
||||
const task = await this.prisma.miniAppReleaseTask.findFirst({
|
||||
where: { id: taskId, brandId: identity.brandId },
|
||||
});
|
||||
if (!task) throw new BadRequestException("小程序发布任务不存在");
|
||||
const { task, brand } = await this.releases.uploadContext(identity.brandId, taskId);
|
||||
if (task.status !== "built" && task.status !== "failed")
|
||||
throw new BadRequestException("当前小程序发布任务不能重复上传");
|
||||
const staleBuilding = isStaleBuilding(task);
|
||||
if (task.buildStatus === "building" && !staleBuilding)
|
||||
throw new ConflictException("小程序构建正在进行中,请稍后");
|
||||
const brand = await this.prisma.brandProfile.findUniqueOrThrow({
|
||||
where: { id: identity.brandId },
|
||||
select: { miniAppSettings: true, templateKey: true },
|
||||
});
|
||||
const settings = this.jsonObject(brand.miniAppSettings);
|
||||
const appid = (() => {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user