Skip to content

Commit

Permalink
fix: build error
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerwin committed Mar 25, 2023
1 parent 7b48303 commit d2b04a6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 35 deletions.
2 changes: 1 addition & 1 deletion service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"scripts": {
"start": "esno ./src/index.ts",
"dev": "esno watch ./src/index.ts",
"prod": "node ./build/index.mjs",
"prod": "esno ./build/index.js",
"build": "pnpm clean && tsup",
"clean": "rimraf build",
"lint": "eslint .",
Expand Down
18 changes: 9 additions & 9 deletions service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ app.all('*', (_, res, next) => {

router.get('/chatrooms', auth, async (req, res) => {
try {
const userId = new ObjectId(req.headers.userId as string)
const userId = req.headers.userId as string
const rooms = await getChatRooms(userId)
const result = []
rooms.forEach((r) => {
Expand All @@ -50,7 +50,7 @@ router.get('/chatrooms', auth, async (req, res) => {

router.post('/room-create', auth, async (req, res) => {
try {
const userId = new ObjectId(req.headers.userId as string)
const userId = req.headers.userId as string
const { title, roomId } = req.body as { title: string; roomId: number }
const room = await createChatRoom(userId, title, roomId)
res.send({ status: 'Success', message: null, data: room })
Expand All @@ -63,7 +63,7 @@ router.post('/room-create', auth, async (req, res) => {

router.post('/room-rename', auth, async (req, res) => {
try {
const userId = new ObjectId(req.headers.userId as string)
const userId = req.headers.userId as string
const { title, roomId } = req.body as { title: string; roomId: number }
const room = await renameChatRoom(userId, title, roomId)
res.send({ status: 'Success', message: null, data: room })
Expand All @@ -76,7 +76,7 @@ router.post('/room-rename', auth, async (req, res) => {

router.post('/room-delete', auth, async (req, res) => {
try {
const userId = new ObjectId(req.headers.userId as string)
const userId = req.headers.userId as string
const { roomId } = req.body as { roomId: number }
if (!roomId || !await existsChatRoom(userId, roomId)) {
res.send({ status: 'Fail', message: 'Unknow room', data: null })
Expand All @@ -93,7 +93,7 @@ router.post('/room-delete', auth, async (req, res) => {

router.get('/chat-hisroty', auth, async (req, res) => {
try {
const userId = new ObjectId(req.headers.userId as string)
const userId = req.headers.userId as string
const roomId = +req.query.roomid
const lastTime = req.query.lasttime as string
if (!roomId || !await existsChatRoom(userId, roomId)) {
Expand Down Expand Up @@ -146,7 +146,7 @@ router.get('/chat-hisroty', auth, async (req, res) => {

router.post('/chat-delete', auth, async (req, res) => {
try {
const userId = new ObjectId(req.headers.userId as string)
const userId = req.headers.userId as string
const { roomId, uuid, inversion } = req.body as { roomId: number; uuid: number; inversion: boolean }
if (!roomId || !await existsChatRoom(userId, roomId)) {
res.send({ status: 'Fail', message: 'Unknow room', data: null })
Expand All @@ -163,7 +163,7 @@ router.post('/chat-delete', auth, async (req, res) => {

router.post('/chat-clear-all', auth, async (req, res) => {
try {
const userId = new ObjectId(req.headers.userId as string)
const userId = req.headers.userId as string
await deleteAllChatRooms(userId)
res.send({ status: 'Success', message: null, data: null })
}
Expand All @@ -175,7 +175,7 @@ router.post('/chat-clear-all', auth, async (req, res) => {

router.post('/chat-clear', auth, async (req, res) => {
try {
const userId = new ObjectId(req.headers.userId as string)
const userId = req.headers.userId as string
const { roomId } = req.body as { roomId: number }
if (!roomId || !await existsChatRoom(userId, roomId)) {
res.send({ status: 'Fail', message: 'Unknow room', data: null })
Expand Down Expand Up @@ -426,7 +426,7 @@ router.post('/setting-mail', rootAuth, async (req, res) => {
router.post('/mail-test', rootAuth, async (req, res) => {
try {
const config = req.body as MailConfig
const userId = new ObjectId(req.headers.userId as string)
const userId = req.headers.userId as string
const user = await getUserById(userId)
await sendTestMail(user.email, config)
res.send({ status: 'Success', message: '发送成功 | Successfully', data: null })
Expand Down
16 changes: 2 additions & 14 deletions service/src/storage/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,13 @@ export class UserInfo {
}
}

export class UserOauth {
userId: number
oauthType: OauthType
oauthId: string

constructor(userId: number, oauthType: OauthType, oauthId: string) {
this.userId = userId
this.oauthType = oauthType
this.oauthId = oauthId
}
}

export class ChatRoom {
_id: ObjectId
roomId: number
userId: number
userId: string
title: string
status: Status = Status.Normal
constructor(userId: number, title: string, roomId: number) {
constructor(userId: string, title: string, roomId: number) {
this.userId = userId
this.title = title
this.roomId = roomId
Expand Down
20 changes: 10 additions & 10 deletions service/src/storage/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export async function updateChat(chatId: string, response: string, messageId: st
await chatCol.updateOne(query, update)
}

export async function createChatRoom(userId: ObjectId, title: string, roomId: number) {
export async function createChatRoom(userId: string, title: string, roomId: number) {
const room = new ChatRoom(userId, title, roomId)
await roomCol.insertOne(room)
return room
}
export async function renameChatRoom(userId: ObjectId, title: string, roomId: number) {
export async function renameChatRoom(userId: string, title: string, roomId: number) {
const query = { userId, roomId }
const update = {
$set: {
Expand All @@ -50,25 +50,25 @@ export async function renameChatRoom(userId: ObjectId, title: string, roomId: nu
return result
}

export async function deleteChatRoom(userId: ObjectId, roomId: number) {
export async function deleteChatRoom(userId: string, roomId: number) {
const result = await roomCol.updateOne({ roomId, userId }, { $set: { status: Status.Deleted } })
await clearChat(roomId)
return result
}

export async function getChatRooms(userId: ObjectId) {
export async function getChatRooms(userId: string) {
const cursor = await roomCol.find({ userId, status: { $ne: Status.Deleted } })
const rooms = []
await cursor.forEach(doc => rooms.push(doc))
return rooms
}

export async function existsChatRoom(userId: ObjectId, roomId: number) {
export async function existsChatRoom(userId: string, roomId: number) {
const room = await roomCol.findOne({ roomId, userId })
return !!room
}

export async function deleteAllChatRooms(userId: ObjectId) {
export async function deleteAllChatRooms(userId: string) {
await roomCol.updateMany({ userId, status: Status.Normal }, { $set: { status: Status.Deleted } })
await chatCol.updateMany({ userId, status: Status.Normal }, { $set: { status: Status.Deleted } })
}
Expand Down Expand Up @@ -133,8 +133,8 @@ export async function createUser(email: string, password: string): Promise<UserI
return userInfo
}

export async function updateUserInfo(userId: ObjectId, user: UserInfo) {
const result = userCol.updateOne({ _id: userId }
export async function updateUserInfo(userId: string, user: UserInfo) {
const result = userCol.updateOne({ _id: new ObjectId(userId) }
, { $set: { name: user.name, description: user.description, avatar: user.avatar } })
return result
}
Expand All @@ -144,8 +144,8 @@ export async function getUser(email: string): Promise<UserInfo> {
return await userCol.findOne({ email }) as UserInfo
}

export async function getUserById(userId: ObjectId): Promise<UserInfo> {
return await userCol.findOne({ _id: userId }) as UserInfo
export async function getUserById(userId: string): Promise<UserInfo> {
return await userCol.findOne({ _id: new ObjectId(userId) }) as UserInfo
}

export async function verifyUser(email: string) {
Expand Down
2 changes: 1 addition & 1 deletion service/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default defineConfig({
entry: ['src/index.ts'],
outDir: 'build',
target: 'es2020',
format: ['esm'],
format: ['cjs'],
splitting: false,
sourcemap: true,
minify: false,
Expand Down

0 comments on commit d2b04a6

Please sign in to comment.