Skip to content

Commit

Permalink
fix(meetings): Merge list endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
HagerDakroury committed Jul 15, 2021
1 parent a95bfb9 commit a2f2399
Showing 1 changed file with 36 additions and 61 deletions.
97 changes: 36 additions & 61 deletions meetings/src/routes/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import axios from "axios";

const router = express.Router();

router.get(`${baseUrl}/listhost`, async (req: Request, res: Response) => {
router.get(`${baseUrl}/list`, async (req: Request, res: Response) => {
const status = req.query.status;

let resV;
Expand All @@ -20,7 +20,6 @@ router.get(`${baseUrl}/listhost`, async (req: Request, res: Response) => {
};
resV = await axios.post(
"http://auth-service:4002/api/v1/auth/verify",
{ role: "instructor" },
config
);
} catch (err) {
Expand All @@ -31,70 +30,46 @@ router.get(`${baseUrl}/listhost`, async (req: Request, res: Response) => {
}

const user = resV.data._id;
const role = resV.data.role;

let meetings;
if (role == "instructor") {
let meetings;

try {
if (status == "ended" || status == "incoming") {
meetings = await Meeting.find({ host: user, status: status });
} else {
meetings = await Meeting.find({ host: user });
}

if (!meetings) {
return res.status(201).send("No meetings found");
}

return res.status(201).send(meetings);
} catch (error) {
return res.status(400);
}
});

router.get(`${baseUrl}/liststudent`, async (req: Request, res: Response) => {
const status = req.query.status;

let resV;
//validating the user's token
try {
const token = req.header("Authorization")?.split(" ")[1];
let config = {
headers: {
Authorization: "Bearer " + token,
},
};
resV = await axios.post(
"http://auth:3000/api/v1/auth/verify",
{ role: "student" },
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 {
if (status == "ended" || status == "incoming") {
meetings = await Meeting.find({ host: user, status: status });
} else {
meetings = await Meeting.find({ host: user });
}

let userMeetings;
if (!meetings) {
return res.status(201).send("No meetings found");
}

try {
const userMeeting = await UserMeetings.findOne({ name: user });
const userMeetingsIds = userMeeting?.meetings;

if (status === "ended" || status === "incoming") {
userMeetings = await Meeting.find({
_id: { $in: userMeetingsIds },
status: status,
});
} else {
userMeetings = await Meeting.find({ _id: { $in: userMeetingsIds } });
return res.status(201).send(meetings);
} catch (error) {
return res.status(400);
}
} else if (role == "student") {
let userMeetings;

try {
const userMeeting = await UserMeetings.findOne({ name: user });
const userMeetingsIds = userMeeting?.meetings;

if (status === "ended" || status === "incoming") {
userMeetings = await Meeting.find({
_id: { $in: userMeetingsIds },
status: status,
});
} else {
userMeetings = await Meeting.find({ _id: { $in: userMeetingsIds } });
}

return res.status(201).send(userMeetings);
} catch (error) {
return res.status(400);
}

return res.status(201).send(userMeetings);
} catch (error) {
return res.status(400);
}
});

Expand Down

0 comments on commit a2f2399

Please sign in to comment.