1
1
mirror of https://github.com/theoludwig/p61-project.git synced 2024-07-17 07:00:12 +02:00
p61-project/domain/use-cases/HabitGoalProgressUpdate.ts

67 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2024-04-11 12:32:09 +02:00
import type { GoalProgress } from "../entities/Goal"
import type { HabitHistory } from "../entities/HabitHistory"
2024-04-11 23:03:45 +02:00
import type { HabitProgress } from "../entities/HabitProgress"
2024-04-11 12:32:09 +02:00
import type { HabitProgressCreateRepository } from "../repositories/HabitProgressCreate"
import type { HabitProgressUpdateRepository } from "../repositories/HabitProgressUpdate"
2024-04-11 23:03:45 +02:00
export interface HabitGoalProgressUpdateUseCaseOptions {
2024-04-11 12:32:09 +02:00
date: Date
goalProgress: GoalProgress
habitHistory: HabitHistory
}
2024-04-11 23:03:45 +02:00
export interface HabitGoalProgressUpdateUseCaseDependencyOptions {
habitProgressCreateRepository: HabitProgressCreateRepository
habitProgressUpdateRepository: HabitProgressUpdateRepository
}
2024-04-11 12:32:09 +02:00
export class HabitGoalProgressUpdateUseCase
2024-04-11 23:03:45 +02:00
implements HabitGoalProgressUpdateUseCaseDependencyOptions
2024-04-11 12:32:09 +02:00
{
2024-04-11 23:03:45 +02:00
public habitProgressCreateRepository: HabitProgressCreateRepository
public habitProgressUpdateRepository: HabitProgressUpdateRepository
2024-04-11 12:32:09 +02:00
2024-04-11 23:03:45 +02:00
public constructor(options: HabitGoalProgressUpdateUseCaseDependencyOptions) {
this.habitProgressCreateRepository = options.habitProgressCreateRepository
this.habitProgressUpdateRepository = options.habitProgressUpdateRepository
2024-04-11 12:32:09 +02:00
}
2024-04-11 23:03:45 +02:00
public async execute(
options: HabitGoalProgressUpdateUseCaseOptions,
): Promise<HabitProgress> {
const { date, goalProgress, habitHistory } = options
if (goalProgress.isBoolean()) {
const currentHabitProgress = habitHistory.getProgressesByDate(date)[0]
if (currentHabitProgress == null) {
return await this.habitProgressCreateRepository.execute({
habitProgressData: {
date,
goalProgress,
habitId: habitHistory.habit.id,
},
})
}
return await this.habitProgressUpdateRepository.execute({
habitProgressData: {
date,
goalProgress,
id: currentHabitProgress.id,
},
})
}
2024-05-23 00:31:21 +02:00
if (goalProgress.isNumeric()) {
return await this.habitProgressCreateRepository.execute({
habitProgressData: {
date,
goalProgress,
habitId: habitHistory.habit.id,
},
})
}
2024-04-11 23:03:45 +02:00
throw new Error("Not implemented")
2024-04-11 12:32:09 +02:00
}
}