-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39161e7
commit 35a02e9
Showing
5 changed files
with
199 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { serializePrisma } from '../store/utils'; | ||
import type { Request, Response } from 'express'; | ||
import { logger, prisma } from '../shared'; | ||
|
||
const chatController = { | ||
list: async (req: Request, res : Response) => { | ||
try { | ||
// console.log(req.query) | ||
const sessionId:any = req.query.sessionId; | ||
// console.log("sessionId = " + sessionId); | ||
const { cursor = undefined, limit = 25 } = req.query; | ||
// console.log("cursor : "+ cursor+ " limit = " + limit) | ||
const chats = ( | ||
await prisma.chat.findMany({ | ||
cursor: cursor ? { pkId: Number(cursor) } : undefined, | ||
take: Number(limit), | ||
skip: cursor ? 1 : 0, | ||
where: { sessionId }, | ||
}) | ||
).map((c : any) => serializePrisma(c)); | ||
|
||
// console.log(chats); | ||
|
||
res.status(200).json({ | ||
data: chats, | ||
cursor: | ||
chats.length !== 0 && chats.length === Number(limit) ? chats[chats.length - 1].pkId : null, | ||
}); | ||
} catch (e) { | ||
const message = 'An error occured during chat list'; | ||
logger.error(e, message); | ||
res.status(500).json({ error: message }); | ||
} | ||
}, | ||
find: async (req :Request, res : Response ) => { | ||
try { | ||
const { sessionId, jid } = req.params; | ||
const { cursor = undefined, limit = 25 } = req.query; | ||
const messages = ( | ||
await prisma.message.findMany({ | ||
cursor: cursor ? { pkId: Number(cursor) } : undefined, | ||
take: Number(limit), | ||
skip: cursor ? 1 : 0, | ||
where: { sessionId, remoteJid: jid }, | ||
orderBy: { messageTimestamp: 'desc' }, | ||
}) | ||
).map((m : any) => serializePrisma(m)); | ||
|
||
res.status(200).json({ | ||
data: messages, | ||
cursor: | ||
messages.length !== 0 && messages.length === Number(limit) | ||
? messages[messages.length - 1].pkId | ||
: null, | ||
}); | ||
} catch (e) { | ||
const message = 'An error occured during chat find'; | ||
logger.error(e, message); | ||
res.status(500).json({ error: message }); | ||
} | ||
}, | ||
} | ||
|
||
export default chatController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Router } from "express" | ||
import { body, query } from "express-validator" | ||
import requestValidator from "../middleware/requestValidator" | ||
import validateSession from "../middleware/validateSession" | ||
import chatController from "../controller/chatController" | ||
|
||
const chatRoutes = Router(); | ||
|
||
chatRoutes.get( | ||
'/chat', | ||
query('cursor').isNumeric().optional(), | ||
query('limit').isNumeric().optional(), | ||
query('sessionId').isString().notEmpty(), | ||
requestValidator, | ||
chatController.list | ||
) | ||
|
||
chatRoutes.get( | ||
':jid', | ||
query('cursor').isNumeric().optional(), | ||
query('limit').isNumeric().optional(), | ||
query('sessionId').isString().notEmpty(), | ||
requestValidator, | ||
chatController.find | ||
) | ||
|
||
|
||
export default chatRoutes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters