Skip to content

Commit e607a69

Browse files
committed
chatUsage 中,userId 记录为 ObjectID 类型,以方便之后的 aggregate 等操作
同时做了一点点微小的 typo fix 和 reformat
1 parent 6c14af8 commit e607a69

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
@@ -142,13 +142,13 @@ async function containsSensitiveWords(audit: AuditConfig, text: string): Promise
142142
}
143143
return false
144144
}
145-
let cachedBanlance: number | undefined
145+
let cachedBalance: number | undefined
146146
let cacheExpiration = 0
147147

148148
async function fetchBalance() {
149149
const now = new Date().getTime()
150-
if (cachedBanlance && cacheExpiration > now)
151-
return Promise.resolve(cachedBanlance.toFixed(3))
150+
if (cachedBalance && cacheExpiration > now)
151+
return Promise.resolve(cachedBalance.toFixed(3))
152152

153153
// 计算起始日期和结束日期
154154
const startDate = new Date(now - 90 * 24 * 60 * 60 * 1000)
@@ -206,10 +206,10 @@ async function fetchBalance() {
206206
const totalUsage = usageData.total_usage / 100
207207

208208
// 计算剩余额度
209-
cachedBanlance = totalAmount - totalUsage
209+
cachedBalance = totalAmount - totalUsage
210210
cacheExpiration = now + 60 * 60 * 1000
211211

212-
return Promise.resolve(cachedBanlance.toFixed(3))
212+
return Promise.resolve(cachedBalance.toFixed(3))
213213
}
214214
catch (error) {
215215
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
@@ -49,7 +49,7 @@ export async function updateChat(chatId: string, response: string, messageId: st
4949
await chatCol.updateOne(query, update)
5050
}
5151

52-
export async function insertChatUsage(userId: string, roomId: number, chatId: ObjectId, messageId: string, usage: UsageResponse) {
52+
export async function insertChatUsage(userId: ObjectId, roomId: number, chatId: ObjectId, messageId: string, usage: UsageResponse) {
5353
const chatUsage = new ChatUsage(userId, roomId, chatId, messageId, usage)
5454
await usageCol.insertOne(chatUsage)
5555
return chatUsage
@@ -67,8 +67,7 @@ export async function renameChatRoom(userId: string, title: string, roomId: numb
6767
title,
6868
},
6969
}
70-
const result = await roomCol.updateOne(query, update)
71-
return result
70+
return await roomCol.updateOne(query, update)
7271
}
7372

7473
export async function deleteChatRoom(userId: string, roomId: number) {
@@ -156,7 +155,7 @@ export async function deleteChat(roomId: number, uuid: number, inversion: boolea
156155
},
157156
}
158157
}
159-
chatCol.updateOne(query, update)
158+
await chatCol.updateOne(query, update)
160159
}
161160

162161
export async function createUser(email: string, password: string): Promise<UserInfo> {
@@ -170,15 +169,13 @@ export async function createUser(email: string, password: string): Promise<UserI
170169
}
171170

172171
export async function updateUserInfo(userId: string, user: UserInfo) {
173-
const result = userCol.updateOne({ _id: new ObjectId(userId) }
172+
return userCol.updateOne({ _id: new ObjectId(userId) }
174173
, { $set: { name: user.name, description: user.description, avatar: user.avatar } })
175-
return result
176174
}
177175

178176
export async function updateUserPassword(userId: string, password: string) {
179-
const result = userCol.updateOne({ _id: new ObjectId(userId) }
177+
return userCol.updateOne({ _id: new ObjectId(userId) }
180178
, { $set: { password, updateTime: new Date().toLocaleString() } })
181-
return result
182179
}
183180

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

0 commit comments

Comments
 (0)