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