Skip to content

Commit 431157e

Browse files
authored
Merge pull request #299 from eisbilir/fix/role-issues-01
fix: Role search and create issues #290 #291 #292 #293
2 parents 1022313 + 39bd1c0 commit 431157e

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

src/common/helper.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,15 @@ esIndexPropertyMapping[config.get('esConfig.ES_INDEX_RESOURCE_BOOKING')] = {
221221
updatedBy: { type: 'keyword' }
222222
}
223223
esIndexPropertyMapping[config.get('esConfig.ES_INDEX_ROLE')] = {
224-
name: { type: 'keyword' },
224+
name: {
225+
type: 'keyword',
226+
normalizer: 'lowercaseNormalizer'
227+
},
225228
description: { type: 'keyword' },
226-
listOfSkills: { type: 'keyword' },
229+
listOfSkills: {
230+
type: 'keyword',
231+
normalizer: 'lowercaseNormalizer'
232+
},
227233
rates: {
228234
properties: {
229235
global: { type: 'integer' },
@@ -1199,6 +1205,24 @@ async function getTopcoderSkills (criteria) {
11991205
}
12001206
}
12011207

1208+
/**
1209+
* Function to search and retrive all skills from v5/skills
1210+
* - only returns skills from Topcoder Skills Provider defined by `TOPCODER_SKILL_PROVIDER_ID`
1211+
*
1212+
* @param {Object} criteria the search criteria
1213+
* @returns the request result
1214+
*/
1215+
async function getAllTopcoderSkills (criteria) {
1216+
const skills = await getTopcoderSkills(_.assign(criteria, { page: 1, perPage: 100 }))
1217+
while (skills.page * skills.perPage <= skills.total) {
1218+
const newSkills = await getTopcoderSkills(_.assign(criteria, { page: skills.page + 1, perPage: 100 }))
1219+
skills.result = [...skills.result, ...newSkills.result]
1220+
skills.page = newSkills.page
1221+
skills.total = newSkills.total
1222+
}
1223+
return skills.result
1224+
}
1225+
12021226
/**
12031227
* Function to get skill by id
12041228
* @param {String} skillId the skill Id
@@ -1745,29 +1769,27 @@ async function substituteStringByObject (string, object) {
17451769
return string
17461770
}
17471771

1748-
17491772
/**
17501773
* Get tags from tagging service
17511774
* @param {String} description The challenge description
17521775
* @returns {Array} array of tags
17531776
*/
17541777
async function getTags (description) {
1755-
const data = { text: description, extract_confidence: false}
1756-
const type = "emsi/internal_no_refresh"
1757-
const url = `${config.TC_API}/contest-tagging/${type}`;
1778+
const data = { text: description, extract_confidence: false }
1779+
const type = 'emsi/internal_no_refresh'
1780+
const url = `${config.TC_API}/contest-tagging/${type}`
17581781
const res = await request
17591782
.post(url)
17601783
.set('Accept', 'application/json')
17611784
.send(querystring.stringify(data))
17621785

17631786
localLogger.debug({
17641787
context: 'getTags',
1765-
message: `response body: ${JSON.stringify(res.body)}`,
1766-
});
1767-
return _.get(res, 'body');
1788+
message: `response body: ${JSON.stringify(res.body)}`
1789+
})
1790+
return _.get(res, 'body')
17681791
}
17691792

1770-
17711793
/**
17721794
* @param {Object} currentUser the user performing the action
17731795
* @param {Object} data title of project and any other info
@@ -1819,6 +1841,7 @@ module.exports = {
18191841
getMembers,
18201842
getProjectById,
18211843
getTopcoderSkills,
1844+
getAllTopcoderSkills,
18221845
getSkillById,
18231846
ensureJobById,
18241847
ensureResourceBookingById,

src/services/RoleService.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ async function _checkUserPermissionForWriteDeleteRole (currentUser) {
3232
* @returns {undefined}
3333
*/
3434
async function _cleanAndValidateSkillNames (skills) {
35-
// remove duplicates, leading and trailing whitespaces, remove empties and convert to lowercase.
36-
const cleanedSkills = _.uniq(_.filter(_.map(skills, skill => _.toLower(_.trim(skill))), skill => !_.isEmpty(skill)))
35+
// remove duplicates, leading and trailing whitespaces, empties.
36+
const cleanedSkills = _.uniq(_.filter(_.map(skills, skill => _.trim(skill)), skill => !_.isEmpty(skill)))
3737
if (cleanedSkills.length > 0) {
3838
// search skills if they are exists
39-
const { result } = await helper.getTopcoderSkills({ name: _.join(cleanedSkills, ',') })
39+
const result = await helper.getAllTopcoderSkills({ name: _.join(cleanedSkills, ',') })
4040
const skillNames = _.map(result, 'name')
4141
// find skills that not valid
42-
const unValidSkills = _.differenceWith(cleanedSkills, skillNames, (a, b) => _.toLower(a) === _.toLower(b))
42+
const unValidSkills = _.differenceBy(cleanedSkills, skillNames, _.toLower)
4343
if (unValidSkills.length > 0) {
4444
throw new errors.BadRequestError(`skills: "${unValidSkills}" are not valid`)
4545
}
46-
return cleanedSkills
46+
return _.intersectionBy(skillNames, cleanedSkills, _.toLower)
4747
} else {
4848
return null
4949
}
@@ -232,7 +232,7 @@ deleteRole.schema = Joi.object().keys({
232232
*/
233233
async function searchRoles (currentUser, criteria) {
234234
// clean skill names and convert into an array
235-
criteria.skillsList = _.filter(_.map(_.split(_.trim(criteria.skillsList), ','), skill => _.toLower(_.trim(skill))), skill => !_.isEmpty(skill))
235+
criteria.skillsList = _.filter(_.map(_.split(criteria.skillsList, ','), skill => _.trim(skill)), skill => !_.isEmpty(skill))
236236
try {
237237
const esQuery = {
238238
index: config.get('esConfig.ES_INDEX_ROLE'),
@@ -274,7 +274,9 @@ async function searchRoles (currentUser, criteria) {
274274
const filter = { [Op.and]: [] }
275275
// Apply skill name filters. listOfSkills array should include all skills provided in criteria.
276276
if (criteria.skillsList) {
277-
filter[Op.and].push({ listOfSkills: { [Op.contains]: criteria.skillsList } })
277+
_.each(criteria.skillsList, skill => {
278+
filter[Op.and].push(models.Sequelize.literal(`LOWER('${skill}') in (SELECT lower(x) FROM unnest("list_of_skills"::text[]) x)`))
279+
})
278280
}
279281
// Apply name filter, allow partial match and ignore case
280282
if (criteria.keyword) {

0 commit comments

Comments
 (0)