- Implemented API endpoints to fetch attendee details and statistics. - Created new route for fetching attendee by ID and their attendance stats. - Added schemas for attendee statistics. - Developed frontend components to display attendee details and attendance statistics. - Integrated attendee detail provider to manage state and data fetching. - Added tests for attendee retrieval and statistics calculation.
127 lines
3.4 KiB
TypeScript
127 lines
3.4 KiB
TypeScript
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
|
import { Hono } from 'hono';
|
|
|
|
const db = {
|
|
group: {
|
|
findUnique: mock(),
|
|
},
|
|
attendee: {
|
|
findFirst: mock(),
|
|
},
|
|
attendance: {
|
|
groupBy: mock(),
|
|
},
|
|
};
|
|
|
|
mock.module('@/lib/prisma', () => ({
|
|
default: db,
|
|
getPrismaClient: mock(),
|
|
UnitOfWork: class {},
|
|
}));
|
|
|
|
import prisma from '@/lib/prisma';
|
|
import getAttendeeStatsRoute from '@/modules/attendees/features/get-stats/route';
|
|
|
|
const userId = 'user-1';
|
|
|
|
function makeApp(userValue: unknown) {
|
|
const app = new Hono();
|
|
app.use('*', async (c, next) => {
|
|
c.set('user', userValue as never);
|
|
await next();
|
|
});
|
|
app.route('/groups', getAttendeeStatsRoute);
|
|
return app;
|
|
}
|
|
|
|
function makeGroup(overrides: Record<string, unknown> = {}) {
|
|
return { id: 'group-1', name: 'Funcional', createdById: userId, members: [], ...overrides };
|
|
}
|
|
|
|
function countRow(status: string, count: number) {
|
|
return { status, _count: { _all: count } };
|
|
}
|
|
|
|
describe('GET /groups/:groupId/attendees/:attendeeId/stats', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('cuenta asistencias, ausencias y justificadas con su porcentaje', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(makeGroup());
|
|
prisma.attendee.findFirst.mockResolvedValue({ id: 'a-1' });
|
|
prisma.attendance.groupBy.mockResolvedValue([
|
|
countRow('PRESENT', 17),
|
|
countRow('ABSENT', 2),
|
|
countRow('EXCUSED', 1),
|
|
]);
|
|
|
|
const res = await makeApp({ id: userId }).request(
|
|
'/groups/group-1/attendees/a-1/stats',
|
|
);
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(await res.json()).toEqual({
|
|
attendeeId: 'a-1',
|
|
groupId: 'group-1',
|
|
presentCount: 17,
|
|
absentCount: 2,
|
|
excusedCount: 1,
|
|
totalMarked: 20,
|
|
attendanceRate: 85,
|
|
});
|
|
});
|
|
|
|
it('devuelve conteos en cero cuando el participante no tiene registros', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(makeGroup());
|
|
prisma.attendee.findFirst.mockResolvedValue({ id: 'a-1' });
|
|
prisma.attendance.groupBy.mockResolvedValue([]);
|
|
|
|
const res = await makeApp({ id: userId }).request(
|
|
'/groups/group-1/attendees/a-1/stats',
|
|
);
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(await res.json()).toEqual({
|
|
attendeeId: 'a-1',
|
|
groupId: 'group-1',
|
|
presentCount: 0,
|
|
absentCount: 0,
|
|
excusedCount: 0,
|
|
totalMarked: 0,
|
|
attendanceRate: 0,
|
|
});
|
|
});
|
|
|
|
it('devuelve 404 si el participante no pertenece al grupo', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(makeGroup());
|
|
prisma.attendee.findFirst.mockResolvedValue(null);
|
|
|
|
const res = await makeApp({ id: userId }).request(
|
|
'/groups/group-1/attendees/nope/stats',
|
|
);
|
|
|
|
expect(res.status).toBe(404);
|
|
expect(await res.json()).toMatchObject({ code: 'not_found' });
|
|
});
|
|
|
|
it('devuelve 403 si el usuario no tiene acceso al grupo', async () => {
|
|
prisma.group.findUnique.mockResolvedValue(
|
|
makeGroup({ createdById: 'otro-user', members: [] }),
|
|
);
|
|
|
|
const res = await makeApp({ id: userId }).request(
|
|
'/groups/group-1/attendees/a-1/stats',
|
|
);
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(await res.json()).toMatchObject({ code: 'group_access_denied' });
|
|
});
|
|
|
|
it('devuelve 401 si no hay sesión', async () => {
|
|
const res = await makeApp(undefined).request('/groups/group-1/attendees/a-1/stats');
|
|
|
|
expect(res.status).toBe(401);
|
|
});
|
|
});
|