Skip to content

Commit 3815c82

Browse files
committed
refactor(ucdays): improve method names and file structure
1 parent a278ae7 commit 3815c82

File tree

11 files changed

+432
-375
lines changed

11 files changed

+432
-375
lines changed

src/api/login.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { jwtVerify } from "jose";
77
import response from "../utils/response";
88
import Strings from "../config/strings";
99
import Student from "../db/models/student";
10-
import UnivStudent from "../db/models/univ_events/student";
10+
import TatakFormStudent from "../db/models/tatakform/student";
1111
import Log from "../utils/log";
1212

1313
/**
@@ -67,7 +67,7 @@ async function getLogin(context: ElysiaContext) {
6767
const role = payload.role as AuthType;
6868
let student;
6969
if(role === AuthType.UNIV_ACCOUNT || role === AuthType.COLLEGE_ADMIN){
70-
student = await UnivStudent.getByStudentId(payload.student_id as string);
70+
student = await TatakFormStudent.getByStudentId(payload.student_id as string);
7171
}else{
7272
// If token is valid, get student
7373
student = await Student.getByStudentId(payload.student_id as string);

src/api/univ_events/admin.ts renamed to src/api/tatakforms/admin.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import type { ElysiaContext, ResponseBody } from "../../types";
22
import { createSessionToken } from "../../utils/security";
33
import { status501 } from "../../routes";
4-
import { AuthType, ErrorTypes } from "../../types/enums";
5-
import { jwtVerify } from "jose";
4+
import { AuthType } from "../../types/enums";
65
import response from "../../utils/response";
76
import Strings from "../../config/strings";
87

9-
import Student from "../../db/models/univ_events/student";
108
import Log from "../../utils/log";
11-
import UnivAdmin from "../../db/models/univ_events/admin";
9+
import TatakFormAdmin from "../../db/models/tatakform/admin";
1210

1311
/**
14-
* UC Days Login API
12+
* Tatakforms Admin Login API
1513
* @author TotalElderBerry (huhu)
1614
*/
1715
export function login(context: ElysiaContext): Promise<ResponseBody | undefined> | ResponseBody {
@@ -27,17 +25,15 @@ export function login(context: ElysiaContext): Promise<ResponseBody | undefined>
2725

2826

2927
/**
30-
* POST /ucdays2024/login
28+
* POST /tatakforms/admin/login
3129
* @param context Elysia context
3230
*/
3331
async function postLogin(context: ElysiaContext) {
3432
// Get request data
3533
let { username, password } = context.body || {};
36-
const student_id = username;
37-
// If type isnt specified
38-
34+
3935
// If student_id is not specified
40-
if (!student_id) {
36+
if (!username) {
4137
context.set.status = 400;
4238
return response.error("Username is required");
4339
}
@@ -49,26 +45,26 @@ async function postLogin(context: ElysiaContext) {
4945
}
5046

5147
try {
52-
const student = await UnivAdmin.getByUsernameAndPassword(student_id.trim(),password);
53-
48+
// Get admin
49+
const admin = await TatakFormAdmin.getByUsernameAndPassword(username.trim(), password);
5450
// Data to be stored in the token
55-
const data = { role: AuthType.COLLEGE_ADMIN, ...student };
51+
const data = { role: AuthType.COLLEGE_ADMIN, ...admin };
5652
// Create access token (1 day)
5753
const accessToken = await createSessionToken(false, data, "1d");
5854
// Create refresh token (15 days)
5955
const refreshToken = await createSessionToken(true, data, "15d");
6056

6157
// Log the login
6258
Log.login({
63-
student_id: student.student_id,
59+
student_id: admin.student_id,
6460
type: data.role,
65-
name: `${student.first_name} ${student.last_name}`,
66-
students_id: student.id,
61+
name: `${admin.first_name} ${admin.last_name}`,
62+
students_id: admin.id,
6763
});
6864

6965
// Remove password from user
7066
delete username.password;
71-
67+
// Return success
7268
return response.success(Strings.LOGIN_SUCCESS, { data, accessToken, refreshToken });
7369
}
7470

src/api/tatakforms/attendance.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import type { ElysiaContext, ResponseBody } from "../../types";
2+
import { ErrorTypes } from "../../types/enums";
3+
import { status501 } from "../../routes";
4+
5+
import TatakFormStudent from "../../db/models/tatakform/student";
6+
import response from "../../utils/response";
7+
import Strings from "../../config/strings";
8+
import Attendance from "../../db/models/tatakform/attendance";
9+
import Tatakform from "../../db/models/tatakform";
10+
11+
/**
12+
* Tatakform Attendance API
13+
* @author TotalElderBerry (Unknown af)
14+
* @param context
15+
*/
16+
export function attendance(context: ElysiaContext): Promise<ResponseBody | undefined> | ResponseBody {
17+
switch (context.request.method) {
18+
case "GET":
19+
if (context.params?.eventId) {
20+
return getAllStudentsAttended(context);
21+
}
22+
23+
return getAttendance(context);
24+
case "POST":
25+
return postAttendance(context);
26+
case "OPTIONS":
27+
return response.success();
28+
}
29+
30+
return status501(context);
31+
}
32+
33+
/**
34+
* POST /tatakforms/attendance/:slug
35+
* Mark student as present in specific day
36+
*/
37+
async function postAttendance(context: ElysiaContext) {
38+
try {
39+
const slug = context.params?.slug;
40+
if (slug) {
41+
const tatak_event = await Tatakform.getBySlug(slug);
42+
const student = await TatakFormStudent.getByStudentId(context.body.student_id);
43+
44+
if (tatak_event) {
45+
await Attendance.attendStudent(context.body.student_id, tatak_event);
46+
}
47+
48+
return response.success("Attended successfully", { student_id: context.body.student_id });
49+
}
50+
} catch (error) {
51+
// if list of errors
52+
if (Array.isArray(error)) {
53+
context.set.status = 400;
54+
return response.error(error[0], error[1]);
55+
}
56+
57+
// If database error
58+
if (error === ErrorTypes.DB_ERROR) {
59+
context.set.status = 500;
60+
return response.error(Strings.STUDENT_POST_ERROR);
61+
}
62+
63+
return response.error(error)
64+
}
65+
}
66+
67+
/**
68+
* GET /tatakforms/attendance/
69+
* Fetches the attedance history of a student in all events
70+
*
71+
* GET /tatakforms/attendance/:slug
72+
* Fetches the attedance history of a student in an event
73+
* @param context
74+
*
75+
*/
76+
async function getAttendance(context: ElysiaContext) {
77+
try {
78+
// Get slug
79+
const slug = context.params?.slug;
80+
81+
// If slug is specified
82+
if (slug) {
83+
// Get event by slug
84+
const tatakform = await Tatakform.getBySlug(slug);
85+
// Get attendance history by event
86+
const attendance = await Attendance.getAttendanceByEvent(context.user?.student_id, tatakform.id);
87+
88+
// Return response
89+
return response.success("Fetch Successful", attendance);
90+
}
91+
92+
// Otherwise, get all attendance history
93+
const history = await Attendance.getAttendance(context.user?.student_id);
94+
return response.success("Fetch Successful", history);
95+
}
96+
97+
catch (error) {
98+
// if list of errors
99+
if (Array.isArray(error)) {
100+
context.set.status = 400;
101+
return response.error(error[0], error[1]);
102+
}
103+
104+
// If database error
105+
if (error === ErrorTypes.DB_ERROR) {
106+
context.set.status = 500;
107+
return response.error("Fetching Attendance Error");
108+
}
109+
110+
return response.error(error)
111+
}
112+
}
113+
114+
/**
115+
* GET /tatakforms/attendance/event/:eventId
116+
* Get all students attendance by event and college
117+
*/
118+
async function getAllStudentsAttended(context: ElysiaContext) {
119+
try {
120+
// Get event id
121+
const eventId = context.params?.eventId;
122+
123+
// If event id is specified
124+
if (eventId) {
125+
// Get all students attended by event and college
126+
const attendance = await Attendance.getStudentsAttendedByEventAndCollege(eventId, context.user.college_id);
127+
return response.success("Success", attendance)
128+
}
129+
}
130+
131+
catch (error) {
132+
return response.error(error)
133+
}
134+
}

src/api/univ_events/login.ts renamed to src/api/tatakforms/login.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { ElysiaContext, ResponseBody } from "../../types";
22
import { createSessionToken } from "../../utils/security";
33
import { status501 } from "../../routes";
4-
import { AuthType, ErrorTypes } from "../../types/enums";
5-
import { jwtVerify } from "jose";
4+
import { AuthType } from "../../types/enums";
65
import response from "../../utils/response";
76
import Strings from "../../config/strings";
87

9-
import Student from "../../db/models/univ_events/student";
8+
import TatakFormStudent from "../../db/models/tatakform/student";
109
import Log from "../../utils/log";
1110

1211
/**
@@ -26,23 +25,17 @@ export function login(context: ElysiaContext): Promise<ResponseBody | undefined>
2625

2726

2827
/**
29-
* POST /ucdays2024/login
28+
* POST /tatakforms/login
3029
* @param context Elysia context
3130
*/
3231
async function postLogin(context: ElysiaContext) {
3332
// Get request data
34-
let { username, password } = context.body || {};
35-
const student_id = username;
36-
// If type isnt specified
37-
// if (!type) {
38-
// context.set.status = 400;
39-
// return response.error("Type is required");
40-
// }
33+
let { student_id, password } = context.body || {};
4134

4235
// If student_id is not specified
4336
if (!student_id) {
4437
context.set.status = 400;
45-
return response.error("Username is required");
38+
return response.error("Student ID is required");
4639
}
4740

4841
// If password is not specified
@@ -52,7 +45,8 @@ async function postLogin(context: ElysiaContext) {
5245
}
5346

5447
try {
55-
const student = await Student.getByStudentId(student_id.trim());
48+
// Get student
49+
const student = await TatakFormStudent.getByStudentId(student_id.trim());
5650

5751
// Compare password
5852
if (!(await Bun.password.verify(password, student.password || ""))) {
@@ -76,8 +70,8 @@ async function postLogin(context: ElysiaContext) {
7670
});
7771

7872
// Remove password from user
79-
delete username.password;
80-
73+
delete data.password;
74+
// Return success
8175
return response.success(Strings.LOGIN_SUCCESS, { data, accessToken, refreshToken });
8276
}
8377

src/api/univ_events/students.ts renamed to src/api/tatakforms/students.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ElysiaContext, ResponseBody } from "../../types";
22
import { ErrorTypes } from "../../types/enums";
33
import { status501 } from "../../routes";
44

5-
import UnivStudent from "../../db/models/univ_events/student";
5+
import TatakFormStudent from "../../db/models/tatakform/student";
66
import response from "../../utils/response";
77
import Strings from "../../config/strings";
88
import Log from "../../utils/log";
@@ -29,7 +29,7 @@ export function students(context: ElysiaContext): Promise<ResponseBody | undefin
2929
async function postStudents(context: ElysiaContext) {
3030
try {
3131
// Insert student
32-
await UnivStudent.insert(context.body);
32+
await TatakFormStudent.insert(context.body);
3333
// If no error, student is created
3434
return response.success("You have successfully registered! 💛");
3535
}

0 commit comments

Comments
 (0)