1
1
mirror of https://github.com/theoludwig/p61-project.git synced 2024-07-17 07:00:12 +02:00
p61-project/infrastructure/repositories/supabase/lib/GetHabitProgressHistory.ts

50 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
{
2024-03-16 00:36:44 +01:00
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
}
}