I'm a junior full-stack developer from Alexandria, Egypt. This is another post about FaceMark, my face-recognition attendance system (a NestJS API plus a Python AI service). This one is about the method that writes an attendance record, and why almost none of it is the writing.

The insert is the last step

The gauntlet

When a staff member records attendance, my create() method runs five checks before it touches the database. Here they are, straight from the service:

// 1. Verify Student exists and is actually a student const student = await this.userRepo.findById(dto.studentId); if (!student || student.userType !== Role.STUDENT) { throw new NotFoundException(`Student Validation Failed: Student with ID ${dto.studentId} not found or is not registered as a student.`); } // 2. Verify Course exists const course = await this.courseRepo.findOne({ where: { courseId: dto.courseId, isDeleted: false }, relations: ['enrollments', 'instructor'] }); if (!course) { throw new NotFoundException(`Course Validation Failed: Course with ID ${dto.courseId} was not found in our active records.`); } // 3. Permission check: Staff can only record attendance for their own courses if (user.userType === Role.STAFF && course.instructor?.userAccountId !== user.userAccountId) { throw new ForbiddenException(`Access Denied: As a Staff member, you can only record attendance for your own assigned courses.`); } // 4. Verify Student is enrolled in Course const isEnrolled = course.enrollments?.some(e => e.studentId === student.userAccountId); if (!isEnrolled) { throw new ForbiddenException(`Access Denied: Student ${student.username} is not enrolled in course ${course.name}.`); } // 5. Determine and verify staffId let effectiveStaffId: number; if (user.userType === Role.STAFF) { effectiveStaffId = user.userAccountId; } else { const staff = await this.userRepo.findById(dto.staffId); if (!staff || staff.userType !== Role.STAFF) { throw new NotFoundException(`Verification Failed: Staff member with ID ${dto.staffId} was not found or does not have a Staff role.`); } effectiveStaffId = dto.staffId; } 

Each check is a different kind of no. Student isn't real? NotFoundException. Staff recording for someone else's course? ForbiddenException. That split isn't an accident: my exception filter (post two in this series) turns each type into a different HTTP status, so the frontend can tell "this doesn't exist" apart from "you can't touch this."

Check 3 is the one I care about most. My first post was about guards that check your role. Guards can't know whether this course belongs to you, though. Ownership checks live in the service, where the entities are loaded. Role at the door, ownership at the desk.

Check 5 exists because admins record attendance on behalf of staff. A staff member gets their own ID stamped; an admin has to name a real staff member, and the method verifies that person is actually staff.

The write is small compared to the checks

After all that, the actual write is one call:

const result = await this.attendanceRepo.create({ recordDate: this.normalizeDate(dto.recordDate), student: student, course: course, staffId: effectiveStaffId, attendanceStatusId: dto.attendanceStatusId, room: dto.room, sessionType: dto.sessionType, sessionNumber: dto.sessionNumber, lectureNumber: dto.lectureNumber, faceConfidence: dto.faceConfidence, detected: dto.detected !== undefined ? dto.detected : true, accuracy: dto.accuracy, processingTime: dto.processingTime, recognitionRate: dto.recognitionRate, checkInTime: dto.checkInTime, }); 

The fields tell you what this system actually is: faceConfidence, detected, processingTime, recognitionRate. Every record keeps the numbers the recognition pipeline produced, so a bad scan can be audited later instead of argued about.

Alerts are fire-and-forget on purpose

The last block runs after the save:

if (dto.attendanceStatusId === 2) { this.triggerAbsenceAlert(student.userAccountId, course.courseId, dateStr).catch(() => {}); } if (dto.attendanceStatusId !== 1) { // Only check if not Present to optimize this.alertService.checkStudentLowAttendance(student.userAccountId, course.courseId).catch(err => { console.error(`[Alert Error] Failed to check low attendance for student ${student.userAccountId}:`, err); }); } 

Notice there's no await. Attendance is the important write; alerts are nice-to-have. If the alert service is slow or down, the attendance record still lands and the staff member still gets their response. The empty .catch(() => {}) on the absence alert says the quiet part out loud: if the alert fails, we swallow it.

Is that the right call? For a school project, yes. A parent notification can be retried or sent late; a blocked attendance write means a line of students waiting at the door. In a bigger system I'd push these onto a queue so nothing is lost. Here, the tradeoff is documented in the code itself, and that's the part I'm still learning to do well.

What I take from this

  • Validation order is a design decision: cheap existence checks first, permission checks once entities are loaded, the write last.
  • Different failures deserve different exceptions. Your future frontend will thank you.
  • Decide explicitly what can fail silently and what can't. Then write it down in the code, because "we'll add a queue later" ages fast.

Everything above is from the real repo.

I'm a junior full-stack developer (Angular, NestJS, TypeScript, Python) in Alexandria, Egypt, open to junior roles and freelance work. More posts from this project: