Skip to content

Commit 2bc9de5

Browse files
authored
Merge pull request #376 from mbaghel/roles-finalfix
Roles finalfix challenge
2 parents 0361dff + 8043bcd commit 2bc9de5

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

docs/swagger.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5249,6 +5249,9 @@ components:
52495249
jobDescription:
52505250
type: string
52515251
description: "The description of the job."
5252+
jobTitle:
5253+
type: string
5254+
description: "An optional job title."
52525255
- type: object
52535256
required:
52545257
- skills
@@ -5281,6 +5284,10 @@ components:
52815284
format: float
52825285
description: "Rate at which searched skills match the given role"
52835286
example: 0.75
5287+
jobTitle:
5288+
type: string
5289+
description: "Optional job title."
5290+
example: "Lead Application Developer"
52845291
SubmitTeamRequestBody:
52855292
properties:
52865293
teamName:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const config = require('config')
2+
3+
/**
4+
* Add jobTitle field to the RoleSearchRequest model.
5+
*/
6+
7+
module.exports = {
8+
up: async (queryInterface, Sequelize) => {
9+
await queryInterface.addColumn({ tableName: 'role_search_requests', schema: config.DB_SCHEMA_NAME }, 'job_title',
10+
{
11+
type: Sequelize.STRING(100),
12+
allowNull: true
13+
})
14+
},
15+
down: async (queryInterface, Sequelize) => {
16+
await queryInterface.removeColumn({ tableName: 'role_search_requests', schema: config.DB_SCHEMA_NAME}, 'job_title')
17+
}
18+
}

src/models/RoleSearchRequest.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ module.exports = (sequelize) => {
6262
type: Sequelize.UUID
6363
})
6464
},
65+
jobTitle: {
66+
field: 'job_title',
67+
type: Sequelize.STRING(100),
68+
allowNull: true
69+
},
6570
createdBy: {
6671
field: 'created_by',
6772
type: Sequelize.UUID,

src/services/TeamService.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const errors = require('../common/errors')
1313
const JobService = require('./JobService')
1414
const ResourceBookingService = require('./ResourceBookingService')
1515
const HttpStatus = require('http-status-codes')
16-
const { Op } = require('sequelize')
16+
const { Op, where, fn, col } = require('sequelize')
1717
const models = require('../models')
1818
const stopWords = require('../../data/stopWords.json')
1919
const { getAuditM2Muser } = require('../common/helper')
@@ -776,11 +776,12 @@ async function roleSearchRequest (currentUser, data) {
776776
}
777777
data.roleId = role.id
778778
// create roleSearchRequest entity with found roleId
779-
const { id: roleSearchRequestId } = await createRoleSearchRequest(currentUser, data)
779+
const { id: roleSearchRequestId, jobTitle } = await createRoleSearchRequest(currentUser, data)
780+
const entity = jobTitle ? { jobTitle, roleSearchRequestId } : { roleSearchRequestId };
780781
// clean Role
781782
role = await _cleanRoleDTO(currentUser, role)
782783
// return Role
783-
return _.assign(role, { roleSearchRequestId })
784+
return _.assign(role, entity)
784785
}
785786

786787
roleSearchRequest.schema = Joi.object()
@@ -789,8 +790,10 @@ roleSearchRequest.schema = Joi.object()
789790
data: Joi.object().keys({
790791
roleId: Joi.string().uuid(),
791792
jobDescription: Joi.string().max(255),
792-
skills: Joi.array().items(Joi.string().uuid().required())
793-
}).required().min(1)
793+
skills: Joi.array().items(Joi.string().uuid().required()),
794+
jobTitle: Joi.string().max(100),
795+
previousRoleSearchRequestId: Joi.string().uuid()
796+
}).required().or('roleId', 'jobDescription', 'skills')
794797
}).required()
795798

796799
/**
@@ -799,17 +802,30 @@ roleSearchRequest.schema = Joi.object()
799802
* @returns {Role} the best matching Role
800803
*/
801804
async function getRoleBySkills (skills) {
805+
// Case-insensitive search for roles matching any of the given skills
802806
const lowerCaseSkills = skills.map(skill => skill.toLowerCase())
803-
// find all roles which includes any of the given skills
804807
const queryCriteria = {
805-
where: { listOfSkills: { [Op.overlap]: lowerCaseSkills } },
808+
where: where(
809+
fn(
810+
'string_to_array',
811+
fn(
812+
'lower',
813+
fn(
814+
'array_to_string',
815+
col('list_of_skills'),
816+
','
817+
)
818+
),
819+
','
820+
),
821+
{[Op.overlap]: lowerCaseSkills }),
806822
raw: true
807823
}
808824
const roles = await Role.findAll(queryCriteria)
809825
if (roles.length > 0) {
810826
let result = _.each(roles, role => {
811-
// calculate each found roles matching rate
812-
role.skillsMatch = _.intersection(role.listOfSkills, lowerCaseSkills).length / skills.length
827+
// calculate each found roles matching rate (must again be made case-insensitive)
828+
role.skillsMatch = _.intersection(role.listOfSkills.map(skill => skill.toLowerCase()), lowerCaseSkills).length / skills.length
813829
// each role can have multiple rates, get the maximum of global rates
814830
role.maxGlobal = _.maxBy(role.rates, 'global').global
815831
})

0 commit comments

Comments
 (0)