-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from crux-bphc/add-unenroll-endpoint
Add unenroll endpoint
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters