refactor: 将数据库访问从控制器下沉到服务层

This commit is contained in:
que01 committed 2026-10-14 17:53:49 +08:00
1 parent f158f906d5
commit 3ae7b462f5
21 files changed
+1046 -857

No files matched your search

@@ -0,0 +1,30 @@
import { BadRequestException, Injectable } from "@nestjs/common";
import { PrismaService } from "../../prisma.service.js";
import { signIdentity } from "../../auth.js";
import type { WechatSession } from "../../wechat-auth.js";
import { PublicBrandContextService } from "../public/public-brand-context.service.js";
@Injectable()
export class CustomerSessionService {
constructor(
private readonly prisma: PrismaService,
private readonly brandContext: PublicBrandContextService,
) {}
async loginWithWechat(session: WechatSession, brandSlug?: string): Promise<{ token: string }> {
const brandId = await this.brandContext.resolveId(brandSlug);
const brand = await this.prisma.brandProfile.findUnique({
where: { id: brandId },
select: { id: true },
});
if (!brand) throw new BadRequestException("品牌尚未初始化");
await this.prisma.customerUser.upsert({
where: { brandId_openid: { brandId: brand.id, openid: session.openid } },
update: { lastLoginAt: new Date() },
create: { brandId: brand.id, openid: session.openid },
});
return {
token: signIdentity({ subject: session.openid, brandId: brand.id, role: "customer" }),
};
}
}
@@ -1,19 +1,9 @@
import {
BadRequestException,
Body,
Controller,
Post,
Query,
Req,
UnauthorizedException,
} from "@nestjs/common";
import { Body, Controller, Post, Query, Req, UnauthorizedException } from "@nestjs/common";
import type { FastifyRequest } from "fastify";
import { PrismaService } from "../../prisma.service.js";
import { WechatAuthService } from "../../wechat-auth.js";
import { AdminAuthService } from "../../admin-auth.service.js";
import { AdminLoginDto, WechatLoginDto } from "../../admin-auth.dto.js";
import { signIdentity } from "../../auth.js";
import { PublicBrandContextService } from "../public/public-brand-context.service.js";
import { CustomerSessionService } from "./customer-session.service.js";
import { ChangeAdminPasswordDto } from "../../admin.dto.js";
import { readIdentity } from "../../auth.js";
@@ -21,8 +11,7 @@ import { readIdentity } from "../../auth.js";
export class CustomerSessionController {
constructor(
private readonly wechatAuth: WechatAuthService,
private readonly prisma: PrismaService,
private readonly brandContext: PublicBrandContextService,
private readonly sessions: CustomerSessionService,
) {}
@Post("wechat")
@@ -31,20 +20,7 @@ export class CustomerSessionController {
@Query("brandSlug") brandSlug?: string,
): Promise<{ token: string }> {
const session = await this.wechatAuth.exchangeCode(body.code);
const brandId = await this.brandContext.resolveId(brandSlug);
const brand = await this.prisma.brandProfile.findUnique({
where: { id: brandId },
select: { id: true },
});
if (!brand) throw new BadRequestException("品牌尚未初始化");
await this.prisma.customerUser.upsert({
where: { brandId_openid: { brandId: brand.id, openid: session.openid } },
update: { lastLoginAt: new Date() },
create: { brandId: brand.id, openid: session.openid },
});
return {
token: signIdentity({ subject: session.openid, brandId: brand.id, role: "customer" }),
};
return this.sessions.loginWithWechat(session, brandSlug);
}
}