Upsert reading log entries to have one per day per book

This commit is contained in:
Evan Fiordeliso 2025-06-30 21:33:55 -04:00
parent 7706544710
commit a7276211d4
1 changed files with 14 additions and 1 deletions

View File

@ -8,6 +8,14 @@ export interface ReadingLogEntry {
createdAt: Date;
}
function isSameDay(a: Date, b: Date): boolean {
return (
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
);
}
export class ReadingLog {
private entries: ReadingLogEntry[] = [];
@ -77,8 +85,13 @@ export class ReadingLog {
createdAt: new Date(),
};
if (lastEntry && isSameDay(lastEntry.createdAt, newEntry.createdAt)) {
newEntry.pagesRead += lastEntry.pagesRead;
await this.updateEntry(this.entries.indexOf(lastEntry), lastEntry);
} else {
await this.addRawEntry(newEntry);
}
}
public async addRawEntry(entry: ReadingLogEntry) {
this.entries.push(entry);