Skip to content

Commit

Permalink
fix(meetings): Seperate join subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
HagerDakroury committed Jul 16, 2021
1 parent 08e9e9c commit ac5a967
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 36 deletions.
2 changes: 2 additions & 0 deletions meetings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -27,6 +28,7 @@ app.use(listMeetingsRouter);
app.use(startMeetingsRouter);
app.use(joinMeetingsRouter);
app.use(endMeetingsRouter);
app.use(subscribeMeetingsRouter);

const port = 4000;

Expand Down
56 changes: 20 additions & 36 deletions meetings/src/routes/join.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
71 changes: 71 additions & 0 deletions meetings/src/routes/subscribe.ts
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit ac5a967

Please sign in to comment.