1
|
|
|
import {CommandHandler} from '@nestjs/cqrs'; |
2
|
|
|
import {Inject} from '@nestjs/common'; |
3
|
|
|
import {CreatePayStubCommand} from './CreatePayStubCommand'; |
4
|
|
|
import {IPayStubRepository} from 'src/Domain/Accounting/Repository/IPayStubRepository'; |
5
|
|
|
import {PayStub} from 'src/Domain/Accounting/PayStub.entity'; |
6
|
|
|
import {IUserRepository} from 'src/Domain/User/Repository/IUserRepository'; |
7
|
|
|
import {IFileRepository} from 'src/Domain/File/Repository/IFileRepository'; |
8
|
|
|
import {UserNotFoundException} from 'src/Domain/User/Exception/UserNotFoundException'; |
9
|
|
|
import {FileNotFoundException} from 'src/Domain/File/Exception/FileNotFoundException'; |
10
|
|
|
|
11
|
|
|
@CommandHandler(CreatePayStubCommand) |
12
|
|
|
export class CreatePayStubCommandHandler { |
13
|
|
|
constructor( |
14
|
|
|
@Inject('IPayStubRepository') |
15
|
|
|
private readonly payStubRepository: IPayStubRepository, |
16
|
|
|
@Inject('IUserRepository') |
17
|
|
|
private readonly userRepository: IUserRepository, |
18
|
|
|
@Inject('IFileRepository') |
19
|
|
|
private readonly fileRepository: IFileRepository |
20
|
|
|
) {} |
21
|
|
|
|
22
|
|
|
public async execute(command: CreatePayStubCommand): Promise<string> { |
23
|
|
|
const {date, fileId, userId} = command; |
24
|
|
|
|
25
|
|
|
const user = await this.userRepository.findOneById(userId); |
26
|
|
|
if (!user) { |
27
|
|
|
throw new UserNotFoundException(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
const file = await this.fileRepository.findOneById(fileId); |
31
|
|
|
if (!file) { |
32
|
|
|
throw new FileNotFoundException(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
const payStub = await this.payStubRepository.save( |
36
|
|
|
new PayStub(date, file, user) |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
return payStub.getId(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|