|
1
|
|
|
import { mock, instance, when, verify, deepEqual, anything } from 'ts-mockito'; |
|
2
|
|
|
import { CustomerRepository } from 'src/Infrastructure/Customer/Repository/CustomerRepository'; |
|
3
|
|
|
import { GenerateInvoiceCommandHandler } from './GenerateInvoiceCommandHandler'; |
|
4
|
|
|
import { GenerateInvoiceCommand } from './GenerateInvoiceCommand'; |
|
5
|
|
|
import { User } from 'src/Domain/HumanResource/User/User.entity'; |
|
6
|
|
|
import { CustomerNotFoundException } from 'src/Domain/Customer/Exception/CustomerNotFoundException'; |
|
7
|
|
|
import { InvoiceIdGenerator } from 'src/Domain/Accounting/Generators/InvoiceIdGenerator'; |
|
8
|
|
|
import { InvoiceStatus } from 'src/Domain/Accounting/Invoice.entity'; |
|
9
|
|
|
import { EventRepository } from 'src/Infrastructure/FairCalendar/Repository/EventRepository'; |
|
10
|
|
|
import { InvoiceRepository } from 'src/Infrastructure/Accounting/Repository/InvoiceRepository'; |
|
11
|
|
|
import { InvoiceItemRepository } from 'src/Infrastructure/Accounting/Repository/InvoiceItemRepository'; |
|
12
|
|
|
import { DateUtilsAdapter } from 'src/Infrastructure/Adapter/DateUtilsAdapter'; |
|
13
|
|
|
import { Customer } from 'src/Domain/Customer/Customer.entity'; |
|
14
|
|
|
import { NoBillableEventsFoundException } from 'src/Domain/Accounting/Exception/NoBillableEventsFoundException'; |
|
15
|
|
|
|
|
16
|
|
|
describe('GenerateInvoiceCommandHandler', () => { |
|
17
|
|
|
let customerRepository: CustomerRepository; |
|
18
|
|
|
let eventRepository: EventRepository; |
|
19
|
|
|
let invoiceRepository: InvoiceRepository; |
|
20
|
|
|
let invoiceItemRepository: InvoiceItemRepository; |
|
21
|
|
|
let invoiceIdGenerator: InvoiceIdGenerator; |
|
22
|
|
|
let dateUtilsAdapter: DateUtilsAdapter; |
|
23
|
|
|
let handler: GenerateInvoiceCommandHandler; |
|
24
|
|
|
|
|
25
|
|
|
const user = mock(User); |
|
26
|
|
|
const customer = mock(Customer); |
|
27
|
|
|
const date = new Date('2020-11-23T17:43:14.299Z'); |
|
28
|
|
|
const command = new GenerateInvoiceCommand( |
|
29
|
|
|
'a491ccc9-df7c-4fc6-8e90-db816208f689', |
|
30
|
|
|
InvoiceStatus.DRAFT, |
|
31
|
|
|
30, |
|
32
|
|
|
date, |
|
33
|
|
|
instance(user) |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
beforeEach(() => { |
|
37
|
|
|
customerRepository = mock(CustomerRepository); |
|
38
|
|
|
invoiceIdGenerator = mock(InvoiceIdGenerator); |
|
39
|
|
|
eventRepository = mock(EventRepository); |
|
40
|
|
|
invoiceRepository = mock(InvoiceRepository); |
|
41
|
|
|
invoiceItemRepository = mock(InvoiceItemRepository); |
|
42
|
|
|
dateUtilsAdapter = mock(DateUtilsAdapter); |
|
43
|
|
|
|
|
44
|
|
|
handler = new GenerateInvoiceCommandHandler( |
|
45
|
|
|
instance(customerRepository), |
|
46
|
|
|
instance(eventRepository), |
|
47
|
|
|
instance(invoiceRepository), |
|
48
|
|
|
instance(invoiceItemRepository), |
|
49
|
|
|
instance(dateUtilsAdapter), |
|
50
|
|
|
instance(invoiceIdGenerator), |
|
51
|
|
|
); |
|
52
|
|
|
}); |
|
53
|
|
|
|
|
54
|
|
|
it('testCustomerNotFound', async () => { |
|
55
|
|
|
when( |
|
56
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
57
|
|
|
).thenResolve(null); |
|
58
|
|
|
|
|
59
|
|
|
try { |
|
60
|
|
|
await handler.execute(command); |
|
61
|
|
|
} catch (e) { |
|
62
|
|
|
expect(e).toBeInstanceOf(CustomerNotFoundException); |
|
63
|
|
|
expect(e.message).toBe('crm.customers.errors.not_found'); |
|
64
|
|
|
verify( |
|
65
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
66
|
|
|
).once(); |
|
67
|
|
|
verify(invoiceIdGenerator.generate()).never(); |
|
68
|
|
|
verify(eventRepository.findBillableEventsByMonthAndCustomer(anything(), anything())).never(); |
|
69
|
|
|
verify(dateUtilsAdapter.addDaysToDate(anything(), anything())).never(); |
|
70
|
|
|
verify(invoiceRepository.save(anything())).never(); |
|
71
|
|
|
verify(invoiceItemRepository.save(anything())).never(); |
|
72
|
|
|
} |
|
73
|
|
|
}); |
|
74
|
|
|
|
|
75
|
|
|
it('testNoBillableEventsFoundException', async () => { |
|
76
|
|
|
when( |
|
77
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
78
|
|
|
).thenResolve(instance(customer)); |
|
79
|
|
|
when(invoiceIdGenerator.generate()).thenResolve('FS-2020-0001'); |
|
80
|
|
|
when( |
|
81
|
|
|
eventRepository.findBillableEventsByMonthAndCustomer(date, instance(customer)) |
|
82
|
|
|
).thenResolve([]); |
|
83
|
|
|
|
|
84
|
|
|
try { |
|
85
|
|
|
await handler.execute(command); |
|
86
|
|
|
} catch (e) { |
|
87
|
|
|
expect(e).toBeInstanceOf(NoBillableEventsFoundException); |
|
88
|
|
|
expect(e.message).toBe('accounting.invoice.errors.no_billable_events_found'); |
|
89
|
|
|
verify( |
|
90
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
|
91
|
|
|
).once(); |
|
92
|
|
|
verify(invoiceIdGenerator.generate()).once(); |
|
93
|
|
|
verify( |
|
94
|
|
|
eventRepository.findBillableEventsByMonthAndCustomer(date, instance(customer)) |
|
95
|
|
|
).once(); |
|
96
|
|
|
verify(dateUtilsAdapter.addDaysToDate(anything(), anything())).never(); |
|
97
|
|
|
verify(invoiceRepository.save(anything())).never(); |
|
98
|
|
|
verify(invoiceItemRepository.save(anything())).never(); |
|
99
|
|
|
} |
|
100
|
|
|
}); |
|
101
|
|
|
}); |
|
102
|
|
|
|