feat: refactor character history schema and related logic for improved date handling and data integrity

This commit is contained in:
2026-03-02 12:19:56 +01:00
parent ef7ef4dd6c
commit 60d2474f51
6 changed files with 1126 additions and 12 deletions

View File

@@ -139,19 +139,19 @@ async function applyCharacterOverrides(
);
}
function getDateKey(date: Date): string {
return date.toISOString().split('T')[0];
export function getDateKey(date: Date): number {
return normalizeDay(date).getTime();
}
function normalizeDay(date: Date = new Date()): Date {
export function normalizeDay(date: Date = new Date()): Date {
const normalized = new Date(date);
normalized.setHours(1, 0, 0, 0);
return normalized;
}
function pickDailyCharacter(characters: CharacterWithRelations[], date: Date): CharacterWithRelations {
const dateStr = getDateKey(date);
const seed = dateStr.split('-').reduce((acc, value) => acc + parseInt(value), 0);
const timestamp = getDateKey(date);
const seed = Math.floor(timestamp / 1000 / 60 / 60 / 24);
const index = seed % characters.length;
return characters[index];
}
@@ -254,9 +254,9 @@ export async function getYesterdayCharacter(
date: Date = new Date(),
characters?: CharacterWithRelations[]
): Promise<CharacterWithRelations | null> {
const baseDate = normalizeDay(date);
baseDate.setDate(baseDate.getDate() - 1);
const yesterdayDate = getDateKey(baseDate);
const yesterday = new Date(date);
yesterday.setDate(yesterday.getDate() - 1);
const yesterdayDate = getDateKey(yesterday);
const [yesterdayEntry] = await db
.select()