Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added info routes #1453

Merged
merged 7 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import manageRouter from "./routes/manage";
import arenaRouter from "./routes/arena";
import competitionRouter from "./routes/competition";
import notificationRouter from "./routes/notification";

import chatRoute from "./routes/chat";
import mentorRoute from "./routes/mentor";
import noticeRoute from "./routes/notice";
const app = express();

const whitelist =
Expand Down Expand Up @@ -55,5 +57,7 @@ app.use("/manage", manageRouter);
app.use("/arena", arenaRouter);
app.use("/competition", competitionRouter);
app.use("/notification", notificationRouter);

app.use("/chat", chatRoute);
app.use("/mentor", mentorRoute);
app.use("/notice", noticeRoute);
export default app;
272 changes: 271 additions & 1 deletion src/hasura/mentor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { gql } from "graphql-request";
import { client } from "..";

export interface Freshman_Insert_Input {
realname: string;
student_no: string;
year: number;
uuid: string;
}
export const get_mentor_info_list = async () => {
const query: any = await client.request(
gql`
Expand Down Expand Up @@ -123,3 +128,268 @@ export const insert_mentor_application = async (mentor_uuid: string, student_uui
);
return query.insert_mentor_application_one?.id ?? null;
}
//newly added
export const insert_mentor_info = async (mentor_uuid: string) => {
const query: any = await client.request(
gql`
mutation InsertMentorInfo($mentor_uuid: uuid!) {
insert_mentor_info_one(
object: { mentor_uuid: $mentor_uuid }
on_conflict: { constraint: mentor_info_pkey }
) {
updated_at
}
}
`,
{ mentor_uuid: mentor_uuid }
);
return query.insert_mentor_info_one?.updated_at ?? null;
}

export const update_mentor_info_available = async (uuid: string, available: boolean) => {
const query: any = await client.request(
gql`
mutation UpdateMentorInfoAvailable($uuid: uuid!, $available: Boolean!) {
update_mentor_info_by_pk(
pk_columns: { mentor_uuid: $uuid }
_set: { available: $available }
) {
available
}
}
`,
{ uuid: uuid, available: available }
);
return query.update_mentor_info_by_pk?.available ?? null;
}

export const update_mentor_info_description = async (mentor_uuid: string,updateFields: Partial<{achievement: string, background: string, field: string, intro: string}>) => {
if(Object.keys(updateFields).length === 0) return null;
const setFields: any = {};
if(updateFields.achievement) setFields.achievement = updateFields.achievement;
if(updateFields.background) setFields.background = updateFields.background;
if(updateFields.field) setFields.field = updateFields.field;
if(updateFields.intro) setFields.intro = updateFields.intro;
const variablesString = Object.keys(setFields)
.map(key => `$${key} : String`)
.join('\n');
const setString = Object.keys(setFields)
.map(key => `${key}: $${key}`)
.join('\n');
const mutation = gql`
mutation UpdateMentorInfoDescription($mentor_uuid: uuid!
${variablesString}) {
update_mentor_info_by_pk(pk_columns: {mentor_uuid: $mentor_uuid }
_set: { ${setString} }
) {
mentor_uuid
}
}
`;
console.log(mutation);
const variables: {[key: string]: any} = {
mentor_uuid: mentor_uuid,
achievement: updateFields.achievement,
background: updateFields.background,
field: updateFields.field,
intro: updateFields.intro
};
try {
const response: any = await client.request(mutation, variables);
return response.update_mentor_info_by_pk?.mentor_uuid ?? null;
} catch (error) {
console.error('Error updating mentor info', error);
throw error;
}
}

export const update_mentor_application_status = async (id: string, status: string) => {
const query: any = await client.request(
gql`
mutation UpdateMentorApplicationStatus($id: uuid!, $status: String!) {
update_mentor_application_by_pk(
pk_columns: { id: $id }
_set: { status: $status }
) {
status
}
}
`,
{ id: id, status: status }
);
return query.update_mentor_application_by_pk?.status ?? null;
}

export const update_mentor_application_statement = async (id: string, statement: string) => {
const query: any = await client.request(
gql`
mutation UpdateMentorApplicationStatement($id: uuid!, $statement: String!) {
update_mentor_application_by_pk(
pk_columns: { id: $id }
_set: { statement: $statement }
) {
statement
}
}
`,
{ id: id, statement: statement }
);
return query.update_mentor_application_by_pk?.statement ?? null;
}

export const delete_mentor_application = async (id: string) => {
const query: any = await client.request(
gql`
mutation DeleteMentorApplication($id: uuid!) {
delete_mentor_application_by_pk(id: $id) {
id
}
}
`,
{ id: id }
);
return query.delete_mentor_application_by_pk?.id ?? null;
}
export const update_mentor_application_chat_status = async (id: string, chat_status: boolean) => {
try{
const query: any = await client.request(
gql`
mutation UpdateMentorApplicationChatStatus($id: uuid!, $chat_status: Boolean!) {
update_mentor_application_by_pk(
pk_columns: { id: $id }
_set: { chat_status: $chat_status }
) {
chat_status
}
}
`,
{ id: id, chat_status: chat_status }
);
return query.update_mentor_application_by_pk?.chat_status ?? null;
}
catch(e){
console.log(e);
console.log("error");
}
}
export const insert_mentor_application_schedule =
async (
year: number,
start_A: Date,
start_B: Date,
start_C: Date,
start_D: Date,
start_E: Date,
end_A: Date,
end_B: Date,
end_C: Date,
end_D: Date,
end_E: Date
) => {
console.log(end_E);
const query: any = await client.request(
gql`

mutation InsertMentorApplicationSchedule(
$year: Int!
$start_A: timestamptz!
$start_B: timestamptz!
$start_C: timestamptz!
$start_D: timestamptz!
$start_E: timestamptz!
$end_A: timestamptz!
$end_B: timestamptz!
$end_C: timestamptz!
$end_D: timestamptz!
$end_E: timestamptz!
) {
insert_mentor_time_one(
object: {
activateIn: $year
start_A: $start_A
start_B: $start_B
start_C: $start_C
start_D: $start_D
start_E: $start_E
end_A: $end_A
end_B: $end_B
end_C: $end_C
end_D: $end_D
end_E: $end_E
}
on_conflict: {
constraint: mentor_time_pkey
update_columns: [
start_A
start_B
start_C
start_D
start_E
end_A
end_B
end_C
end_D
end_E
]})
{
activateIn
}
}
`,
{
year: year,
start_A: start_A,
start_B: start_B,
start_C: start_C,
start_D: start_D,
start_E: start_E,
end_A: end_A,
end_B: end_B,
end_C: end_C,
end_D: end_D,
end_E: end_E
}
);
return query.insert_mentor_time_one?.activateIn ?? null;
}

//export const insert_freshman_info_list = async (fresman_data: Array<Freshman_Insert_Input>) => {
// const query: any = await client.request(
// gql`
// mutation InsertFreshmanInfoList($freshmanData: [freshman_insert_input!]!) {
// insert_freshman(
// objects: $freshmanData
// on_conflict: {
// constraint: freshman_pkey
// update_columns: [realname, student_no, year, uuid]
// }
// ) {
// affected_rows
// }
// }
// `,
// {fresmanData: fresman_data }
// );
// return query.insert_freshman_info?.affected_rows ?? 0;
//}
//

export const insert_freshman_info_list = async (fresman_data: Freshman_Insert_Input) => {
const query: any = await client.request(
gql`
mutation InsertFreshmanInfoList($freshmanData: [freshman_insert_input!]!) {
insert_freshman(
objects: $freshmanData
on_conflict: {
constraint: freshman_pkey
update_columns: [realname, student_no, year, uuid]
}
) {
affected_rows
}
}
`,
{freshmanData: fresman_data }
);
return query.insert_freshman?.affected_rows ?? 0;
}
53 changes: 53 additions & 0 deletions src/hasura/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { gql } from "graphql-request";
import { client } from "..";

export const add_message = async (from_uuid: string, to_uuid: string, payload: string) => {
const query: any = await client.request(
gql`
mutation AddMessage($from_uuid: uuid!, $to_uuid: uuid!, $payload: String!) {
insert_mentor_message(
objects: { from_uuid: $from_uuid, to_uuid: $to_uuid, payload: $payload }
) {
returning {
id
}
}
}
`,
{
from_uuid: from_uuid,
to_uuid: to_uuid,
payload: payload
}
);
return query.insert_mentor_message ?? null;
}

export const subsribe_to_messages = async (from_uuid: string, to_uuid: string) => {
const query: any = await client.request(
gql`
subscription SubscribeToMessages($from_uuid: uuid!, $to_uuid: uuid!) {
mentor_message(
order_by: { created_at: asc }
where: {
_or: [
{ _and: { from_uuid: { _eq: $from_uuid }, to_uuid: { _eq: $to_uuid } } }
{ _and: { from_uuid: { _eq: $to_uuid }, to_uuid: { _eq: $from_uuid } } }
]
}
) {
created_at
from_uuid
id
payload
to_uuid
}
}
`,
{
from_uuid: from_uuid,
to_uuid: to_uuid
}
);
return query.mentor_message ?? null;
}
Loading
Loading