返回 AiToEarn
ai-log.repository.ts
根目录 / project / aitoearn-backend / libs / mongodb / src / repositories / ai-log.repository.ts
1 import { InjectModel } from '@nestjs/mongoose'
2 import { Pagination, RangeFilter, UserType } from '@yikart/common'
3 import { FilterQuery, Model, UpdateQuery } from 'mongoose'
4 import { AiLogChannel, AiLogStatus, AiLogType } from '../enums'
5 import { AiLog } from '../schemas'
6 import { BaseRepository } from './base.repository'
7
8 export interface ListAiLogParams extends Pagination {
9 userId?: string
10 userType?: UserType
11 type?: AiLogType
12 status?: AiLogStatus
13 model?: string
14 channel?: AiLogChannel
15 channels?: AiLogChannel[]
16 createdAt?: RangeFilter<Date>
17 }
18
19 export interface ListAiLogFilter {
20 userId?: string
21 userType?: UserType
22 type?: AiLogType
23 status?: AiLogStatus
24 model?: string
25 channel?: AiLogChannel
26 channels?: AiLogChannel[]
27 createdAt?: RangeFilter<Date>
28 }
29
30 export class AiLogRepository extends BaseRepository<AiLog> {
31 constructor(
32 @InjectModel(AiLog.name) aiLogModel: Model<AiLog>,
33 ) {
34 super(aiLogModel)
35 }
36
37 async listWithPagination(params: ListAiLogParams) {
38 const { page, pageSize, userId, userType, type, status, model, channel, channels, createdAt } = params
39
40 const filter: FilterQuery<AiLog> = {}
41 if (userId)
42 filter.userId = userId
43 if (userType)
44 filter.userType = userType
45 if (type)
46 filter.type = type
47 if (status)
48 filter.status = status
49 if (model)
50 filter.model = model
51 if (channel)
52 filter.channel = channel
53 else if (channels)
54 filter.channel = { $in: channels }
55 if (createdAt) {
56 filter.createdAt = {}
57 if (createdAt[0])
58 filter.createdAt.$gte = createdAt[0]
59 if (createdAt[1])
60 filter.createdAt.$lte = createdAt[1]
61 }
62
63 return await this.findWithPagination({
64 page,
65 pageSize,
66 filter,
67 options: { sort: { createdAt: -1 } },
68 })
69 }
70
71 async list(params: ListAiLogFilter) {
72 const { userId, userType, type, status, model, channel, channels, createdAt } = params
73
74 const filter: FilterQuery<AiLog> = {}
75 if (userId)
76 filter.userId = userId
77 if (userType)
78 filter.userType = userType
79 if (type)
80 filter.type = type
81 if (status)
82 filter.status = status
83 if (model)
84 filter.model = model
85 if (channel)
86 filter.channel = channel
87 else if (channels)
88 filter.channel = { $in: channels }
89 if (createdAt) {
90 filter.createdAt = {}
91 if (createdAt[0])
92 filter.createdAt.$gte = createdAt[0]
93 if (createdAt[1])
94 filter.createdAt.$lte = createdAt[1]
95 }
96
97 return await this.find(filter, { sort: { createdAt: -1 } })
98 }
99
100 async listGeneratingByType(type: AiLogType, channel?: AiLogChannel) {
101 const filter: FilterQuery<AiLog> = {
102 type,
103 status: AiLogStatus.Generating,
104 taskId: { $exists: true, $ne: null },
105 }
106 if (channel)
107 filter.channel = channel
108 return await this.find(filter, { sort: { createdAt: -1 } })
109 }
110
111 async listByStatusAndStartedAtBefore(status: AiLogStatus, startedBefore: Date, limit: number) {
112 return await this.find(
113 {
114 status,
115 startedAt: { $lt: startedBefore },
116 },
117 { sort: { startedAt: 1 }, limit },
118 )
119 }
120
121 async updateByIdAndStatus(id: string, status: AiLogStatus, update: UpdateQuery<AiLog>) {
122 return await this.updateOne({ _id: id, status }, update)
123 }
124
125 async getByTaskId(taskId: string) {
126 return await this.findOne({ taskId })
127 }
128
129 async getByIdAndUserId(id: string, userId: string, userType: UserType) {
130 return await this.findOne({ _id: id, userId, userType })
131 }
132
133 async listByIdsAndUserId(ids: string[], userId: string, userType: UserType): Promise<AiLog[]> {
134 return this.find(
135 { _id: { $in: ids }, userId, userType },
136 { sort: { createdAt: -1 } },
137 )
138 }
139
140 async countByUserIdAndStatus(userId: string, userType: UserType, type: AiLogType, status: AiLogStatus): Promise<number> {
141 return this.count({ userId, userType, type, status })
142 }
143
144 async listByUserIdAndTypeAndStatusAndRequestVersionAndCreatedAt(
145 userId: string,
146 type: AiLogType,
147 status: AiLogStatus,
148 requestVersion: string,
149 startAt: Date,
150 limit: number,
151 ) {
152 return await this.find(
153 {
154 userId,
155 type,
156 status,
157 'request.version': requestVersion,
158 'createdAt': { $gte: startAt },
159 },
160 { sort: { createdAt: -1 }, limit },
161 )
162 }
163
164 async listUserIdsByTypeAndStatusAndUpdatedAt(type: AiLogType, status: AiLogStatus, startAt: Date): Promise<string[]> {
165 return await this.model.distinct('userId', {
166 type,
167 status,
168 updatedAt: { $gte: startAt },
169 }).exec()
170 }
171 }
172
172 lines TYPESCRIPT