refactor habit compete

This commit is contained in:
Alexander Andreev 2026-01-02 16:04:44 +03:00
parent 19b7f5d93d
commit f0177d31c0

View File

@ -8,6 +8,16 @@ interface CompletionResponse {
updatedCoins: number;
}
/**
* Creates a Date object for the start of a given day in UTC.
* This is duplicated here as per the instruction not to create new shared utilities.
*/
function getStartOfDay(date: Date): Date {
const startOfDay = new Date(date);
startOfDay.setUTCHours(0, 0, 0, 0);
return startOfDay;
}
export default defineEventHandler(async (event): Promise<CompletionResponse> => {
const userId = await getUserIdFromSession(event);
const habitId = parseInt(event.context.params?.id || '', 10);
@ -31,8 +41,7 @@ export default defineEventHandler(async (event): Promise<CompletionResponse> =>
}
// Normalize date to the beginning of the day for consistent checks
const startOfToday = new Date();
startOfToday.setUTCHours(0, 0, 0, 0);
const startOfToday = getStartOfDay(new Date()); // Correctly get a Date object
const existingCompletion = await prisma.habitCompletion.findFirst({
where: {