返回 AiToEarn
user.repository.ts
根目录 / project / aitoearn-backend / libs / mongodb / src / repositories / user.repository.ts
1 import { InjectModel } from '@nestjs/mongoose'
2 import { Model } from 'mongoose'
3 import { UserType } from '../enums'
4 import { User, UserAiInfo } from '../schemas'
5 import { BaseRepository, LeanDoc } from './base.repository'
6
7 export class UserRepository extends BaseRepository<User> {
8 constructor(
9 @InjectModel(User.name)
10 userModel: Model<User>,
11 ) {
12 super(userModel)
13 }
14
15 override async getById(id: string): Promise<LeanDoc<User> | null> {
16 let userInfo
17 try {
18 userInfo = await this.model.findById(id).lean({ virtuals: true }).exec()
19 }
20 catch {
21 return null
22 }
23 return userInfo as LeanDoc<User> | null
24 }
25
26 async updateAiConfigById(userId: string, aiConfig: Partial<UserAiInfo>): Promise<boolean> {
27 const res = await this.model.updateOne(
28 { _id: userId },
29 { $set: { aiInfo: aiConfig } },
30 )
31 return res.modifiedCount > 0
32 }
33
34 async updateAiConfigItemById(userId: string, type: 'image' | 'edit' | 'video' | 'agent', value: {
35 defaultModel: string
36 option?: Record<string, any>
37 }): Promise<boolean> {
38 const res = await this.model.updateOne(
39 { _id: userId },
40 { $set: { [`aiInfo.${type}`]: { defaultModel: value.defaultModel, option: value.option } } },
41 )
42 return res.modifiedCount > 0
43 }
44
45 async updateUserTypeById(userId: string, userType: UserType): Promise<boolean> {
46 const res = await this.model.updateOne(
47 { _id: userId },
48 { $set: { userType } },
49 )
50 return res.modifiedCount > 0
51 }
52 }
53
53 lines TYPESCRIPT