2024-04-08 23:21:36 +02:00
|
|
|
import { getISODate, getWeekNumber } from "@/utils/dates"
|
2024-03-16 00:36:44 +01:00
|
|
|
import type { Habit } from "./Habit"
|
|
|
|
import type { HabitProgress } from "./HabitProgress"
|
2024-04-08 23:21:36 +02:00
|
|
|
import type { GoalProgress } from "./Goal"
|
|
|
|
import { GoalBooleanProgress, GoalNumericProgress } from "./Goal"
|
2024-03-16 00:36:44 +01:00
|
|
|
|
2024-03-22 10:23:28 +01:00
|
|
|
export interface HabitHistoryJSON {
|
2024-03-16 00:36:44 +01:00
|
|
|
habit: Habit
|
|
|
|
progressHistory: HabitProgress[]
|
|
|
|
}
|
|
|
|
|
2024-03-22 10:23:28 +01:00
|
|
|
export class HabitHistory implements HabitHistoryJSON {
|
2024-03-16 00:36:44 +01:00
|
|
|
public habit: Habit
|
2024-04-08 23:21:36 +02:00
|
|
|
|
|
|
|
private _progressHistory: HabitProgress[] = []
|
2024-03-16 00:36:44 +01:00
|
|
|
|
2024-03-22 10:23:28 +01:00
|
|
|
public constructor(options: HabitHistoryJSON) {
|
2024-03-16 00:36:44 +01:00
|
|
|
const { habit, progressHistory } = options
|
|
|
|
this.habit = habit
|
|
|
|
this.progressHistory = progressHistory
|
|
|
|
}
|
2024-04-08 23:21:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Progress History sorted chronologically (from old to most recent progress at the end).
|
|
|
|
*/
|
|
|
|
public get progressHistory(): HabitProgress[] {
|
|
|
|
return this._progressHistory
|
|
|
|
}
|
|
|
|
|
|
|
|
public set progressHistory(progressHistory: HabitProgress[]) {
|
|
|
|
this._progressHistory = [...progressHistory]
|
|
|
|
this._progressHistory.sort((a, b) => {
|
|
|
|
return a.date.getTime() - b.date.getTime()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-11 12:32:09 +02:00
|
|
|
public getProgressesByDate(date: Date): HabitProgress[] {
|
2024-04-08 23:21:36 +02:00
|
|
|
return this._progressHistory.filter((progress) => {
|
|
|
|
if (this.habit.goal.frequency === "monthly") {
|
|
|
|
return (
|
|
|
|
date.getFullYear() === progress.date.getFullYear() &&
|
|
|
|
date.getMonth() === progress.date.getMonth()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (this.habit.goal.frequency === "weekly") {
|
|
|
|
return (
|
|
|
|
getWeekNumber(date) === getWeekNumber(progress.date) &&
|
|
|
|
date.getFullYear() === progress.date.getFullYear()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (this.habit.goal.frequency === "daily") {
|
|
|
|
return getISODate(date) === getISODate(progress.date)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
public getGoalProgressByDate(date: Date): GoalProgress {
|
|
|
|
const progresses = this.getProgressesByDate(date)
|
|
|
|
if (this.habit.goal.isBoolean()) {
|
|
|
|
const lastSavedProgress = progresses[progresses.length - 1]
|
|
|
|
return new GoalBooleanProgress({
|
|
|
|
goal: this.habit.goal,
|
|
|
|
progress: lastSavedProgress?.goalProgress?.isCompleted() ?? false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.habit.goal.isNumeric()) {
|
|
|
|
return new GoalNumericProgress({
|
|
|
|
goal: this.habit.goal,
|
|
|
|
progress: progresses.reduce((sum, current) => {
|
|
|
|
const goalProgress = current.goalProgress as GoalNumericProgress
|
|
|
|
return sum + goalProgress.progress
|
|
|
|
}, 0),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("Invalid")
|
|
|
|
}
|
2024-03-16 00:36:44 +01:00
|
|
|
}
|