返回 AiToEarn
account-group.repository.ts
根目录 / project / aitoearn-backend / libs / mongodb / src / repositories / account-group.repository.ts
1 import { InjectModel } from '@nestjs/mongoose'
2 import { Model } from 'mongoose'
3 import { AccountGroup } from '../schemas/account-group.schema'
4 import { BaseRepository } from './base.repository'
5
6 export class AccountGroupRepository extends BaseRepository<AccountGroup> {
7 constructor(
8 @InjectModel(AccountGroup.name)
9 private readonly accountGroupModel: Model<AccountGroup>,
10 ) { super(accountGroupModel) }
11
12 // 获取默认用户组, 没有则创建
13 async getDefaultGroup(userId: string): Promise<AccountGroup> {
14 return await this.accountGroupModel
15 .findOneAndUpdate(
16 { userId, isDefault: true },
17 {
18 $setOnInsert: {
19 isDefault: true,
20 name: 'default',
21 rank: 1,
22 userId,
23 },
24 },
25 { upsert: true, new: true },
26 )
27 .lean({ virtuals: true })
28 .exec()
29 }
30
31 /**
32 * 添加组
33 * @param accountGroup
34 */
35 async createAccountGroup(
36 accountGroup: Partial<AccountGroup>,
37 ): Promise<AccountGroup> {
38 const created = await this.accountGroupModel.create(accountGroup)
39 return created.toObject()
40 }
41
42 /**
43 * 更新组
44 * @param accountGroup
45 */
46 async updateAccountGroup(
47 id: string,
48 accountGroup: Partial<AccountGroup>,
49 ): Promise<boolean> {
50 const res = await this.accountGroupModel.findByIdAndUpdate(
51 id,
52 { $set: accountGroup },
53 ).lean({ virtuals: true })
54 return !!res
55 }
56
57 /**
58 * 更新组
59 * @param accountGroup
60 */
61 async getAccountGorupListByIds(
62 ids: string[],
63 userId: string,
64 ) {
65 const accountGorupList = await this.accountGroupModel
66 .find({ userId, _id: { $in: ids } })
67 .lean({ virtuals: true })
68 .exec()
69 return accountGorupList
70 }
71
72 /**
73 * 删除多个组
74 * @param ids
75 * @param userId
76 */
77 async deleteAccountGroup(ids: string[], userId: string): Promise<boolean> {
78 const res = await this.accountGroupModel.deleteMany({
79 _id: { $in: ids },
80 userId,
81 })
82 return res.deletedCount > 0
83 }
84
85 /**
86 * 获取所有组
87 * @param userId
88 * @returns
89 */
90 async getAccountGroup(userId: string): Promise<AccountGroup[]> {
91 const accountGroupList: AccountGroup[] = await this.accountGroupModel.find({
92 userId,
93 }).lean({ virtuals: true })
94
95 // 创建默认用户组
96 if (accountGroupList.length === 0) {
97 const accountGroup = await this.getDefaultGroup(userId)
98 accountGroupList.push(accountGroup)
99 }
100
101 return accountGroupList
102 }
103
104 // 排序
105 async sortRank(userId: string, list: { id: string, rank: number }[]) {
106 const promises = list.map(element =>
107 this.accountGroupModel.updateOne(
108 { userId, _id: element.id },
109 { $set: { rank: element.rank } },
110 ),
111 )
112
113 await Promise.all(promises)
114 return true
115 }
116
117 // 根据名称获取账户组列表
118 async getAccountGroupByName(userId: string, name: string): Promise<AccountGroup[]> {
119 const accountGroupList: AccountGroup[] = await this.accountGroupModel.find({
120 userId,
121 name: { $regex: name, $options: 'i' },
122 }).sort({ rank: 1 }).lean({ virtuals: true })
123
124 return accountGroupList
125 }
126
127 async listIdsByIp(ip: string): Promise<string[]> {
128 const groups = await this.accountGroupModel
129 .find({ ip: { $regex: ip, $options: 'i' } }, { _id: 1 })
130 .lean({ virtuals: true })
131 return groups.map(g => g._id.toString())
132 }
133 }
134
134 lines TYPESCRIPT