Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Replace created/udpated with user IDs #41

Merged
merged 1 commit into from
Mar 10, 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
1 change: 1 addition & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ module.exports = {
V5_PROJECTS_API_URL: process.env.V5_PROJECTS_API_URL || 'https://api.topcoder-dev.com/v5/projects',
V5_CHALLENGE_MIGRATION_API_URL: process.env.V5_CHALLENGE_MIGRATION_API_URL || 'https://api.topcoder-dev.com/v5/challenge-migration',
V4_ES_FEEDER_API_URL: process.env.V4_ES_FEEDER_API_URL || 'https://api.topcoder-dev.com/v4/esfeeder/challenges',
MEMBER_API_URL: process.env.MEMBER_API_URL || 'https://api.topcoder-dev.com/v5/members',

V5_GROUPS_API_URL: process.env.V5_GROUPS_API_URL || 'https://api.topcoder-dev.com/v5/groups',

Expand Down
31 changes: 30 additions & 1 deletion src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,34 @@ async function forceV4ESFeeder (legacyId) {
await request.put(`${config.V4_ES_FEEDER_API_URL}`).send(body).set({ Authorization: `Bearer ${token}` })
}

/**
* Get the member ID by handle
* @param {String} handle the handle
*/
async function getMemberIdByHandle (handle) {
const m2mToken = await getM2MToken()
let memberId
try {
const res = await getRequest(`${config.MEMBER_API_URL}/${handle}`, m2mToken)
if (_.get(res, 'body.userId')) {
memberId = res.body.userId
}
// handle return from v3 API, handle and memberHandle are the same under case-insensitive condition
handle = _.get(res, 'body.handle')
} catch (error) {
// re-throw all error except 404 Not-Founded, BadRequestError should be thrown if 404 occurs
if (error.status !== 404) {
throw error
}
}

if (_.isUndefined(memberId)) {
throw new Error(`User with handle: ${handle} doesn't exist`)
}

return memberId
}

module.exports = {
getInformixConnection,
getKafkaOptions,
Expand All @@ -171,5 +199,6 @@ module.exports = {
putRequest,
postRequest,
postBusEvent,
forceV4ESFeeder
forceV4ESFeeder,
getMemberIdByHandle
}
17 changes: 11 additions & 6 deletions src/services/ProcessorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ async function processCreate (message) {
logger.debug('Parsed Payload', saveDraftContestDTO)
const challengeUuid = message.payload.id

const createdByUserId = await helper.getMemberIdByHandle(_.get(message, 'payload.createdBy'))

logger.debug('processCreate :: beforeTry')
try {
logger.info(`processCreate :: Skip Forums - ${config.V4_CHALLENGE_API_URL}?filter=skipForum=true body: ${JSON.stringify({ param: _.omit(saveDraftContestDTO, ['groupsToBeAdded', 'groupsToBeDeleted']) })}`)
Expand Down Expand Up @@ -586,7 +588,7 @@ async function processCreate (message) {
}, m2mToken)
// Repost all challenge resource on Kafka so they will get created on legacy by the legacy-challenge-resource-processor
await rePostResourcesOnKafka(challengeUuid, m2mToken)
await timelineService.enableTimelineNotifications(legacyId, _.get(message, 'payload.createdBy'))
await timelineService.enableTimelineNotifications(legacyId, createdByUserId)
logger.debug('End of processCreate')
return legacyId
} catch (e) {
Expand Down Expand Up @@ -651,14 +653,17 @@ async function processUpdate (message) {
return
}

const createdByUserId = await helper.getMemberIdByHandle(_.get(message, 'payload.createdBy'))
const updatedByUserId = await helper.getMemberIdByHandle(_.get(message, 'payload.updatedBy') || _.get(message, 'payload.createdBy'))

let legacyId = message.payload.legacyId
if (message.payload.status === constants.challengeStatuses.New) {
logger.debug(`Will skip creating on legacy as status is ${constants.challengeStatuses.New}`)
return
} else if (!legacyId) {
logger.debug('Legacy ID does not exist. Will create...')
legacyId = await processCreate(message)
await recreatePhases(legacyId, message.payload.phases, _.get(message, 'payload.updatedBy') || _.get(message, 'payload.createdBy'))
await recreatePhases(legacyId, message.payload.phases, updatedByUserId)
}
const m2mToken = await helper.getM2MToken()

Expand Down Expand Up @@ -706,7 +711,7 @@ async function processUpdate (message) {
metaValue = constants.supportedMetadata[metadataKey].method(message.payload, constants.supportedMetadata[metadataKey].defaultValue)
if (metaValue !== null && metaValue !== '') {
logger.info(`Setting ${constants.supportedMetadata[metadataKey].description} to ${metaValue}`)
await metadataService.createOrUpdateMetadata(legacyId, metadataKey, metaValue, _.get(message, 'payload.updatedBy') || _.get(message, 'payload.createdBy'))
await metadataService.createOrUpdateMetadata(legacyId, metadataKey, metaValue, updatedByUserId)
}
} catch (e) {
logger.warn(`Failed to set ${constants.supportedMetadata[metadataKey].description} to ${metaValue}`)
Expand Down Expand Up @@ -750,10 +755,10 @@ async function processUpdate (message) {
} else {
logger.info('Will skip syncing phases as the challenge is a task...')
}
await updateMemberPayments(legacyId, message.payload.prizeSets, _.get(message, 'payload.updatedBy') || _.get(message, 'payload.createdBy'))
await updateMemberPayments(legacyId, message.payload.prizeSets, updatedByUserId)
await associateChallengeGroups(saveDraftContestDTO.groupsToBeAdded, saveDraftContestDTO.groupsToBeDeleted, legacyId)
await associateChallengeTerms(message.payload.terms, legacyId, _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy') || _.get(message, 'payload.createdBy'))
await setCopilotPayment(message.payload.id, legacyId, _.get(message, 'payload.prizeSets'), _.get(message, 'payload.createdBy'), _.get(message, 'payload.updatedBy') || _.get(message, 'payload.createdBy'), m2mToken)
await associateChallengeTerms(message.payload.terms, legacyId, createdByUserId, updatedByUserId)
await setCopilotPayment(message.payload.id, legacyId, _.get(message, 'payload.prizeSets'), createdByUserId, updatedByUserId, m2mToken)

try {
await helper.forceV4ESFeeder(legacyId)
Expand Down