Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/common/challenge-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const { hasAdminRole } = require("./role-helper");
const { ensureAcessibilityToModifiedGroups } = require("./group-helper");
const { ChallengeStatusEnum } = require("@prisma/client");

const SUBMISSION_PHASE_PRIORITY = ["Topcoder Submission", "Submission"];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[💡 maintainability]
Consider adding a comment or documentation explaining the purpose and order of SUBMISSION_PHASE_PRIORITY. This will help future developers understand why these specific phases are prioritized and in this order.


class ChallengeHelper {
/**
* @param {Object} challenge the challenge object
Expand Down Expand Up @@ -356,7 +358,9 @@ class ChallengeHelper {
enrichChallengeForResponse(challenge, track, type, options = {}) {
if (challenge.phases && challenge.phases.length > 0) {
const registrationPhase = _.find(challenge.phases, (p) => p.name === "Registration");
const submissionPhase = _.find(challenge.phases, (p) => p.name === "Submission");
const submissionPhase =
_.find(challenge.phases, (p) => p.name === SUBMISSION_PHASE_PRIORITY[0]) ||

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ correctness]
The logic for finding the submissionPhase relies on the order of SUBMISSION_PHASE_PRIORITY. If additional submission phases are added in the future, ensure they are added to SUBMISSION_PHASE_PRIORITY in the correct order. Consider adding a test case to verify the behavior when multiple submission phases are present.

_.find(challenge.phases, (p) => p.name === SUBMISSION_PHASE_PRIORITY[1]);

// select last started open phase as current phase
_.forEach(challenge.phases, (p) => {
Expand Down
40 changes: 24 additions & 16 deletions src/common/prisma-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const Decimal = require("decimal.js");
const constants = require("../../app-constants");
const { PrizeSetTypeEnum } = require("@prisma/client");
const { dedupeChallengeTerms } = require("./helper");

const SUBMISSION_PHASE_PRIORITY = ["Topcoder Submission", "Submission"];
/**
* Convert phases data to prisma model.
*
Expand All @@ -11,6 +13,7 @@ const { dedupeChallengeTerms } = require("./helper");
* @param {Object} auditFields createdBy and updatedBy
*/
function convertChallengePhaseSchema(challenge, result, auditFields) {
const phases = _.isArray(challenge.phases) ? challenge.phases : [];
// keep phase data
const phaseFields = [
"name",
Expand All @@ -25,24 +28,29 @@ function convertChallengePhaseSchema(challenge, result, auditFields) {
"challengeSource",
];
// current phase names
result.currentPhaseNames = _.map(
_.filter(challenge.phases, (p) => p.isOpen === true),
"name"
);
// get registration date and submission date
_.forEach(challenge.phases, (p) => {
if (p.name === "Registration") {
result.registrationStartDate = p.actualStartDate || p.scheduledStartDate;
result.registrationEndDate = p.actualEndDate || p.scheduledEndDate;
} else if (p.name === "Submission") {
result.submissionStartDate = p.actualStartDate || p.scheduledStartDate;
result.submissionEndDate = p.actualEndDate || p.scheduledEndDate;
}
});
result.currentPhaseNames = _.map(_.filter(phases, (p) => p.isOpen === true), "name");

const registrationPhase = _.find(phases, (p) => p.name === "Registration");
const submissionPhase =
_.find(phases, (p) => p.name === SUBMISSION_PHASE_PRIORITY[0]) ||

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ performance]
The logic for finding the submissionPhase could be optimized by using a single _.find call with a condition that checks for both phase names in the priority order. This would avoid iterating over the phases array twice.

_.find(phases, (p) => p.name === SUBMISSION_PHASE_PRIORITY[1]);

if (registrationPhase) {
result.registrationStartDate =
registrationPhase.actualStartDate || registrationPhase.scheduledStartDate;
result.registrationEndDate =
registrationPhase.actualEndDate || registrationPhase.scheduledEndDate;
}
if (submissionPhase) {
result.submissionStartDate =
submissionPhase.actualStartDate || submissionPhase.scheduledStartDate;
result.submissionEndDate =
submissionPhase.actualEndDate || submissionPhase.scheduledEndDate;
}
// set phases array data
if (!_.isEmpty(challenge.phases)) {
if (!_.isEmpty(phases)) {
result.phases = {
create: _.map(challenge.phases, (p) => {
create: _.map(phases, (p) => {
const phaseData = {
phase: { connect: { id: p.phaseId } },
..._.pick(p, phaseFields),
Expand Down