Skip to content

Commit 6114255

Browse files
author
sachin-maheshwari
authored
Merge pull request #527 from yoution/feature/shapeup4-cqrs-update
Feature/shapeup4 cqrs update
2 parents d6529d8 + e7fcf75 commit 6114255

File tree

3 files changed

+81
-14
lines changed

3 files changed

+81
-14
lines changed

src/eventHandlers/WorkPeriodPaymentEventHandler.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ const logger = require('../common/logger')
99
const helper = require('../common/helper')
1010
const { ActiveWorkPeriodPaymentStatuses } = require('../../app-constants')
1111
const WorkPeriod = models.WorkPeriod
12+
const {
13+
processUpdate: processUpdateEs
14+
} = require('../esProcessors/WorkPeriodProcessor')
1215

16+
const sequelize = models.sequelize
1317
/**
1418
* When a WorkPeriodPayment is updated or created, the workPeriod related to
1519
* that WorkPeriodPayment should be updated also.
@@ -39,8 +43,25 @@ async function updateWorkPeriod (payload) {
3943
})
4044
return
4145
}
42-
const updated = await workPeriodModel.update(data)
43-
await helper.postEvent(config.TAAS_WORK_PERIOD_UPDATE_TOPIC, _.omit(updated.toJSON(), 'payments'), { oldValue: workPeriod, key: `resourceBooking.id:${workPeriod.resourceBookingId}` })
46+
47+
const key = `resourceBooking.id:${workPeriod.resourceBookingId}`
48+
let entity
49+
try {
50+
await sequelize.transaction(async (t) => {
51+
const updated = await workPeriodModel.update(data, { transaction: t })
52+
entity = updated.toJSON()
53+
54+
entity = _.omit(entity, ['payments'])
55+
await processUpdateEs({ ...entity, key })
56+
})
57+
} catch (e) {
58+
if (entity) {
59+
helper.postErrorEvent(config.TAAS_ERROR_TOPIC, entity, 'workperiod.update')
60+
}
61+
throw e
62+
}
63+
await helper.postEvent(config.TAAS_WORK_PERIOD_UPDATE_TOPIC, entity, { oldValue: workPeriod, key })
64+
4465
logger.debug({
4566
component: 'WorkPeriodPaymentEventHandler',
4667
context: 'updateWorkPeriod',

src/services/JobService.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ async function createJob (currentUser, job, onTeamCreating) {
201201
}
202202
throw e
203203
}
204+
204205
await helper.postEvent(config.TAAS_JOB_CREATE_TOPIC, entity, { onTeamCreating })
205206
return entity
206207
}

src/services/PaymentSchedulerService.js

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ const _ = require('lodash')
22
const config = require('config')
33
const moment = require('moment')
44
const models = require('../models')
5-
const { getMemberDetailsByHandle, getChallenge, getChallengeResource, sleep, postEvent } = require('../common/helper')
5+
const { getMemberDetailsByHandle, getChallenge, getChallengeResource, sleep, postEvent, postErrorEvent } = require('../common/helper')
66
const logger = require('../common/logger')
77
const { createChallenge, addResourceToChallenge, activateChallenge, closeChallenge } = require('./PaymentService')
88
const { ChallengeStatus, PaymentSchedulerStatus, PaymentProcessingSwitch } = require('../../app-constants')
99

10+
const {
11+
processUpdate
12+
} = require('../esProcessors/WorkPeriodPaymentProcessor')
13+
14+
const sequelize = models.sequelize
1015
const WorkPeriodPayment = models.WorkPeriodPayment
1116
const WorkPeriod = models.WorkPeriod
1217
const PaymentScheduler = models.PaymentScheduler
@@ -88,9 +93,22 @@ async function processPayment (workPeriodPayment) {
8893
}
8994
} else {
9095
const oldValue = workPeriodPayment.toJSON()
91-
const updated = await workPeriodPayment.update({ status: 'in-progress' })
92-
// Update the modified status to es
93-
await postEvent(config.TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC, updated.toJSON(), { oldValue, key: `workPeriodPayment.billingAccountId:${updated.billingAccountId}` })
96+
let entity
97+
let key
98+
try {
99+
await sequelize.transaction(async (t) => {
100+
const updated = await workPeriodPayment.update({ status: 'in-progress' }, { transaction: t })
101+
key = `workPeriodPayment.billingAccountId:${updated.billingAccountId}`
102+
entity = updated.toJSON()
103+
await processUpdate({ ...entity, key })
104+
})
105+
} catch (e) {
106+
if (entity) {
107+
postErrorEvent(config.TAAS_ERROR_TOPIC, entity, 'workperiodpayment.update')
108+
}
109+
throw e
110+
}
111+
await postEvent(config.TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC, entity, { oldValue: oldValue, key })
94112
}
95113
// Check whether the number of processed records per minute exceeds the specified number, if it exceeds, wait for the next minute before processing
96114
await checkWait(PaymentSchedulerStatus.START_PROCESS)
@@ -112,11 +130,24 @@ async function processPayment (workPeriodPayment) {
112130
}
113131

114132
const oldValue = workPeriodPayment.toJSON()
115-
// 5. update wp and save it should only update already existent Work Period Payment record with created "challengeId" and "status=completed".
116-
const updated = await workPeriodPayment.update({ challengeId: paymentScheduler.challengeId, status: 'completed' })
117-
// Update the modified status to es
118-
await postEvent(config.TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC, updated.toJSON(), { oldValue, key: `workPeriodPayment.billingAccountId:${updated.billingAccountId}` })
119133

134+
let key
135+
let entity
136+
try {
137+
await sequelize.transaction(async (t) => {
138+
// 5. update wp and save it should only update already existent Work Period Payment record with created "challengeId" and "status=completed".
139+
const updated = await workPeriodPayment.update({ challengeId: paymentScheduler.challengeId, status: 'completed' }, { transaction: t })
140+
entity = updated.toJSON()
141+
await processUpdate({ ...entity, key })
142+
})
143+
} catch (e) {
144+
if (entity) {
145+
postErrorEvent(config.TAAS_ERROR_TOPIC, entity, 'workperiodpayment.update')
146+
}
147+
throw e
148+
}
149+
// Update the modified status to es
150+
await postEvent(config.TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC, entity, { oldValue: oldValue, key })
120151
await paymentScheduler.update({ step: PaymentSchedulerStatus.CLOSE_CHALLENGE, userId: paymentScheduler.userId, status: 'completed' })
121152

122153
localLogger.info(`Processed workPeriodPayment ${workPeriodPayment.id} successfully`, 'processPayment')
@@ -125,10 +156,24 @@ async function processPayment (workPeriodPayment) {
125156
logger.logFullError(err, { component: 'PaymentSchedulerService', context: 'processPayment' })
126157
const statusDetails = { errorMessage: extractErrorMessage(err), errorCode: _.get(err, 'status', -1), retry: _.get(err, 'retry', -1), step: _.get(err, 'step'), challengeId: paymentScheduler ? paymentScheduler.challengeId : null }
127158
const oldValue = workPeriodPayment.toJSON()
128-
// If payment processing failed Work Periods Payment "status" should be changed to "failed" and populate "statusDetails" field with error details in JSON format.
129-
const updated = await workPeriodPayment.update({ statusDetails, status: 'failed' })
130-
// Update the modified status to es
131-
await postEvent(config.TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC, updated.toJSON(), { oldValue, key: `workPeriodPayment.billingAccountId:${updated.billingAccountId}` })
159+
160+
let entity
161+
let key
162+
try {
163+
await sequelize.transaction(async (t) => {
164+
// If payment processing failed Work Periods Payment "status" should be changed to "failed" and populate "statusDetails" field with error details in JSON format.
165+
const updated = await workPeriodPayment.update({ statusDetails, status: 'failed' }, { transaction: t })
166+
key = `workPeriodPayment.billingAccountId:${updated.billingAccountId}`
167+
entity = updated.toJSON()
168+
await processUpdate({ ...entity, key })
169+
})
170+
} catch (e) {
171+
if (entity) {
172+
postErrorEvent(config.TAAS_ERROR_TOPIC, entity, 'workperiodpayment.update')
173+
}
174+
throw e
175+
}
176+
await postEvent(config.TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC, entity, { oldValue: oldValue, key })
132177

133178
if (paymentScheduler) {
134179
await paymentScheduler.update({ step: _.get(err, 'step'), userId: paymentScheduler.userId, status: 'failed' })

0 commit comments

Comments
 (0)