1
|
|
|
import {CommandHandler} from '@nestjs/cqrs'; |
2
|
|
|
import {Inject} from '@nestjs/common'; |
3
|
|
|
import {CreatePaySlipCommand} from './CreatePaySlipCommand'; |
4
|
|
|
import {IPaySlipRepository} from 'src/Domain/HumanResource/PaySlip/Repository/IPaySlipRepository'; |
5
|
|
|
import {PaySlip} from 'src/Domain/HumanResource/PaySlip/PaySlip.entity'; |
6
|
|
|
import {IUserRepository} from 'src/Domain/HumanResource/User/Repository/IUserRepository'; |
7
|
|
|
import {IFileRepository} from 'src/Domain/File/Repository/IFileRepository'; |
8
|
|
|
import {UserNotFoundException} from 'src/Domain/HumanResource/User/Exception/UserNotFoundException'; |
9
|
|
|
import {FileNotFoundException} from 'src/Domain/File/Exception/FileNotFoundException'; |
10
|
|
|
import {IsPaySlipAlreadyExist} from 'src/Domain/HumanResource/PaySlip/Specification/IsPaySlipAlreadyExist'; |
11
|
|
|
import {PaySlipAlreadyExistException} from 'src/Domain/HumanResource/PaySlip/Exception/PaySlipAlreadyExistException'; |
12
|
|
|
|
13
|
|
|
@CommandHandler(CreatePaySlipCommand) |
14
|
|
|
export class CreatePaySlipCommandHandler { |
15
|
|
|
constructor( |
16
|
|
|
@Inject('IPaySlipRepository') |
17
|
|
|
private readonly paySlipRepository: IPaySlipRepository, |
18
|
|
|
@Inject('IUserRepository') |
19
|
|
|
private readonly userRepository: IUserRepository, |
20
|
|
|
@Inject('IFileRepository') |
21
|
|
|
private readonly fileRepository: IFileRepository, |
22
|
|
|
@Inject('IsPaySlipAlreadyExist') |
23
|
|
|
private readonly isPaySlipAlreadyExist: IsPaySlipAlreadyExist |
24
|
|
|
) {} |
25
|
|
|
|
26
|
|
|
public async execute(command: CreatePaySlipCommand): Promise<string> { |
27
|
|
|
const {date, fileId, userId} = command; |
28
|
|
|
|
29
|
|
|
const user = await this.userRepository.findOneById(userId); |
30
|
|
|
if (!user) { |
31
|
|
|
throw new UserNotFoundException(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
const file = await this.fileRepository.findOneById(fileId); |
35
|
|
|
if (!file) { |
36
|
|
|
throw new FileNotFoundException(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ( |
40
|
|
|
true === |
41
|
|
|
(await this.isPaySlipAlreadyExist.isSatisfiedBy(user, new Date(date))) |
42
|
|
|
) { |
43
|
|
|
this.fileRepository.remove(file); |
44
|
|
|
|
45
|
|
|
throw new PaySlipAlreadyExistException(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
const paySlip = await this.paySlipRepository.save( |
49
|
|
|
new PaySlip(date, file, user) |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
return paySlip.getId(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|