Skip to content

Commit 0af5af7

Browse files
committed
Add prettier script
1 parent 456d8b1 commit 0af5af7

File tree

8 files changed

+217
-204
lines changed

8 files changed

+217
-204
lines changed

collab-service/.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "es5",
3+
"tabWidth": 2,
4+
"semi": true,
5+
"singleQuote": false
6+
}
Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,74 @@
1-
import { createRoom, get_roomID, heartbeat, get_all_rooms } from '../model/repository.js';
2-
import crypto from 'crypto';
1+
import {
2+
createRoom,
3+
get_roomID,
4+
heartbeat,
5+
get_all_rooms,
6+
} from "../model/repository.js";
7+
import crypto from "crypto";
38

49
// Create a room between two users
510
export async function create_room(req, res) {
6-
const { user1, user2 } = req.body;
11+
const { user1, user2 } = req.body;
712

8-
if (!user1 || !user2) {
9-
return res.status(400).json({ error: 'Both user1 and user2 are required' });
10-
}
13+
if (!user1 || !user2) {
14+
return res.status(400).json({ error: "Both user1 and user2 are required" });
15+
}
1116

12-
// Generate a unique room ID by hashing the two user IDs
13-
const roomId = crypto.createHash('sha256').update(user1 + user2).digest('hex');
14-
const room = await createRoom(user1, user2, roomId);
17+
// Generate a unique room ID by hashing the two user IDs
18+
const roomId = crypto
19+
.createHash("sha256")
20+
.update(user1 + user2)
21+
.digest("hex");
22+
const room = await createRoom(user1, user2, roomId);
1523

16-
if (room) {
17-
res.status(201).json(room);
18-
} else {
19-
res.status(500).json({ error: 'Failed to create room' });
20-
}
24+
if (room) {
25+
res.status(201).json(room);
26+
} else {
27+
res.status(500).json({ error: "Failed to create room" });
28+
}
2129
}
2230

2331
// Get room ID by user
2432
export async function get_room_by_user(req, res) {
25-
const { user } = req.params;
33+
const { user } = req.params;
2634

27-
if (!user) {
28-
return res.status(400).json({ error: 'User is required' });
29-
}
35+
if (!user) {
36+
return res.status(400).json({ error: "User is required" });
37+
}
3038

31-
const room = await get_roomID(user);
39+
const room = await get_roomID(user);
3240

33-
if (room) {
34-
res.status(200).json(room);
35-
} else {
36-
res.status(404).json({ error: `Room not found for user: ${user}` });
37-
}
41+
if (room) {
42+
res.status(200).json(room);
43+
} else {
44+
res.status(404).json({ error: `Room not found for user: ${user}` });
45+
}
3846
}
3947

4048
// Update heartbeat for a room
4149
export async function update_heartbeat(req, res) {
42-
const { roomId } = req.params;
50+
const { roomId } = req.params;
4351

44-
if (!roomId) {
45-
return res.status(400).json({ error: 'Room ID is required' });
46-
}
52+
if (!roomId) {
53+
return res.status(400).json({ error: "Room ID is required" });
54+
}
4755

48-
const updatedRoom = await heartbeat(roomId);
56+
const updatedRoom = await heartbeat(roomId);
4957

50-
if (updatedRoom) {
51-
res.status(200).json(updatedRoom);
52-
} else {
53-
res.status(404).json({ error: `Room with ID ${roomId} not found` });
54-
}
58+
if (updatedRoom) {
59+
res.status(200).json(updatedRoom);
60+
} else {
61+
res.status(404).json({ error: `Room with ID ${roomId} not found` });
62+
}
5563
}
5664

5765
// Get all rooms
5866
export async function get_all_rooms_controller(req, res) {
59-
const rooms = await get_all_rooms();
67+
const rooms = await get_all_rooms();
6068

61-
if (rooms) {
62-
res.status(200).json(rooms);
63-
} else {
64-
res.status(500).json({ error: 'Failed to retrieve rooms' });
65-
}
69+
if (rooms) {
70+
res.status(200).json(rooms);
71+
} else {
72+
res.status(500).json({ error: "Failed to retrieve rooms" });
73+
}
6674
}

collab-service/app/index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import express from 'express';
2-
import cors from 'cors';
3-
import collabRoutes from './routes/collab-routes.js';
1+
import express from "express";
2+
import cors from "cors";
3+
import collabRoutes from "./routes/collab-routes.js";
44

55
const app = express();
66

@@ -9,14 +9,13 @@ app.use(express.json());
99
app.use(cors()); // config cors so that front-end can use
1010
app.options("*", cors());
1111

12-
1312
// To handle CORS Errors
1413
app.use((req, res, next) => {
1514
res.header("Access-Control-Allow-Origin", "*"); // "*" -> Allow all links to access
1615

1716
res.header(
1817
"Access-Control-Allow-Headers",
19-
"Origin, X-Requested-With, Content-Type, Accept, Authorization",
18+
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
2019
);
2120

2221
// Browsers usually send this before PUT or POST Requests
@@ -29,10 +28,8 @@ app.use((req, res, next) => {
2928
next();
3029
});
3130

32-
3331
app.use("/collab", collabRoutes);
3432

35-
3633
app.get("/", (req, res, next) => {
3734
console.log("Sending Greetings!");
3835
res.json({
@@ -56,5 +53,4 @@ app.use((error, req, res, next) => {
5653
});
5754
});
5855

59-
60-
export default app;
56+
export default app;
Lines changed: 85 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,106 @@
1-
import { connect, mongoose } from 'mongoose';
1+
import { connect, mongoose } from "mongoose";
22
import UsersSession from "./usersSession-model.js";
33

44
export async function connectToMongo() {
5-
await connect('mongodb+srv://admin:[email protected]/');
5+
await connect(
6+
"mongodb+srv://admin:[email protected]/"
7+
);
68
}
79

810
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+
}
2224
}
2325

2426
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+
}
3234
}
3335

3436
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+
}
4446
}
4547

4648
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+
}
5456
}
5557

56-
5758
// Function to add a new message to chatHistory with transaction support
5859
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
7476
? sessionDoc.chatHistory[sessionDoc.chatHistory.length - 1].messageIndex
7577
: -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

Comments
 (0)