Skip to content

Commit b5e4e23

Browse files
committed
fix(tatakforms): can scan student from another department
1 parent 3c9b8ec commit b5e4e23

File tree

3 files changed

+103
-16
lines changed

3 files changed

+103
-16
lines changed

src/db/models/college.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,42 @@ class College {
8787
});
8888
}
8989

90+
/**
91+
* Get college by id
92+
*/
93+
public static getById(id: number): Promise<CollegeModel> {
94+
return new Promise(async (resolve, reject) => {
95+
// Get database instance
96+
const db = Database.getInstance();
97+
98+
try {
99+
// Get college by acronym
100+
const college = await db.query<CollegeModel[]>(`SELECT * FROM colleges WHERE disabled = 0 AND id = ? LIMIT 1`, [id]);
101+
102+
// If no results
103+
if (college.length === 0) {
104+
Log.e(`College with id ${id} not found`);
105+
return reject(`College with id ${id} not found`);
106+
107+
}
108+
109+
// Get all courses
110+
const courses = await db.query<CourseModel[]>(`SELECT * FROM colleges_courses WHERE college_id = ?`, [college[0].id]);
111+
// Set the courses to the college
112+
college[0].courses = courses;
113+
114+
// Return the college
115+
resolve(college[0]);
116+
}
117+
118+
// Log error and reject promise
119+
catch (e) {
120+
Log.e(e);
121+
reject(ErrorTypes.DB_ERROR);
122+
}
123+
});
124+
}
125+
90126
/**
91127
* Get college by acronym
92128
* @param acronym

src/db/models/tatakform/attendance.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { AttendanceModel, TatakformModel } from "../../../types/models";
44
import Database from "../..";
55
import Log from "../../../utils/log";
66
import Tatakform from "./tatakform";
7+
import TatakFormStudent from "./student";
8+
import College from "../college";
79

810
enum Days {
911
DAY1AM = "day1_am",
@@ -31,22 +33,33 @@ class TatakFormAttendance {
3133
*/
3234
public static attendStudent(data: { studentId: string, dateStamp: string, collegeId: number, event: TatakformModel }) {
3335
return new Promise(async (resolve, reject) => {
34-
// Get database instance
35-
const db = Database.getInstance();
36-
// Get column name
37-
const columnName = TatakFormAttendance.getCurrentDay(data.event, data.dateStamp);
38-
39-
// Switch column name
40-
switch(columnName) {
41-
case EventStatus.NOT_ACCEPTING:
42-
return reject("Event is not accepting attendance.");
43-
case EventStatus.NOT_YET_OPEN:
44-
return reject("Event is not yet open.");
45-
case EventStatus.ALREADY_CLOSED:
46-
return reject("Event is already closed.");
47-
}
48-
4936
try {
37+
// Get column name
38+
const columnName = TatakFormAttendance.getCurrentDay(data.event, data.dateStamp);
39+
40+
// Switch column name
41+
switch(columnName) {
42+
case EventStatus.NOT_ACCEPTING:
43+
return reject("Event is not accepting attendance.");
44+
case EventStatus.NOT_YET_OPEN:
45+
return reject("Event is not yet open.");
46+
case EventStatus.ALREADY_CLOSED:
47+
return reject("Event is already closed.");
48+
}
49+
50+
// Get student's college data
51+
const college = await TatakFormStudent.getCollegeFromStudentId(data.studentId);
52+
53+
// If not synced
54+
if (college.id !== data.collegeId) {
55+
// Get college from college id
56+
const c = await College.getById(data.collegeId);
57+
// Reject promise
58+
return reject(`Student is not part of ${c.name} college/department.`);
59+
}
60+
61+
// Get database instance
62+
const db = Database.getInstance();
5063
// Query
5164
let query = "";
5265

src/db/models/tatakform/student.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TatakformStudent, UnivStudentModel } from "../../../types/models";
1+
import type { CollegeModel, TatakformStudent, UnivStudentModel } from "../../../types/models";
22
import type { MariaUpdateResult } from "../../../types";
33

44
import { PaginationOutput } from "../../../types/request";
@@ -14,6 +14,7 @@ import Database from "../..";
1414
import Log from "../../../utils/log";
1515
import Strings from "../../../config/strings";
1616
import Config from "../../../config";
17+
import College from "../college";
1718

1819
/**
1920
* TatakForm Student model
@@ -377,6 +378,43 @@ class TatakFormStudent {
377378
});
378379
}
379380

381+
/**
382+
* Get college from student id
383+
*/
384+
public static getCollegeFromStudentId(student_id: string): Promise<CollegeModel> {
385+
return new Promise(async (resolve, reject) => {
386+
// Get database instance
387+
const db = Database.getInstance();
388+
389+
try {
390+
// Get colleges
391+
const colleges = await College.getAll();
392+
// Get student
393+
const student = await TatakFormStudent.getByStudentId(student_id);
394+
395+
// Find college
396+
for (const college of colleges) {
397+
if (!college.courses) continue;
398+
399+
for (const course of college.courses) {
400+
if (course.id === student.course_id) {
401+
return resolve(college);
402+
}
403+
}
404+
}
405+
406+
// If not found
407+
reject("College not found");
408+
}
409+
410+
// Log error and reject promise
411+
catch (e) {
412+
Log.e(e);
413+
reject(e);
414+
}
415+
});
416+
}
417+
380418
/**
381419
* Check whether a student exists
382420
* @param key Student column

0 commit comments

Comments
 (0)