Skip to content

Synced Topcoder roles #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2020
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
77 changes: 66 additions & 11 deletions src/services/vanilla.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,65 @@ async function manageVanillaUser (data) {
throw new Error('The group wasn\'t not found by challengeID')
}
let { body: [vanillaUser] } = await vanillaClient.getUserByName(username)

const { text: topcoderProfileResponseStr, status } = await topcoderApi.getUserDetailsByHandle(username)

const topcoderProfileResponse = JSON.parse(topcoderProfileResponseStr).result
if (status !== 200) {
throw new Error('Couldn\'t load Topcoder profile', topcoderProfileResponse.content)
}
const topcoderProfile = JSON.parse(topcoderProfileResponseStr).result.content[0]

const {text: topcoderRolesResponseStr} = await topcoderApi.getRoles(topcoderProfile.id)
const topcoderRolesResponse = JSON.parse(topcoderRolesResponseStr).result.content
const topcoderRoleNames = _.map(topcoderRolesResponse, 'roleName')

const{ body: allVanillaRoles } = await vanillaClient.getAllRoles()

// Add all missing Topcoder roles
await addTopcoderRoles(allVanillaRoles, topcoderRoleNames)

const { body: allNewVanillaRoles } = await vanillaClient.getAllRoles()

let allTopcoderRoles = _.filter(allNewVanillaRoles, { type: 'topcoder' })

let nonTopcoderRoles = _.filter(allNewVanillaRoles, role => role['type'] != 'topcoder')
let nonTopcoderRoleIDs = _.map(nonTopcoderRoles, 'roleID')

let userTopcoderRoles = _.filter(allTopcoderRoles, role => topcoderRoleNames.includes(role['name']))
let userTopcoderRoleIDs = _.map(userTopcoderRoles, 'roleID')

if (!vanillaUser) {
logger.info(`The '${username}' user wasn't found in Vanilla`)
const { text: topcoderProfileResponseStr, status } = await topcoderApi.getUserDetailsByHandle(username)

const topcoderProfileResponse = JSON.parse(topcoderProfileResponseStr).result
if (status !== 200) {
throw new Error('Couldn\'t load Topcoder profile', topcoderProfileResponse.content)
}
const topcoderProfile = JSON.parse(topcoderProfileResponseStr).result.content[0]
const { body: roles } = await vanillaClient.getAllRoles()
const defaultRoles = _.filter(roles, { type: 'member' })
const roleIDs = _.map(defaultRoles, 'roleID')
const defaultVanillaRoles = _.filter(allNewVanillaRoles, { type: 'member' })
const defaultVanillaRoleIDs = _.map(defaultVanillaRoles, 'roleID')

const userData = {
bypassSpam: true,
email: topcoderProfile.email + '2',
email: topcoderProfile.email,
emailConfirmed: true,
name: username,
password: utils.randomValue(8),
photo: null,
roleID: roleIDs
roleID: [...defaultVanillaRoleIDs, ...userTopcoderRoleIDs]
}

const { body: user } = await vanillaClient.addUser(userData)
vanillaUser = user
logger.info(`New user with UserID=${vanillaUser.userID} was added.`)
} else {
// Get a full user profile with roles
const { body: user } = await vanillaClient.getUser(vanillaUser.userID)
vanillaUser = user

// Sync Topcoder roles
const allCurrentUserRoleIDs = _.map(vanillaUser.roles, 'roleID');
const currentVanillaRoleIDs = _.intersection(allCurrentUserRoleIDs, nonTopcoderRoleIDs)
const userData = {
roleID: [...currentVanillaRoleIDs, ...userTopcoderRoleIDs]
}
await vanillaClient.updateUser(vanillaUser.userID, userData)
}

const { body: categories } = await vanillaClient.getCategoriesByParentUrlCode(challengeId)
Expand Down Expand Up @@ -83,6 +118,26 @@ async function manageVanillaUser (data) {
}
}

async function addTopcoderRoles(allVanillaRoles, topcoderRoleNames) {
const allTopcoderRoles = _.filter(allVanillaRoles, { type: 'topcoder' })
const userTopcoderRoles = _.filter(allTopcoderRoles, role => topcoderRoleNames.includes(role['name']))
const userTopcoderRoleIDs = _.map(userTopcoderRoles, 'roleID')

if(topcoderRoleNames.length != userTopcoderRoleIDs.length) {
const missingRoles = _.difference(topcoderRoleNames, _.map(userTopcoderRoles, 'name'))
logger.info('Missing roles:' + JSON.stringify(missingRoles))
for(const missingRole of missingRoles) {
await vanillaClient.createRole({
canSession: 1,
description: 'Added by Challenge Forum Processor',
name: missingRole,
type: 'topcoder'
})
logger.info(`Missing roles ${missingRole} was added`)
}
}
}

/**
* Create a vanilla forum group for a challenge.
*
Expand Down
13 changes: 12 additions & 1 deletion src/utils/topcoder-api.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,20 @@ async function updateChallenge (challengeId, data) {
return reqToAPI('PATCH', path, data)
}

/**
* Gets the roles for an user
* @param {int} userId User's ID
*/
async function getRoles (userId) {
const path = `${config.TOPCODER.API_URL}/v3/roles?filter=subjectID%3D${userId}`
return reqToAPI('GET', path)
}


module.exports = {
getUserDetailsById,
getUserDetailsByHandle,
getChallenge,
updateChallenge
updateChallenge,
getRoles
}