@@ -1172,7 +1172,6 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
1172
1172
}
1173
1173
1174
1174
helper . ensureNoDuplicateOrNullElements ( data . tags , 'tags' )
1175
- helper . ensureNoDuplicateOrNullElements ( data . attachmentIds , 'attachmentIds' )
1176
1175
helper . ensureNoDuplicateOrNullElements ( data . groups , 'groups' )
1177
1176
// helper.ensureNoDuplicateOrNullElements(data.gitRepoURLs, 'gitRepoURLs')
1178
1177
@@ -1220,10 +1219,9 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
1220
1219
if ( data . groups ) {
1221
1220
await ensureAcessibilityToModifiedGroups ( currentUser , data , challenge )
1222
1221
}
1223
-
1224
1222
let newAttachments
1225
- if ( isFull || ! _ . isUndefined ( data . attachmentIds ) ) {
1226
- newAttachments = await helper . getByIds ( 'Attachment' , data . attachmentIds || [ ] )
1223
+ if ( isFull || ! _ . isUndefined ( data . attachments ) ) {
1224
+ newAttachments = data . attachments
1227
1225
}
1228
1226
1229
1227
await ensureAccessibleForChallenge ( currentUser , challenge )
@@ -1270,16 +1268,6 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
1270
1268
newTermsOfUse = await helper . validateChallengeTerms ( data . terms )
1271
1269
}
1272
1270
1273
- // find out attachment ids to delete
1274
- const attachmentIdsToDelete = [ ]
1275
- if ( isFull || ! _ . isUndefined ( data . attachmentIds ) ) {
1276
- _ . forEach ( challenge . attachments || [ ] , ( attachment ) => {
1277
- if ( ! _ . find ( data . attachmentIds || [ ] , ( id ) => id === attachment . id ) ) {
1278
- attachmentIdsToDelete . push ( attachment . id )
1279
- }
1280
- } )
1281
- }
1282
-
1283
1271
await validateChallengeData ( data )
1284
1272
if ( ( challenge . status === constants . challengeStatuses . Completed || challenge . status === constants . challengeStatuses . Cancelled ) && data . status && data . status !== challenge . status ) {
1285
1273
throw new errors . BadRequestError ( `Cannot change ${ challenge . status } challenge status to ${ data . status } status` )
@@ -1358,10 +1346,10 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
1358
1346
_ . intersection ( challenge [ key ] , value ) . length !== value . length ) {
1359
1347
op = '$PUT'
1360
1348
}
1361
- } else if ( key === 'attachmentIds ' ) {
1349
+ } else if ( key === 'attachments ' ) {
1362
1350
const oldIds = _ . map ( challenge . attachments || [ ] , ( a ) => a . id )
1363
1351
if ( oldIds . length !== value . length ||
1364
- _ . intersection ( oldIds , value ) . length !== value . length ) {
1352
+ _ . intersection ( oldIds , _ . map ( value , a => a . id ) ) . length !== value . length ) {
1365
1353
op = '$PUT'
1366
1354
}
1367
1355
} else if ( key === 'groups' ) {
@@ -1394,7 +1382,7 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
1394
1382
if ( _ . isUndefined ( updateDetails [ op ] ) ) {
1395
1383
updateDetails [ op ] = { }
1396
1384
}
1397
- if ( key === 'attachmentIds ' ) {
1385
+ if ( key === 'attachments ' ) {
1398
1386
updateDetails [ op ] . attachments = newAttachments
1399
1387
} else if ( key === 'terms' ) {
1400
1388
updateDetails [ op ] . terms = newTermsOfUse
@@ -1404,7 +1392,7 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
1404
1392
if ( key !== 'updated' && key !== 'updatedBy' ) {
1405
1393
let oldValue
1406
1394
let newValue
1407
- if ( key === 'attachmentIds ' ) {
1395
+ if ( key === 'attachments ' ) {
1408
1396
oldValue = challenge . attachments ? JSON . stringify ( challenge . attachments ) : 'NULL'
1409
1397
newValue = JSON . stringify ( newAttachments )
1410
1398
} else if ( key === 'terms' ) {
@@ -1445,7 +1433,7 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
1445
1433
// send null to Elasticsearch to clear the field
1446
1434
data . metadata = null
1447
1435
}
1448
- if ( isFull && _ . isUndefined ( data . attachmentIds ) && challenge . attachments ) {
1436
+ if ( isFull && _ . isUndefined ( data . attachments ) && challenge . attachments ) {
1449
1437
if ( ! updateDetails [ '$DELETE' ] ) {
1450
1438
updateDetails [ '$DELETE' ] = { }
1451
1439
}
@@ -1553,7 +1541,7 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
1553
1541
await models . AuditLog . batchPut ( auditLogs )
1554
1542
}
1555
1543
1556
- delete data . attachmentIds
1544
+ delete data . attachments
1557
1545
delete data . terms
1558
1546
_ . assign ( challenge , data )
1559
1547
if ( ! _ . isUndefined ( newAttachments ) ) {
@@ -1566,13 +1554,6 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
1566
1554
data . terms = newTermsOfUse
1567
1555
}
1568
1556
1569
- // delete unused attachments
1570
- for ( const attachmentId of attachmentIdsToDelete ) {
1571
- await helper . deleteFromS3 ( attachmentId )
1572
- const attachment = await helper . getById ( 'Attachment' , attachmentId )
1573
- await attachment . delete ( )
1574
- }
1575
-
1576
1557
if ( challenge . phases && challenge . phases . length > 0 ) {
1577
1558
await getPhasesAndPopulate ( challenge )
1578
1559
}
@@ -1641,7 +1622,7 @@ function sanitizeChallenge (challenge) {
1641
1622
'startDate' ,
1642
1623
'status' ,
1643
1624
'task' ,
1644
- 'attachmentIds ' ,
1625
+ 'attachments ' ,
1645
1626
'groups'
1646
1627
] )
1647
1628
if ( ! _ . isUndefined ( sanitized . name ) ) {
@@ -1762,7 +1743,12 @@ fullyUpdateChallenge.schema = {
1762
1743
legacyId : Joi . number ( ) . integer ( ) . positive ( ) ,
1763
1744
startDate : Joi . date ( ) ,
1764
1745
status : Joi . string ( ) . valid ( _ . values ( constants . challengeStatuses ) ) . required ( ) ,
1765
- attachmentIds : Joi . array ( ) . items ( Joi . optionalId ( ) ) ,
1746
+ attachments : Joi . array ( ) . items ( Joi . object ( ) . keys ( {
1747
+ name : Joi . string ( ) . required ( ) ,
1748
+ url : Joi . string ( ) . uri ( ) . required ( ) ,
1749
+ fileSize : Joi . fileSize ( ) ,
1750
+ description : Joi . string ( )
1751
+ } ) ) ,
1766
1752
groups : Joi . array ( ) . items ( Joi . optionalId ( ) ) ,
1767
1753
// gitRepoURLs: Joi.array().items(Joi.string().uri()),
1768
1754
winners : Joi . array ( ) . items ( Joi . object ( ) . keys ( {
@@ -1851,7 +1837,12 @@ partiallyUpdateChallenge.schema = {
1851
1837
projectId : Joi . number ( ) . integer ( ) . positive ( ) ,
1852
1838
legacyId : Joi . number ( ) . integer ( ) . positive ( ) ,
1853
1839
status : Joi . string ( ) . valid ( _ . values ( constants . challengeStatuses ) ) ,
1854
- attachmentIds : Joi . array ( ) . items ( Joi . optionalId ( ) ) ,
1840
+ attachments : Joi . array ( ) . items ( Joi . object ( ) . keys ( {
1841
+ name : Joi . string ( ) . required ( ) ,
1842
+ url : Joi . string ( ) . uri ( ) . required ( ) ,
1843
+ fileSize : Joi . fileSize ( ) ,
1844
+ description : Joi . string ( )
1845
+ } ) ) ,
1855
1846
groups : Joi . array ( ) . items ( Joi . id ( ) ) , // group names
1856
1847
// gitRepoURLs: Joi.array().items(Joi.string().uri()),
1857
1848
winners : Joi . array ( ) . items ( Joi . object ( ) . keys ( {
0 commit comments