Skip to content

Support creating multiple attachments #361

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
merged 1 commit into from
Jan 6, 2021
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
2 changes: 1 addition & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2095,7 +2095,7 @@ paths:
tags:
- Attachments
description: >
Create a new attachment in the system.
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.
security:
- bearer: []
produces:
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/AttachmentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Controller for attachment endpoints
*/
const HttpStatus = require('http-status-codes')
const _ = require('lodash')
const service = require('../services/AttachmentService')

/**
Expand All @@ -10,7 +11,8 @@ const service = require('../services/AttachmentService')
* @param {Object} res the response
*/
async function createAttachment (req, res) {
const result = await service.createAttachment(req.authUser, req.params.challengeId, req.body)
const body = _.isArray(req.body) ? req.body : [req.body]
const result = await service.createAttachment(req.authUser, req.params.challengeId, body)
res.status(HttpStatus.CREATED).send(result)
}

Expand Down
24 changes: 14 additions & 10 deletions src/services/AttachmentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,40 @@ async function _getChallengeAttachment (challengeId, attachmentId) {
/**
* Create attachment.
* @param {String} challengeId the challenge id
* @param {Object} attachment the attachment to created
* @param {Array} attachments the attachments to be created
* @returns {Object} the created attachment
*/
async function createAttachment (currentUser, challengeId, attachment) {
async function createAttachment (currentUser, challengeId, attachments) {
const challenge = await helper.getById('Challenge', challengeId)
await helper.ensureUserCanModifyChallenge(currentUser, challenge)
validateUrl(attachment.url)
const attachmentObject = { id: uuid(), challengeId, ...attachment }
const ret = await helper.create('Attachment', attachmentObject)
const newAttachments = []
for (const attachment of attachments) {
validateUrl(attachment.url)
const attachmentObject = { id: uuid(), challengeId, ...attachment }
const newAttachment = await helper.create('Attachment', attachmentObject)
await helper.postBusEvent(constants.Topics.ChallengeAttachmentCreated, ret)
newAttachments.push(newAttachment)
}
// update challenge object
await challengeService.partiallyUpdateChallenge(currentUser, challengeId, {
attachments: [
..._.get(challenge, 'attachments', []),
ret
...newAttachments
]
})
// post bus event
await helper.postBusEvent(constants.Topics.ChallengeAttachmentCreated, ret)
return ret
return newAttachments
}

createAttachment.schema = {
currentUser: Joi.any(),
challengeId: Joi.id(),
attachment: Joi.object().keys({
attachments: Joi.array().items(Joi.object().keys({
name: Joi.string().required(),
url: Joi.string().uri().required(),
fileSize: Joi.fileSize(),
description: Joi.string()
}).required()
})).required().min(1)
}

/**
Expand Down