Skip to content

Commit

Permalink
fix: move mentor application web to api (#1426)
Browse files Browse the repository at this point in the history
* fix: move mentor application web to api

* chore
  • Loading branch information
zzdhybthu committed Sep 2, 2024
1 parent 135de27 commit 6f40b2b
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 30 deletions.
125 changes: 125 additions & 0 deletions src/hasura/mentor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { gql } from "graphql-request";
import { client } from "..";

export const get_mentor_info_list = async () => {
const query: any = await client.request(
gql`
query GetMentorInfoList {
mentor_info(order_by: { available: desc, max_applicants: desc }) {
achievement
available
background
field
intro
max_applicants
mentor_uuid
user {
department
email
realname
}
}
}
`
);
return query.mentor_info;
}

export const get_mentor_info = async (uuid: string) => {
const query: any = await client.request(
gql`
query GetMentorInfo($uuid: uuid!) {
mentor_info_by_pk(mentor_uuid: $uuid) {
achievement
available
background
field
intro
max_applicants
mentor_uuid
user {
department
email
realname
}
}
}
`,
{ uuid: uuid }
);
return query.mentor_info_by_pk ?? null;
}


export const get_mentor_applications_count = async (uuid: string, year: number) => {
const query: any = await client.request(
gql`
query GetMentorApplicationsCount($uuid: uuid!, $year: Int!) {
mentor_application_aggregate(
where: { _and: { mentor_uuid: { _eq: $uuid }, year: { _eq: $year } } }
) {
aggregate {
count
}
}
}
`,
{ uuid: uuid, year: year }
);
return query.mentor_application_aggregate?.aggregate?.count ?? 0;
}


export const get_mentor_applications_approved_count = async (uuid: string, year: number) => {
const query: any = await client.request(
gql`
query GetMentorApplicationsApprovedCount($uuid: uuid!, $year: Int!) {
mentor_application_aggregate(
where: {
_and: {
mentor_uuid: { _eq: $uuid }
_and: { year: { _eq: $year }, status: { _eq: "approved" } }
}
}
) {
aggregate {
count
}
}
}
`,
{ uuid: uuid, year: year }
);
return query.mentor_application_aggregate?.aggregate?.count ?? 0;
}

export const insert_mentor_application = async (mentor_uuid: string, student_uuid: string, year: number, statement: string) => {
const query: any = await client.request(
gql`
mutation InsertMentorApplication(
$mentor_uuid: uuid!
$student_uuid: uuid!
$year: Int!
$statement: String = ""
) {
insert_mentor_application_one(
object: {
statement: $statement
mentor_uuid: $mentor_uuid
student_uuid: $student_uuid
year: $year
}
) {
id
}
}
`,
{
mentor_uuid: mentor_uuid,
student_uuid: student_uuid,
year: year,
statement: statement
}
);
return query.insert_mentor_application_one?.id ?? null;
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import http from "http";
import app from "./app";
import { GraphQLClient } from "graphql-request";
import { queue_element } from "./helpers/docker_queue";
import docker_cron from "./helpers/docker_queue";
// import docker_cron from "./helpers/docker_queue";

dotenv.config();
const debug = Debug("eesast-api");
Expand Down Expand Up @@ -49,7 +49,7 @@ export const docker_queue: queue_element[] = [];
// export const docker_queue: queue_element[] = JSON.parse(
// fs.readFileSync("/data/queue_data.json").toString()
// );
docker_cron();
// docker_cron();

// weekly_cron();
// weekly_init();
Expand Down
113 changes: 85 additions & 28 deletions src/routes/application.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from "express";
import { gql } from "graphql-request";
import { client } from "..";
import * as MentHasFunc from "../hasura/mentor";

const router = express.Router();

Expand Down Expand Up @@ -43,39 +44,95 @@ router.get("/info/honor", async (req, res) => {
}
})

/* 查询当年的新生导师申请时间段
* @return {time: {start_A, end_A, ..., start_E, end_E}}
*/
router.get("/info/mentor", async (req, res) => {
router.get("/info/mentor/:year", async (req, res) => {
try {
const year = new Date().getFullYear();
const q_mentor_time: any = await client.request(
gql`
query MyQuery($activateIn: Int!){
mentor_time_by_pk(activateIn: $activateIn) {
start_A
end_A
start_B
end_B
start_C
end_C
start_D
end_D
start_E
end_E
}
const year: number = parseInt(req.params.year, 10);
if (isNaN(year)) {
return res.status(450).send("Error: Invalid year provided");
}
const mentor_info = await MentHasFunc.get_mentor_info_list();
if (!mentor_info) {
return res.status(451).send("Error: No mentor info found");
}
const info = await Promise.all(
mentor_info.map(async (mentor: any) => {
const applicationsCount = await MentHasFunc.get_mentor_applications_count(mentor.mentor_uuid, year);
const applicationsApprovedCount = await MentHasFunc.get_mentor_applications_approved_count(mentor.mentor_uuid, year);
return {
...mentor,
total_applicants: applicationsCount,
matched_applicants: applicationsApprovedCount
}
`,
{
activateIn: year
}
)
if (!q_mentor_time?.mentor_time_by_pk) {
return res.status(500).send("Error: No mentor time found");
}
return res.status(200).send({time: q_mentor_time.mentor_time_by_pk});
));
return res.status(200).send(info);
} catch (err) {
return res.status(500).send(err);
}
})

// const insert_mentor_application = async (mentor_uuid: string, student_uuid: string, year: number, statement: string) => {

router.post("/mentor/insert_one", async (req, res) => {
const mentor_uuid: string = req.body.mentor_uuid;
const student_uuid: string = req.body.student_uuid;
if (!mentor_uuid || !student_uuid) {
return res.status(450).send("Error: Invalid mentor_uuid or student_uuid provided");
}
const year: number = parseInt(req.body.year, 10);
if (isNaN(year)) {
return res.status(451).send("Error: Invalid year provided");
}
const statement: string = req.body.statement ?? "";

const mentor_info = await MentHasFunc.get_mentor_info(mentor_uuid);
if (!mentor_info) {
return res.status(452).send("Error: No mentor info found");
}
const mentor_applications_count = await MentHasFunc.get_mentor_applications_count(mentor_uuid, year);
if (mentor_applications_count >= mentor_info.max_applicants) {
return res.status(453).send("Error: Exceeds max_applicants");
}
const insert_id = await MentHasFunc.insert_mentor_application(mentor_uuid, student_uuid, year, statement);
if (!insert_id) {
return res.status(454).send("Error: Insert mentor application failed");
}
return res.status(200).send(insert_id);
})

// /* 查询当年的新生导师申请时间段
// * @return {time: {start_A, end_A, ..., start_E, end_E}}
// */
// router.get("/info/mentor", async (req, res) => {
// try {
// const year = new Date().getFullYear();
// const q_mentor_time: any = await client.request(
// gql`
// query MyQuery($activateIn: Int!){
// mentor_time_by_pk(activateIn: $activateIn) {
// start_A
// end_A
// start_B
// end_B
// start_C
// end_C
// start_D
// end_D
// start_E
// end_E
// }
// }
// `,
// {
// activateIn: year
// }
// )
// if (!q_mentor_time?.mentor_time_by_pk) {
// return res.status(500).send("Error: No mentor time found");
// }
// return res.status(200).send({time: q_mentor_time.mentor_time_by_pk});
// } catch (err) {
// return res.status(500).send(err);
// }
// })
export default router;

0 comments on commit 6f40b2b

Please sign in to comment.