diff --git a/.circleci/config.yml b/.circleci/config.yml index 69a0800..9e93d47 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 diff --git a/config/default.js b/config/default.js index 632ce30..054b30e 100644 --- a/config/default.js +++ b/config/default.js @@ -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) } diff --git a/index.js b/index.js index b0fbb8e..ae801ab 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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') @@ -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) { diff --git a/src/common/helper.js b/src/common/helper.js index c6e0b70..86ac1a5 100644 --- a/src/common/helper.js +++ b/src/common/helper.js @@ -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 @@ -131,7 +150,7 @@ module.exports = { sleep, getM2MToken, getM2MUbahnToken, - getUbahnResource, + getV5SkillResource, deleteUserSkill, updateUserSkill, createUserSkill,