Skip to content

Commit 3893a53

Browse files
author
James Cori
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents 27eb9db + e5d98c4 commit 3893a53

File tree

6 files changed

+41
-36
lines changed

6 files changed

+41
-36
lines changed

docs/swagger.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ paths:
20952095
tags:
20962096
- Attachments
20972097
description: >
2098-
Create a new attachment in the system.
2098+
Create a new attachment in the system. If you want to create multiple attachment, you can pass an array of objects instead of a single object.
20992099
security:
21002100
- bearer: []
21012101
produces:

src/common/helper.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,19 @@ function getESClient () {
639639
/**
640640
* Ensure project exist
641641
* @param {String} projectId the project id
642-
* @param {String} userToken the user token
642+
* @param {String} currentUser the user
643643
*/
644-
async function ensureProjectExist (projectId, userToken) {
644+
async function ensureProjectExist (projectId, currentUser) {
645645
let token = await getM2MToken()
646646
const url = `${config.PROJECTS_API_URL}/${projectId}`
647647
try {
648-
await axios.get(url, { headers: { Authorization: `Bearer ${token}` } })
648+
const res = await axios.get(url, { headers: { Authorization: `Bearer ${token}` } })
649+
if (currentUser.isMachine || hasAdminRole(currentUser)) {
650+
return
651+
}
652+
if (!_.find(_.get(res, 'data.members', []), m => _.toString(m.userId) === _.toString(currentUser.userId))) {
653+
throw new errors.ForbiddenError(`You don't have access to project with ID: ${projectId}`)
654+
}
649655
} catch (err) {
650656
if (_.get(err, 'response.status') === HttpStatus.NOT_FOUND) {
651657
throw new errors.BadRequestError(`Project with id: ${projectId} doesn't exist`)

src/controllers/AttachmentController.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Controller for attachment endpoints
33
*/
44
const HttpStatus = require('http-status-codes')
5+
const _ = require('lodash')
56
const service = require('../services/AttachmentService')
67

78
/**
@@ -10,7 +11,8 @@ const service = require('../services/AttachmentService')
1011
* @param {Object} res the response
1112
*/
1213
async function createAttachment (req, res) {
13-
const result = await service.createAttachment(req.authUser, req.params.challengeId, req.body)
14+
const body = _.isArray(req.body) ? req.body : [req.body]
15+
const result = await service.createAttachment(req.authUser, req.params.challengeId, body)
1416
res.status(HttpStatus.CREATED).send(result)
1517
}
1618

src/controllers/ChallengeController.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function searchChallenges (req, res) {
2424
*/
2525
async function createChallenge (req, res) {
2626
logger.debug(`createChallenge User: ${JSON.stringify(req.authUser)} - Body: ${JSON.stringify(req.body)}`)
27-
const result = await service.createChallenge(req.authUser, req.body, req.userToken)
27+
const result = await service.createChallenge(req.authUser, req.body)
2828
res.status(HttpStatus.CREATED).send(result)
2929
}
3030

@@ -45,7 +45,7 @@ async function getChallenge (req, res) {
4545
*/
4646
async function fullyUpdateChallenge (req, res) {
4747
logger.debug(`fullyUpdateChallenge User: ${JSON.stringify(req.authUser)} - ChallengeID: ${req.params.challengeId} - Body: ${JSON.stringify(req.body)}`)
48-
const result = await service.fullyUpdateChallenge(req.authUser, req.params.challengeId, req.body, req.userToken)
48+
const result = await service.fullyUpdateChallenge(req.authUser, req.params.challengeId, req.body)
4949
res.send(result)
5050
}
5151

@@ -56,7 +56,7 @@ async function fullyUpdateChallenge (req, res) {
5656
*/
5757
async function partiallyUpdateChallenge (req, res) {
5858
logger.debug(`partiallyUpdateChallenge User: ${JSON.stringify(req.authUser)} - ChallengeID: ${req.params.challengeId} - Body: ${JSON.stringify(req.body)}`)
59-
const result = await service.partiallyUpdateChallenge(req.authUser, req.params.challengeId, req.body, req.userToken)
59+
const result = await service.partiallyUpdateChallenge(req.authUser, req.params.challengeId, req.body)
6060
res.send(result)
6161
}
6262

src/services/AttachmentService.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,40 @@ async function _getChallengeAttachment (challengeId, attachmentId) {
5252
/**
5353
* Create attachment.
5454
* @param {String} challengeId the challenge id
55-
* @param {Object} attachment the attachment to created
55+
* @param {Array} attachments the attachments to be created
5656
* @returns {Object} the created attachment
5757
*/
58-
async function createAttachment (currentUser, challengeId, attachment) {
58+
async function createAttachment (currentUser, challengeId, attachments) {
5959
const challenge = await helper.getById('Challenge', challengeId)
6060
await helper.ensureUserCanModifyChallenge(currentUser, challenge)
61-
validateUrl(attachment.url)
62-
const attachmentObject = { id: uuid(), challengeId, ...attachment }
63-
const ret = await helper.create('Attachment', attachmentObject)
61+
const newAttachments = []
62+
for (const attachment of attachments) {
63+
validateUrl(attachment.url)
64+
const attachmentObject = { id: uuid(), challengeId, ...attachment }
65+
const newAttachment = await helper.create('Attachment', attachmentObject)
66+
await helper.postBusEvent(constants.Topics.ChallengeAttachmentCreated, newAttachment)
67+
newAttachments.push(newAttachment)
68+
}
6469
// update challenge object
6570
await challengeService.partiallyUpdateChallenge(currentUser, challengeId, {
6671
attachments: [
6772
..._.get(challenge, 'attachments', []),
68-
ret
73+
...newAttachments
6974
]
7075
})
7176
// post bus event
72-
await helper.postBusEvent(constants.Topics.ChallengeAttachmentCreated, ret)
73-
return ret
77+
return newAttachments
7478
}
7579

7680
createAttachment.schema = {
7781
currentUser: Joi.any(),
7882
challengeId: Joi.id(),
79-
attachment: Joi.object().keys({
83+
attachments: Joi.array().items(Joi.object().keys({
8084
name: Joi.string().required(),
8185
url: Joi.string().uri().required(),
8286
fileSize: Joi.fileSize(),
8387
description: Joi.string()
84-
}).required()
88+
})).required().min(1)
8589
}
8690

8791
/**

src/services/ChallengeService.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,9 @@ async function populatePhases (phases, startDate, timelineTemplateId) {
813813
* Create challenge.
814814
* @param {Object} currentUser the user who perform operation
815815
* @param {Object} challenge the challenge to created
816-
* @param {String} userToken the user token
817816
* @returns {Object} the created challenge
818817
*/
819-
async function createChallenge (currentUser, challenge, userToken) {
818+
async function createChallenge (currentUser, challenge) {
820819
if (!_.isUndefined(_.get(challenge, 'legacy.reviewType'))) {
821820
_.set(challenge, 'legacy.reviewType', _.toUpper(_.get(challenge, 'legacy.reviewType')))
822821
}
@@ -825,7 +824,7 @@ async function createChallenge (currentUser, challenge, userToken) {
825824
if (challenge.status === constants.challengeStatuses.Active) {
826825
throw new errors.BadRequestError('You cannot create an Active challenge. Please create a Draft challenge and then change the status to Active.')
827826
}
828-
await helper.ensureProjectExist(challenge.projectId, userToken)
827+
await helper.ensureProjectExist(challenge.projectId, currentUser)
829828
const { track, type } = await validateChallengeData(challenge)
830829
if (_.get(type, 'isTask')) {
831830
_.set(challenge, 'task.isTask', true)
@@ -1021,8 +1020,7 @@ createChallenge.schema = {
10211020
id: Joi.id(),
10221021
roleId: Joi.id()
10231022
}))
1024-
}).required(),
1025-
userToken: Joi.any()
1023+
}).required()
10261024
}
10271025

10281026
/**
@@ -1176,16 +1174,15 @@ async function validateWinners (winners, challengeId) {
11761174
* @param {Object} currentUser the user who perform operation
11771175
* @param {String} challengeId the challenge id
11781176
* @param {Object} data the challenge data to be updated
1179-
* @param {String} userToken the user token
11801177
* @param {Boolean} isFull the flag indicate it is a fully update operation.
11811178
* @returns {Object} the updated challenge
11821179
*/
1183-
async function update (currentUser, challengeId, data, userToken, isFull) {
1180+
async function update (currentUser, challengeId, data, isFull) {
11841181
if (!_.isUndefined(_.get(data, 'legacy.reviewType'))) {
11851182
_.set(data, 'legacy.reviewType', _.toUpper(_.get(data, 'legacy.reviewType')))
11861183
}
11871184
if (data.projectId) {
1188-
await helper.ensureProjectExist(data.projectId, userToken)
1185+
await helper.ensureProjectExist(data.projectId, currentUser)
11891186
}
11901187

11911188
helper.ensureNoDuplicateOrNullElements(data.tags, 'tags')
@@ -1697,11 +1694,10 @@ function sanitizeChallenge (challenge) {
16971694
* @param {Object} currentUser the user who perform operation
16981695
* @param {String} challengeId the challenge id
16991696
* @param {Object} data the challenge data to be updated
1700-
* @param {String} userToken the user token
17011697
* @returns {Object} the updated challenge
17021698
*/
1703-
async function fullyUpdateChallenge (currentUser, challengeId, data, userToken) {
1704-
return update(currentUser, challengeId, sanitizeChallenge(data), userToken, true)
1699+
async function fullyUpdateChallenge (currentUser, challengeId, data) {
1700+
return update(currentUser, challengeId, sanitizeChallenge(data), true)
17051701
}
17061702

17071703
fullyUpdateChallenge.schema = {
@@ -1785,20 +1781,18 @@ fullyUpdateChallenge.schema = {
17851781
roleId: Joi.id()
17861782
}).unknown(true)).optional().allow([]),
17871783
overview: Joi.any().forbidden()
1788-
}).unknown(true).required(),
1789-
userToken: Joi.any()
1784+
}).unknown(true).required()
17901785
}
17911786

17921787
/**
17931788
* Partially update challenge.
17941789
* @param {Object} currentUser the user who perform operation
17951790
* @param {String} challengeId the challenge id
17961791
* @param {Object} data the challenge data to be updated
1797-
* @param {String} userToken the user token
17981792
* @returns {Object} the updated challenge
17991793
*/
1800-
async function partiallyUpdateChallenge (currentUser, challengeId, data, userToken) {
1801-
return update(currentUser, challengeId, sanitizeChallenge(data), userToken)
1794+
async function partiallyUpdateChallenge (currentUser, challengeId, data) {
1795+
return update(currentUser, challengeId, sanitizeChallenge(data))
18021796
}
18031797

18041798
partiallyUpdateChallenge.schema = {
@@ -1879,8 +1873,7 @@ partiallyUpdateChallenge.schema = {
18791873
}).unknown(true)).min(1),
18801874
terms: Joi.array().items(Joi.id().optional()).optional().allow([]),
18811875
overview: Joi.any().forbidden()
1882-
}).unknown(true).required(),
1883-
userToken: Joi.any()
1876+
}).unknown(true).required()
18841877
}
18851878

18861879
/**

0 commit comments

Comments
 (0)