Skip to content

Commit df59e38

Browse files
committed
add input checks of jobId and userId
1 parent 9b19efa commit df59e38

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

docs/swagger.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,12 @@ paths:
499499
application/json:
500500
schema:
501501
$ref: '#/components/schemas/Error'
502+
'404':
503+
description: Not Found
504+
content:
505+
application/json:
506+
schema:
507+
$ref: '#/components/schemas/Error'
502508
'500':
503509
description: Internal Server Error
504510
content:
@@ -898,6 +904,12 @@ paths:
898904
application/json:
899905
schema:
900906
$ref: '#/components/schemas/Error'
907+
'404':
908+
description: Not Found
909+
content:
910+
application/json:
911+
schema:
912+
$ref: '#/components/schemas/Error'
901913
'500':
902914
description: Internal Server Error
903915
content:

src/common/helper.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
const querystring = require('querystring')
66
const AWS = require('aws-sdk')
77
const config = require('config')
8+
const HttpStatus = require('http-status-codes')
89
const _ = require('lodash')
910
const request = require('superagent')
1011
const elasticsearch = require('@elastic/elasticsearch')
1112
const errors = require('../common/errors')
1213
const logger = require('./logger')
14+
const models = require('../models')
1315
const busApi = require('@topcoder-platform/topcoder-bus-api-wrapper')
1416

1517
const localLogger = {
@@ -529,6 +531,40 @@ async function ensureUbhanUserId (currentUser) {
529531
}
530532
}
531533

534+
/**
535+
* Ensure job with specific id exists.
536+
*
537+
* @param {String} jobId the job id
538+
* @returns {Object} the job data
539+
*/
540+
async function ensureJobById (jobId) {
541+
return models.Job.findById(jobId)
542+
}
543+
544+
/**
545+
* Ensure user with specific id exists.
546+
*
547+
* @param {String} jobId the user id
548+
* @returns {Object} the user data
549+
*/
550+
async function ensureUserById (userId) {
551+
const token = await getM2Mtoken()
552+
try {
553+
const res = await request
554+
.get(`${config.TC_API}/users/${userId}`)
555+
.set('Authorization', `Bearer ${token}`)
556+
.set('Content-Type', 'application/json')
557+
.set('Accept', 'application/json')
558+
localLogger.debug({ context: 'ensureUserById', message: `response body: ${JSON.stringify(res.body)}` })
559+
return res.body
560+
} catch (err) {
561+
if (err.status === HttpStatus.NOT_FOUND) {
562+
throw new errors.NotFoundError(`id: ${userId} "user" not found`)
563+
}
564+
throw err
565+
}
566+
}
567+
532568
module.exports = {
533569
checkIfExists,
534570
autoWrapExpress,
@@ -553,5 +589,7 @@ module.exports = {
553589
getMembers,
554590
getProjectById,
555591
getSkillById,
556-
getUserSkill
592+
getUserSkill,
593+
ensureJobById,
594+
ensureUserById
557595
}

src/services/JobCandidateService.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ getJobCandidate.schema = Joi.object().keys({
5454
* @returns {Object} the created jobCandidate
5555
*/
5656
async function createJobCandidate (currentUser, jobCandidate) {
57+
await helper.ensureJobById(jobCandidate.jobId) // ensure job exists
58+
await helper.ensureUserById(jobCandidate.userId) // ensure user exists
59+
5760
jobCandidate.id = uuid()
5861
jobCandidate.createdAt = new Date()
5962
jobCandidate.createdBy = await helper.getUserId(currentUser.userId)
@@ -127,6 +130,8 @@ partiallyUpdateJobCandidate.schema = Joi.object().keys({
127130
* @returns {Object} the updated jobCandidate
128131
*/
129132
async function fullyUpdateJobCandidate (currentUser, id, data) {
133+
await helper.ensureJobById(data.jobId) // ensure job exists
134+
await helper.ensureUserById(data.userId) // ensure user exists
130135
return updateJobCandidate(currentUser, id, data)
131136
}
132137

src/services/ResourceBookingService.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ getResourceBooking.schema = Joi.object().keys({
7373
* @returns {Object} the created resourceBooking
7474
*/
7575
async function createResourceBooking (currentUser, resourceBooking) {
76+
if (resourceBooking.jobId) {
77+
await helper.ensureJobById(resourceBooking.jobId) // ensure job exists
78+
}
79+
await helper.ensureUserById(resourceBooking.userId) // ensure user exists
80+
7681
if (!currentUser.isBookingManager && !currentUser.isMachine) {
7782
const connect = await helper.isConnectMember(resourceBooking.projectId, currentUser.jwtToken)
7883
if (!connect) {
@@ -186,6 +191,10 @@ partiallyUpdateResourceBooking.schema = Joi.object().keys({
186191
* @returns {Object} the updated resourceBooking
187192
*/
188193
async function fullyUpdateResourceBooking (currentUser, id, data) {
194+
if (data.jobId) {
195+
await helper.ensureJobById(data.jobId) // ensure job exists
196+
}
197+
await helper.ensureUserById(data.userId) // ensure user exists
189198
return updateResourceBooking(currentUser, id, data)
190199
}
191200

0 commit comments

Comments
 (0)