@@ -13,7 +13,7 @@ const errors = require('../common/errors')
1313const JobService = require ( './JobService' )
1414const ResourceBookingService = require ( './ResourceBookingService' )
1515const HttpStatus = require ( 'http-status-codes' )
16- const { Op } = require ( 'sequelize' )
16+ const { Op, where , fn , col } = require ( 'sequelize' )
1717const models = require ( '../models' )
1818const stopWords = require ( '../../data/stopWords.json' )
1919const { 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
786787roleSearchRequest . 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 */
801804async 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