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

Commit 0571437

Browse files
Fix issue where updating existing values was not possible, only creation of values was allowed
1 parent 48e6e6c commit 0571437

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/common/helper.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ async function createUbahnRecord (path, data) {
147147
}
148148
}
149149

150+
/**
151+
* Function to patch data to ubahn api to update ubahn record
152+
* @param {String} path api path
153+
* @param {Object} data request body
154+
* @returns {Promise} the updated record
155+
*/
156+
async function updateUBahnRecord (path, data) {
157+
const token = await getUbahnM2Mtoken()
158+
159+
logger.debug(`request PATCH ${path} with data: ${JSON.stringify(data)}`)
160+
try {
161+
const res = await axios.patch(`${config.UBAHN_API_URL}${path}`, data, { headers: { Authorization: `Bearer ${token}` } })
162+
return res.data
163+
} catch (err) {
164+
logger.error(err)
165+
throw Error(`patch ${path} with data: ${JSON.stringify(data)} failed`)
166+
}
167+
}
168+
150169
/**
151170
* Creates user in Topcoder (sso user)
152171
* @param {Object} user The user to create
@@ -226,6 +245,7 @@ module.exports = {
226245
parseExcel,
227246
getUbahnSingleRecord,
228247
createUbahnRecord,
248+
updateUBahnRecord,
229249
createUserInTopcoder,
230250
updateProcessStatus,
231251
uploadFailedRecord

src/services/ProcessorService.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,15 @@ async function createUserSkill (userId, skillProviderName, skillName, certifierI
122122
}
123123
const skillProvider = await helper.getUbahnSingleRecord('/skillsProviders', { name: skillProviderName })
124124
const skill = await helper.getUbahnSingleRecord('/skills', { skillProviderId: skillProvider.id, name: skillName })
125-
await helper.createUbahnRecord(`/users/${userId}/skills`, { certifierId, certifiedDate, metricValue, skillId: skill.id })
125+
126+
// Does the skill already exist on the user?
127+
const existingSkill = await helper.getUbahnSingleRecord(`/users/${userId}/skills/${skill.id}`, {}, true)
128+
129+
if (!existingSkill) {
130+
await helper.createUbahnRecord(`/users/${userId}/skills`, { certifierId, certifiedDate, metricValue, skillId: skill.id })
131+
} else {
132+
await helper.updateUBahnRecord(`/users/${userId}/skills/${skill.id}`, { certifierId, certifiedDate, metricValue })
133+
}
126134
}
127135

128136
/**
@@ -143,7 +151,13 @@ async function createAchievement (userId, providerName, certifierId, certifiedDa
143151
return
144152
}
145153
const achievementsProvider = await helper.getUbahnSingleRecord('/achievementsProviders', { name: providerName })
146-
await helper.createUbahnRecord(`/users/${userId}/achievements`, { certifierId, certifiedDate, name, uri, achievementsProviderId: achievementsProvider.id })
154+
const existingAchievement = await helper.getUbahnSingleRecord(`/users/${userId}/achievements/${achievementsProvider.id}`, {}, true)
155+
156+
if (!existingAchievement) {
157+
await helper.createUbahnRecord(`/users/${userId}/achievements`, { certifierId, certifiedDate, name, uri, achievementsProviderId: achievementsProvider.id })
158+
} else {
159+
await helper.updateUBahnRecord(`/users/${userId}/achievements/${achievementsProvider.id}`, { certifierId, certifiedDate, name, uri })
160+
}
147161
}
148162

149163
/**
@@ -164,7 +178,14 @@ async function createUserAttributes (userId, record) {
164178
const attributeGroup = await helper.getUbahnSingleRecord('/attributeGroups', { name: record[`attributeGroupName${i}`] })
165179
const attribute = await helper.getUbahnSingleRecord('/attributes', { attributeGroupId: attributeGroup.id, name: record[`attributeName${i}`] })
166180
const value = _.toString(record[`attributeValue${i}`])
167-
await helper.createUbahnRecord(`/users/${userId}/attributes`, { attributeId: attribute.id, value })
181+
182+
const existingAttribute = await helper.getUbahnSingleRecord(`/users/${userId}/attributes/${attribute.id}`, {}, true)
183+
184+
if (!existingAttribute) {
185+
await helper.createUbahnRecord(`/users/${userId}/attributes`, { attributeId: attribute.id, value })
186+
} else {
187+
await helper.updateUBahnRecord(`/users/${userId}/attributes/${attribute.id}`, { value })
188+
}
168189
i++
169190
}
170191
}

0 commit comments

Comments
 (0)