Skip to content

Add ability to switch on/off actual payments via config #211

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
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion app-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ const ChallengeStatus = {
COMPLETED: 'Completed'
}

const PaymentProcessingSwitch = {
ON: 'ON',
OFF: 'OFF'
}

module.exports = {
UserRoles,
FullManagePermissionRoles,
Scopes,
ChallengeStatus
ChallengeStatus,
PaymentProcessingSwitch
}
3 changes: 2 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,6 @@ module.exports = {
DEFAULT_TRACK_ID: process.env.DEFAULT_TRACK_ID || '9b6fc876-f4d9-4ccb-9dfd-419247628825',

// default time zone for Work Periods
WORK_PERIOD_TIME_ZONE: process.env.WORK_PERIOD_TIME_ZONE || 'America/New_York'
WORK_PERIOD_TIME_ZONE: process.env.WORK_PERIOD_TIME_ZONE || 'America/New_York',
PAYMENT_PROCESSING_SWITCH: process.env.PAYMENT_PROCESSING_SWITCH || 'OFF'
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@
"test/unit/**"
]
}
}
}
13 changes: 13 additions & 0 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const fs = require('fs')
const Joi = require('joi')
const path = require('path')
const logger = require('./common/logger')
const constants = require('../app-constants')
const config = require('config')

Joi.page = () => Joi.number().integer().min(1).default(1)
Joi.perPage = () => Joi.number().integer().min(1).default(20)
Expand Down Expand Up @@ -36,3 +38,14 @@ function buildServices (dir) {
}

buildServices(path.join(__dirname, 'services'))

// validate some configurable parameters for the app
const paymentProcessingSwitchSchema = Joi.string().label('PAYMENT_PROCESSING_SWITCH').valid(
...Object.values(constants.PaymentProcessingSwitch)
)
try {
Joi.attempt(config.PAYMENT_PROCESSING_SWITCH, paymentProcessingSwitchSchema)
} catch (err) {
console.error(err.message)
process.exit(1)
}
3 changes: 2 additions & 1 deletion src/controllers/WorkPeriodPaymentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
const service = require('../services/WorkPeriodPaymentService')
const helper = require('../common/helper')
const config = require('config')

