Skip to content

add input checks of jobId and userId to JobCandidate and ResouceBooking services #55

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
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
12 changes: 12 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
'404':
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Internal Server Error
content:
Expand Down Expand Up @@ -898,6 +904,12 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
'404':
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Internal Server Error
content:
Expand Down
40 changes: 39 additions & 1 deletion src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
const querystring = require('querystring')
const AWS = require('aws-sdk')
const config = require('config')
const HttpStatus = require('http-status-codes')
const _ = require('lodash')
const request = require('superagent')
const elasticsearch = require('@elastic/elasticsearch')
const errors = require('../common/errors')
const logger = require('./logger')
const models = require('../models')
const busApi = require('@topcoder-platform/topcoder-bus-api-wrapper')

const localLogger = {
Expand Down Expand Up @@ -529,6 +531,40 @@ async function ensureUbhanUserId (currentUser) {
}
}

/**
* Ensure job with specific id exists.
*
* @param {String} jobId the job id
* @returns {Object} the job data
*/
async function ensureJobById (jobId) {
return models.Job.findById(jobId)
}

/**
* Ensure user with specific id exists.
*
* @param {String} jobId the user id
* @returns {Object} the user data
*/
async function ensureUserById (userId) {
const token = await getM2Mtoken()
try {
const res = await request
.get(`${config.TC_API}/users/${userId}`)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
localLogger.debug({ context: 'ensureUserById', message: `response body: ${JSON.stringify(res.body)}` })
return res.body
} catch (err) {
if (err.status === HttpStatus.NOT_FOUND) {
throw new errors.NotFoundError(`id: ${userId} "user" not found`)
}
throw err
}
}

module.exports = {
checkIfExists,
autoWrapExpress,
Expand All @@ -553,5 +589,7 @@ module.exports = {
getMembers,
getProjectById,
getSkillById,
getUserSkill
getUserSkill,
ensureJobById,
ensureUserById
}
5 changes: 5 additions & 0 deletions src/services/JobCandidateService.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ getJobCandidate.schema = Joi.object().keys({
* @returns {Object} the created jobCandidate
*/
async function createJobCandidate (currentUser, jobCandidate) {
await helper.ensureJobById(jobCandidate.jobId) // ensure job exists
await helper.ensureUserById(jobCandidate.userId) // ensure user exists

jobCandidate.id = uuid()
jobCandidate.createdAt = new Date()
jobCandidate.createdBy = await helper.getUserId(currentUser.userId)
Expand Down Expand Up @@ -127,6 +130,8 @@ partiallyUpdateJobCandidate.schema = Joi.object().keys({
* @returns {Object} the updated jobCandidate
*/
async function fullyUpdateJobCandidate (currentUser, id, data) {
await helper.ensureJobById(data.jobId) // ensure job exists
await helper.ensureUserById(data.userId) // ensure user exists
return updateJobCandidate(currentUser, id, data)
}

Expand Down
9 changes: 9 additions & 0 deletions src/services/ResourceBookingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ getResourceBooking.schema = Joi.object().keys({
* @returns {Object} the created resourceBooking
*/
async function createResourceBooking (currentUser, resourceBooking) {
if (resourceBooking.jobId) {
await helper.ensureJobById(resourceBooking.jobId) // ensure job exists
}
await helper.ensureUserById(resourceBooking.userId) // ensure user exists

if (!currentUser.isBookingManager && !currentUser.isMachine) {
const connect = await helper.isConnectMember(resourceBooking.projectId, currentUser.jwtToken)
if (!connect) {
Expand Down Expand Up @@ -186,6 +191,10 @@ partiallyUpdateResourceBooking.schema = Joi.object().keys({
* @returns {Object} the updated resourceBooking
*/
async function fullyUpdateResourceBooking (currentUser, id, data) {
if (data.jobId) {
await helper.ensureJobById(data.jobId) // ensure job exists
}
await helper.ensureUserById(data.userId) // ensure user exists
return updateResourceBooking(currentUser, id, data)
}

Expand Down