Skip to content

Commit cb4b41d

Browse files
authored
Merge pull request #112 from zhujunsan/kerwin_main
chatUsage 中,userId 记录改为 ObjectID 类型,以方便之后的 aggregate 等操作
2 parents 15d9c1c + e607a69 commit cb4b41d

File tree

4 files changed

+17
-19
lines changed

4 files changed

+17
-19
lines changed

service/src/chatgpt/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ async function containsSensitiveWords(audit: AuditConfig, text: string): Promise
145145
}
146146
return false
147147
}
148-
let cachedBanlance: number | undefined
148+
let cachedBalance: number | undefined
149149
let cacheExpiration = 0
150150

151151
async function fetchBalance() {
152152
const now = new Date().getTime()
153-
if (cachedBanlance && cacheExpiration > now)
154-
return Promise.resolve(cachedBanlance.toFixed(3))
153+
if (cachedBalance && cacheExpiration > now)
154+
return Promise.resolve(cachedBalance.toFixed(3))
155155

156156
// 计算起始日期和结束日期
157157
const startDate = new Date(now - 90 * 24 * 60 * 60 * 1000)
@@ -209,10 +209,10 @@ async function fetchBalance() {
209209
const totalUsage = usageData.total_usage / 100
210210

211211
// 计算剩余额度
212-
cachedBanlance = totalAmount - totalUsage
212+
cachedBalance = totalAmount - totalUsage
213213
cacheExpiration = now + 60 * 60 * 1000
214214

215-
return Promise.resolve(cachedBanlance.toFixed(3))
215+
return Promise.resolve(cachedBalance.toFixed(3))
216216
}
217217
catch (error) {
218218
global.console.error(error)

service/src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express from 'express'
22
import jwt from 'jsonwebtoken'
33
import * as dotenv from 'dotenv'
4+
import { ObjectId } from 'mongodb'
45
import type { RequestProps } from './types'
56
import type { ChatContext, ChatMessage } from './chatgpt'
67
import { chatConfig, chatReplyProcess, containsSensitiveWords, initApi, initAuditService } from './chatgpt'
@@ -264,7 +265,7 @@ router.post('/chat', auth, async (req, res) => {
264265
response.data.text,
265266
response.data.id,
266267
response.data.detail?.usage as UsageResponse,
267-
previousResponse)
268+
previousResponse as [])
268269
}
269270
else {
270271
await updateChat(message._id as unknown as string,
@@ -274,7 +275,7 @@ router.post('/chat', auth, async (req, res) => {
274275
}
275276

276277
if (response.data.usage) {
277-
await insertChatUsage(req.headers.userId as string,
278+
await insertChatUsage(new ObjectId(req.headers.userId as string),
278279
roomId,
279280
message._id,
280281
response.data.id,
@@ -368,7 +369,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
368369
result.data.text,
369370
result.data.id,
370371
result.data.detail?.usage as UsageResponse,
371-
previousResponse)
372+
previousResponse as [])
372373
}
373374
else {
374375
await updateChat(message._id as unknown as string,
@@ -378,7 +379,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
378379
}
379380

380381
if (result.data.detail?.usage) {
381-
await insertChatUsage(req.headers.userId as string,
382+
await insertChatUsage(new ObjectId(req.headers.userId),
382383
roomId,
383384
message._id,
384385
result.data.id,

service/src/storage/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class UsageResponse {
9595

9696
export class ChatUsage {
9797
_id: ObjectId
98-
userId: string
98+
userId: ObjectId
9999
roomId: number
100100
chatId: ObjectId
101101
messageId: string
@@ -104,7 +104,7 @@ export class ChatUsage {
104104
totalTokens: number
105105
estimated: boolean
106106
dateTime: number
107-
constructor(userId: string, roomId: number, chatId: ObjectId, messageId: string, usage: UsageResponse) {
107+
constructor(userId: ObjectId, roomId: number, chatId: ObjectId, messageId: string, usage: UsageResponse) {
108108
this.userId = userId
109109
this.roomId = roomId
110110
this.chatId = chatId

service/src/storage/mongo.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function updateChat(chatId: string, response: string, messageId: st
5454
await chatCol.updateOne(query, update)
5555
}
5656

57-
export async function insertChatUsage(userId: string, roomId: number, chatId: ObjectId, messageId: string, usage: UsageResponse) {
57+
export async function insertChatUsage(userId: ObjectId, roomId: number, chatId: ObjectId, messageId: string, usage: UsageResponse) {
5858
const chatUsage = new ChatUsage(userId, roomId, chatId, messageId, usage)
5959
await usageCol.insertOne(chatUsage)
6060
return chatUsage
@@ -72,8 +72,7 @@ export async function renameChatRoom(userId: string, title: string, roomId: numb
7272
title,
7373
},
7474
}
75-
const result = await roomCol.updateOne(query, update)
76-
return result
75+
return await roomCol.updateOne(query, update)
7776
}
7877

7978
export async function deleteChatRoom(userId: string, roomId: number) {
@@ -161,7 +160,7 @@ export async function deleteChat(roomId: number, uuid: number, inversion: boolea
161160
},
162161
}
163162
}
164-
chatCol.updateOne(query, update)
163+
await chatCol.updateOne(query, update)
165164
}
166165

167166
export async function createUser(email: string, password: string): Promise<UserInfo> {
@@ -175,15 +174,13 @@ export async function createUser(email: string, password: string): Promise<UserI
175174
}
176175

177176
export async function updateUserInfo(userId: string, user: UserInfo) {
178-
const result = userCol.updateOne({ _id: new ObjectId(userId) }
177+
return userCol.updateOne({ _id: new ObjectId(userId) }
179178
, { $set: { name: user.name, description: user.description, avatar: user.avatar } })
180-
return result
181179
}
182180

183181
export async function updateUserPassword(userId: string, password: string) {
184-
const result = userCol.updateOne({ _id: new ObjectId(userId) }
182+
return userCol.updateOne({ _id: new ObjectId(userId) }
185183
, { $set: { password, updateTime: new Date().toLocaleString() } })
186-
return result
187184
}
188185

189186
export async function getUser(email: string): Promise<UserInfo> {

0 commit comments

Comments
 (0)