Skip to content

Commit

Permalink
Merge pull request #74 from crux-bphc/add-unenroll-endpoint
Browse files Browse the repository at this point in the history
Add unenroll endpoint
  • Loading branch information
Anurav-Garg authored Aug 5, 2023
2 parents 010eb00 + 4c20217 commit 11055b6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
"ts-node": "10.7.0"
},
"dependencies": {
"@types/cors": "^2.8.13",
"@types/body-parser": "^1.19.2",
"@types/cookie-parser": "^1.4.3",
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^16.11.10",
"@types/supertest": "^2.0.12",
"body-parser": "^1.19.1",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"cross-fetch": "^4.0.0",
"dotenv": "^16.3.1",
"express": "^4.17.2",
"openid-client": "^5.4.2",
Expand Down
38 changes: 38 additions & 0 deletions pnpm-lock.yaml

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

41 changes: 41 additions & 0 deletions src/controllers/user/cmsFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Request, Response } from "express";
import { validate } from "../../middleware/zodValidateRequest";
import { z } from "zod";
import {
namedIntegerType,
namedNonEmptyStringType,
} from "../../types/zodFieldTypes";
import fetch from "cross-fetch";

const unenrollDataSchema = z.object({
body: z.object({
enrollID: namedIntegerType("enrollID"),
sesskey: namedNonEmptyStringType("sesskey"),
cookie: namedNonEmptyStringType("cookie"),
}),
});

export const unenrollValidator = validate(unenrollDataSchema);

export const unenroll = async (req: Request, res: Response) => {
const enrollID: number = req.body.enrollID;
const sesskey: string = req.body.sesskey;
const cookie: string = req.body.cookie;

try {
const unenrolResponse = await fetch(
`https://cms.bits-hyderabad.ac.in/enrol/self/unenrolself.php?confirm=1&enrolid=${enrollID}&sesskey=${sesskey}`,
{
method: "GET",
headers: {
cookie: `MoodleSession=${cookie};`,
},
}
);
return res.status(unenrolResponse.status).end();
} catch (err: any) {
// will replace the console.log with a logger when we have one
console.log("Error unenrolling from course: ", err.message);
return res.status(500).json({ message: "Internal Server Error" });
}
};
2 changes: 2 additions & 0 deletions src/routers/userRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
getUserDetailsValidator,
} from "../controllers/user/getUserDetails";
import { authenticate } from "../middleware/auth";
import { unenroll, unenrollValidator } from "../controllers/user/cmsFunctions";

const userRouter = express.Router();

userRouter.get("/:id?", authenticate, getUserDetailsValidator, getUserDetails);
userRouter.post("/edit", authenticate, editUserValidator, editUser);
userRouter.post("/unenroll", authenticate, unenrollValidator, unenroll);

export default userRouter;

0 comments on commit 11055b6

Please sign in to comment.