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/GetHabitsByUserId.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-03-16 00:36:44 +01:00
import type { GetHabitsByUserIdRepository } from "@/domain/repositories/GetHabitsByUserId"
2024-03-15 22:48:28 +01:00
import { SupabaseRepository } from "./_SupabaseRepository"
2024-03-16 00:36:44 +01:00
import { Habit } from "@/domain/entities/Habit"
import type { Goal } from "@/domain/entities/Goal"
import { GoalBoolean, GoalNumeric } from "@/domain/entities/Goal"
2024-03-15 22:48:28 +01:00
export class GetHabitsByUserIdSupabaseRepository
extends SupabaseRepository
implements GetHabitsByUserIdRepository
{
// execute: GetHabitsByUserIdRepository["execute"] = async (options) => {
// const { userId } = options
// const { data, error } = await this.supabaseClient
// .from("habits")
// .select("*")
// .eq("user_id", userId)
execute: GetHabitsByUserIdRepository["execute"] = async () => {
const { data, error } = await this.supabaseClient.from("habits").select("*")
if (error != null) {
throw new Error(error.message)
}
return data.map((item) => {
let goal: Goal
if (item.goal_target != null && item.goal_target_unit != null) {
goal = new GoalNumeric({
frequency: item.goal_frequency,
target: {
value: item.goal_target,
unit: item.goal_target_unit,
},
})
} else {
goal = new GoalBoolean({
frequency: item.goal_frequency,
})
}
const habit = new Habit({
id: item.id.toString(),
name: item.name,
color: item.color,
icon: item.icon,
userId: item.user_id.toString(),
startDate: new Date(item.start_date),
endDate: item.end_date != null ? new Date(item.end_date) : undefined,
goal,
})
return habit
})
}
}