Files
gruperly/apps/backend/src/modules/classes/lib/sessions.ts
Jose Selesan 69275c37b4 chore: update comments and documentation for clarity and consistency across various files
- Translated comments from Spanish to English in vite.config.ts, docker-compose.yml, attendance-risk.ts, analytics.ts, attendance.ts, attendees.ts, enums.ts, groups.ts, and onboarding.ts.
- Improved descriptions for better understanding of code functionality and purpose.
- Ensured uniformity in terminology and phrasing throughout the codebase.
2026-10-01 16:06:54 -03:00

65 lines
1.6 KiB
TypeScript

import type { WeekDay } from '@gruperly/shared';
import { todayOccurrence } from '@/modules/home/lib/scheduler';
import type { ClassesDb } from './helpers';
export type TodaySessionCandidate = {
groupId: string;
startsAt: Date;
};
type ScheduledGroup = {
id: string;
days: WeekDay[] | null;
time: string | null;
};
// Computes which groups have a class today (from days/time and the teacher's time zone).
export function buildTodayCandidates(
groups: ScheduledGroup[],
now: Date,
timeZone: string,
): TodaySessionCandidate[] {
const candidates: TodaySessionCandidate[] = [];
for (const group of groups) {
if (!group.days || group.days.length === 0 || !group.time) {
continue;
}
const startsAt = todayOccurrence({
days: group.days,
time: group.time,
now,
timeZone,
});
if (startsAt) {
candidates.push({ groupId: group.id, startsAt });
}
}
return candidates;
}
// Materializes (idempotent upsert) today's ClassSession rows and returns their ids.
export async function ensureTodaySessions(
db: Pick<ClassesDb, 'classSession'>,
candidates: TodaySessionCandidate[],
): Promise<string[]> {
const sessionIds: string[] = [];
for (const candidate of candidates) {
const session = await db.classSession.upsert({
where: {
groupId_startsAt: {
groupId: candidate.groupId,
startsAt: candidate.startsAt,
},
},
create: {
groupId: candidate.groupId,
startsAt: candidate.startsAt,
},
update: {},
select: { id: true },
});
sessionIds.push(session.id);
}
return sessionIds;
}