Skip to content

fix auto calculation of days worked #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
user_handle: user.handle,
start_date: period.startDate,
end_date: period.endDate,
days_worked: period.daysWorked,
days_worked: null,
payment_status: 'pending',
created_by: config.m2m.M2M_AUDIT_USER_ID,
created_at: new Date()
Expand Down
4 changes: 2 additions & 2 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1260,10 +1260,10 @@ async function createChallengeResource (data, token) {
* Populates workPeriods from start and end date of resource booking
* @param {Date} start start date of the resource booking
* @param {Date} end end date of the resource booking
* @returns {Array} information about workPeriods
* @returns {Array<{startDate:Date, endDate:Date, daysWorked:number}>} information about workPeriods
*/
function extractWorkPeriods (start, end) {
// canculate daysWorked for a week
// calculate maximum possible daysWorked for a week
function getDaysWorked (week) {
if (weeks === 1) {
return Math.min(endDay, 5) - Math.max(startDay, 1) + 1
Expand Down
10 changes: 5 additions & 5 deletions src/eventHandlers/ResourceBookingEventHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ async function updateWorkPeriods (payload) {
const workPeriodsToAdd = _.differenceBy(newWorkPeriods, workPeriods, 'startDate')
// find which workperiods' daysWorked propery should be updated
let workPeriodsToUpdate = _.intersectionBy(newWorkPeriods, workPeriods, 'startDate')
workPeriodsToUpdate = _.differenceWith(workPeriodsToUpdate, workPeriods, (a, b) => b.startDate === a.startDate && b.daysWorked === a.daysWorked)
// find which workperiods' daysWorked property is preset and exceeds the possible maximum
workPeriodsToUpdate = _.differenceWith(workPeriodsToUpdate, workPeriods, (a, b) => b.startDate === a.startDate && _.defaultTo(b.daysWorked, a.daysWorked) <= a.daysWorked)
// include id
workPeriodsToUpdate = _.map(workPeriodsToUpdate, wpu => {
wpu.id = _.filter(workPeriods, ['startDate', wpu.startDate])[0].id
Expand All @@ -186,7 +187,7 @@ async function updateWorkPeriods (payload) {
logger.debug({
component: 'ResourceBookingEventHandler',
context: 'updateWorkPeriods',
message: `id: ${payload.value.id} resource booking has no change in dates - ignored`
message: `id: ${payload.value.id} resource booking has no change in dates that affect work periods - ignored`
})
return
}
Expand Down Expand Up @@ -248,8 +249,7 @@ async function deleteWorkPeriods (payload) {

/**
* Calls WorkPeriodService to create workPeriods
* @param {Array<{startDate:Date,
* endDate:Date, daysWorked:number}>} periods work period data
* @param {Array<{startDate:Date, endDate:Date}>} periods work period data
* @param {string} resourceBookingId resourceBookingId of work period
* @returns {undefined}
*/
Expand All @@ -259,7 +259,7 @@ async function _createWorkPeriods (periods, resourceBookingId) {
resourceBookingId: resourceBookingId,
startDate: period.startDate,
endDate: period.endDate,
daysWorked: period.daysWorked,
daysWorked: null,
paymentStatus: 'pending'
})))
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/PaymentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ async function createChallenge (challenge, token) {
pureV5Task: true
},
tags: ['Other'],
startDate: new Date(),
startDate: new Date()
}

if (challenge.billingAccountId) {
body.billing = {
billingAccountId: challenge.billingAccountId,
markup: 0, // for TaaS payments we always use 0 markup
markup: 0 // for TaaS payments we always use 0 markup
}
}
try {
Expand Down
322 changes: 218 additions & 104 deletions test/unit/ResourceBookingService.test.js

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions test/unit/WorkPeriodPaymentService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const sinon = require('sinon')
const models = require('../../src/models')
const service = require('../../src/services/WorkPeriodPaymentService')
const paymentService = require('../../src/services/PaymentService')
const testData = require('./common/testData')
const commonData = require('./common/CommonData')
const testData = require('./common/WorkPeriodPaymentData')
const helper = require('../../src/common/helper')
// const esClient = helper.getESClient()
const busApiClient = helper.getBusApiClient()
Expand Down Expand Up @@ -35,7 +36,7 @@ describe('workPeriod service test', () => {
})

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

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

try {
await service.createWorkPeriodPayment(testData.currentUser, testData.workPeriodPayment01.request)
await service.createWorkPeriodPayment(commonData.currentUser, testData.workPeriodPayment01.request)
} catch (err) {
expect(err.message).to.include('"ResourceBooking" Billing account is not assigned to the resource booking')
}
})

describe('when PAYMENT_PROCESSING_SWITCH is ON/OFF', async () => {
it('do not create payment if PAYMENT_PROCESSING_SWITCH is OFF', async () => {
await service.createWorkPeriodPayment(testData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'OFF' })
await service.createWorkPeriodPayment(commonData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'OFF' })
expect(stubCreatePayment.calledOnce).to.be.false
})
it('create payment if PAYMENT_PROCESSING_SWITCH is ON', async () => {
await service.createWorkPeriodPayment(testData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'ON' })
await service.createWorkPeriodPayment(commonData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'ON' })
expect(stubCreatePayment.calledOnce).to.be.true
})
})
Expand Down
12 changes: 12 additions & 0 deletions test/unit/common/CommonData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const currentUser = {
userId: '00000000-0000-0000-0000-000000000000',
isMachine: true
}
const UserTCConnCopilot = {
userId: '4709473d-f060-4102-87f8-4d51ff0b34c1',
handle: 'TCConnCopilot'
}
module.exports = {
currentUser,
UserTCConnCopilot
}
Loading