index.js
2.31 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { HttpResponse, http } from 'msw'
import { db } from '@db/apps/chat/db'
export const handlerAppsChat = [
http.get(('/api/apps/chat/chats-and-contacts'), ({ request }) => {
const url = new URL(request.url)
const q = url.searchParams.get('q') || ''
const qLowered = q.toLowerCase()
const chatsContacts = db.chats
.map(chat => {
const contact = JSON.parse(JSON.stringify(db.contacts.find(c => c.id === chat.userId)))
contact.chat = { id: chat.id, unseenMsgs: chat.unseenMsgs, lastMessage: chat.messages.at(-1) }
return contact
})
.reverse()
const profileUserData = db.profileUser
const response = {
chatsContacts: chatsContacts.filter(c => c.fullName.toLowerCase().includes(qLowered)),
contacts: db.contacts.filter(c => c.fullName.toLowerCase().includes(qLowered)),
profileUser: profileUserData,
}
return HttpResponse.json(response, { status: 200 })
}),
http.get(('/api/apps/chat/chats/:userId'), ({ params }) => {
const userId = Number(params.userId)
const chat = db.chats.find(e => e.userId === userId)
if (chat)
chat.unseenMsgs = 0
return HttpResponse.json({
chat,
contact: db.contacts.find(c => c.id === userId),
}, {
status: 200,
})
}),
http.post(('/api/apps/chat/chats/:userId'), async ({ request, params }) => {
// Get user id from URL
const chatId = Number(params.userId)
// Get message from post data
const { message, senderId } = await request.json()
let activeChat = db.chats.find(chat => chat.userId === chatId)
const newMessageData = {
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: newMessageData }
if (isNewChat)
response.chat = activeChat
return HttpResponse.json(response, { status: 201 })
}),
]