Spaces:
Sleeping

gymco / src /modules /attendance /attendance.controller.js
moustafa7's picture
fix: Gap analysis — tenant isolation, memory leak, pagination
8eee19e
Raw History Blame Contribute Delete
4.48 kB
import { asyncHandler } from '../../common/middleware/asyncHandler.js';
import { ApiError } from '../../common/errors/ApiError.js';
import { getPagination, paginated } from '../../common/utils/pagination.js';
import { created, ok } from '../../common/utils/response.js';
import { attendanceRepository } from './attendance.repository.js';
import { attendanceService } from './attendance.service.js';
import crypto from 'node:crypto';
import QRCode from 'qrcode';
export const attendanceController = {
list: asyncHandler(async (req, res) => {
const pagination = getPagination(req.query);
const [items, total] = await attendanceRepository.list({ gymId: req.gymId, ...req.query, ...pagination });
ok(res, paginated(items, total, pagination.page, pagination.limit));
}),
myAttendance: asyncHandler(async (req, res) => {
if (!req.user.memberId) throw ApiError.forbidden('Member profile is not linked to this account');
const pagination = getPagination(req.query);
const result = await attendanceRepository.listForMember(req.gymId, req.user.memberId, { skip: pagination.skip, take: pagination.take });
if (Array.isArray(result)) return ok(res, result);
const [items, total] = result;
ok(res, paginated(items, total, pagination.page, pagination.limit));
}),
coachAttendance: asyncHandler(async (req, res) => {
const pagination = getPagination(req.query);
const result = await attendanceRepository.listForCoach(req.gymId, req.user.sub, { skip: pagination.skip, take: pagination.take });
if (Array.isArray(result)) return ok(res, result);
const [items, total] = result;
ok(res, paginated(items, total, pagination.page, pagination.limit));
}),
coachWorkAttendance: asyncHandler(async (req, res) => {
const coachId = req.user.role === 'coach' ? req.user.sub : req.query.coachId;
ok(res, await attendanceRepository.listCoachAttendance(req.gymId, coachId));
}),
coachQr: asyncHandler(async (req, res) => {
const coachId = req.user.role === 'coach' ? req.user.sub : req.params.id;
const coach = await attendanceRepository.findCoach(req.gymId, coachId);
if (!coach) throw ApiError.notFound('Coach not found');
const qrToken = coach.qrToken || crypto.randomUUID();
const updated = await attendanceRepository.updateCoachQr(req.gymId, coachId, qrToken);
const qrImage = await QRCode.toDataURL(JSON.stringify({ gymId: req.gymId, coachId, qrToken, type: 'coach' }));
ok(res, { coach: updated, qrToken, qrImage }, 'Coach QR generated');
}),
qrCheckIn: asyncHandler(async (req, res) => {
created(res, await attendanceService.qrCheckIn(req.gymId, req.body.qrToken), 'QR check-in processed');
}),
qrScan: asyncHandler(async (req, res) => {
created(res, await attendanceService.scan({ gymId: req.gymId, userId: req.user.sub, identifier: req.body.qrToken, source: 'qr' }), 'QR scan processed');
}),
barcodeScan: asyncHandler(async (req, res) => {
created(res, await attendanceService.scan({ gymId: req.gymId, userId: req.user.sub, identifier: req.body.barcodeId, source: 'barcode' }), 'Barcode scan processed');
}),
scanPreview: asyncHandler(async (req, res) => {
const member = await attendanceRepository.findMemberByScanIdentifier(req.gymId, req.params.identifier);
if (!member) throw ApiError.notFound('Member not found');
const activeSubscription = member.subscriptions?.find((item) => item.status === 'active' && new Date(item.endDate) > new Date()) || null;
ok(res, { member, activeSubscription, lastAttendance: member.attendance?.[0] || null });
}),
coachQrCheckIn: asyncHandler(async (req, res) => {
created(res, await attendanceService.coachQrCheckIn(req.gymId, req.body.qrToken), 'Coach QR check-in processed');
}),
markCoachAbsent: asyncHandler(async (req, res) => {
ok(res, await attendanceService.markCoachAbsent(req.gymId, req.params.id, req.body), 'Coach absence recorded with salary deduction');
}),
manualCheckIn: asyncHandler(async (req, res) => {
created(res, await attendanceService.manualCheckIn(req.gymId, req.body), 'Manual check-in recorded');
}),
manualToggle: asyncHandler(async (req, res) => {
created(res, await attendanceService.manualToggle(req.gymId, req.body), 'Manual attendance processed');
}),
checkOut: asyncHandler(async (req, res) => {
ok(res, await attendanceRepository.checkOut(req.gymId, req.params.id, req.body?.checkOut), 'Check-out recorded');
}),
};