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

Adapting for new V5 skills api #5

Merged
merged 8 commits into from
Aug 19, 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 .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ workflows:
branches:
only:
- develop
- feature/skills-api-shapeup
# production builds are executed on "master" branch only.
- "build-prod":
context : org-global
Expand Down
6 changes: 4 additions & 2 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ module.exports = {
AUTH0_CLIENT_SECRET: process.env.AUTH0_CLIENT_SECRET,
// The ubahn api url
UBAHN_API_URL: process.env.UBAHN_API_URL || 'https://api.topcoder-dev.com/v5',
// TC BETA API url
TC_BETA_API_URL: process.env.TC_BETA_API_URL || 'https://api.topcoder-dev.com/v5.1',
// The topcoder api url
TC_API_URL: process.env.TC_API_URL || 'https://api.topcoder-dev.com',
// The skill provider name
SKILL_PROVIDER_NAME: process.env.SKILL_PROVIDER_NAME || 'Topcoder',
// The taxonomy name
TAXONOMY_NAME: process.env.TAXONOMY_NAME || 'Topcoder',
// The pause time between two create operations, default value: 1000 ms
SLEEP_TIME: parseInt(process.env.SLEEP_TIME || 1000)
}
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const helper = require('./src/common/helper')
* @param {String} userId the userId
* @param {String} tagId the tag id
* @param {Number} score the skill score
* @param {String} skillProviderId the skillProvider id
* @param {String} taxonomyId the taxonomy id
* @param {boolean} isDelete if delete the skill
* @returns {Promise}
*/
async function syncUserSkill(userId, tagId, score, skillProviderId, isDelete) {
async function syncUserSkill(userId, tagId, score, taxonomyId, isDelete) {
const name = await helper.getTagName(tagId)
const skill = await helper.getUbahnResource('skills', { skillProviderId, name })
const skill = await helper.getV5SkillResource('skills', { taxonomyId, name })
const skillExist = await helper.checkUserSkillExist(userId, skill.id)
if (isDelete && skillExist) {
helper.deleteUserSkill(userId, skill.id)
Expand All @@ -35,7 +35,7 @@ async function syncUserSkill(userId, tagId, score, skillProviderId, isDelete) {
module.exports.handle = async (event) => {
try {
console.log(`Received event: `, JSON.stringify(event))
const skillProvider = await helper.getUbahnResource('skillsProviders', { name: config.SKILL_PROVIDER_NAME })
const taxonomy = await helper.getV5SkillResource('taxonomies', { name: config.TAXONOMY_NAME })
for (const record of event.Records) {
try {
const handle = _.get(record, 'dynamodb.NewImage.userHandle.S')
Expand All @@ -53,15 +53,15 @@ module.exports.handle = async (event) => {
console.log('Skills to delete:', JSON.stringify(deleteSkills))
const user = await helper.getUser(handle)
for (const key of _.keys(createSkills)) {
await syncUserSkill(user.id, key, _.get(createSkills,`${key}.score`, 0), skillProvider.id)
await syncUserSkill(user.id, key, _.get(createSkills,`${key}.score`, 0), taxonomy.id)
await helper.sleep()
}
for (const key of _.keys(updateSkills)) {
await syncUserSkill(user.id, key, _.get(createSkills,`${key}.score`, 0), skillProvider.id)
await syncUserSkill(user.id, key, _.get(createSkills,`${key}.score`, 0), taxonomy.id)
await helper.sleep()
}
for (const key of _.keys(deleteSkills)) {
await syncUserSkill(user.id, key, 0, skillProvider.id, true)
await syncUserSkill(user.id, key, 0, taxonomy.id, true)
await helper.sleep()
}
} catch (e) {
Expand Down
23 changes: 21 additions & 2 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,26 @@ const getM2MUbahnToken = async () => {
}

/**
* Get the u-bahn record
* Get the V5 Skill/Taxonomy record
* @param {String} path the resource path
* @param {String} params the query params
* @returns {Object} the u-bahn user
*/
async function getV5SkillResource (path, params) {
const token = await getM2MUbahnToken()
const res = await axios.get(`${config.TC_BETA_API_URL}/${path}`, {
params,
headers: { Authorization: `Bearer ${token}` }
})
const result = _.head(_.get(res, 'data'))
if (!result) {
throw Error(`Cannot find u-bahn resource ${path} with params ${JSON.stringify(params)}`)
}
return result
}

/**
* Get the ubahn record
* @param {String} path the resource path
* @param {String} params the query params
* @returns {Object} the u-bahn user
Expand Down Expand Up @@ -131,7 +150,7 @@ module.exports = {
sleep,
getM2MToken,
getM2MUbahnToken,
getUbahnResource,
getV5SkillResource,
deleteUserSkill,
updateUserSkill,
createUserSkill,
Expand Down