@@ -10,35 +10,40 @@ const { expect } = Code
10
10
11
11
// Test helpers
12
12
const BillRunError = require ( '../../../../app/errors/bill-run.error.js' )
13
- const BillRunHelper = require ( '../../../support/helpers/bill-run.helper.js' )
14
- const BillRunModel = require ( '../../../../app/models/bill-run.model.js' )
15
13
16
14
// Things we need to stub
15
+ const BillRunModel = require ( '../../../../app/models/bill-run.model.js' )
17
16
const ChargingModuleGenerateBillRunRequest = require ( '../../../../app/requests/charging-module/generate-bill-run.request.js' )
18
17
const FeatureFlagsConfig = require ( '../../../../config/feature-flags.config.js' )
19
18
const FetchChargeVersionsService = require ( '../../../../app/services/bill-runs/supplementary/fetch-charge-versions.service.js' )
20
19
const HandleErroredBillRunService = require ( '../../../../app/services/bill-runs/handle-errored-bill-run.service.js' )
21
20
const LegacyRefreshBillRunRequest = require ( '../../../../app/requests/legacy/refresh-bill-run.request.js' )
22
21
const ProcessBillingPeriodService = require ( '../../../../app/services/bill-runs/supplementary/process-billing-period.service.js' )
23
- const UnflagUnbilledLicencesService = require ( '../../../../app/services/bill-runs/supplementary/ unflag-unbilled-licences.service.js' )
22
+ const UnflagUnbilledSupplementaryLicencesService = require ( '../../../../app/services/bill-runs/unflag-unbilled-supplementary -licences.service.js' )
24
23
25
24
// Thing under test
26
- const SupplementaryProcessBillRunService = require ( '../../../../app/services/bill-runs/supplementary/process-bill-run.service.js' )
25
+ const ProcessBillRunService = require ( '../../../../app/services/bill-runs/supplementary/process-bill-run.service.js' )
27
26
28
- describe ( 'Supplementary Process Bill Run service' , ( ) => {
27
+ describe ( 'Bill Runs - Supplementary - Process Bill Run service' , ( ) => {
29
28
const billingPeriods = [
30
29
{ startDate : new Date ( '2023-04-01' ) , endDate : new Date ( '2024-03-31' ) } ,
31
30
{ startDate : new Date ( '2022-04-01' ) , endDate : new Date ( '2023-03-31' ) }
32
31
]
32
+ const billRun = { id : '410c84a5-39d3-441a-97ca-6104e14d00a2' }
33
33
34
- let billRun
34
+ let billRunPatchStub
35
35
let chargingModuleGenerateBillRunRequestStub
36
36
let handleErroredBillRunStub
37
37
let legacyRefreshBillRunRequestStub
38
38
let notifierStub
39
39
40
40
beforeEach ( async ( ) => {
41
- billRun = await BillRunHelper . add ( )
41
+ billRunPatchStub = Sinon . stub ( ) . resolves ( )
42
+
43
+ Sinon . stub ( BillRunModel , 'query' ) . returns ( {
44
+ findById : Sinon . stub ( ) . returnsThis ( ) ,
45
+ patch : billRunPatchStub
46
+ } )
42
47
43
48
handleErroredBillRunStub = Sinon . stub ( HandleErroredBillRunService , 'go' )
44
49
chargingModuleGenerateBillRunRequestStub = Sinon . stub ( ChargingModuleGenerateBillRunRequest , 'send' )
@@ -62,20 +67,31 @@ describe('Supplementary Process Bill Run service', () => {
62
67
describe ( 'when the service is called' , ( ) => {
63
68
beforeEach ( ( ) => {
64
69
Sinon . stub ( FetchChargeVersionsService , 'go' ) . resolves ( { chargeVersions : [ ] , licenceIdsForPeriod : [ ] } )
65
- Sinon . stub ( UnflagUnbilledLicencesService , 'go' )
70
+ Sinon . stub ( UnflagUnbilledSupplementaryLicencesService , 'go' )
66
71
} )
67
72
68
73
describe ( 'and nothing is billed' , ( ) => {
69
74
beforeEach ( ( ) => {
70
75
Sinon . stub ( ProcessBillingPeriodService , 'go' ) . resolves ( false )
71
76
} )
72
77
73
- it ( 'sets the Bill Run status to empty' , async ( ) => {
74
- await SupplementaryProcessBillRunService . go ( billRun , billingPeriods )
78
+ it ( 'sets the bill run status first to "processing" and then to "empty"' , async ( ) => {
79
+ await ProcessBillRunService . go ( billRun , billingPeriods )
80
+
81
+ expect ( billRunPatchStub . calledTwice ) . to . be . true ( )
82
+ expect ( billRunPatchStub . firstCall . firstArg ) . to . equal ( { status : 'processing' } )
83
+ expect ( billRunPatchStub . secondCall . firstArg ) . to . equal ( { status : 'empty' } )
84
+ } )
85
+
86
+ it ( 'logs the time taken' , async ( ) => {
87
+ await ProcessBillRunService . go ( billRun , billingPeriods )
75
88
76
- const result = await BillRunModel . query ( ) . findById ( billRun . id )
89
+ const args = notifierStub . omg . firstCall . args
77
90
78
- expect ( result . status ) . to . equal ( 'empty' )
91
+ expect ( args [ 0 ] ) . to . equal ( 'Process bill run complete' )
92
+ expect ( args [ 1 ] . timeTakenMs ) . to . exist ( )
93
+ expect ( args [ 1 ] . billRunId ) . to . equal ( billRun . id )
94
+ expect ( args [ 1 ] . type ) . to . equal ( 'supplementary' )
79
95
} )
80
96
} )
81
97
@@ -84,28 +100,27 @@ describe('Supplementary Process Bill Run service', () => {
84
100
Sinon . stub ( ProcessBillingPeriodService , 'go' ) . resolves ( true )
85
101
} )
86
102
87
- it ( 'sets the Bill Run status to processing' , async ( ) => {
88
- await SupplementaryProcessBillRunService . go ( billRun , billingPeriods )
89
-
90
- const result = await BillRunModel . query ( ) . findById ( billRun . id )
103
+ it ( 'sets the bill run status to "processing"' , async ( ) => {
104
+ await ProcessBillRunService . go ( billRun , billingPeriods )
91
105
92
- expect ( result . status ) . to . equal ( 'processing' )
106
+ expect ( billRunPatchStub . calledOnce ) . to . be . true ( )
107
+ expect ( billRunPatchStub . firstCall . firstArg ) . to . equal ( { status : 'processing' } , { skip : [ 'updatedAt' ] } )
93
108
} )
94
109
95
110
it ( 'tells the charging module API to "generate" the bill run' , async ( ) => {
96
- await SupplementaryProcessBillRunService . go ( billRun , billingPeriods )
111
+ await ProcessBillRunService . go ( billRun , billingPeriods )
97
112
98
113
expect ( chargingModuleGenerateBillRunRequestStub . called ) . to . be . true ( )
99
114
} )
100
115
101
116
it ( 'tells the legacy service to start its refresh job' , async ( ) => {
102
- await SupplementaryProcessBillRunService . go ( billRun , billingPeriods )
117
+ await ProcessBillRunService . go ( billRun , billingPeriods )
103
118
104
119
expect ( legacyRefreshBillRunRequestStub . called ) . to . be . true ( )
105
120
} )
106
121
107
- it ( 'it logs the time taken' , async ( ) => {
108
- await SupplementaryProcessBillRunService . go ( billRun , billingPeriods )
122
+ it ( 'logs the time taken' , async ( ) => {
123
+ await ProcessBillRunService . go ( billRun , billingPeriods )
109
124
110
125
const args = notifierStub . omg . firstCall . args
111
126
@@ -127,15 +142,15 @@ describe('Supplementary Process Bill Run service', () => {
127
142
} )
128
143
129
144
it ( 'calls HandleErroredBillRunService with appropriate error code' , async ( ) => {
130
- await SupplementaryProcessBillRunService . go ( billRun , billingPeriods )
145
+ await ProcessBillRunService . go ( billRun , billingPeriods )
131
146
132
147
const handlerArgs = handleErroredBillRunStub . firstCall . args
133
148
134
149
expect ( handlerArgs [ 1 ] ) . to . equal ( BillRunModel . errorCodes . failedToProcessChargeVersions )
135
150
} )
136
151
137
152
it ( 'logs the error' , async ( ) => {
138
- await SupplementaryProcessBillRunService . go ( billRun , billingPeriods )
153
+ await ProcessBillRunService . go ( billRun , billingPeriods )
139
154
140
155
const args = notifierStub . omfg . firstCall . args
141
156
@@ -158,15 +173,15 @@ describe('Supplementary Process Bill Run service', () => {
158
173
} )
159
174
160
175
it ( 'calls HandleErroredBillRunService with the error code' , async ( ) => {
161
- await SupplementaryProcessBillRunService . go ( billRun , billingPeriods )
176
+ await ProcessBillRunService . go ( billRun , billingPeriods )
162
177
163
178
const handlerArgs = handleErroredBillRunStub . firstCall . args
164
179
165
180
expect ( handlerArgs [ 1 ] ) . to . equal ( BillRunModel . errorCodes . failedToPrepareTransactions )
166
181
} )
167
182
168
183
it ( 'logs the error' , async ( ) => {
169
- await SupplementaryProcessBillRunService . go ( billRun , billingPeriods )
184
+ await ProcessBillRunService . go ( billRun , billingPeriods )
170
185
171
186
const args = notifierStub . omfg . firstCall . args
172
187
@@ -186,19 +201,19 @@ describe('Supplementary Process Bill Run service', () => {
186
201
187
202
Sinon . stub ( FetchChargeVersionsService , 'go' ) . resolves ( { chargeVersions : [ ] , licenceIdsForPeriod : [ ] } )
188
203
Sinon . stub ( ProcessBillingPeriodService , 'go' ) . resolves ( false )
189
- Sinon . stub ( UnflagUnbilledLicencesService , 'go' ) . rejects ( thrownError )
204
+ Sinon . stub ( UnflagUnbilledSupplementaryLicencesService , 'go' ) . rejects ( thrownError )
190
205
} )
191
206
192
207
it ( 'calls HandleErroredBillRunService without an error code' , async ( ) => {
193
- await SupplementaryProcessBillRunService . go ( billRun , billingPeriods )
208
+ await ProcessBillRunService . go ( billRun , billingPeriods )
194
209
195
210
const handlerArgs = handleErroredBillRunStub . firstCall . args
196
211
197
212
expect ( handlerArgs [ 1 ] ) . to . be . undefined ( )
198
213
} )
199
214
200
215
it ( 'logs the error' , async ( ) => {
201
- await SupplementaryProcessBillRunService . go ( billRun , billingPeriods )
216
+ await ProcessBillRunService . go ( billRun , billingPeriods )
202
217
203
218
const args = notifierStub . omfg . firstCall . args
204
219
0 commit comments