Passed
Pull Request — master (#165)
by Mathieu
02:00
created

server/src/Application/Accounting/Command/Invoice/GenerateInvoiceCommandHandler.ts   A

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 82
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 5
mnd 4
bc 4
fnc 1
bpm 4
cpm 5
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B GenerateInvoiceCommandHandler.execute 0 50 5
1
import { CommandHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { ICustomerRepository } from 'src/Domain/Customer/Repository/ICustomerRepository';
4
import { GenerateInvoiceCommand } from './GenerateInvoiceCommand';
5
import { CustomerNotFoundException } from 'src/Domain/Customer/Exception/CustomerNotFoundException';
6
import { IEventRepository } from 'src/Domain/FairCalendar/Repository/IEventRepository';
7
import { Invoice } from 'src/Domain/Accounting/Invoice.entity';
8
import { InvoiceItem } from 'src/Domain/Accounting/InvoiceItem.entity';
9
import { IInvoiceRepository } from 'src/Domain/Accounting/Repository/IInvoiceRepository';
10
import { IInvoiceItemRepository } from 'src/Domain/Accounting/Repository/IInvoiceItemRepository';
11
import { InvoiceIdGenerator } from 'src/Domain/Accounting/Generators/InvoiceIdGenerator';
12
import { NoBillableEventsFoundException } from 'src/Domain/Accounting/Exception/NoBillableEventsFoundException';
13
import { IDateUtils } from 'src/Application/IDateUtils';
14
15
@CommandHandler(GenerateInvoiceCommand)
16
export class GenerateInvoiceCommandHandler {
17
  constructor(
18
    @Inject('ICustomerRepository')
19
    private readonly customerRepository: ICustomerRepository,
20
    @Inject('IEventRepository')
21
    private readonly eventRepository: IEventRepository,
22
    @Inject('IInvoiceRepository')
23
    private readonly billingRepository: IInvoiceRepository,
24
    @Inject('IInvoiceItemRepository')
25
    private readonly billingItemRepository: IInvoiceItemRepository,
26
    @Inject('IDateUtils')
27
    private readonly dateUtils: IDateUtils,
28
    private readonly billingIdGenerator: InvoiceIdGenerator
29
  ) {}
30
31
  public async execute(command: GenerateInvoiceCommand): Promise<string> {
32
    const { customerId, status, date, user } = command;
33
34
    const customer = await this.customerRepository.findOneById(customerId);
35
    if (!customer) {
36
      throw new CustomerNotFoundException();
37
    }
38
39
    const [ billingId, events ] = await Promise.all([
40
      this.billingIdGenerator.generate(),
41
      this.eventRepository.findBillableEventsByMonthAndCustomer(date, customer)
42
    ]);
43
44
    if (!events) {
45
      throw new NoBillableEventsFoundException();
46
    }
47
48
    const date = this.dateUtils.getCurrentDate();
49
    //const expiryDate = new Date(Date.now() + days * 24*60*60*1000);
50
51
52
    const billing = new Invoice(billingId, status, '', user, customer);
53
    const billingItems: InvoiceItem[] = [];
54
55
    for (const {
56
      time,
57
      billable,
58
      day_duration,
59
      project_name,
60
      task_name,
61
      first_name,
62
      last_name,
63
      amount
64
    } of events) {
65
      billingItems.push(
66
        new InvoiceItem(
67
          billing,
68
          `${project_name} - ${task_name} - ${first_name} ${last_name}`,
69
          Math.ceil(time / day_duration * 100) / 100,
70
          Number(amount),
71
          billable ? 0 : 100
72
        )
73
      );
74
    }
75
76
    await this.billingRepository.save(billing);
77
    await this.billingItemRepository.save(billingItems);
78
79
    return billing.getId();
80
  }
81
}
82