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/domain/use-cases/HabitGoalProgressUpdate.ts

57 lines
1.9 KiB
TypeScript
Raw 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,
},
})
}
throw new Error("Not implemented")
2024-04-11 12:32:09 +02:00
}
}