Skip to content

Commit f1d1d09

Browse files
committed
Validate uuid for procssing db data
1 parent 6603c8c commit f1d1d09

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

src/services/InterviewService.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const Joi = require('joi')
77
const moment = require('moment')
88
const config = require('config')
99
const { Op, ForeignKeyConstraintError } = require('sequelize')
10-
const { v4: uuid } = require('uuid')
10+
const { v4: uuid, validate: uuidValidate } = require('uuid')
1111
const { Interviews: InterviewConstants } = require('../../app-constants')
1212
const helper = require('../common/helper')
1313
const logger = require('../common/logger')
@@ -170,15 +170,24 @@ async function getInterviewById (currentUser, id, fromDb = false) {
170170
}
171171
// either ES query failed or `fromDb` is set - fallback to DB
172172
logger.info({ component: 'InterviewService', context: 'getInterviewById', message: 'try to query db for data' })
173-
174-
const interview = await Interview.findOne({
175-
where: {
176-
[Op.or]: [
177-
{ id },
178-
{ xaiId: id }
179-
]
180-
}
181-
})
173+
var interview
174+
if (uuidValidate(id)) {
175+
interview = await Interview.findOne({
176+
where: {
177+
[Op.or]: [
178+
{ id }
179+
]
180+
}
181+
})
182+
} else {
183+
interview = await Interview.findOne({
184+
where: {
185+
[Op.or]: [
186+
{ xaiId: id }
187+
]
188+
}
189+
})
190+
}
182191
// throw NotFound error if doesn't exist
183192
if (!!interview !== true) {
184193
throw new errors.NotFoundError(`Interview doesn't exist with id/xaiId: ${id}`)
@@ -363,14 +372,24 @@ partiallyUpdateInterviewByRound.schema = Joi.object().keys({
363372
* @returns {Object} the patched interview object
364373
*/
365374
async function partiallyUpdateInterviewById (currentUser, id, data) {
366-
const interview = await Interview.findOne({
367-
where: {
368-
[Op.or]: [
369-
{ id },
370-
{ xaiId: id }
371-
]
372-
}
373-
})
375+
var interview
376+
if (uuidValidate(id)) {
377+
interview = await Interview.findOne({
378+
where: {
379+
[Op.or]: [
380+
{ id }
381+
]
382+
}
383+
})
384+
} else {
385+
interview = await Interview.findOne({
386+
where: {
387+
[Op.or]: [
388+
{ xaiId: id }
389+
]
390+
}
391+
})
392+
}
374393
// throw NotFound error if doesn't exist
375394
if (!!interview !== true) {
376395
throw new errors.NotFoundError(`Interview doesn't exist with id/xaiId: ${id}`)

0 commit comments

Comments
 (0)