|
| 1 | +import { connect, mongoose } from "mongoose"; |
| 2 | +import UsersSession from "./usersSession-model.js"; |
| 3 | + |
| 4 | +export async function connectToMongo() { |
| 5 | + await connect(process.env.DB_URI); |
| 6 | +} |
| 7 | + |
| 8 | +export async function newRoom(user1, user2, roomId) { |
| 9 | + try { |
| 10 | + const newRoom = new UsersSession({ |
| 11 | + users: [user1, user2], |
| 12 | + roomId: roomId, |
| 13 | + lastUpdated: new Date(), |
| 14 | + }); |
| 15 | + |
| 16 | + const savedRoom = await newRoom.save(); |
| 17 | + return savedRoom; |
| 18 | + } catch (error) { |
| 19 | + console.error("Error creating room:", error); |
| 20 | + return null; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +export async function getRoomId(user) { |
| 25 | + try { |
| 26 | + const room = await UsersSession.findOne({ users: user }); |
| 27 | + return room; |
| 28 | + } catch (error) { |
| 29 | + console.error("Error finding room for ${user}:", error); |
| 30 | + return null; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export async function heartbeat(roomId) { |
| 35 | + try { |
| 36 | + const room = await UsersSession.findOne({ roomId: roomId }); |
| 37 | + room.lastUpdated = new Date(); |
| 38 | + await room.save(); |
| 39 | + return room; |
| 40 | + } catch (error) { |
| 41 | + console.error("Error updating room ${roomId}:", error); |
| 42 | + return null; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +export async function getAllRooms() { |
| 47 | + try { |
| 48 | + const rooms = await UsersSession.find({}); |
| 49 | + return rooms; |
| 50 | + } catch (error) { |
| 51 | + console.error("Error getting all rooms:", error); |
| 52 | + return null; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Function to add a new message to chatHistory with transaction support |
| 57 | +export async function addMessageToChat(roomId, userId, text) { |
| 58 | + // Start a session for the transaction |
| 59 | + const session = await mongoose.startSession(); |
| 60 | + |
| 61 | + try { |
| 62 | + session.startTransaction(); |
| 63 | + |
| 64 | + // Find the session document by roomId within the transaction |
| 65 | + const sessionDoc = await UsersSession.findOne({ roomId }).session(session); |
| 66 | + |
| 67 | + if (!sessionDoc) { |
| 68 | + throw new Error("Room not found"); |
| 69 | + } |
| 70 | + |
| 71 | + // Determine the next message index within the transaction |
| 72 | + const lastMessageIndex = |
| 73 | + sessionDoc.chatHistory.length > 0 |
| 74 | + ? sessionDoc.chatHistory[sessionDoc.chatHistory.length - 1].messageIndex |
| 75 | + : -1; |
| 76 | + |
| 77 | + // Create the new message with incremented messageIndex |
| 78 | + const newMessage = { |
| 79 | + messageIndex: lastMessageIndex + 1, |
| 80 | + userId, |
| 81 | + text, |
| 82 | + timestamp: new Date(), |
| 83 | + }; |
| 84 | + |
| 85 | + // Add the new message to chatHistory within the transaction |
| 86 | + sessionDoc.chatHistory.push(newMessage); |
| 87 | + |
| 88 | + // Save the document within the transaction |
| 89 | + await sessionDoc.save({ session }); |
| 90 | + |
| 91 | + // Commit the transaction |
| 92 | + await session.commitTransaction(); |
| 93 | + |
| 94 | + // End the session and return the new message |
| 95 | + session.endSession(); |
| 96 | + return newMessage; |
| 97 | + } catch (error) { |
| 98 | + // If an error occurs, abort the transaction |
| 99 | + await session.abortTransaction(); |
| 100 | + session.endSession(); |
| 101 | + console.error("Error adding message to chat:", error); |
| 102 | + throw error; |
| 103 | + } |
| 104 | +} |
0 commit comments