Skip to content

Commit b4b1a2c

Browse files
Modify challenge object when an attachment gets modified
1 parent 73fa4fe commit b4b1a2c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/services/AttachmentService.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const s3ParseUrl = require('../common/s3ParseUrl')
1313
const models = require('../models')
1414
const logger = require('../common/logger')
1515
const constants = require('../../app-constants')
16+
const challengeService = require('./ChallengeService')
1617

1718
const bucketWhitelist = config.AMAZON.BUCKET_WHITELIST.split(',').map((bucketName) => bucketName.trim())
1819

@@ -60,6 +61,13 @@ async function createAttachment (currentUser, challengeId, attachment) {
6061
validateUrl(attachment.url)
6162
const attachmentObject = { id: uuid(), challengeId, ...attachment }
6263
const ret = await helper.create('Attachment', attachmentObject)
64+
// update challenge object
65+
await challengeService.partiallyUpdateChallenge(currentUser, challengeId, {
66+
attachments: [
67+
...challenge.attachments,
68+
ret
69+
]
70+
})
6371
// post bus event
6472
await helper.postBusEvent(constants.Topics.ChallengeAttachmentCreated, ret)
6573
return ret
@@ -114,6 +122,16 @@ async function update (currentUser, challengeId, attachmentId, data, isFull) {
114122
}
115123

116124
const ret = await helper.update(attachment, data)
125+
// update challenge object
126+
const newAttachments = challenge.attachments
127+
try {
128+
newAttachments[_.findIndex(newAttachments, a => a.id === attachmentId)] = ret
129+
await challengeService.partiallyUpdateChallenge(currentUser, challengeId, {
130+
attachments: newAttachments
131+
})
132+
} catch (e) {
133+
logger.warn(`The attachment ${attachmentId} does not exist on the challenge object`)
134+
}
117135
// post bus event
118136
await helper.postBusEvent(constants.Topics.ChallengeAttachmentUpdated,
119137
isFull ? ret : _.assignIn({ id: attachmentId }, data))
@@ -179,6 +197,16 @@ async function deleteAttachment (currentUser, challengeId, attachmentId) {
179197
await helper.deleteFromS3(s3UrlObject.bucket, s3UrlObject.key)
180198
}
181199
await attachment.delete()
200+
// update challenge object
201+
const newAttachments = challenge.attachments
202+
try {
203+
newAttachments.splice(_.findIndex(newAttachments, a => a.id === attachmentId), 1)
204+
await challengeService.partiallyUpdateChallenge(currentUser, challengeId, {
205+
attachments: newAttachments
206+
})
207+
} catch (e) {
208+
logger.warn(`The attachment ${attachmentId} does not exist on the challenge object`)
209+
}
182210
// post bus event
183211
await helper.postBusEvent(constants.Topics.ChallengeAttachmentDeleted, attachment)
184212
return attachment

0 commit comments

Comments
 (0)