Files
timetracker/backend/src/utils/timeUtils.ts
simon.franken 6a6a3ba00b refactoring
2026-02-18 10:26:15 +01:00

30 lines
944 B
TypeScript

import { prisma } from "../prisma/client";
/**
* Checks whether a user already has a time entry that overlaps with the given
* [startTime, endTime] interval.
*
* @param userId - The user whose entries are checked.
* @param startTime - Start of the interval to check.
* @param endTime - End of the interval to check.
* @param excludeId - Optional time-entry id to exclude (used when updating an
* existing entry so it does not collide with itself).
*/
export async function hasOverlappingEntries(
userId: string,
startTime: Date,
endTime: Date,
excludeId?: string,
): Promise<boolean> {
const count = await prisma.timeEntry.count({
where: {
userId,
...(excludeId ? { id: { not: excludeId } } : {}),
// An entry overlaps when it starts before our end AND ends after our start.
startTime: { lt: endTime },
endTime: { gt: startTime },
},
});
return count > 0;
}