import { Inject } from '@nestjs/common' ; import { QueryHandler } from '@nestjs/cqrs' ; import { IEventRepository } from 'src/Domain/FairCalendar/Repository/IEventRepository' ; import { IDateUtils } from 'src/Application/IDateUtils' ; import { ProjectView } from 'src/Application/Project/View/ProjectView' ; import { TaskView } from 'src/Application/Task/View/TaskView' ; import { ILeaveRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRepository' ; import { FairCalendarView } from '../View/FairCalendarView' ; import { GetMonthlyFairCalendarQuery } from './GetMonthlyFairCalendarQuery' ; import { Leave } from 'src/Domain/HumanResource/Leave/Leave.entity' ; import { Event } from 'src/Domain/FairCalendar/Event.entity' ; @ QueryHandler ( GetMonthlyFairCalendarQuery ) export class GetMonthlyFairCalendarQueryHandler { constructor ( @ Inject ( 'IEventRepository' ) private readonly eventRepository , @ Inject ( 'ILeaveRepository' ) private readonly leaveRepository , @ Inject ( 'IDateUtils' ) private readonly dateUtils ) { } public async execute ( query ) { const { date , userId } = query ; const formatedDate = this . dateUtils . format ( date , 'y-MM-dd' ) ; const [ events , leaves ] = await Promise . all ( [ this . eventRepository . findMonthlyEvents ( formatedDate , userId ) , this . leaveRepository . findMonthlyLeaves ( formatedDate , userId ) ] ) ; return [ ... this . buildEvents ( events ) , ... this . buildLeaves ( leaves ) ] ; } private buildEvents ( events ) { const result = [ ] ; for ( const event of events ) { const project = event . getProject ( ) ; const task = event . getTask ( ) ; result . push ( new FairCalendarView ( event . getType ( ) , event . getTime ( ) , event . getDate ( ) , event . getSummary ( ) , event . getId ( ) , event . isBillable ( ) , project ? new ProjectView ( project . getId ( ) , project . getName ( ) , project . getDayDuration ( ) ) : null , task ? new TaskView ( task . getId ( ) , task . getName ( ) ) : null ) ) ; } return result ; } private buildLeaves ( leaves ) { const result = [ ] ; for ( const leave of leaves ) { result . push ( new FairCalendarView ( leave . getType ( ) , leave . getTime ( ) , leave . getDate ( ) ) ) ; } return result ; } }