refactor: 将数据库访问从控制器下沉到服务层
This commit is contained in:
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" }),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user