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.

[⚠️ performance]
Consider using a Set for SUBMISSION_PHASE_PRIORITY to improve lookup performance when checking phase names. This change can enhance performance, especially if the list grows or is checked frequently.


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.

[⚠️ maintainability]
The current logic assumes that only the first two elements in SUBMISSION_PHASE_PRIORITY will be checked. If more submission types are added in the future, this logic will need to be updated. Consider iterating over SUBMISSION_PHASE_PRIORITY to make the code more maintainable and adaptable to future changes.

_.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.

[⚠️ maintainability]
The logic for finding the submissionPhase relies on the order of SUBMISSION_PHASE_PRIORITY. If more submission types are added in the future, this logic will need to be updated. Consider using a more flexible approach, such as iterating through SUBMISSION_PHASE_PRIORITY and finding the first match.

_.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