diff --git a/meetings/index.ts b/meetings/index.ts index 62903c9..50b08b1 100644 --- a/meetings/index.ts +++ b/meetings/index.ts @@ -9,6 +9,7 @@ import { listMeetingsRouter } from "./src/routes/list"; import { startMeetingsRouter } from "./src/routes/start"; import { joinMeetingsRouter } from "./src/routes/join"; import { endMeetingsRouter } from "./src/routes/end"; +import { subscribeMeetingsRouter } from "./src/routes/subscribe"; const app = express(); connectDb().then(() => { @@ -27,6 +28,7 @@ app.use(listMeetingsRouter); app.use(startMeetingsRouter); app.use(joinMeetingsRouter); app.use(endMeetingsRouter); +app.use(subscribeMeetingsRouter); const port = 4000; diff --git a/meetings/src/routes/join.ts b/meetings/src/routes/join.ts index 24dfd01..60512b7 100644 --- a/meetings/src/routes/join.ts +++ b/meetings/src/routes/join.ts @@ -1,7 +1,7 @@ import express, { Request, Response } from "express"; import { createTwilioToken } from "../utils/twilioToken"; import { baseUrl } from "../config"; -import { UserMeetings, newUserMeetings, Meeting } from "../models/model"; +import { UserMeetings, Meeting } from "../models/model"; import axios from "axios"; const router = express.Router(); @@ -32,48 +32,32 @@ router.post(`${baseUrl}/join/:id`, async (req: Request, res: Response) => { const user = resV.data._id; try { - // checking if the user have meetings and creating a new entry if not let userMeetings = await UserMeetings.findOne({ name: user }); - if (!userMeetings) { - userMeetings = await newUserMeetings({ - name: user, - meetings: [], - }); - } - - if (!id.match(/^[0-9a-fA-F]{24}$/)) { - return res.status(400).send("Meeting is not found"); - } - //checking if the meeting exist - const meeting = await Meeting.findOne({ _id: id }); - if (!meeting) { - return res.status(400).send("Meeting is not found"); - } + if (userMeetings) { + if (!id.match(/^[0-9a-fA-F]{24}$/)) { + return res.status(400).send("Meeting is not found"); + } - let message = "Meeting already added before"; - //check if the meeting is already in the user meetings - if (!userMeetings.meetings.includes(id)) { - let message = "Meeting added successfully"; - userMeetings.meetings.push(id); - } + //checking if the meeting exist + const meeting = await Meeting.findOne({ _id: id }); + if (!meeting) { + return res.status(400).send("Meeting is not found"); + } - userMeetings - .save() - .then((doc) => {}) - .catch((err) => { - res.status(400).send({ error: err }); - }); + //check if the meeting is already in the user meetings + if (!userMeetings.meetings.includes(id)) { + res.status(400).send("You're not a part of this meeting"); + } - //if meeting in progress, generate token + if (meeting.status === "current") { + const roomName = meeting.title + "-" + meeting.course; + const token = createTwilioToken(user, roomName); + return res.status(201).send(token); + } - if (meeting.status === "current") { - const roomName = meeting.title + "-" + meeting.course; - const token = createTwilioToken(user, roomName); - return res.status(201).send(token); + return res.status(400).send("Meeting hasn'r started yet"); } - - return res.status(201).send(message); } catch (error) { return res.status(400).send(error.message); } diff --git a/meetings/src/routes/subscribe.ts b/meetings/src/routes/subscribe.ts new file mode 100644 index 0000000..7203a41 --- /dev/null +++ b/meetings/src/routes/subscribe.ts @@ -0,0 +1,71 @@ +import express, { Request, Response } from "express"; +import { baseUrl } from "../config"; +import { UserMeetings, newUserMeetings, Meeting } from "../models/model"; +import axios from "axios"; + +const router = express.Router(); + +router.post(`${baseUrl}/subscribe/:id`, async (req: Request, res: Response) => { + const id = req.params.id; + + let resV; + //validating the user's token + try { + const token = req.header("Authorization")?.split(" ")[1]; + let config = { + headers: { + Authorization: "Bearer " + token, + }, + }; + resV = await axios.get( + "http://auth-service:4002/api/v1/auth/verify", + config + ); + } catch (err) { + return res.status(400).send("User is not authorized"); + } + if (resV.status == 400) { + return res.status(400).send("User is not authorized"); + } + + const user = resV.data._id; + + try { + // checking if the user have meetings and creating a new entry if not + let userMeetings = await UserMeetings.findOne({ name: user }); + if (!userMeetings) { + userMeetings = await newUserMeetings({ + name: user, + meetings: [], + }); + } + + if (!id.match(/^[0-9a-fA-F]{24}$/)) { + return res.status(400).send("Meeting is not found"); + } + + //checking if the meeting exist + const meeting = await Meeting.findOne({ _id: id }); + if (!meeting) { + return res.status(400).send("Meeting is not found"); + } + + //check if the meeting is already in the user meetings + if (!userMeetings.meetings.includes(id)) { + userMeetings.meetings.push(id); + } + + userMeetings + .save() + .then((doc) => {}) + .catch((err) => { + res.status(400).send(err); + }); + + return res.status(201).send(meeting); + } catch (error) { + return res.status(400).send(error.message); + } +}); + +export { router as subscribeMeetingsRouter };