import { Inject } from '@nestjs/common' ; import { CommandHandler } from '@nestjs/cqrs' ; import { CreateLeaveRequestCommand } from './CreateLeaveRequestCommand' ; import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository' ; import { LeaveRequest } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity' ; import { DoesLeaveRequestExistForPeriod } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestExistForPeriod' ; import { LeaveRequestAlreadyExistForThisPeriodException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestAlreadyExistForThisPeriodException' ; import { DoesEventsOrLeaveExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesEventsOrLeaveExistForPeriod' ; import { EventsOrLeavesAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsOrLeavesAlreadyExistForThisPeriodException' ; @ CommandHandler ( CreateLeaveRequestCommand ) export class CreateLeaveRequestCommandHandler { constructor ( @ Inject ( 'ILeaveRequestRepository' ) private readonly leaveRequestRepository , private readonly doesLeaveRequestExistForPeriod , private readonly doesEventsOrLeaveExistForPeriod ) { } public async execute ( command ) { const { user , endDate , endsAllDay , type , startDate , startsAllDay , comment } = command ; if ( true === ( await this . doesLeaveRequestExistForPeriod . isSatisfiedBy ( user , startDate , endDate ) ) ) { throw new LeaveRequestAlreadyExistForThisPeriodException ( ) ; } if ( true === ( await this . doesEventsOrLeaveExistForPeriod . isSatisfiedBy ( user , startDate , endDate ) ) ) { throw new EventsOrLeavesAlreadyExistForThisPeriodException ( ) ; } const leaveRequest = await this . leaveRequestRepository . save ( new LeaveRequest ( user , type , startDate , startsAllDay , endDate , endsAllDay , comment ) ) ; return leaveRequest . getId ( ) ; } }