Skip to content

Commit 055ac8c

Browse files
committed
Ensure consistent challenge type and track in responses
1 parent 6b5fef9 commit 055ac8c

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/common/challenge-helper.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,23 @@ class ChallengeHelper {
335335
}
336336
}
337337

338-
enrichChallengeForResponse(challenge, track, type) {
338+
/**
339+
* Enrich challenge for API responses. Normalizes dates, phases and ensures
340+
* `track` and `type` fields have a consistent shape.
341+
*
342+
* By default, `track` and `type` are returned as objects:
343+
* track: { id, name, track }
344+
* type: { id, name }
345+
*
346+
* If options.asString === true, `track` and `type` are returned as display strings
347+
* (legacy behavior used for bus events to avoid breaking consumers).
348+
*
349+
* @param {Object} challenge
350+
* @param {Object} [track]
351+
* @param {Object} [type]
352+
* @param {{ asString?: boolean }} [options]
353+
*/
354+
enrichChallengeForResponse(challenge, track, type, options = {}) {
339355
if (challenge.phases && challenge.phases.length > 0) {
340356
const registrationPhase = _.find(challenge.phases, (p) => p.name === "Registration");
341357
const submissionPhase = _.find(challenge.phases, (p) => p.name === "Submission");
@@ -385,12 +401,30 @@ class ChallengeHelper {
385401
if (challenge.endDate)
386402
challenge.endDate = ChallengeHelper.convertDateToISOString(challenge.endDate);
387403

404+
const asString = options.asString === true;
405+
388406
if (track) {
389-
challenge.track = track.name;
407+
if (asString) {
408+
challenge.track = track.name;
409+
} else {
410+
challenge.track = {
411+
id: track.id,
412+
name: track.name,
413+
// Prefer the canonical enum value if present; else derive from name/abbreviation
414+
track: track.track || (track.abbreviation ? String(track.abbreviation).toUpperCase() : String(track.name || '').toUpperCase().replace(/\s+/g, '_')),
415+
};
416+
}
390417
}
391418

392419
if (type) {
393-
challenge.type = type.name;
420+
if (asString) {
421+
challenge.type = type.name;
422+
} else {
423+
challenge.type = {
424+
id: type.id,
425+
name: type.name,
426+
};
427+
}
394428
}
395429
if (challenge.metadata) {
396430
challenge.metadata = challenge.metadata.map((m) => {

src/services/ChallengeService.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3203,7 +3203,8 @@ async function indexChallengeAndPostToKafka(updatedChallenge, track, type) {
32033203
);
32043204

32053205
prismaHelper.convertModelToResponse(updatedChallenge);
3206-
enrichChallengeForResponse(updatedChallenge, track, type);
3206+
// Keep legacy string values for bus event payload to avoid breaking consumers
3207+
enrichChallengeForResponse(updatedChallenge, track, type, { asString: true });
32073208

32083209
await helper.postBusEvent(constants.Topics.ChallengeUpdated, updatedChallenge, {
32093210
key:

0 commit comments

Comments
 (0)