| 1 | import type { Locale } from '@yikart/common' |
| 2 | import { Injectable } from '@nestjs/common' |
| 3 | import { AppException, ResponseCode } from '@yikart/common' |
| 4 | import { UserAiInfo, UserRepository, UserType } from '@yikart/mongodb' |
| 5 | import { ReportLocationDto, UpdateUserInfoDto } from './user.dto' |
| 6 | |
| 7 | @Injectable() |
| 8 | export class UserService { |
| 9 | constructor( |
| 10 | private readonly userRepository: UserRepository, |
| 11 | ) { } |
| 12 | |
| 13 | /** |
| 14 | * Get user information |
| 15 | * @param id |
| 16 | * @returns |
| 17 | */ |
| 18 | async getUserInfoById(id: string) { |
| 19 | const res = await this.userRepository.getById(id) |
| 20 | if (!res) |
| 21 | throw new AppException(ResponseCode.UserNotFound) |
| 22 | |
| 23 | return res |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Update user information |
| 28 | * @param id |
| 29 | * @param newdData |
| 30 | * @returns |
| 31 | */ |
| 32 | async updateUserInfo( |
| 33 | id: string, |
| 34 | newdData: UpdateUserInfoDto, |
| 35 | ): Promise<boolean> { |
| 36 | const res = await this.userRepository.updateById(id, { $set: newdData }) |
| 37 | return res !== null |
| 38 | } |
| 39 | |
| 40 | async updateLocation(id: string, data: ReportLocationDto): Promise<boolean> { |
| 41 | const res = await this.userRepository.updateById(id, { $set: { location: data } }) |
| 42 | return res !== null |
| 43 | } |
| 44 | |
| 45 | async setAiConfig(userId: string, aiConfig: Partial<UserAiInfo>): Promise<boolean> { |
| 46 | const res = await this.userRepository.updateAiConfigById(userId, aiConfig) |
| 47 | return res |
| 48 | } |
| 49 | |
| 50 | async setAiConfigItem(userId: string, type: 'image' | 'edit' | 'video' | 'agent', value: { |
| 51 | defaultModel: string |
| 52 | // eslint-disable-next-line ts/no-explicit-any |
| 53 | option?: Record<string, any> |
| 54 | }): Promise<boolean> { |
| 55 | const res = await this.userRepository.updateAiConfigItemById(userId, type, value) |
| 56 | return res |
| 57 | } |
| 58 | |
| 59 | async updateLocale(userId: string, locale: Locale): Promise<boolean> { |
| 60 | const res = await this.userRepository.updateById(userId, { $set: { locale } }) |
| 61 | return res !== null |
| 62 | } |
| 63 | |
| 64 | async switchUserType(userId: string, userType: UserType): Promise<boolean> { |
| 65 | const result = await this.userRepository.updateUserTypeById(userId, userType) |
| 66 | return result |
| 67 | } |
| 68 | } |
| 69 |