Skip to content

Commit 6e2b6ba

Browse files
author
James Cori
committed
Merge branch 'develop'
2 parents ddf213e + dc73975 commit 6e2b6ba

File tree

6 files changed

+90
-86
lines changed

6 files changed

+90
-86
lines changed

Diff for: docs/swagger.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,11 @@ paths:
17361736
description: Filter by timeline template id
17371737
required: false
17381738
type: string
1739+
- name: isDefault
1740+
in: query
1741+
description: Filter by is default flag
1742+
required: false
1743+
type: boolean
17391744
responses:
17401745
'200':
17411746
description: OK
@@ -2580,6 +2585,9 @@ definitions:
25802585
timelineTemplateId:
25812586
type: string
25822587
format: UUID
2588+
isDefault:
2589+
type: boolean
2590+
default: false
25832591
required:
25842592
- typeId
25852593
- trackId

Diff for: src/controllers/ChallengeController.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const HttpStatus = require('http-status-codes')
55
const service = require('../services/ChallengeService')
66
const helper = require('../common/helper')
7+
const logger = require('../common/logger')
78

89
/**
910
* Search challenges
@@ -22,6 +23,7 @@ async function searchChallenges (req, res) {
2223
* @param {Object} res the response
2324
*/
2425
async function createChallenge (req, res) {
26+
logger.debug(`createChallenge User: ${JSON.stringify(req.authUser)} - Body: ${JSON.stringify(req.body)}`)
2527
const result = await service.createChallenge(req.authUser, req.body, req.userToken)
2628
res.status(HttpStatus.CREATED).send(result)
2729
}
@@ -42,6 +44,7 @@ async function getChallenge (req, res) {
4244
* @param {Object} res the response
4345
*/
4446
async function fullyUpdateChallenge (req, res) {
47+
logger.debug(`fullyUpdateChallenge User: ${JSON.stringify(req.authUser)} - ChallengeID: ${req.params.challengeId} - Body: ${JSON.stringify(req.body)}`)
4548
const result = await service.fullyUpdateChallenge(req.authUser, req.params.challengeId, req.body, req.userToken)
4649
res.send(result)
4750
}
@@ -52,6 +55,7 @@ async function fullyUpdateChallenge (req, res) {
5255
* @param {Object} res the response
5356
*/
5457
async function partiallyUpdateChallenge (req, res) {
58+
logger.debug(`partiallyUpdateChallenge User: ${JSON.stringify(req.authUser)} - ChallengeID: ${req.params.challengeId} - Body: ${JSON.stringify(req.body)}`)
5559
const result = await service.partiallyUpdateChallenge(req.authUser, req.params.challengeId, req.body, req.userToken)
5660
res.send(result)
5761
}

Diff for: src/models/ChallengeTimelineTemplate.js

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ const schema = new Schema({
2323
timelineTemplateId: {
2424
type: String,
2525
required: true
26+
},
27+
isDefault: {
28+
type: Boolean,
29+
required: false
2630
}
2731
},
2832
{

Diff for: src/scripts/seed/ChallengeTimelineTemplate.json

+32-16
Original file line numberDiff line numberDiff line change
@@ -3,96 +3,112 @@
33
"id": "6a29fccb-81ab-49f8-905d-0db0e840881a",
44
"trackId": "5fa04185-041f-49a6-bfd1-fe82533cd6c8",
55
"typeId": "927abff4-7af9-4145-8ba1-577c16e64e2e",
6-
"timelineTemplateId": "7ebf1c69-f62f-4d3a-bdfb-fe9ddb56861c"
6+
"timelineTemplateId": "7ebf1c69-f62f-4d3a-bdfb-fe9ddb56861c",
7+
"isDefault": false
78
},
89
{
910
"id": "edd528d7-b2d6-48e4-80ab-cfccc3cfb6cb",
1011
"trackId": "5fa04185-041f-49a6-bfd1-fe82533cd6c8",
1112
"typeId": "927abff4-7af9-4145-8ba1-577c16e64e2e",
12-
"timelineTemplateId": "d4201ca4-8437-4d63-9957-3f7708184b07"
13+
"timelineTemplateId": "d4201ca4-8437-4d63-9957-3f7708184b07",
14+
"isDefault": true
1315
},
1416
{
1517
"id": "4a59472b-0743-4dae-9739-1f5b327e1255",
1618
"trackId": "5fa04185-041f-49a6-bfd1-fe82533cd6c8",
1719
"typeId": "927abff4-7af9-4145-8ba1-577c16e64e2e",
18-
"timelineTemplateId": "2d0807fa-ece1-4328-a260-76f5f6b559e0"
20+
"timelineTemplateId": "2d0807fa-ece1-4328-a260-76f5f6b559e0",
21+
"isDefault": false
1922
},
2023
{
2124
"id": "fccd933a-34c3-497f-850e-00d6f499e703",
2225
"trackId": "5fa04185-041f-49a6-bfd1-fe82533cd6c8",
2326
"typeId": "dc876fa4-ef2d-4eee-b701-b555fcc6544c",
24-
"timelineTemplateId": "0a0fed34-cb5a-47f5-b0cb-6e2ee7de8dcb"
27+
"timelineTemplateId": "0a0fed34-cb5a-47f5-b0cb-6e2ee7de8dcb",
28+
"isDefault": true
2529
},
2630
{
2731
"id": "26de5868-f405-46d6-bf82-f3966b2a382d",
2832
"trackId": "5fa04185-041f-49a6-bfd1-fe82533cd6c8",
2933
"typeId": "ecd58c69-238f-43a4-a4bb-d172719b9f31",
30-
"timelineTemplateId": "53a307ce-b4b3-4d6f-b9a1-3741a58f77e6"
34+
"timelineTemplateId": "53a307ce-b4b3-4d6f-b9a1-3741a58f77e6",
35+
"isDefault": true
3136
},
3237
{
3338
"id": "4df06ca4-367f-4768-93b4-94ba18625983",
3439
"trackId": "9b6fc876-f4d9-4ccb-9dfd-419247628825",
3540
"typeId": "927abff4-7af9-4145-8ba1-577c16e64e2e",
36-
"timelineTemplateId": "7ebf1c69-f62f-4d3a-bdfb-fe9ddb56861c"
41+
"timelineTemplateId": "7ebf1c69-f62f-4d3a-bdfb-fe9ddb56861c",
42+
"isDefault": true
3743
},
3844
{
3945
"id": "140b24d5-5259-4449-bc1f-3780f3b01555",
4046
"trackId": "9b6fc876-f4d9-4ccb-9dfd-419247628825",
4147
"typeId": "dc876fa4-ef2d-4eee-b701-b555fcc6544c",
42-
"timelineTemplateId": "0a0fed34-cb5a-47f5-b0cb-6e2ee7de8dcb"
48+
"timelineTemplateId": "0a0fed34-cb5a-47f5-b0cb-6e2ee7de8dcb",
49+
"isDefault": true
4350
},
4451
{
4552
"id": "3cd4e42f-141f-4118-950c-fb815569fb34",
4653
"trackId": "9b6fc876-f4d9-4ccb-9dfd-419247628825",
4754
"typeId": "ecd58c69-238f-43a4-a4bb-d172719b9f31",
48-
"timelineTemplateId": "53a307ce-b4b3-4d6f-b9a1-3741a58f77e6"
55+
"timelineTemplateId": "53a307ce-b4b3-4d6f-b9a1-3741a58f77e6",
56+
"isDefault": true
4957
},
5058
{
5159
"id": "87ab69d5-09ec-45ef-b3d1-f6f6f1fdf2f3",
5260
"trackId": "c0f5d461-8219-4c14-878a-c3a3f356466d",
5361
"typeId": "927abff4-7af9-4145-8ba1-577c16e64e2e",
54-
"timelineTemplateId": "7ebf1c69-f62f-4d3a-bdfb-fe9ddb56861c"
62+
"timelineTemplateId": "7ebf1c69-f62f-4d3a-bdfb-fe9ddb56861c",
63+
"isDefault": true
5564
},
5665
{
5766
"id": "9e4da450-d29a-44e6-aa80-26d9d5798637",
5867
"trackId": "c0f5d461-8219-4c14-878a-c3a3f356466d",
5968
"typeId": "927abff4-7af9-4145-8ba1-577c16e64e2e",
60-
"timelineTemplateId": "6969125a-a12f-4b89-8de6-e66b0056f36b"
69+
"timelineTemplateId": "6969125a-a12f-4b89-8de6-e66b0056f36b",
70+
"isDefault": false
6171
},
6272
{
6373
"id": "92207659-e2e3-4578-85d5-02a36f384907",
6474
"trackId": "c0f5d461-8219-4c14-878a-c3a3f356466d",
6575
"typeId": "dc876fa4-ef2d-4eee-b701-b555fcc6544c",
66-
"timelineTemplateId": "0a0fed34-cb5a-47f5-b0cb-6e2ee7de8dcb"
76+
"timelineTemplateId": "0a0fed34-cb5a-47f5-b0cb-6e2ee7de8dcb",
77+
"isDefault": true
6778
},
6879
{
6980
"id": "ab235f79-6606-41b2-afaf-4956824fcbcc",
7081
"trackId": "c0f5d461-8219-4c14-878a-c3a3f356466d",
7182
"typeId": "ecd58c69-238f-43a4-a4bb-d172719b9f31",
72-
"timelineTemplateId": "53a307ce-b4b3-4d6f-b9a1-3741a58f77e6"
83+
"timelineTemplateId": "53a307ce-b4b3-4d6f-b9a1-3741a58f77e6",
84+
"isDefault": true
7385
},
7486
{
7587
"id": "1be20f5f-9356-49cf-b3df-a7be8099b5d9",
7688
"trackId": "36e6a8d0-7e1e-4608-a673-64279d99c115",
7789
"typeId": "927abff4-7af9-4145-8ba1-577c16e64e2e",
78-
"timelineTemplateId": "7ebf1c69-f62f-4d3a-bdfb-fe9ddb56861c"
90+
"timelineTemplateId": "7ebf1c69-f62f-4d3a-bdfb-fe9ddb56861c",
91+
"isDefault": true
7992
},
8093
{
8194
"id": "71b5e844-9847-43b0-aa66-9bcc620704b3",
8295
"trackId": "36e6a8d0-7e1e-4608-a673-64279d99c115",
8396
"typeId": "927abff4-7af9-4145-8ba1-577c16e64e2e",
84-
"timelineTemplateId": "f1bcb2c7-3ee4-4fb5-8d0b-efe52c015963"
97+
"timelineTemplateId": "f1bcb2c7-3ee4-4fb5-8d0b-efe52c015963",
98+
"isDefault": false
8599
},
86100
{
87101
"id": "00660d04-1e84-467b-a450-281cb4ec4137",
88102
"trackId": "36e6a8d0-7e1e-4608-a673-64279d99c115",
89103
"typeId": "dc876fa4-ef2d-4eee-b701-b555fcc6544c",
90-
"timelineTemplateId": "0a0fed34-cb5a-47f5-b0cb-6e2ee7de8dcb"
104+
"timelineTemplateId": "0a0fed34-cb5a-47f5-b0cb-6e2ee7de8dcb",
105+
"isDefault": true
91106
},
92107
{
93108
"id": "68ae6341-f571-4be1-9d86-44773c294538",
94109
"trackId": "36e6a8d0-7e1e-4608-a673-64279d99c115",
95110
"typeId": "ecd58c69-238f-43a4-a4bb-d172719b9f31",
96-
"timelineTemplateId": "53a307ce-b4b3-4d6f-b9a1-3741a58f77e6"
111+
"timelineTemplateId": "53a307ce-b4b3-4d6f-b9a1-3741a58f77e6",
112+
"isDefault": true
97113
}
98114
]

Diff for: src/services/ChallengeService.js

+8-64
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,12 @@ async function searchChallenges (currentUser, criteria) {
196196
}
197197

198198
if (criteria.name) {
199-
boolQuery.push({ wildcard: { name: `*${_.toLower(criteria.name)}*` } })
199+
boolQuery.push({ match: { 'name': criteria.name } })
200200
}
201201
if (criteria.description) {
202-
boolQuery.push({ wildcard: { description: `*${_.toLower(criteria.description)}*` } })
202+
boolQuery.push({ match: { 'description': criteria.description } })
203203
}
204+
204205
if (criteria.forumId) {
205206
boolQuery.push({ match_phrase: { 'legacy.forumId': criteria.forumId } })
206207
}
@@ -782,7 +783,8 @@ async function createChallenge (currentUser, challenge, userToken) {
782783
if (challenge.typeId && challenge.trackId) {
783784
const [challengeTimelineTemplate] = await ChallengeTimelineTemplateService.searchChallengeTimelineTemplates({
784785
typeId: challenge.typeId,
785-
trackId: challenge.trackId
786+
trackId: challenge.trackId,
787+
isDefault: true
786788
})
787789
if (!challengeTimelineTemplate) {
788790
throw new errors.BadRequestError(`The selected trackId ${challenge.trackId} and typeId: ${challenge.typeId} does not have a default timeline template. Please provide a timelineTemplateId`)
@@ -1036,64 +1038,14 @@ function isDifferentPhases (phases = [], otherPhases) {
10361038
}
10371039
}
10381040

1039-
/**
1040-
* Check whether given two Prize Array are the same.
1041-
* @param {Array} prizes the first Prize Array
1042-
* @param {Array} otherPrizes the second Prize Array
1043-
* @returns {Boolean} true if the same, false otherwise
1044-
*/
1045-
function isSamePrizeArray (prizes, otherPrizes) {
1046-
const length = otherPrizes.length
1047-
if (prizes.length === otherPrizes.length) {
1048-
let used = Array(length).fill(false)
1049-
for (const prize of prizes) {
1050-
let index = -1
1051-
for (let i = 0; i < length; i++) {
1052-
if (!used[i] && prize.description === otherPrizes[i].description &&
1053-
prize.type === otherPrizes[i].type &&
1054-
prize.value === otherPrizes[i].value) {
1055-
used[i] = true
1056-
index = i
1057-
break
1058-
}
1059-
}
1060-
if (index === -1) {
1061-
return false
1062-
}
1063-
}
1064-
return true
1065-
} else {
1066-
return false
1067-
}
1068-
}
1069-
10701041
/**
10711042
* Check whether given two PrizeSet Array are different.
10721043
* @param {Array} prizeSets the first PrizeSet Array
10731044
* @param {Array} otherPrizeSets the second PrizeSet Array
10741045
* @returns {Boolean} true if different, false otherwise
10751046
*/
1076-
function isDifferentPrizeSets (prizeSets = [], otherPrizeSets) {
1077-
const length = otherPrizeSets.length
1078-
if (prizeSets.length === otherPrizeSets.length) {
1079-
let used = Array(length).fill(false)
1080-
for (const set of prizeSets) {
1081-
let index = -1
1082-
for (let i = 0; i < length; i++) {
1083-
if (!used[i] && set.type === otherPrizeSets[i].type &&
1084-
set.description === otherPrizeSets[i].description &&
1085-
isSamePrizeArray(set.prizes, otherPrizeSets[i].prizes)) {
1086-
used[i] = true
1087-
index = i
1088-
break
1089-
}
1090-
}
1091-
if (index === -1) {
1092-
return true
1093-
}
1094-
}
1095-
}
1096-
return false
1047+
function isDifferentPrizeSets (prizeSets = [], otherPrizeSets = []) {
1048+
return !_.isEqual(_.sortBy(prizeSets, 'type'), _.sortBy(otherPrizeSets, 'type'))
10971049
}
10981050

10991051
/**
@@ -1376,7 +1328,7 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
13761328
oldValue = challenge[key] ? JSON.stringify(challenge[key]) : 'NULL'
13771329
newValue = JSON.stringify(value)
13781330
}
1379-
logger.debug(`Audit Log: Key ${key} OldValue: ${oldValue} NewValue: ${newValue}`)
1331+
// logger.debug(`Audit Log: Key ${key} OldValue: ${oldValue} NewValue: ${newValue}`)
13801332
auditLogs.push({
13811333
id: uuid(),
13821334
challengeId,
@@ -1517,14 +1469,6 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
15171469

15181470
delete data.attachmentIds
15191471
delete data.terms
1520-
if (data.phases) {
1521-
_.each(data.phases, p => {
1522-
delete p.name
1523-
if (p.description) {
1524-
delete p.description
1525-
}
1526-
})
1527-
}
15281472
_.assign(challenge, data)
15291473
if (!_.isUndefined(newAttachments)) {
15301474
challenge.attachments = newAttachments

Diff for: src/services/ChallengeTimelineTemplateService.js

+34-6
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,38 @@ const constants = require('../../app-constants')
1616
* @returns {Array} the search result
1717
*/
1818
async function searchChallengeTimelineTemplates (criteria) {
19-
const list = await helper.scan('ChallengeTimelineTemplate')
20-
const records = _.filter(list, e => (!criteria.typeId || criteria.typeId === e.typeId) &&
21-
(!criteria.timelineTemplateId || criteria.timelineTemplateId === e.timelineTemplateId))
19+
let records = await helper.scan('ChallengeTimelineTemplate')
20+
if (criteria.typeId) records = _.filter(records, e => (criteria.typeId === e.typeId))
21+
if (criteria.trackId) records = _.filter(records, e => (criteria.trackId === e.trackId))
22+
if (criteria.timelineTemplateId) records = _.filter(records, e => (criteria.timelineTemplateId === e.timelineTemplateId))
23+
if (!_.isUndefined(criteria.isDefault)) records = _.filter(records, e => (e.isDefault === (criteria.isDefault === 'true')))
2224
return records
2325
}
2426

2527
searchChallengeTimelineTemplates.schema = {
2628
criteria: Joi.object().keys({
2729
typeId: Joi.optionalId(),
2830
trackId: Joi.optionalId(),
29-
timelineTemplateId: Joi.optionalId()
31+
timelineTemplateId: Joi.optionalId(),
32+
isDefault: Joi.boolean()
3033
})
3134
}
3235

36+
/**
37+
* Unset existing default timeline template in order to create a new one
38+
* @param {String} typeId the type ID
39+
* @param {String} trackId the track ID
40+
*/
41+
async function unsetDefaultTimelineTemplate (typeId, trackId) {
42+
const records = await searchChallengeTimelineTemplates({ typeId, trackId, isDefault: true })
43+
if (records.length === 0) {
44+
return
45+
}
46+
for (const record of records) {
47+
await fullyUpdateChallengeTimelineTemplate(record.id, { ...record, isDefault: false })
48+
}
49+
}
50+
3351
/**
3452
* Create challenge type timeline template.
3553
* @param {Object} data the data to create challenge type timeline template
@@ -46,6 +64,10 @@ async function createChallengeTimelineTemplate (data) {
4664
await helper.getById('ChallengeTrack', data.trackId)
4765
await helper.getById('TimelineTemplate', data.timelineTemplateId)
4866

67+
if (data.isDefault) {
68+
await unsetDefaultTimelineTemplate(data.typeId, data.trackId)
69+
}
70+
4971
const ret = await helper.create('ChallengeTimelineTemplate', _.assign({ id: uuid() }, data))
5072
// post bus event
5173
await helper.postBusEvent(constants.Topics.ChallengeTimelineTemplateCreated, ret)
@@ -56,7 +78,8 @@ createChallengeTimelineTemplate.schema = {
5678
data: Joi.object().keys({
5779
typeId: Joi.id(),
5880
trackId: Joi.id(),
59-
timelineTemplateId: Joi.id()
81+
timelineTemplateId: Joi.id(),
82+
isDefault: Joi.boolean().default(false).required()
6083
}).required()
6184
}
6285

@@ -84,7 +107,8 @@ async function fullyUpdateChallengeTimelineTemplate (challengeTimelineTemplateId
84107

85108
if (record.typeId === data.typeId &&
86109
record.trackId === data.trackId &&
87-
record.timelineTemplateId === data.timelineTemplateId) {
110+
record.timelineTemplateId === data.timelineTemplateId &&
111+
record.isDefault === data.isDefault) {
88112
// no change
89113
return record
90114
}
@@ -99,6 +123,10 @@ async function fullyUpdateChallengeTimelineTemplate (challengeTimelineTemplateId
99123
await helper.getById('ChallengeTrack', data.trackId)
100124
await helper.getById('TimelineTemplate', data.timelineTemplateId)
101125

126+
if (data.isDefault) {
127+
await unsetDefaultTimelineTemplate(data.typeId, data.trackId)
128+
}
129+
102130
const ret = await helper.update(record, data)
103131
// post bus event
104132
await helper.postBusEvent(constants.Topics.ChallengeTimelineTemplateUpdated, ret)

0 commit comments

Comments
 (0)