From 015a796f63ab48c7c1ffcaa4d29729c0568f4eaa Mon Sep 17 00:00:00 2001 From: eisbilir Date: Wed, 16 Jun 2021 03:12:41 +0300 Subject: [PATCH] fix: project creation & missing job data --- src/common/helper.js | 6 ++--- src/services/TeamService.js | 44 ++++++++++++++++++------------------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/common/helper.js b/src/common/helper.js index 45e4afe4..4b536f10 100644 --- a/src/common/helper.js +++ b/src/common/helper.js @@ -1867,11 +1867,11 @@ async function getTags (description) { * @param {Object} data title of project and any other info * @returns {Object} the project created */ -async function createProject (data) { - const token = await getM2MToken() +async function createProject (currentUser, data) { + const token = currentUser.jwtToken const res = await request .post(`${config.TC_API}/projects/`) - .set('Authorization', `Bearer ${token}`) + .set('Authorization', token) .set('Content-Type', 'application/json') .set('Accept', 'application/json') .send(data) diff --git a/src/services/TeamService.js b/src/services/TeamService.js index 566262a3..02135f64 100644 --- a/src/services/TeamService.js +++ b/src/services/TeamService.js @@ -918,7 +918,6 @@ async function getSkillIdsByNames (skills) { // endpoint returns the partial matched skills // we need to filter by exact match case insensitive const filteredSkills = _.filter(result, tcSkill => _.some(skills, skill => _.toLower(skill) === _.toLower(tcSkill.name))) - console.log(filteredSkills) const skillIds = _.map(filteredSkills, 'id') return skillIds } @@ -1014,19 +1013,13 @@ async function createTeam (currentUser, data) { const projectRequestBody = { name: data.teamName, description: data.teamDescription, - type: 'app_dev', + type: 'talent-as-a-service', details: { positions: data.positions } } // create project with given data - const project = await helper.createProject(projectRequestBody) - // we created the project with m2m token - // so we have to add the current user as a member to the project - // the role of the user in the project will be determined by user's current roles. - if (!currentUser.isMachine) { - await helper.createProjectMember(project.id, { userId: currentUser.userId }) - } + const project = await helper.createProject(currentUser, projectRequestBody) // create jobs for the given positions. await Promise.all(_.map(data.positions, async position => { const roleSearchRequest = roleSearchRequests[position.roleSearchRequestId] @@ -1034,11 +1027,12 @@ async function createTeam (currentUser, data) { projectId: project.id, title: position.roleName, numPositions: position.numberOfResources, - rateType: 'weekly', - skills: roleSearchRequest.skills - } - if (roleSearchRequest.jobDescription) { - job.description = roleSearchRequest.jobDescription + rateType: position.rateType, + workload: position.workload, + skills: roleSearchRequest.skills, + description: roleSearchRequest.jobDescription, + roleIds: [roleSearchRequest.roleId], + resourceType: roleSearchRequest.resourceType } if (position.startMonth) { job.startDate = position.startMonth @@ -1046,9 +1040,6 @@ async function createTeam (currentUser, data) { if (position.durationWeeks) { job.duration = position.durationWeeks } - if (roleSearchRequest.roleId) { - job.roleIds = [roleSearchRequest.roleId] - } await JobService.createJob(currentUser, job) })) return { projectId: project.id } @@ -1066,7 +1057,10 @@ createTeam.schema = Joi.object() roleSearchRequestId: Joi.string().uuid().required(), numberOfResources: Joi.number().integer().min(1).required(), durationWeeks: Joi.number().integer().min(1), - startMonth: Joi.date() + startMonth: Joi.date(), + rateType: Joi.rateType().default('weekly'), + workload: Joi.workload().default('full-time'), + resourceType: Joi.string() }).required() ).required() }).required() @@ -1084,19 +1078,23 @@ async function _validateRoleSearchRequests (roleSearchRequestIds) { const roleSearchRequest = await RoleSearchRequest.findById(roleSearchRequestId) // store the found roleSearchRequest to avoid unnecessary DB calls roleSearchRequests[roleSearchRequestId] = roleSearchRequest.toJSON() - // we can't create a job without skills - if (!roleSearchRequest.roleId && !roleSearchRequest.skills) { - throw new errors.ConflictError(`roleSearchRequestId: ${roleSearchRequestId} must have roleId or skills`) + // we can't create a job without a role + if (!roleSearchRequest.roleId) { + throw new errors.ConflictError(`roleSearchRequestId: ${roleSearchRequestId} must have roleId`) } + const role = await Role.findById(roleSearchRequest.roleId) // if roleSearchRequest doesn't have skills, we have to get skills through role if (!roleSearchRequest.skills) { - const role = await Role.findById(roleSearchRequest.roleId) if (!role.listOfSkills) { throw new errors.ConflictError(`role: ${role.id} must have skills`) } - // store the found skills + // store role's skills roleSearchRequests[roleSearchRequestId].skills = await getSkillIdsByNames(role.listOfSkills) } + if (!roleSearchRequest.jobDescription) { + roleSearchRequests[roleSearchRequestId].jobDescription = role.description + } + roleSearchRequests[roleSearchRequestId].resourceType = role.name })) return roleSearchRequests }