Skip to content

Commit

Permalink
Updating Eligibility
Browse files Browse the repository at this point in the history
  • Loading branch information
SeonerVorteX committed May 27, 2024
1 parent 6e27dd4 commit 62b967a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 30 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ HOST_URL=
UNEC_BASE_URL=
API_PORT=
JWT_ACCESS_TOKEN_SECRET=
MONGO_URI=
MONGO_URI=
AUHT_WEBHOOK_URL=
EXAM_WEBHOOK_URL=
21 changes: 18 additions & 3 deletions src/controllers/exams/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import { examLog } from "../../utils/webhook";

export const getAllExams = async (req: Request, res: Response) => {
try {
return res.status(200).json(exams).end();
const user = get(req, "identity.user") as UserType;
const group = user.group;
const data = exams.filter((exam) => exam.eligibleGroups.includes(group));

return res.status(200).json(data).end();
} catch (err) {
const errorHandler = new ErrorManager(res);
logger.error("An error occured while getting all exams");
Expand All @@ -27,7 +31,13 @@ export const getExam = async (req: Request, res: Response) => {
try {
const errorManager = new ErrorManager(res);
const examId = parseInt(get(req, "params.examId"));
const exam = exams.find((exam) => exam.id == examId) as { id: number; title: string; shortName: string };
const user = get(req, "identity.user") as UserType;
const group = user.group;
const exam = exams.filter((exam) => exam.eligibleGroups.includes(group)).find((exam) => exam.id == examId) as {
id: number;
title: string;
shortName: string;
};

if (!exam) {
return errorManager.handleError(new APIError("exam", "payload", "EXAM_NOT_FOUND"));
Expand All @@ -53,12 +63,17 @@ export const startExam = async (req: Request, res: Response) => {
const errorManager = new ErrorManager(res);
const body = get(req, "body") as StartExamPayload;
const user = get(req, "identity.user") as UserType;
const group = user.group;
const examId = parseInt(get(req, "params.examId"));

if (!examId || isNaN(examId) || examId < 1) {
return errorManager.handleError(new APIError("exam", "payload", "INVALID_EXAM_ID"));
}
const exam = exams.find((exam) => exam.id == examId) as { id: number; title: string };
const exam = exams.filter((exam) => exam.eligibleGroups.includes(group)).find((exam) => exam.id == examId) as {
id: number;
title: string;
shortName: string;
};

if (!exam) {
errorManager.handleError(new APIError("exam", "payload", "EXAM_NOT_FOUND"));
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ console.clear();

config();
const app = express();
app.enable("trust proxy");

// Middlewares
app.use(
Expand All @@ -25,7 +26,6 @@ app.use(
credentials: true,
})
);

app.use(flash());
app.use(compression());
app.use(cookieParser());
Expand Down
27 changes: 13 additions & 14 deletions src/middlewares/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { ErrorManager } from "../helpers/managers/ErrorManager";
import { APIError } from "../errors/APIError";
import { verifyAccessToken } from "../helpers/security/jwt";
import { RequestIdentity } from "types/types";
import { eligibleGroups } from "../configurations/configs.json";

export const checkUser = async (req: Request, res: Response, next: NextFunction) => {
try {
Expand Down Expand Up @@ -52,19 +51,19 @@ export const isAuthenticated = async (req: Request, res: Response, next: NextFun
}
};

export const isEligible = async (req: Request, res: Response, next: NextFunction) => {
const identity: RequestIdentity = get(req, "identity");
const errorHandler = new ErrorManager(res);
// export const isEligible = async (req: Request, res: Response, next: NextFunction) => {
// const identity: RequestIdentity = get(req, "identity");
// const errorHandler = new ErrorManager(res);

if (!identity.user) {
return errorHandler.handleError(new APIError("system", "authorization", "AUTHORIZATION_FAILED"));
}
// if (!identity.user) {
// return errorHandler.handleError(new APIError("system", "authorization", "AUTHORIZATION_FAILED"));
// }

const userGroup = identity.user.group;
// const userGroup = identity.user.group;

if (!eligibleGroups.includes(userGroup)) {
return errorHandler.handleError(new APIError("system", "authorization", "ELIGIBILITY_FAILED"));
} else {
return next();
}
};
// if (!eligibleGroups.includes(userGroup)) {
// return errorHandler.handleError(new APIError("system", "authorization", "ELIGIBILITY_FAILED"));
// } else {
// return next();
// }
// };
20 changes: 10 additions & 10 deletions src/router/exams/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import {
getQuestionForFinishedExam,
startExam,
} from "../../controllers/exams";
import { isAuthenticated, isEligible } from "../../middlewares";
import { isAuthenticated } from "../../middlewares";

const router = Router();

export default (): Router => {
router.get("/all", isAuthenticated, getAllExams);
router.get("/:examId", isAuthenticated, getExam);
router.post("/:examId/start", isAuthenticated, isEligible, startExam);
router.get("/active/:id", isAuthenticated, isEligible, getActiveExam);
router.get("/active/:id/questions", isAuthenticated, isEligible, getAllQuestionsForActiveExam);
router.get("/active/:id/questions/:row", isAuthenticated, isEligible, getQuestionForActiveExam);
router.post("/active/:id/answers", isAuthenticated, isEligible, setAnswersForActiveExam);
router.get("/active/:id/finish", isAuthenticated, isEligible, finishActiveExam);
router.get("/finished/:id", isAuthenticated, isEligible, getFinishedExam);
router.get("/finished/:id/questions", isAuthenticated, isEligible, getAllQuestionsForFinishedExam);
router.get("/finished/:id/questions/:row", isAuthenticated, isEligible, getQuestionForFinishedExam);
router.post("/:examId/start", isAuthenticated, startExam);
router.get("/active/:id", isAuthenticated, getActiveExam);
router.get("/active/:id/questions", isAuthenticated, getAllQuestionsForActiveExam);
router.get("/active/:id/questions/:row", isAuthenticated, getQuestionForActiveExam);
router.post("/active/:id/answers", isAuthenticated, setAnswersForActiveExam);
router.get("/active/:id/finish", isAuthenticated, finishActiveExam);
router.get("/finished/:id", isAuthenticated, getFinishedExam);
router.get("/finished/:id/questions", isAuthenticated, getAllQuestionsForFinishedExam);
router.get("/finished/:id/questions/:row", isAuthenticated, getQuestionForFinishedExam);
return router;
};
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"sourceMap": true,
"noImplicitAny": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"typeRoots": [
"node_modules/@types",
"src/types"
Expand All @@ -19,7 +20,7 @@
"src/**/*.ts"
],
"exclude": [
"node_modules",
"node_modules/",
"src/tests",
"src/helpers/logs",
"storage/"
Expand Down

0 comments on commit 62b967a

Please sign in to comment.