1
|
|
|
import {CommandHandler} from '@nestjs/cqrs'; |
2
|
|
|
import {Inject} from '@nestjs/common'; |
3
|
|
|
import {UpdateDailyRateCommand} from './UpdateDailyRateCommand'; |
4
|
|
|
import {ITaskRepository} from 'src/Domain/Task/Repository/ITaskRepository'; |
5
|
|
|
import {IDailyRateRepository} from 'src/Domain/Billing/Repository/IDailyRateRepository'; |
6
|
|
|
import {IUserRepository} from 'src/Domain/User/Repository/IUserRepository'; |
7
|
|
|
import {ICustomerRepository} from 'src/Domain/Customer/Repository/ICustomerRepository'; |
8
|
|
|
import {AbstractUserCustomerAndTaskGetter} from './AbstractUserCustomerAndTaskGetter'; |
9
|
|
|
import {DailyRateNotFoundException} from 'src/Domain/Billing/Exception/DailyRateNotFoundException'; |
10
|
|
|
import {IsDailyRateAlreadyExist} from 'src/Domain/Billing/Specification/IsDailyRateAlreadyExist'; |
11
|
|
|
import {DailyRateAlreadyExistException} from 'src/Domain/Billing/Exception/DailyRateAlreadyExistException'; |
12
|
|
|
|
13
|
|
|
@CommandHandler(UpdateDailyRateCommand) |
14
|
|
|
export class UpdateDailyRateCommandHandler extends AbstractUserCustomerAndTaskGetter { |
15
|
|
|
constructor( |
16
|
|
|
@Inject('ITaskRepository') taskRepository: ITaskRepository, |
17
|
|
|
@Inject('IUserRepository') userRepository: IUserRepository, |
18
|
|
|
@Inject('ICustomerRepository') customerRepository: ICustomerRepository, |
19
|
|
|
@Inject('IDailyRateRepository') |
20
|
|
|
private readonly dailyRateRepository: IDailyRateRepository, |
21
|
|
|
private readonly isDailyRateAlreadyExist: IsDailyRateAlreadyExist |
22
|
|
|
) { |
23
|
|
|
super(taskRepository, userRepository, customerRepository); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public async execute(command: UpdateDailyRateCommand): Promise<string> { |
27
|
|
|
const {id, customerId, userId, taskId, amount} = command; |
28
|
|
|
|
29
|
|
|
const dailyRate = await this.dailyRateRepository.findOneById(id); |
30
|
|
|
if (!dailyRate) { |
31
|
|
|
throw new DailyRateNotFoundException(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
let user = dailyRate.getUser(); |
35
|
|
|
let customer = dailyRate.getCustomer(); |
36
|
|
|
let task = dailyRate.getTask(); |
37
|
|
|
|
38
|
|
|
if ( |
39
|
|
|
userId !== user.getId() || |
40
|
|
|
taskId !== task.getId() || |
41
|
|
|
customerId !== customer.getId() |
42
|
|
|
) { |
43
|
|
|
[user, customer, task] = await Promise.all([ |
44
|
|
|
this.getUser(userId), |
45
|
|
|
this.getCustomer(customerId), |
46
|
|
|
this.getTask(taskId) |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
if ( |
50
|
|
|
true === |
51
|
|
|
(await this.isDailyRateAlreadyExist.isSatisfiedBy(user, task, customer)) |
52
|
|
|
) { |
53
|
|
|
throw new DailyRateAlreadyExistException(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
dailyRate.update(Math.round(amount * 100), user, customer, task); |
58
|
|
|
|
59
|
|
|
await this.dailyRateRepository.save(dailyRate); |
60
|
|
|
|
61
|
|
|
return dailyRate.getId(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|