Skip to content

Commit a12f747

Browse files
committed
fix daysWorked calculation during automatic WP creation/updates
1 parent 321ce76 commit a12f747

10 files changed

+1292
-665
lines changed

migrations/2021-04-22-2-populate-work-periods-for-resource-bookings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = {
4141
user_handle: user.handle,
4242
start_date: period.startDate,
4343
end_date: period.endDate,
44-
days_worked: period.daysWorked,
44+
days_worked: null,
4545
payment_status: 'pending',
4646
created_by: config.m2m.M2M_AUDIT_USER_ID,
4747
created_at: new Date()

src/common/helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,10 +1260,10 @@ async function createChallengeResource (data, token) {
12601260
* Populates workPeriods from start and end date of resource booking
12611261
* @param {Date} start start date of the resource booking
12621262
* @param {Date} end end date of the resource booking
1263-
* @returns {Array} information about workPeriods
1263+
* @returns {Array<{startDate:Date, endDate:Date, daysWorked:number}>} information about workPeriods
12641264
*/
12651265
function extractWorkPeriods (start, end) {
1266-
// canculate daysWorked for a week
1266+
// canculate maximum possible daysWorked for a week
12671267
function getDaysWorked (week) {
12681268
if (weeks === 1) {
12691269
return Math.min(endDay, 5) - Math.max(startDay, 1) + 1

src/eventHandlers/ResourceBookingEventHandler.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ async function updateWorkPeriods (payload) {
176176
const workPeriodsToAdd = _.differenceBy(newWorkPeriods, workPeriods, 'startDate')
177177
// find which workperiods' daysWorked propery should be updated
178178
let workPeriodsToUpdate = _.intersectionBy(newWorkPeriods, workPeriods, 'startDate')
179-
workPeriodsToUpdate = _.differenceWith(workPeriodsToUpdate, workPeriods, (a, b) => b.startDate === a.startDate && b.daysWorked === a.daysWorked)
179+
// find which workperiods' daysWorked property is preset and exceeds the possible maximum
180+
workPeriodsToUpdate = _.differenceWith(workPeriodsToUpdate, workPeriods, (a, b) => b.startDate === a.startDate && _.defaultTo(b.daysWorked, a.daysWorked) <= a.daysWorked)
180181
// include id
181182
workPeriodsToUpdate = _.map(workPeriodsToUpdate, wpu => {
182183
wpu.id = _.filter(workPeriods, ['startDate', wpu.startDate])[0].id
@@ -186,7 +187,7 @@ async function updateWorkPeriods (payload) {
186187
logger.debug({
187188
component: 'ResourceBookingEventHandler',
188189
context: 'updateWorkPeriods',
189-
message: `id: ${payload.value.id} resource booking has no change in dates - ignored`
190+
message: `id: ${payload.value.id} resource booking has no change in dates that affect work periods - ignored`
190191
})
191192
return
192193
}
@@ -248,8 +249,7 @@ async function deleteWorkPeriods (payload) {
248249

249250
/**
250251
* Calls WorkPeriodService to create workPeriods
251-
* @param {Array<{startDate:Date,
252-
* endDate:Date, daysWorked:number}>} periods work period data
252+
* @param {Array<{startDate:Date, endDate:Date}>} periods work period data
253253
* @param {string} resourceBookingId resourceBookingId of work period
254254
* @returns {undefined}
255255
*/
@@ -259,7 +259,7 @@ async function _createWorkPeriods (periods, resourceBookingId) {
259259
resourceBookingId: resourceBookingId,
260260
startDate: period.startDate,
261261
endDate: period.endDate,
262-
daysWorked: period.daysWorked,
262+
daysWorked: null,
263263
paymentStatus: 'pending'
264264
})))
265265
}

src/services/PaymentService.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ async function createChallenge (challenge, token) {
7979
pureV5Task: true
8080
},
8181
tags: ['Other'],
82-
startDate: new Date(),
82+
startDate: new Date()
8383
}
8484

8585
if (challenge.billingAccountId) {
8686
body.billing = {
8787
billingAccountId: challenge.billingAccountId,
88-
markup: 0, // for TaaS payments we always use 0 markup
88+
markup: 0 // for TaaS payments we always use 0 markup
8989
}
9090
}
9191
try {

test/unit/ResourceBookingService.test.js

Lines changed: 218 additions & 104 deletions
Large diffs are not rendered by default.

test/unit/WorkPeriodPaymentService.test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const sinon = require('sinon')
66
const models = require('../../src/models')
77
const service = require('../../src/services/WorkPeriodPaymentService')
88
const paymentService = require('../../src/services/PaymentService')
9-
const testData = require('./common/testData')
9+
const commonData = require('./common/CommonData')
10+
const testData = require('./common/WorkPeriodPaymentData')
1011
const helper = require('../../src/common/helper')
1112
// const esClient = helper.getESClient()
1213
const busApiClient = helper.getBusApiClient()
@@ -35,7 +36,7 @@ describe('workPeriod service test', () => {
3536
})
3637

3738
it('create work period success', async () => {
38-
const response = await service.createWorkPeriodPayment(testData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'ON' })
39+
const response = await service.createWorkPeriodPayment(commonData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'ON' })
3940
expect(stubGetUserId.calledOnce).to.be.true
4041
expect(stubEnsureWorkPeriodById.calledOnce).to.be.true
4142
expect(stubEnsureResourceBookingById.calledOnce).to.be.true
@@ -45,7 +46,7 @@ describe('workPeriod service test', () => {
4546
})
4647

4748
it('create work period success - billingAccountId is set', async () => {
48-
await service.createWorkPeriodPayment(testData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'ON' })
49+
await service.createWorkPeriodPayment(commonData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'ON' })
4950
expect(stubCreatePayment.calledOnce).to.be.true
5051
expect(stubCreatePayment.args[0][0]).to.include({
5152
billingAccountId: testData.workPeriodPayment01.ensureResourceBookingByIdResponse.billingAccountId
@@ -61,19 +62,19 @@ describe('workPeriod service test', () => {
6162
sinon.stub(helper, 'ensureResourceBookingById').callsFake(async () => testData.workPeriodPayment01.ensureResourceBookingByIdResponse02)
6263

6364
try {
64-
await service.createWorkPeriodPayment(testData.currentUser, testData.workPeriodPayment01.request)
65+
await service.createWorkPeriodPayment(commonData.currentUser, testData.workPeriodPayment01.request)
6566
} catch (err) {
6667
expect(err.message).to.include('"ResourceBooking" Billing account is not assigned to the resource booking')
6768
}
6869
})
6970

7071
describe('when PAYMENT_PROCESSING_SWITCH is ON/OFF', async () => {
7172
it('do not create payment if PAYMENT_PROCESSING_SWITCH is OFF', async () => {
72-
await service.createWorkPeriodPayment(testData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'OFF' })
73+
await service.createWorkPeriodPayment(commonData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'OFF' })
7374
expect(stubCreatePayment.calledOnce).to.be.false
7475
})
7576
it('create payment if PAYMENT_PROCESSING_SWITCH is ON', async () => {
76-
await service.createWorkPeriodPayment(testData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'ON' })
77+
await service.createWorkPeriodPayment(commonData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'ON' })
7778
expect(stubCreatePayment.calledOnce).to.be.true
7879
})
7980
})

test/unit/common/CommonData.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const currentUser = {
2+
userId: '00000000-0000-0000-0000-000000000000',
3+
isMachine: true
4+
}
5+
const UserTCConnCopilot = {
6+
userId: '4709473d-f060-4102-87f8-4d51ff0b34c1',
7+
handle: 'TCConnCopilot'
8+
}
9+
module.exports = {
10+
currentUser,
11+
UserTCConnCopilot
12+
}

0 commit comments

Comments
 (0)