Skip to content

Commit

Permalink
feat: added questions adding
Browse files Browse the repository at this point in the history
  • Loading branch information
woaitsAryan committed Jan 17, 2024
1 parent 0d38cf4 commit eb91d82
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 57 deletions.
32 changes: 15 additions & 17 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ jobs:
- name: Build to test
run: npm run build

Deploy:
needs: Test
runs-on: ubuntu-latest
steps:
- name: Deploying on Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
script: |
cd CCS/CCS-Backend-2022
git reset --hard
git pull
cd ..
docker-compose up --build -d
docker system prune -a -f
# Deploy:
# needs: Test
# runs-on: self-hosted
# steps:
# - name: Checkout repository
# uses: actions/checkout@v3

# - name: Pull Changes
# run: git pull origin main
# working-directory: /home/aryan/CCS-Backend-2022

# - name: Build and Deploy
# run: |
# sudo docker compose up --build -d
# working-directory: /home/aryan/CCS-Backend-2022
2 changes: 1 addition & 1 deletion config/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ f7nAWWlOhIzawxMKZwIDAQAB
task_start_date: process.env.TASK_START_DATE,
email_verified_redirect: `${process.env.ORIGIN_URL}/login`,
origin_url: process.env.ORIGIN_URL,
// admin_token: process.env.ADMIN_TOKEN,
admin_token: process.env.ADMIN_TOKEN,
tech_cutoff: 8,
management_cutoff: 8,
design_cutoff: 7,
Expand Down
79 changes: 51 additions & 28 deletions src/controller/admin.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// import config from "config";
import { Request, Response } from "express";
import config from "config";
import ccsUserModel from "../models/ccsUser.model";
import TaskModel from "../models/task.model";
import UserModel from "../models/user.model";
Expand All @@ -9,44 +10,45 @@ import {
AdminGetUserInput,
AdminPostInput,
AdminPutInput,
// MakeAdminInput,
AdminAddQuestionInput,
MakeAdminInput,
} from "../schema/adminPost.schema";
import {
getAllUsers,
getCcsUserByUsername,
getCcsUserInfoByUsername,
} from "../service/ccsUser.service";
import { getAllQuestion } from "../service/question.service";
import { getAllQuestion, inputAllQuestion } from "../service/question.service";
import errorObject from "../utils/errorObject";
import logger from "../utils/logger";
import standardizeObject from "../utils/standardizeObject";

