Skip to content

Commit

Permalink
Basic implemntation of session service
Browse files Browse the repository at this point in the history
Additions:
- session interface for mongoose to store session information
- session routes to make use of http requests and responses
- code for creation of session, joining, and session deletion
- implemented jwt validation into joining and deletion requests
  • Loading branch information
jayllo-c committed Oct 6, 2024
1 parent 22556fa commit f17ed17
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
216 changes: 216 additions & 0 deletions collaboration-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions collaboration-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"jsonwebtoken": "^9.0.2",
"mode": "^0.3.2",
"model": "^0.0.6",
"mongodb": "^6.9.0",
"mongoose": "^8.7.0",
"redis-adapter": "^0.1.0",
"socket.io": "^4.7.5",
"utils": "^0.3.1"
Expand Down
63 changes: 63 additions & 0 deletions collaboration-service/src/controller/session-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Request, Response } from 'express';
import Session from '../model/session-model';

export const sessionController = {
createSession: async (req: Request, res: Response) => {
const {
session_id, // assuming session id is passed in the request body
participants,
question,
code,
} = req.body;

// const sessionId = Math.random().toString(36).substring(2, 8);
const session = new Session({
session_id: session_id, //sessionId,
date_created: new Date(),
participants,
question,
code,
});

await session.save()
.then((data) => {
res.status(201).json(data);
})
.catch((err) => {
res.status(500).json({ message: (err as Error).message });
});
},
joinSession: async (req: Request, res: Response) => {
const { sessionId, userId } = req.body

try {
// Find a session that the user is a participant of
const session = await Session.findOne({ participants: userId });

if (!session) {
return res.status(404).json({ message: 'Session not found' });
}

res.status(200).json(session);
} catch (err) {
res.status(500).json({ message: (err as Error).message });
}
},
terminateSession: async (req: Request, res: Response) => {
const { sessionId } = req.body;

try {
const session = await Session.findOne({ session_id: sessionId });

if (!session) {
return res.status(404).json({ message: 'Session not found' });
}

await Session.deleteOne({ session_id: sessionId });

res.status(200).json({ message: 'Session terminated successfully' });
} catch (err) {
res.status(500).json({ message: (err as Error).message });
}
}
};
14 changes: 0 additions & 14 deletions collaboration-service/src/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion collaboration-service/src/middleware/jwt-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ export function validateSocketJWT(socket: Socket, next: (err?: Error) => void) {

export function validateJWT (token: string): JwtPayload {
return jwt.verify(token, secretKey) as JwtPayload;
}
}
Loading

0 comments on commit f17ed17

Please sign in to comment.