|
1 | 1 | import express, { Request, Response } from "express";
|
| 2 | +import { validationResult } from "express-validator"; |
| 3 | +import ChatLog from "../models/ChatLog"; |
2 | 4 | import Session, { TSession } from "../models/Session";
|
3 | 5 | import {
|
| 6 | + createChatLogValidators, |
4 | 7 | createSessionValidators,
|
| 8 | + getChatLogValidators, |
5 | 9 | idValidators,
|
6 | 10 | updateSessionValidators,
|
7 | 11 | } from "./validators";
|
8 |
| -import { validationResult } from "express-validator"; |
9 | 12 |
|
10 | 13 | /**
|
11 | 14 | * Router for the collaboration service.
|
@@ -205,4 +208,69 @@ router.delete(
|
205 | 208 | }
|
206 | 209 | );
|
207 | 210 |
|
| 211 | +// Create a single chat log |
| 212 | +router.post("/chat/:collabid/create_chatlog", [...createChatLogValidators], async (req: Request, res: Response) => { |
| 213 | + // TODO: Add validation check to check if a collab ID exists and if the sender and recipient IDs are valid |
| 214 | + |
| 215 | + const errors = validationResult(req); |
| 216 | + if (!errors.isEmpty()) { |
| 217 | + return res.status(400).json({ errors: errors.array() }); |
| 218 | + } |
| 219 | + try { |
| 220 | + const { message, senderId, recipientId, timestampEpoch } = req.body; |
| 221 | + const { collabid } = req.params; |
| 222 | + const chatLog = { |
| 223 | + collabid, |
| 224 | + message, |
| 225 | + senderId, |
| 226 | + recipientId, |
| 227 | + timestampEpoch |
| 228 | + }; |
| 229 | + |
| 230 | + const newChatLog = new ChatLog(chatLog); |
| 231 | + await newChatLog.save(); |
| 232 | + res.status(200).json({ message: "Chat log created successfully", chatLog: newChatLog }); |
| 233 | + } catch (error) { |
| 234 | + console.log(error) |
| 235 | + return res.status(500).send("Internal server error"); |
| 236 | + } |
| 237 | +}) |
| 238 | + |
| 239 | +// Fetch chat logs for a specific collabID with pagination |
| 240 | +router.get("/chat/:collabid/get_chatlogs", [...getChatLogValidators], async (req: Request, res: Response) => { |
| 241 | + try { |
| 242 | + const { collabid } = req.params; |
| 243 | + const page = parseInt(req.query.page as string, 10) || 1; |
| 244 | + const limit = parseInt(req.query.limit as string, 10) || 10; |
| 245 | + |
| 246 | + const skip = (page - 1) * limit; |
| 247 | + |
| 248 | + // Fetch chat logs for the specified collabID, with pagination and sorted by timestamp |
| 249 | + const chatLogs = await ChatLog.find({ collabId: collabid }) |
| 250 | + .sort({ timestamp: -1 }) // Sort by timestamp in descending order (latest first) |
| 251 | + .skip(skip) |
| 252 | + .limit(limit); |
| 253 | + |
| 254 | + chatLogs.reverse(); |
| 255 | + |
| 256 | + // Get total count of chat logs for the given collabID |
| 257 | + const totalLogs = await ChatLog.countDocuments({ collabid }); |
| 258 | + const totalPages = Math.ceil(totalLogs / limit); |
| 259 | + |
| 260 | + res.status(200).json({ |
| 261 | + message: "Chat logs fetched successfully", |
| 262 | + chatLogs: chatLogs, |
| 263 | + pagination: { |
| 264 | + page, |
| 265 | + limit, |
| 266 | + totalPages, |
| 267 | + totalLogs, |
| 268 | + }, |
| 269 | + }); |
| 270 | + } catch (error) { |
| 271 | + console.log(error); |
| 272 | + res.status(500).send("Internal server error"); |
| 273 | + } |
| 274 | +}); |
| 275 | + |
208 | 276 | export default router;
|
0 commit comments