Repository navigation
Expand file tree
/
Copy pathattendanceDB.js
More file actions
88 lines (77 loc) · 2.34 KB
/
Copy pathattendanceDB.js
File metadata and controls
88 lines (77 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class AttendanceDB {
constructor() {
this.attendance = [];
this.nextId = 1;
}
async save(classId, date, entries) {
const dateStr = new Date(date).toDateString();
const existingIndex = this.attendance.findIndex(
(record) => record.classId === classId && record.date === dateStr
);
const attendanceRecord = {
id:
existingIndex >= 0 ? this.attendance[existingIndex].id : this.nextId++,
classId,
date: dateStr,
entries,
createdAt:
existingIndex >= 0
? this.attendance[existingIndex].createdAt
: new Date(),
updatedAt: new Date(),
};
if (existingIndex >= 0) {
this.attendance[existingIndex] = attendanceRecord;
return { message: 'Attendance updated', attendance: attendanceRecord };
} else {
this.attendance.push(attendanceRecord);
return { message: 'Attendance created', attendance: attendanceRecord };
}
}
// Find attendance by class and date
async findByClassAndDate(classId, date) {
const dateStr = new Date(date).toDateString();
return this.attendance.find(
(record) => record.classId === classId && record.date === dateStr
);
}
// Find all attendance for a student
async findByStudent(studentId) {
const studentRecords = [];
this.attendance.forEach((record) => {
const studentEntry = record.entries.find(
(entry) => entry.studentId === studentId
);
if (studentEntry) {
studentRecords.push({
classId: record.classId,
date: record.date,
status: studentEntry.status,
recordId: record.id,
});
}
});
return studentRecords;
}
// Get all attendance records
async findAll() {
return this.attendance;
}
// Get attendance summary for a class
async getClassSummary(classId) {
const classRecords = this.attendance.filter(
(record) => record.classId === classId
);
return classRecords.map((record) => ({
date: record.date,
totalStudents: record.entries.length,
presentCount: record.entries.filter((entry) => entry.status === 'present')
.length,
absentCount: record.entries.filter((entry) => entry.status === 'absent')
.length,
entries: record.entries,
}));
}
}
const attendanceDB = new AttendanceDB();
export default attendanceDB;