Skip to content

Commit 81a3112

Browse files
authored
Merge pull request #436 from yoution/issue-431
fix: Edit payments fields #431
2 parents 21ae6fa + b2a9e91 commit 81a3112

File tree

3 files changed

+92
-8
lines changed

3 files changed

+92
-8
lines changed

docs/Topcoder-bookings-api.postman_collection.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"info": {
3-
"_postman_id": "8ead1433-9679-46de-9baa-d27d59106673",
3+
"_postman_id": "8ead1433-9679-46de-9baa-d27d59106673"
44
"name": "Topcoder-bookings-api",
55
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
66
},
@@ -17231,7 +17231,7 @@
1723117231
],
1723217232
"body": {
1723317233
"mode": "raw",
17234-
"raw": "{\r\n \"status\": \"cancelled\"\r\n}",
17234+
"raw": "{\r\n \"status\": \"cancelled\",\r\n \"days\":1,\r\n \"amount\":2,\r\n \"memberRate\":1,\r\n \"customerRate\":3,\r\n \"billingAccountId\": 23\r\n}",
1723517235
"options": {
1723617236
"raw": {
1723717237
"language": "json"
@@ -17277,7 +17277,7 @@
1727717277
],
1727817278
"body": {
1727917279
"mode": "raw",
17280-
"raw": "{\r\n \"status\": \"cancelled\"\r\n}",
17280+
"raw": "{\r\n \"status\": \"cancelled\",\r\n \"days\":1,\r\n \"amount\":2,\r\n \"memberRate\":1,\r\n \"customerRate\":3,\r\n \"billingAccountId\": 23\r\n}",
1728117281
"options": {
1728217282
"raw": {
1728317283
"language": "json"
@@ -17325,7 +17325,7 @@
1732517325
],
1732617326
"body": {
1732717327
"mode": "raw",
17328-
"raw": "{\r\n \"status\": \"cancelled\"\r\n}",
17328+
"raw": "{\r\n \"status\": \"cancelled\",\r\n \"days\":1,\r\n \"amount\":2,\r\n \"memberRate\":1,\r\n \"customerRate\":3,\r\n \"billingAccountId\": 23\r\n}",
1732917329
"options": {
1733017330
"raw": {
1733117331
"language": "json"
@@ -17373,7 +17373,7 @@
1737317373
],
1737417374
"body": {
1737517375
"mode": "raw",
17376-
"raw": "{\r\n \"status\": \"cancelled\"\r\n}",
17376+
"raw": "{\r\n \"status\": \"cancelled\",\r\n \"days\":1,\r\n \"amount\":2,\r\n \"memberRate\":1,\r\n \"customerRate\":3,\r\n \"billingAccountId\": 23\r\n}",
1737717377
"options": {
1737817378
"raw": {
1737917379
"language": "json"
@@ -17421,7 +17421,7 @@
1742117421
],
1742217422
"body": {
1742317423
"mode": "raw",
17424-
"raw": "{\r\n \"status\": \"cancelled\"\r\n}",
17424+
"raw": "{\r\n \"status\": \"cancelled\",\r\n \"days\":1,\r\n \"amount\":2,\r\n \"memberRate\":1,\r\n \"customerRate\":3,\r\n \"billingAccountId\": 23\r\n}",
1742517425
"options": {
1742617426
"raw": {
1742717427
"language": "json"
@@ -17469,7 +17469,7 @@
1746917469
],
1747017470
"body": {
1747117471
"mode": "raw",
17472-
"raw": "{\r\n \"status\": \"cancelled\"\r\n}",
17472+
"raw": "{\r\n \"status\": \"cancelled\",\r\n \"days\":1,\r\n \"amount\":2,\r\n \"memberRate\":1,\r\n \"customerRate\":3,\r\n \"billingAccountId\": 23\r\n}",
1747317473
"options": {
1747417474
"raw": {
1747517475
"language": "json"

docs/swagger.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4908,6 +4908,30 @@ components:
49084908
type: string
49094909
enum: ["scheduled", "cancelled"]
49104910
description: "The payment status."
4911+
memberRate:
4912+
type: integer
4913+
format: float
4914+
example: 13
4915+
description: "The member rate."
4916+
customerRate:
4917+
type: integer
4918+
format: float
4919+
example: 13
4920+
description: "The customer rate."
4921+
billingAccountId:
4922+
type: integer
4923+
example: 80000071
4924+
description: "the billing account id for payments"
4925+
days:
4926+
type: integer
4927+
minimum: 0
4928+
example: 3
4929+
description: "The workdays to pay"
4930+
amount:
4931+
type: integer
4932+
format: float
4933+
example: 2
4934+
description: "The amount to be paid."
49114935
CheckRun:
49124936
type: object
49134937
properties:

src/services/WorkPeriodPaymentService.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,38 @@ async function _createSingleWorkPeriodPayment (workPeriodPayment, createdBy) {
4848
return _createSingleWorkPeriodPaymentWithWorkPeriodAndResourceBooking(workPeriodPayment, createdBy, correspondingWorkPeriod.toJSON(), correspondingResourceBooking.toJSON())
4949
}
5050

51+
/**
52+
* update challenge
53+
* @param {String} challengeId the challenge id
54+
* @param {Object} data the challenge update data
55+
* @returns {undefined}
56+
*/
57+
async function _updateChallenge (challengeId, data) {
58+
const body = {}
59+
if (data.billingAccountId) {
60+
body.billing = {
61+
billingAccountId: _.toString(data.billingAccountId),
62+
markup: 0 // for TaaS payments we always use 0 markup
63+
}
64+
}
65+
if (data.amount) {
66+
body.prizeSets = [{
67+
type: 'placement',
68+
prizes: [{ type: 'USD', value: data.amount }]
69+
}]
70+
}
71+
72+
if (data.billingAccountId || data.amount) {
73+
try {
74+
await helper.updateChallenge(challengeId, body)
75+
logger.debug({ component: 'WorkPeriodPaymentService', context: 'updateChallenge', message: `Challenge with id ${challengeId} is updated` })
76+
} catch (err) {
77+
logger.error({ component: 'WorkPeriodPaymentService', context: 'updateChallenge', message: err.response.text })
78+
throw new errors.BadRequestError(`Cannot update the the challenge: ${err.response.text}`)
79+
}
80+
}
81+
}
82+
5183
/**
5284
* Create single workPeriodPayment
5385
* @param {Object} workPeriodPayment the workPeriodPayment to be created
@@ -220,7 +252,16 @@ async function updateWorkPeriodPayment (currentUser, id, data) {
220252
_checkUserPermissionForCRUWorkPeriodPayment(currentUser)
221253

222254
const workPeriodPayment = await WorkPeriodPayment.findById(id)
255+
223256
const oldValue = workPeriodPayment.toJSON()
257+
258+
if (oldValue.status === 'in-progress') {
259+
const keys = _.keys(_.pick(data, ['amount', 'days', 'memberRate', 'customerRate', 'billingAccountId']))
260+
if (keys.length) {
261+
throw new errors.BadRequestError(`${JSON.stringify(keys)} cannot be updated when workPeriodPayment status is in-progress`)
262+
}
263+
}
264+
224265
if (data.status === 'cancelled' && oldValue.status === 'in-progress') {
225266
throw new errors.BadRequestError('You cannot cancel a WorkPeriodPayment which is in-progress')
226267
}
@@ -235,6 +276,20 @@ async function updateWorkPeriodPayment (currentUser, id, data) {
235276
throw new errors.BadRequestError('There is no available daysWorked to schedule a payment')
236277
}
237278
}
279+
280+
if (data.days) {
281+
const correspondingWorkPeriod = await helper.ensureWorkPeriodById(workPeriodPayment.workPeriodId) // ensure work period exists
282+
const maxPossibleDays = correspondingWorkPeriod.daysWorked - correspondingWorkPeriod.daysPaid - oldValue.days
283+
if (data.days > maxPossibleDays) {
284+
throw new errors.BadRequestError(`Days cannot be more than not paid days which is ${maxPossibleDays}`)
285+
}
286+
}
287+
288+
// challengeId exist and skip dummy challenge
289+
if (oldValue.challengeId && oldValue.challengeId !== '00000000-0000-0000-0000-000000000000') {
290+
await _updateChallenge(workPeriodPayment.challengeId, data)
291+
}
292+
238293
data.updatedBy = await helper.getUserId(currentUser.userId)
239294
const updated = await workPeriodPayment.update(data)
240295
await helper.postEvent(config.TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC, updated.toJSON(), { oldValue: oldValue, key: `workPeriodPayment.billingAccountId:${updated.billingAccountId}` })
@@ -256,7 +311,12 @@ partiallyUpdateWorkPeriodPayment.schema = Joi.object().keys({
256311
currentUser: Joi.object().required(),
257312
id: Joi.string().uuid().required(),
258313
data: Joi.object().keys({
259-
status: Joi.workPeriodPaymentUpdateStatus()
314+
status: Joi.workPeriodPaymentUpdateStatus(),
315+
amount: Joi.number().min(0),
316+
days: Joi.number().integer(),
317+
memberRate: Joi.number().positive(),
318+
customerRate: Joi.number().positive().allow(null),
319+
billingAccountId: Joi.number().positive().integer()
260320
}).min(1).required()
261321
}).required()
262322

0 commit comments

Comments
 (0)