Skip to content

Commit de8d006

Browse files
Fix challenge service based on the attachment logic updates
1 parent dc63bf8 commit de8d006

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

src/services/ChallengeService.js

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,6 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
11721172
}
11731173

11741174
helper.ensureNoDuplicateOrNullElements(data.tags, 'tags')
1175-
helper.ensureNoDuplicateOrNullElements(data.attachmentIds, 'attachmentIds')
11761175
helper.ensureNoDuplicateOrNullElements(data.groups, 'groups')
11771176
// helper.ensureNoDuplicateOrNullElements(data.gitRepoURLs, 'gitRepoURLs')
11781177

@@ -1220,10 +1219,9 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
12201219
if (data.groups) {
12211220
await ensureAcessibilityToModifiedGroups(currentUser, data, challenge)
12221221
}
1223-
12241222
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
12271225
}
12281226

12291227
await ensureAccessibleForChallenge(currentUser, challenge)
@@ -1270,16 +1268,6 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
12701268
newTermsOfUse = await helper.validateChallengeTerms(data.terms)
12711269
}
12721270

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-
12831271
await validateChallengeData(data)
12841272
if ((challenge.status === constants.challengeStatuses.Completed || challenge.status === constants.challengeStatuses.Cancelled) && data.status && data.status !== challenge.status) {
12851273
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) {
13581346
_.intersection(challenge[key], value).length !== value.length) {
13591347
op = '$PUT'
13601348
}
1361-
} else if (key === 'attachmentIds') {
1349+
} else if (key === 'attachments') {
13621350
const oldIds = _.map(challenge.attachments || [], (a) => a.id)
13631351
if (oldIds.length !== value.length ||
1364-
_.intersection(oldIds, value).length !== value.length) {
1352+
_.intersection(oldIds, _.map(value, a => a.id)).length !== value.length) {
13651353
op = '$PUT'
13661354
}
13671355
} else if (key === 'groups') {
@@ -1394,7 +1382,7 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
13941382
if (_.isUndefined(updateDetails[op])) {
13951383
updateDetails[op] = {}
13961384
}
1397-
if (key === 'attachmentIds') {
1385+
if (key === 'attachments') {
13981386
updateDetails[op].attachments = newAttachments
13991387
} else if (key === 'terms') {
14001388
updateDetails[op].terms = newTermsOfUse
@@ -1404,7 +1392,7 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
14041392
if (key !== 'updated' && key !== 'updatedBy') {
14051393
let oldValue
14061394
let newValue
1407-
if (key === 'attachmentIds') {
1395+
if (key === 'attachments') {
14081396
oldValue = challenge.attachments ? JSON.stringify(challenge.attachments) : 'NULL'
14091397
newValue = JSON.stringify(newAttachments)
14101398
} else if (key === 'terms') {
@@ -1445,7 +1433,7 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
14451433
// send null to Elasticsearch to clear the field
14461434
data.metadata = null
14471435
}
1448-
if (isFull && _.isUndefined(data.attachmentIds) && challenge.attachments) {
1436+
if (isFull && _.isUndefined(data.attachments) && challenge.attachments) {
14491437
if (!updateDetails['$DELETE']) {
14501438
updateDetails['$DELETE'] = {}
14511439
}
@@ -1553,7 +1541,7 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
15531541
await models.AuditLog.batchPut(auditLogs)
15541542
}
15551543

1556-
delete data.attachmentIds
1544+
delete data.attachments
15571545
delete data.terms
15581546
_.assign(challenge, data)
15591547
if (!_.isUndefined(newAttachments)) {
@@ -1566,13 +1554,6 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
15661554
data.terms = newTermsOfUse
15671555
}
15681556

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-
15761557
if (challenge.phases && challenge.phases.length > 0) {
15771558
await getPhasesAndPopulate(challenge)
15781559
}
@@ -1641,7 +1622,7 @@ function sanitizeChallenge (challenge) {
16411622
'startDate',
16421623
'status',
16431624
'task',
1644-
'attachmentIds',
1625+
'attachments',
16451626
'groups'
16461627
])
16471628
if (!_.isUndefined(sanitized.name)) {
@@ -1762,7 +1743,12 @@ fullyUpdateChallenge.schema = {
17621743
legacyId: Joi.number().integer().positive(),
17631744
startDate: Joi.date(),
17641745
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+
})),
17661752
groups: Joi.array().items(Joi.optionalId()),
17671753
// gitRepoURLs: Joi.array().items(Joi.string().uri()),
17681754
winners: Joi.array().items(Joi.object().keys({
@@ -1851,7 +1837,12 @@ partiallyUpdateChallenge.schema = {
18511837
projectId: Joi.number().integer().positive(),
18521838
legacyId: Joi.number().integer().positive(),
18531839
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+
})),
18551846
groups: Joi.array().items(Joi.id()), // group names
18561847
// gitRepoURLs: Joi.array().items(Joi.string().uri()),
18571848
winners: Joi.array().items(Joi.object().keys({

0 commit comments

Comments
 (0)