From 55168ce6ae46491a71d1540af489ea54dede8a4b Mon Sep 17 00:00:00 2001 From: zzdhyb <139191187+zzdhybthu@users.noreply.github.com> Date: Wed, 11 Sep 2024 15:12:37 +0800 Subject: [PATCH] feat: update honor application (#1431) * feat: update honor application * chore --- src/hasura/honor.ts | 176 ++++++++++++++++++++++++++++++++++++++ src/routes/application.ts | 139 +++++++++++++++++++++++++++++- 2 files changed, 312 insertions(+), 3 deletions(-) create mode 100644 src/hasura/honor.ts diff --git a/src/hasura/honor.ts b/src/hasura/honor.ts new file mode 100644 index 00000000..2c74fddb --- /dev/null +++ b/src/hasura/honor.ts @@ -0,0 +1,176 @@ +import { gql } from "graphql-request"; +import { client } from ".."; + +export const query_user_role = async (uuid: string) => { + const query: any = await client.request( + gql` + query QueryUserRole($uuid: uuid!) { + users_by_pk(uuid: $uuid) { + role + } + } + `, + { uuid: uuid } + ); + return query.users_by_pk?.role ?? "anonymous"; +} + +export const query_honor_application = async (id: string) => { + const query: any = await client.request( + gql` + query QueryHonorApplication($id: uuid!) { + honor_application_by_pk(id: $id) { + id + student_uuid + honor + statement + attachment_url + year + status + } + } + `, + { id: id } + ); + return query.honor_application_by_pk ?? null; +} + +export const insert_honor_application = async ( + student_uuid: string, + honor: string, + statement: string, + attachment_url: string | undefined, + year: number +) => { + const query: any = await client.request( + gql` + mutation InsertHonorApplication( + $student_uuid: uuid! + $honor: String! + $statement: String! + $attachment_url: String + $year: Int! + ) { + insert_honor_application_one( + object: { + student_uuid: $student_uuid + honor: $honor + statement: $statement + attachment_url: $attachment_url + year: $year + } + ) { + id + } + } + `, + { + student_uuid: student_uuid, + honor: honor, + statement: statement, + attachment_url: attachment_url, + year: year + } + ); + return query.insert_honor_application_one?.id ?? null; +} + +export const update_honor_application_with_attachment = async ( + id: string, + honor: string, + statement: string, + attachment_url: string, +) => { + const query: any = await client.request( + gql` + mutation UpdateMentorApplication( + $id: uuid! + $honor: String! + $statement: String! + $attachment_url: String! + ) { + update_honor_application_by_pk( + pk_columns: {id: $id} + _set: { + honor: $honor + statement: $statement + attachment_url: $attachment_url + } + ) { + id + } + } + `, + { + id: id, + honor: honor, + statement: statement, + attachment_url: attachment_url + } + ); + return query.update_honor_application_by_pk?.id ?? null; +} + + +export const update_honor_application = async ( + id: string, + honor: string, + statement: string, +) => { + const query: any = await client.request( + gql` + mutation UpdateMentorApplication( + $id: uuid! + $honor: String! + $statement: String! + ) { + update_honor_application_by_pk( + pk_columns: {id: $id} + _set: { + honor: $honor + statement: $statement + } + ) { + id + } + } + `, + { + id: id, + honor: honor, + statement: statement, + } + ); + return query.update_honor_application_by_pk?.id ?? null; +} + +export const delete_honor_application = async (id: string) => { + const query: any = await client.request( + gql` + mutation DeleteMentorApplication($id: uuid!) { + delete_honor_application_by_pk(id: $id) { + id + } + } + `, + { id: id } + ); + return query.delete_honor_application_by_pk?.id ?? null; +} + +export const update_honor_application_status = async (id: string, status: string) => { + const query: any = await client.request( + gql` + mutation UpdateMentorApplicationStatus($id: uuid!, $status: String!) { + update_honor_application_by_pk( + pk_columns: {id: $id} + _set: { status: $status } + ) { + id + } + } + `, + { id: id, status: status } + ); + return query.update_honor_application_by_pk?.id ?? null; +} diff --git a/src/routes/application.ts b/src/routes/application.ts index af2bce9e..aa9dce54 100644 --- a/src/routes/application.ts +++ b/src/routes/application.ts @@ -2,6 +2,7 @@ import express from "express"; import { gql } from "graphql-request"; import { client } from ".."; import * as MentHasFunc from "../hasura/mentor"; +import * as HnrHasFunc from "../hasura/honor"; const router = express.Router(); @@ -38,12 +39,145 @@ router.get("/info/honor", async (req, res) => { ` ) const types: string[] = q_honor_type.honor_type.map((item: { type_name: string }) => item.type_name); - return res.status(200).send({types: types, time: q_honor_time.honor_time_by_pk}); + return res.status(200).send({ types: types, time: q_honor_time.honor_time_by_pk }); } catch (err) { return res.status(500).send(err); } }) +router.post("/honor/insert_one", async (req, res) => { + try { + const student_uuid: string = req.body.student_uuid; + const honor: string = req.body.honor; + const statement: string = req.body.statement ?? ""; + const attachment_url: string = req.body.attachment_url ?? undefined; + + if (!student_uuid || !honor) { + return res.status(450).send("Error: Missing student_uuid or honor"); + } + + const role = await HnrHasFunc.query_user_role(student_uuid); + if (role !== "student" && role !== "counselor") { + return res.status(451).send("Error: Invalid role"); + } + + const year: number = new Date().getFullYear(); + const insert_id = await HnrHasFunc.insert_honor_application(student_uuid, honor, statement, attachment_url, year); + if (!insert_id) { + return res.status(452).send("Error: Insert honor application failed"); + } + return res.status(200).send(insert_id); + } catch (err) { + console.log(err); + return res.status(500).send(err); + } +}) + +router.post("/honor/update_one", async (req, res) => { + try { + const id: string = req.body.id; + const honor: string = req.body.honor; + const statement: string = req.body.statement ?? ""; + const attachment_url: string = req.body.attachment_url ?? undefined; + const student_uuid: string = req.body.student_uuid; + + if (!id || !honor || !student_uuid) { + return res.status(450).send("Error: Missing id or honor or student_uuid"); + } + + const application = await HnrHasFunc.query_honor_application(id); + const role = await HnrHasFunc.query_user_role(student_uuid); + if (!application) { + return res.status(451).send("Error: No honor application found"); + } else if (application.student_uuid !== student_uuid && role !== "counselor") { + return res.status(452).send("Error: Invalid student_uuid"); + } + + const year: number = new Date().getFullYear(); + if (application.year !== year) { + return res.status(453).send("Error: Invalid year"); + } + + if (!attachment_url) { + const response = await HnrHasFunc.update_honor_application(id, honor, statement); + if (!response) { + return res.status(454).send("Error: Update honor application failed"); + } + } else { + const response = await HnrHasFunc.update_honor_application_with_attachment(id, honor, statement, attachment_url); + if (!response) { + return res.status(454).send("Error: Update honor application failed"); + } + } + return res.status(200).send(id); + } catch (err) { + console.log(err); + return res.status(500).send(err); + } +}) + +router.post("/honor/delete_one", async (req, res) => { + try { + const id: string = req.body.id; + const student_uuid: string = req.body.student_uuid; + + if (!id || !student_uuid) { + return res.status(450).send("Error: Missing id or student_uuid"); + } + + const application = await HnrHasFunc.query_honor_application(id); + const role = await HnrHasFunc.query_user_role(student_uuid); + if (!application) { + return res.status(451).send("Error: No honor application found"); + } else if (application.student_uuid !== student_uuid && role !== "counselor") { + return res.status(452).send("Error: Invalid student_uuid"); + } + + const year: number = new Date().getFullYear(); + if (application.year !== year) { + return res.status(453).send("Error: Invalid year"); + } + + const response = await HnrHasFunc.delete_honor_application(id); + if (!response) { + return res.status(454).send("Error: Delete honor application failed"); + } + return res.status(200).send(id); + } catch (err) { + return res.status(500).send(err); + } +}) + +router.post("/honor/update_status_one", async (req, res) => { + try { + const id: string = req.body.id; + const status: string = req.body.status; + const counselor_uuid: string = req.body.counselor_uuid; + + if (!id || !status || !counselor_uuid) { + return res.status(450).send("Error: Missing id or status or counselor_uuid"); + } + + const application = await HnrHasFunc.query_honor_application(id); + const role = await HnrHasFunc.query_user_role(counselor_uuid); + if (!application) { + return res.status(451).send("Error: No honor application found"); + } else if (role !== "counselor") { + return res.status(452).send("Error: Invalid counselor_uuid"); + } + + const response = await HnrHasFunc.update_honor_application_status(id, status); + if (!response) { + return res.status(453).send("Error: Update honor application status failed"); + } + return res.status(200).send(id); + } catch (err) { + return res.status(500).send(err); + } +}) + + + router.get("/info/mentor/:year", async (req, res) => { try { const year: number = parseInt(req.params.year, 10); @@ -64,14 +198,13 @@ router.get("/info/mentor/:year", async (req, res) => { matched_applicants: applicationsApprovedCount } } - )); + )); 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;