Skip to content

Commit 669c1b1

Browse files
committed
fix: winner can not be set for non tasks
1 parent fa398ce commit 669c1b1

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

src/services/ChallengeService.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,15 +1869,15 @@ async function updateChallenge(currentUser, challengeId, data) {
18691869
`Post Bus Event: ${constants.Topics.ChallengeUpdated} ${JSON.stringify(updatedChallenge)}`
18701870
);
18711871

1872+
enrichChallengeForResponse(updatedChallenge, track, type);
1873+
18721874
await helper.postBusEvent(constants.Topics.ChallengeUpdated, updatedChallenge, {
18731875
key:
18741876
updatedChallenge.status === "Completed"
18751877
? `${updatedChallenge.id}:${updatedChallenge.status}`
18761878
: undefined,
18771879
});
18781880

1879-
enrichChallengeForResponse(updatedChallenge, track, type);
1880-
18811881
// Update ES
18821882
// TODO: Uncomment
18831883
await esClient.update({
@@ -1964,12 +1964,14 @@ updateChallenge.schema = {
19641964
selfServiceCopilot: Joi.string().allow(null),
19651965
})
19661966
.unknown(true),
1967-
cancelReason: Joi.string(),
1968-
task: Joi.object().keys({
1969-
isTask: Joi.boolean().default(false),
1970-
isAssigned: Joi.boolean().default(false),
1971-
memberId: Joi.alternatives().try(Joi.string().allow(null), Joi.number().allow(null)),
1972-
}),
1967+
cancelReason: Joi.string().optional(),
1968+
task: Joi.object()
1969+
.keys({
1970+
isTask: Joi.boolean().default(false),
1971+
isAssigned: Joi.boolean().default(false),
1972+
memberId: Joi.alternatives().try(Joi.string().allow(null), Joi.number().allow(null)),
1973+
})
1974+
.optional(),
19731975
billing: Joi.object()
19741976
.keys({
19751977
billingAccountId: Joi.string(),
@@ -1978,10 +1980,10 @@ updateChallenge.schema = {
19781980
.unknown(true),
19791981
trackId: Joi.optionalId(),
19801982
typeId: Joi.optionalId(),
1981-
name: Joi.string(),
1982-
description: Joi.string(),
1983-
privateDescription: Joi.string(),
1984-
descriptionFormat: Joi.string(),
1983+
name: Joi.string().optional(),
1984+
description: Joi.string().optional(),
1985+
privateDescription: Joi.string().optional(),
1986+
descriptionFormat: Joi.string().optional(),
19851987
metadata: Joi.array()
19861988
.items(
19871989
Joi.object()
@@ -1992,7 +1994,7 @@ updateChallenge.schema = {
19921994
.unknown(true)
19931995
)
19941996
.unique((a, b) => a.name === b.name),
1995-
timelineTemplateId: Joi.string(), // changing this to update migrated challenges
1997+
timelineTemplateId: Joi.string().optional(), // changing this to update migrated challenges
19961998
phases: Joi.array()
19971999
.items(
19982000
Joi.object()
@@ -2015,7 +2017,8 @@ updateChallenge.schema = {
20152017
})
20162018
.unknown(true)
20172019
)
2018-
.min(1),
2020+
.min(1)
2021+
.optional(),
20192022
events: Joi.array().items(
20202023
Joi.object()
20212024
.keys({
@@ -2024,17 +2027,20 @@ updateChallenge.schema = {
20242027
key: Joi.string(),
20252028
})
20262029
.unknown(true)
2030+
.optional()
20272031
),
2028-
discussions: Joi.array().items(
2029-
Joi.object().keys({
2030-
id: Joi.optionalId(),
2031-
name: Joi.string().required(),
2032-
type: Joi.string().required().valid(_.values(constants.DiscussionTypes)),
2033-
provider: Joi.string().required(),
2034-
url: Joi.string(),
2035-
options: Joi.array().items(Joi.object()),
2036-
})
2037-
),
2032+
discussions: Joi.array()
2033+
.items(
2034+
Joi.object().keys({
2035+
id: Joi.optionalId(),
2036+
name: Joi.string().required(),
2037+
type: Joi.string().required().valid(_.values(constants.DiscussionTypes)),
2038+
provider: Joi.string().required(),
2039+
url: Joi.string(),
2040+
options: Joi.array().items(Joi.object()),
2041+
})
2042+
)
2043+
.optional(),
20382044
startDate: Joi.date(),
20392045
prizeSets: Joi.array()
20402046
.items(
@@ -2085,7 +2091,7 @@ updateChallenge.schema = {
20852091
})
20862092
.unknown(true)
20872093
)
2088-
.min(1),
2094+
.optional(),
20892095
terms: Joi.array().items(
20902096
Joi.object().keys({
20912097
id: Joi.id(),
@@ -2198,10 +2204,12 @@ function sanitizeChallenge(challenge) {
21982204
if (challenge.events) {
21992205
sanitized.events = _.map(challenge.events, (event) => _.pick(event, ["id", "name", "key"]));
22002206
}
2201-
if (challenge.winners) {
2207+
if (challenge.legacy != null && challenge.legacy.pureV5Task === true && challenge.winners) {
22022208
sanitized.winners = _.map(challenge.winners, (winner) =>
22032209
_.pick(winner, ["userId", "handle", "placement", "type"])
22042210
);
2211+
} else {
2212+
delete challenge.winners;
22052213
}
22062214
if (challenge.discussions) {
22072215
sanitized.discussions = _.map(challenge.discussions, (discussion) => ({
@@ -2219,12 +2227,6 @@ function sanitizeChallenge(challenge) {
22192227
}
22202228
return sanitized;
22212229
}
2222-
/*
2223-
metadata = [ { }, { }, { }] -> in database
2224-
metadata = [ {} ]
2225-
2226-
final state: [ {} ]
2227-
*/
22282230

22292231
function sanitizeData(data, challenge) {
22302232
for (const key in data) {

0 commit comments

Comments
 (0)