@@ -13,6 +13,7 @@ const s3ParseUrl = require('../common/s3ParseUrl')
13
13
const models = require ( '../models' )
14
14
const logger = require ( '../common/logger' )
15
15
const constants = require ( '../../app-constants' )
16
+ const challengeService = require ( './ChallengeService' )
16
17
17
18
const bucketWhitelist = config . AMAZON . BUCKET_WHITELIST . split ( ',' ) . map ( ( bucketName ) => bucketName . trim ( ) )
18
19
@@ -60,6 +61,13 @@ async function createAttachment (currentUser, challengeId, attachment) {
60
61
validateUrl ( attachment . url )
61
62
const attachmentObject = { id : uuid ( ) , challengeId, ...attachment }
62
63
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
+ } )
63
71
// post bus event
64
72
await helper . postBusEvent ( constants . Topics . ChallengeAttachmentCreated , ret )
65
73
return ret
@@ -114,6 +122,16 @@ async function update (currentUser, challengeId, attachmentId, data, isFull) {
114
122
}
115
123
116
124
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
+ }
117
135
// post bus event
118
136
await helper . postBusEvent ( constants . Topics . ChallengeAttachmentUpdated ,
119
137
isFull ? ret : _ . assignIn ( { id : attachmentId } , data ) )
@@ -179,6 +197,16 @@ async function deleteAttachment (currentUser, challengeId, attachmentId) {
179
197
await helper . deleteFromS3 ( s3UrlObject . bucket , s3UrlObject . key )
180
198
}
181
199
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
+ }
182
210
// post bus event
183
211
await helper . postBusEvent ( constants . Topics . ChallengeAttachmentDeleted , attachment )
184
212
return attachment
0 commit comments