// export async function makeAdminHandler(
// req: Request<MakeAdminInput>,
// res: Response
// ) {
// try {
// const user = await UserModel.findOne({ username: req.params.username });
// if (!user) {
// return res.status(200).send(errorObject(404, "check your username"));
// }
// if (!(req.params.token === config.get<string>("admin_token"))) {
// return res.status(200).send(errorObject(403, "You shall not pass!!"));
// }
// user.scope.push("admin");
// return res
// .status(200)
// .send(
// errorObject(
// 200,
// "you are now the proud owner of admin privilege, if u misuse imma spank"
// )
// );
// } catch (e) {
// logger.error(e);
// return res.status(500).send(errorObject(500, "", standardizeObject(e)));
// }
// }
export async function makeAdminHandler(
req: Request<MakeAdminInput>,
res: Response
) {
try {
const user = await UserModel.findOne({ username: req.params.username });
if (!user) {
return res.status(200).send(errorObject(404, "check your username"));
}
if (!(req.params.token === config.get<string>("admin_token"))) {
return res.status(200).send(errorObject(403, "You shall not pass!!"));
}
user.scope.push("admin");
return res
.status(200)
.send(
errorObject(
200,
"you are now the proud owner of admin privilege, if u misuse imma spank"
)
);
} catch (e) {
logger.error(e);
return res.status(500).send(errorObject(500, "", standardizeObject(e)));
}
}
export async function getUsersHandler(req: Request, res: Response) {
try {
const users = await getAllUsers();
Expand All @@ -56,6 +58,27 @@ export async function getUsersHandler(req: Request, res: Response) {
return res.status(500).send(errorObject(500, "", standardizeObject(e)));
}
}

export async function addAllQuestions(
req: Request<
Record<string, never>,
Record<string, never>,
AdminAddQuestionInput
>,
res: Response
) {
try {
if (req.body.auth !== config.get<string>("admin_token")) {
return res.status(200).send(errorObject(403, "Unauthorized!"));
}
const result = await inputAllQuestion(req.body);
return res.status(200).send(errorObject(200, "", result));
} catch (e) {
logger.error(e);
return res.status(500).send(errorObject(500, "", standardizeObject(e)));
}
}

export async function getUserInfoHandler(
req: Request<AdminGetUserInput>,
res: Response
Expand Down
22 changes: 15 additions & 7 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import { submitSchema } from "./schema/submit.schema";
import submitHandler from "./controller/submit.controller";
import requireTime from "./middleware/requireTime";
import {
addAllQuestions,
deleteUserHandler,
getUserInfoHandler,
getUsersHandler,
isCheckingHandler,
// makeAdminHandler,
makeAdminHandler,
updateCcsUserHandler,
} from "./controller/admin.controller";
import questionHandler from "./controller/question.controller";
Expand All @@ -48,6 +49,7 @@ import { resendEmailSchema } from "./schema/resendEmail.schema";
import requireTaskTime from "./middleware/requireTaskTime";
import requireAdmin from "./middleware/requireAdmin";
import {
AdminAddQuestionsSchema,
AdminDeleteUserSchema,
AdminGetUserSchema,
adminPostSchema,
Expand Down Expand Up @@ -126,6 +128,12 @@ function routes(app: Express) {
updateCcsUserHandler
);

app.post(
"/api/admin/addQuestions",
validateResource(AdminAddQuestionsSchema),
addAllQuestions
);

app.delete(
"/api/admin/deleteUser",
apiLimiter,
Expand Down Expand Up @@ -160,12 +168,12 @@ function routes(app: Express) {

app.get("/api/getUser", apiLimiter, requireUser, getUserHandler);

// app.get(
// "/api/lemmein/:username/:token",
// apiLimiter,
// requireUser,
// makeAdminHandler
// );
app.get(
"/api/lemmein/:username/:token",
apiLimiter,
requireUser,
makeAdminHandler
);

app.put(
"/api/users/info",
Expand Down
41 changes: 37 additions & 4 deletions src/schema/adminPost.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ export const AdminGetUserSchema = object({
}),
});

export const AdminAddQuestionsSchema = object({
body: object({
auth: string(),
questions: object(
{
quesId: number(),
question: object({
text: string(),
img: string().array(),
links: string().array(),
}),
difficulty: string(),
domain: string(),
},
{ required_error: "questions object is required" }
).array(),
}),
});

export const AdminPutSchema = object({
body: object({
username: string({ required_error: "username is required" }),
Expand Down Expand Up @@ -81,6 +100,20 @@ export type AdminPostInput = {
mark?: number;
};

export type AdminAddQuestionInput = {
auth: string;
questions: {
quesId: number;
question: {
text: string;
img: string[];
links: string[];
};
difficulty: "Easy" | "Medium" | "Hard";
domain: "tech" | "design" | "management" | "video";
}[];
};

export type AdminGetUserInput = {
regNo?: string;
};
Expand All @@ -93,7 +126,7 @@ export type AdminDeleteUserInput = {
username: string;
};

// export type MakeAdminInput = {
// username: string;
// token: string;
// };
export type MakeAdminInput = {
username: string;
token: string;
};
5 changes: 5 additions & 0 deletions src/service/question.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import questionModel from "../models/question.model";
import { AdminAddQuestionInput } from "../schema/adminPost.schema";

export default async function getQuestion(domain: string, difficulty: string) {
return questionModel.find({ domain, difficulty }).select("quesId question");
Expand All @@ -7,6 +8,10 @@ export async function getAllQuestion() {
return questionModel.find({}).select("_id quesId question");
}

export async function inputAllQuestion(input: AdminAddQuestionInput) {
return questionModel.insertMany(input.questions);
}

// export async function final(easyquestions, mediumquestions, hardquestions) {
// const easyshuffled = easyquestions.sort(() => 0.5 - Math.random());
// let selected = easyshuffled.slice(0, 2);
Expand Down

0 comments on commit eb91d82

Please sign in to comment.