From feb759b3857cebb28baa143d9a6962100399ec88 Mon Sep 17 00:00:00 2001 From: soumitradev Date: Sat, 5 Aug 2023 13:31:16 +0530 Subject: [PATCH 1/3] build: add cross-fetch for fetch API on node --- package.json | 3 ++- pnpm-lock.yaml | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 0e2075a9..52a24701 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,9 @@ "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", @@ -28,6 +28,7 @@ "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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 38fb6ad6..25b3ce3a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -40,6 +40,9 @@ dependencies: cors: specifier: ^2.8.5 version: 2.8.5 + cross-fetch: + specifier: ^4.0.0 + version: 4.0.0 dotenv: specifier: ^16.3.1 version: 16.3.1 @@ -1620,6 +1623,14 @@ packages: /create-require@1.1.1: resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==} + /cross-fetch@4.0.0: + resolution: {integrity: sha512-e4a5N8lVvuLgAWgnCrLr2PP0YyDOTHa9H/Rj54dirp61qXnNq46m82bRhNqIA5VccJtWBvPTFRV3TtvHUKPB1g==} + dependencies: + node-fetch: 2.6.12 + transitivePeerDependencies: + - encoding + dev: false + /cross-spawn@7.0.3: resolution: {integrity: sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==} engines: {node: '>= 8'} @@ -3078,6 +3089,18 @@ packages: engines: {node: '>= 0.6'} dev: false + /node-fetch@2.6.12: + resolution: {integrity: sha512-C/fGU2E8ToujUivIO0H+tpQ6HWo4eEmchoPIoXtxCrVghxdKq+QOHqEZW7tuP3KlV3bC8FRMO5nMCC7Zm1VP6g==} + engines: {node: 4.x || >=6.0.0} + peerDependencies: + encoding: ^0.1.0 + peerDependenciesMeta: + encoding: + optional: true + dependencies: + whatwg-url: 5.0.0 + dev: false + /node-int64@0.4.0: resolution: {integrity: sha512-O5lz91xSOeoXP6DulyHfllpq+Eg00MWitZIbtPfoSEvqIHdl5gfcY6hYzDWnj0qD5tz52PI08u9qUvSVeUBeHw==} dev: true @@ -3826,6 +3849,10 @@ packages: nopt: 1.0.10 dev: true + /tr46@0.0.3: + resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==} + dev: false + /ts-jest@29.1.1(@babel/core@7.22.6)(jest@29.5.0)(typescript@4.5.2): resolution: {integrity: sha512-D6xjnnbP17cC85nliwGiL+tpoKN0StpgE0TeOjXQTU6MVCfsB4v7aW05CgQ/1OywGb0x/oy9hHFnN+sczTiRaA==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -4081,6 +4108,17 @@ packages: makeerror: 1.0.12 dev: true + /webidl-conversions@3.0.1: + resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==} + dev: false + + /whatwg-url@5.0.0: + resolution: {integrity: sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==} + dependencies: + tr46: 0.0.3 + webidl-conversions: 3.0.1 + dev: false + /which@2.0.2: resolution: {integrity: sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==} engines: {node: '>= 8'} From 25c87b8040a503fa9d5beca06ca270c1697e0d1f Mon Sep 17 00:00:00 2001 From: soumitradev Date: Sat, 5 Aug 2023 13:31:36 +0530 Subject: [PATCH 2/3] feat: add unenroll endpoint --- src/controllers/user/cmsFunctions.ts | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/controllers/user/cmsFunctions.ts diff --git a/src/controllers/user/cmsFunctions.ts b/src/controllers/user/cmsFunctions.ts new file mode 100644 index 00000000..e1a98ed2 --- /dev/null +++ b/src/controllers/user/cmsFunctions.ts @@ -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" }); + } +}; From 4c20217ef29ae0d717795f54e2f3dbad52d6facb Mon Sep 17 00:00:00 2001 From: soumitradev Date: Sat, 5 Aug 2023 13:31:50 +0530 Subject: [PATCH 3/3] feat: add unenroll endpoint to user router --- src/routers/userRouter.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/routers/userRouter.ts b/src/routers/userRouter.ts index f00691cb..42950f2f 100644 --- a/src/routers/userRouter.ts +++ b/src/routers/userRouter.ts @@ -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;