[id].post.ts
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { db } from '@/server/fake-db/apps/chat'
import type { Chat, ChatMessage } from '@/server/fake-db/apps/chat/types'
export default defineEventHandler(async event => {
// Get user id from URL
const chatId = getIntId(event, 'Chat id is required to send message')
// Get message from post data
const { message, senderId } = await readBody(event)
let activeChat = db.chats.find(chat => chat.userId === chatId)
const newMessageData: ChatMessage = {
message,
time: String(new Date()),
senderId,
feedback: {
isSent: true,
isDelivered: false,
isSeen: false,
},
}
// If there's new chat for user create one
let isNewChat = false
if (activeChat === undefined) {
isNewChat = true
db.chats.push({
id: db.chats.length + 1,
userId: chatId,
unseenMsgs: 0,
messages: [newMessageData],
})
activeChat = db.chats.at(-1)
}
else {
activeChat.messages.push(newMessageData)
}
const response: { msg: ChatMessage; chat?: Chat } = { msg: newMessageData }
if (isNewChat)
response.chat = activeChat
setResponseStatus(event, 201)
return response
})