/**
* Get workPeriodPayment by id
Expand All @@ -19,7 +20,7 @@ async function getWorkPeriodPayment (req, res) {
* @param res the response
*/
async function createWorkPeriodPayment (req, res) {
res.send(await service.createWorkPeriodPayment(req.authUser, req.body))
res.send(await service.createWorkPeriodPayment(req.authUser, req.body, { paymentProcessingSwitch: config.PAYMENT_PROCESSING_SWITCH }))
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/services/WorkPeriodPaymentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ const moment = require('moment')
const helper = require('../common/helper')
const logger = require('../common/logger')
const errors = require('../common/errors')
const constants = require('../../app-constants')
const models = require('../models')
const { createPayment } = require('./PaymentService')
const PaymentService = require('./PaymentService')

const WorkPeriodPayment = models.WorkPeriodPayment
const esClient = helper.getESClient()
Expand Down Expand Up @@ -89,20 +90,21 @@ getWorkPeriodPayment.schema = Joi.object().keys({
* Create workPeriodPayment
* @param {Object} currentUser the user who perform this operation
* @param {Object} workPeriodPayment the workPeriodPayment to be created
* @param {Object} options the extra options to control the function
* @returns {Object} the created workPeriodPayment
*/
async function createWorkPeriodPayment (currentUser, workPeriodPayment) {
async function createWorkPeriodPayment (currentUser, workPeriodPayment, options = { paymentProcessingSwitch: 'OFF' }) {
// check permission
await _checkUserPermissionForCRUWorkPeriodPayment(currentUser)

const { projectId, userHandle, endDate } = await helper.ensureWorkPeriodById(workPeriodPayment.workPeriodId) // ensure work period exists
const paymentChallenge = await createPayment({
const paymentChallenge = options.paymentProcessingSwitch === constants.PaymentProcessingSwitch.ON ? (await PaymentService.createPayment({
projectId,
userHandle,
amount: workPeriodPayment.amount,
name: `TaaS Payment - ${userHandle} - Week Ending ${moment(endDate).format('D/M/YYYY')}`,
description: `TaaS Payment - ${userHandle} - Week Ending ${moment(endDate).format('D/M/YYYY')}`
})
})) : ({ id: '00000000-0000-0000-0000-000000000000' })
workPeriodPayment.id = uuid.v4()
workPeriodPayment.challengeId = paymentChallenge.id
workPeriodPayment.createdBy = await helper.getUserId(currentUser.userId)
Expand All @@ -128,7 +130,8 @@ createWorkPeriodPayment.schema = Joi.object().keys({
workPeriodId: Joi.string().uuid().required(),
amount: Joi.number().greater(0).allow(null),
status: Joi.workPeriodPaymentStatus().default('completed')
}).required()
}).required(),
options: Joi.object()
}).required()

/**
Expand Down
47 changes: 47 additions & 0 deletions test/unit/WorkPeriodPaymentService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable no-unused-expressions */
process.env.NODE_ENV = 'test'
require('../../src/bootstrap')

// const _ = require('lodash')
const expect = require('chai').expect
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 helper = require('../../src/common/helper')
const eventHandlers = require('../../src/eventHandlers')
// const esClient = helper.getESClient()
const busApiClient = helper.getBusApiClient()
eventHandlers.init()
describe('workPeriod service test', () => {
beforeEach(() => {
sinon.stub(busApiClient, 'postEvent').callsFake(async () => {})
})

afterEach(() => {
sinon.restore()
})

describe('create work period test', () => {
describe('when PAYMENT_PROCESSING_SWITCH is ON/OFF', async () => {
let stubCreatePaymentService

beforeEach(async () => {
sinon.stub(helper, 'ensureWorkPeriodById').callsFake(async () => testData.workPeriodPayment01.ensureWorkPeriodByIdResponse)
sinon.stub(helper, 'getUserId').callsFake(async () => {})
sinon.stub(models.WorkPeriodPayment, 'create').callsFake(() => testData.workPeriodPayment01.response)
stubCreatePaymentService = sinon.stub(paymentService, 'createPayment').callsFake(async () => testData.workPeriodPayment01.createPaymentResponse)
})

it('do not create payment if PAYMENT_PROCESSING_SWITCH is OFF', async () => {
await service.createWorkPeriodPayment(testData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'OFF' })
expect(stubCreatePaymentService.calledOnce).to.be.false
})
it('create payment if PAYMENT_PROCESSING_SWITCH is ON', async () => {
await service.createWorkPeriodPayment(testData.currentUser, testData.workPeriodPayment01.request, { paymentProcessingSwitch: 'ON' })
expect(stubCreatePaymentService.calledOnce).to.be.true
})
})
})
})
35 changes: 34 additions & 1 deletion test/unit/common/testData.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,43 @@ resourceBookingUpdate.updateResponse4.toJSON = function () {
resourceBookingUpdate.response4.update = function () {
return resourceBookingUpdate.updateResponse4
}

const workPeriodPayment01 = {
request: {
workPeriodId: '467b4df7-ced4-41b9-9710-b83808cddaf4',
amount: 600,
status: 'completed'
},
response: {
workPeriodId: '467b4df7-ced4-41b9-9710-b83808cddaf4',
amount: 600,
status: 'completed',
id: '01971e6f-0f09-4a2a-bc2e-2adac0f00622',
challengeId: '00000000-0000-0000-0000-000000000000',
createdBy: '57646ff9-1cd3-4d3c-88ba-eb09a395366c',
updatedAt: '2021-04-21T12:58:07.535Z',
createdAt: '2021-04-21T12:58:07.535Z',
updatedBy: null
},
ensureWorkPeriodByIdResponse: {
projectId: 111,
userHandle: 'pshah_manager',
endDate: '2021-03-13'
},
createPaymentResponse: {
id: 'c65f0cbf-b197-423d-91cc-db6e3bad9075'
}
}

workPeriodPayment01.response.toJSON = function () {
return workPeriodPayment01.response
}

module.exports = {
currentUser,
UserTCConnCopilot,
resourceBooking5Week,
resourceBooking1Week,
resourceBookingUpdate
resourceBookingUpdate,
workPeriodPayment01
}