Skip to content

Commit 66a2ec2

Browse files
committed
fix: Edit payments fields #431
1 parent 3204533 commit 66a2ec2

File tree

3 files changed

+91
-8
lines changed

3 files changed

+91
-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": "0bd597ba-4bc2-4ea1-be33-45776b80c1ce",
3+
"_postman_id": "9d8bfd2b-8864-49fd-8c6e-4cff10095cc3",
44
"name": "Topcoder-bookings-api",
55
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
66
},
@@ -17279,7 +17279,7 @@
1727917279
],
1728017280
"body": {
1728117281
"mode": "raw",
17282-
"raw": "{\r\n \"status\": \"cancelled\"\r\n}",
17282+
"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}",
1728317283
"options": {
1728417284
"raw": {
1728517285
"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"
@@ -17517,7 +17517,7 @@
1751717517
],
1751817518
"body": {
1751917519
"mode": "raw",
17520-
"raw": "{\r\n \"status\": \"cancelled\"\r\n}",
17520+
"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}",
1752117521
"options": {
1752217522
"raw": {
1752317523
"language": "json"

docs/swagger.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4887,6 +4887,30 @@ components:
48874887
type: string
48884888
enum: ["scheduled", "cancelled"]
48894889
description: "The payment status."
4890+
memberRate:
4891+
type: integer
4892+
format: float
4893+
example: 13
4894+
description: "The member rate."
4895+
customerRate:
4896+
type: integer
4897+
format: float
4898+
example: 13
4899+
description: "The customer rate."
4900+
billingAccountId:
4901+
type: integer
4902+
example: 80000071
4903+
description: "the billing account id for payments"
4904+
days:
4905+
type: integer
4906+
minimum: 0
4907+
example: 3
4908+
description: "The workdays to pay"
4909+
amount:
4910+
type: integer
4911+
format: float
4912+
example: 2
4913+
description: "The amount to be paid."
48904914
CheckRun:
48914915
type: object
48924916
properties:
@@ -5873,3 +5897,4 @@ components:
58735897
message:
58745898
type: string
58755899

5900+

src/services/WorkPeriodPaymentService.js

Lines changed: 59 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+
* Create single workPeriodPayment
53+
* @param {Object} workPeriodPayment the workPeriodPayment to be created
54+
* @param {String} createdBy the authUser id
55+
* @returns {Object} the created workPeriodPayment
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
@@ -207,7 +239,15 @@ async function updateWorkPeriodPayment (currentUser, id, data) {
207239
_checkUserPermissionForCRUWorkPeriodPayment(currentUser)
208240

209241
const workPeriodPayment = await WorkPeriodPayment.findById(id)
242+
210243
const oldValue = workPeriodPayment.toJSON()
244+
245+
if (oldValue.status === 'in-progress') {
246+
_.each(_.pick(data, ['amount', 'days', 'memberRate', 'customerRate', 'billingAccountId']), (value, key) => {
247+
throw new errors.BadRequestError(`${key} cannot be updated when workPeriodPayment status is in-progress`)
248+
})
249+
}
250+
211251
if (data.status === 'cancelled' && oldValue.status === 'in-progress') {
212252
throw new errors.BadRequestError('You cannot cancel a WorkPeriodPayment which is in-progress')
213253
}
@@ -222,6 +262,19 @@ async function updateWorkPeriodPayment (currentUser, id, data) {
222262
throw new errors.BadRequestError('There is no available daysWorked to schedule a payment')
223263
}
224264
}
265+
266+
if (data.days) {
267+
const correspondingWorkPeriod = await helper.ensureWorkPeriodById(workPeriodPayment.workPeriodId) // ensure work period exists
268+
const maxPossibleDays = correspondingWorkPeriod.daysWorked - correspondingWorkPeriod.daysPaid
269+
if (data.days > maxPossibleDays) {
270+
throw new errors.BadRequestError(`Days cannot be more than not paid days which is ${maxPossibleDays}`)
271+
}
272+
}
273+
274+
if (oldValue.challengeId) {
275+
await _updateChallenge(workPeriodPayment.challengeId, data)
276+
}
277+
225278
data.updatedBy = await helper.getUserId(currentUser.userId)
226279
const updated = await workPeriodPayment.update(data)
227280
await helper.postEvent(config.TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC, updated.toJSON(), { oldValue: oldValue, key: `workPeriodPayment.billingAccountId:${updated.billingAccountId}` })
@@ -243,7 +296,12 @@ partiallyUpdateWorkPeriodPayment.schema = Joi.object().keys({
243296
currentUser: Joi.object().required(),
244297
id: Joi.string().uuid().required(),
245298
data: Joi.object().keys({
246-
status: Joi.workPeriodPaymentUpdateStatus()
299+
status: Joi.workPeriodPaymentUpdateStatus(),
300+
amount: Joi.number().min(0),
301+
days: Joi.number().integer(),
302+
memberRate: Joi.number().positive().required(),
303+
customerRate: Joi.number().positive().allow(null),
304+
billingAccountId: Joi.number().positive().integer().required()
247305
}).min(1).required()
248306
}).required()
249307

0 commit comments

Comments
 (0)