This repository has been archived on 2024-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
p61-project/infrastructure/supabase/repositories/GetHabitProgressHistory.ts

52 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-03-16 00:36:44 +01:00
import type { GetHabitProgressHistoryRepository } from "@/domain/repositories/GetHabitProgressHistory"
2024-03-15 22:48:28 +01:00
import { SupabaseRepository } from "./_SupabaseRepository"
2024-03-16 00:36:44 +01:00
import { HabitProgress } from "@/domain/entities/HabitProgress"
import type { GoalProgress } from "@/domain/entities/Goal"
2024-03-15 22:48:28 +01:00
import {
GoalBooleanProgress,
GoalNumericProgress,
2024-03-16 00:36:44 +01:00
} from "@/domain/entities/Goal"
2024-03-15 22:48:28 +01:00
2024-03-16 00:36:44 +01:00
export class GetHabitProgressHistorySupabaseRepository
2024-03-15 22:48:28 +01:00
extends SupabaseRepository
2024-03-16 00:36:44 +01:00
implements GetHabitProgressHistoryRepository
2024-03-15 22:48:28 +01:00
{
public execute: GetHabitProgressHistoryRepository["execute"] = async (
options,
) => {
2024-03-15 22:48:28 +01:00
const { habit } = options
const { data, error } = await this.supabaseClient
.from("habits_progresses")
.select("*")
.eq("habit_id", habit.id)
if (error != null) {
throw new Error(error.message)
}
2024-03-16 00:36:44 +01:00
const habitProgressHistory = data.map((item) => {
2024-03-15 22:48:28 +01:00
let goalProgress: GoalProgress | null = null
if (habit.goal.isNumeric()) {
goalProgress = new GoalNumericProgress({
goal: habit.goal,
progress: item.goal_progress,
})
} else if (habit.goal.isBoolean()) {
goalProgress = new GoalBooleanProgress({
goal: habit.goal,
progress: item.goal_progress === 1,
})
}
if (goalProgress == null) {
throw new Error("Goal progress is null.")
}
const habitProgress = new HabitProgress({
id: item.id.toString(),
habitId: item.habit_id.toString(),
goalProgress,
date: new Date(item.date),
})
return habitProgress
})
2024-03-16 00:36:44 +01:00
return habitProgressHistory
2024-03-15 22:48:28 +01:00
}
}