feat(api): add timezone support for group fetching and increase page size
feat(attendance): refactor attendance summary logic and update tests feat(analytics): extend GroupRiskSummary schema with absence and attendance counts feat(groups): enhance group schema with attendee and waitlist counts chore(skills): add frontend design skill with guidelines and license feat(attendee-detail): implement AttendeePhoneLine component for phone display feat(groups): create AttendanceBadge and CapacityIndicator components for group cards feat(groups): add card utility functions for attendance alerts and capacity summaries test(groups): implement tests for attendance alert and capacity summary logic
This commit is contained in:
@@ -8,17 +8,30 @@ import type {
|
||||
import { ok } from '@gruperly/shared';
|
||||
import { getPaginationMetadata, getPaginationOffset } from '@/lib/pagination';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { DEFAULT_TIME_ZONE, nextClassOccurrence } from '@/modules/home/lib/scheduler';
|
||||
import {
|
||||
buildGroupWhereForUser,
|
||||
type GroupDb,
|
||||
type GroupRecord,
|
||||
toGroupDto,
|
||||
type GroupListRecord,
|
||||
toGroupListItemDto,
|
||||
} from '../../lib';
|
||||
|
||||
type ListGroupsDeps = {
|
||||
db?: Pick<GroupDb, 'group'>;
|
||||
now?: Date;
|
||||
};
|
||||
|
||||
const OCCUPANCY_COUNT_INCLUDE = {
|
||||
_count: {
|
||||
select: {
|
||||
// Miembros del grupo. Sin filtro de estado, igual que cuenta el detalle.
|
||||
attendees: true,
|
||||
// Lista de espera: los que todavía esperan cupo (los revisados salen).
|
||||
waitlist: { where: { status: 'PENDING' as const } },
|
||||
},
|
||||
},
|
||||
} satisfies Prisma.GroupInclude;
|
||||
|
||||
export class ListGroups {
|
||||
constructor(private readonly deps: ListGroupsDeps = {}) {}
|
||||
|
||||
@@ -27,21 +40,59 @@ export class ListGroups {
|
||||
userId: string,
|
||||
): Promise<Result<GroupList, ProblemDetails>> {
|
||||
const db = this.deps.db ?? prisma;
|
||||
const now = this.deps.now ?? new Date();
|
||||
const where = buildGroupWhereForUser(userId) as Prisma.GroupWhereInput;
|
||||
|
||||
const [records, total] = await Promise.all([
|
||||
db.group.findMany({
|
||||
where,
|
||||
skip: getPaginationOffset(query),
|
||||
take: query.pageSize,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
db.group.count({ where }),
|
||||
]);
|
||||
// Traemos los grupos del usuario con sus contadores y los ordenamos acá:
|
||||
// la próxima clase se deriva de días + hora, no hay columna por la que
|
||||
// ordenar en la base. Es el mismo motor que usa el resumen de home, con la
|
||||
// zona horaria de la persona (o la del servidor si no viaja).
|
||||
const records: GroupListRecord[] = await db.group.findMany({
|
||||
where,
|
||||
include: OCCUPANCY_COUNT_INCLUDE,
|
||||
});
|
||||
|
||||
const timeZone = query.timeZone ?? DEFAULT_TIME_ZONE;
|
||||
const withNextClass = records.map((record) => ({
|
||||
record,
|
||||
nextAt: nextOccurrenceAt(record, now, timeZone),
|
||||
}));
|
||||
|
||||
/* Un grupo sin horario no tiene clase que comparar, así que va al final;
|
||||
entre grupos con la misma clase (o sin horario) desempata el más nuevo,
|
||||
que era el criterio anterior de la lista. */
|
||||
const ordered = withNextClass.sort((a, b) => {
|
||||
if (a.nextAt !== null && b.nextAt !== null) {
|
||||
return a.nextAt - b.nextAt || b.record.createdAt.getTime() - a.record.createdAt.getTime();
|
||||
}
|
||||
if (a.nextAt !== null) {
|
||||
return -1;
|
||||
}
|
||||
if (b.nextAt !== null) {
|
||||
return 1;
|
||||
}
|
||||
return b.record.createdAt.getTime() - a.record.createdAt.getTime();
|
||||
});
|
||||
|
||||
const offset = getPaginationOffset(query);
|
||||
const page = ordered.slice(offset, offset + query.pageSize);
|
||||
|
||||
return ok({
|
||||
data: records.map((record: GroupRecord) => toGroupDto(record)),
|
||||
pagination: getPaginationMetadata(query, total),
|
||||
data: page.map(({ record }) => toGroupListItemDto(record)),
|
||||
pagination: getPaginationMetadata(query, ordered.length),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function nextOccurrenceAt(record: GroupListRecord, now: Date, timeZone: string): number | null {
|
||||
if (!record.days?.length || !record.time) {
|
||||
return null;
|
||||
}
|
||||
const occurrence = nextClassOccurrence({
|
||||
days: record.days,
|
||||
time: record.time,
|
||||
now,
|
||||
timeZone,
|
||||
});
|
||||
return occurrence ? occurrence.occurrenceAt.getTime() : null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user