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

48 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-03-16 00:36:44 +01:00
import { HabitHistory } from "../entities/HabitHistory"
2024-03-15 22:48:28 +01:00
import { HabitsTracker } from "../entities/HabitsTracker"
import type { User } from "../entities/User"
2024-03-16 00:36:44 +01:00
import type { GetHabitProgressHistoryRepository } from "../repositories/GetHabitProgressHistory"
2024-03-15 22:48:28 +01:00
import type { GetHabitsByUserIdRepository } from "../repositories/GetHabitsByUserId"
export interface RetrieveHabitsTrackerUseCaseDependencyOptions {
getHabitsByUserIdRepository: GetHabitsByUserIdRepository
2024-03-16 00:36:44 +01:00
getHabitProgressHistoryRepository: GetHabitProgressHistoryRepository
2024-03-15 22:48:28 +01:00
}
export interface RetrieveHabitsTrackerUseCaseOptions {
userId: User["id"]
}
export class RetrieveHabitsTrackerUseCase
implements RetrieveHabitsTrackerUseCaseDependencyOptions
{
public getHabitsByUserIdRepository: GetHabitsByUserIdRepository
2024-03-16 00:36:44 +01:00
public getHabitProgressHistoryRepository: GetHabitProgressHistoryRepository
2024-03-15 22:48:28 +01:00
public constructor(options: RetrieveHabitsTrackerUseCaseDependencyOptions) {
this.getHabitsByUserIdRepository = options.getHabitsByUserIdRepository
2024-03-16 00:36:44 +01:00
this.getHabitProgressHistoryRepository =
options.getHabitProgressHistoryRepository
2024-03-15 22:48:28 +01:00
}
public async execute(
options: RetrieveHabitsTrackerUseCaseOptions,
): Promise<HabitsTracker> {
const { userId } = options
const habits = await this.getHabitsByUserIdRepository.execute({ userId })
const habitProgressHistories = await Promise.all(
habits.map(async (habit) => {
2024-03-16 00:36:44 +01:00
const progressHistory =
await this.getHabitProgressHistoryRepository.execute({
2024-03-15 22:48:28 +01:00
habit,
2024-03-16 00:36:44 +01:00
})
return new HabitHistory({ habit, progressHistory })
2024-03-15 22:48:28 +01:00
}),
)
2024-03-16 00:36:44 +01:00
const habitsTracker = new HabitsTracker({
habitsHistory: habitProgressHistories,
})
2024-03-15 22:48:28 +01:00
return habitsTracker
}
}