diff --git a/src/common/challenge-helper.js b/src/common/challenge-helper.js index b6a3db22..b9f8eba0 100644 --- a/src/common/challenge-helper.js +++ b/src/common/challenge-helper.js @@ -119,7 +119,9 @@ class ChallengeHelper { // Ensure descriptionFormat is either 'markdown' or 'html' if (data.descriptionFormat && !_.includes(["markdown", "html"], data.descriptionFormat)) { - throw new errors.BadRequestError("The property 'descriptionFormat' must be either 'markdown' or 'html'"); + throw new errors.BadRequestError( + "The property 'descriptionFormat' must be either 'markdown' or 'html'" + ); } // Ensure unchangeable fields are not changed @@ -299,10 +301,10 @@ class ChallengeHelper { } } - challenge.created = new Date(challenge.created).toISOString(); - challenge.updated = new Date(challenge.updated).toISOString(); - challenge.startDate = new Date(challenge.startDate).toISOString(); - challenge.endDate = new Date(challenge.endDate).toISOString(); + if (challenge.created) challenge.created = new Date(challenge.created).toISOString(); + if (challenge.updated) challenge.updated = new Date(challenge.updated).toISOString(); + if (challenge.startDate) challenge.startDate = new Date(challenge.startDate).toISOString(); + if (challenge.endDate) challenge.endDate = new Date(challenge.endDate).toISOString(); if (track) { challenge.track = track.name; diff --git a/src/services/ChallengeService.js b/src/services/ChallengeService.js index e8f8e06f..45603fd7 100644 --- a/src/services/ChallengeService.js +++ b/src/services/ChallengeService.js @@ -1127,28 +1127,25 @@ async function createChallenge(currentUser, challenge, userToken) { enrichChallengeForResponse(ret, track, type); - const isLocal = process.env.LOCAL == "true"; - if (!isLocal) { - // Create in ES - await esClient.create({ - index: config.get("ES.ES_INDEX"), - type: config.get("ES.OPENSEARCH") == "false" ? config.get("ES.ES_TYPE") : undefined, - refresh: config.get("ES.ES_REFRESH"), - id: ret.id, - body: ret, - }); + // Create in ES + await esClient.create({ + index: config.get("ES.ES_INDEX"), + type: config.get("ES.OPENSEARCH") == "false" ? config.get("ES.ES_TYPE") : undefined, + refresh: config.get("ES.ES_REFRESH"), + id: ret.id, + body: ret, + }); - // If the challenge is self-service, add the creating user as the "client manager", *not* the manager - // This is necessary for proper handling of the vanilla embed on the self-service work item dashboard + // If the challenge is self-service, add the creating user as the "client manager", *not* the manager + // This is necessary for proper handling of the vanilla embed on the self-service work item dashboard - if (challenge.legacy.selfService) { - if (currentUser.handle) { - await helper.createResource(ret.id, ret.createdBy, config.CLIENT_MANAGER_ROLE_ID); - } - } else { - if (currentUser.handle) { - await helper.createResource(ret.id, ret.createdBy, config.MANAGER_ROLE_ID); - } + if (challenge.legacy.selfService) { + if (currentUser.handle) { + await helper.createResource(ret.id, ret.createdBy, config.CLIENT_MANAGER_ROLE_ID); + } + } else { + if (currentUser.handle) { + await helper.createResource(ret.id, ret.createdBy, config.MANAGER_ROLE_ID); } } @@ -1501,6 +1498,10 @@ async function updateChallenge(currentUser, challengeId, data) { data = sanitizeData(sanitizeChallenge(data), challenge); console.debug("Sanitized Data:", data); + if (data.phases != null && data.startDate == null) { + data.startDate = challenge.startDate; + } + validateChallengeUpdateRequest(currentUser, challenge, data); const projectId = _.get(challenge, "projectId"); @@ -1887,64 +1888,61 @@ async function updateChallenge(currentUser, challengeId, data) { : undefined, }); - const isLocal = process.env.LOCAL == "true"; - if (!isLocal) { - // Update ES - await esClient.update({ - index: config.get("ES.ES_INDEX"), - type: config.get("ES.OPENSEARCH") == "false" ? config.get("ES.ES_TYPE") : undefined, - refresh: config.get("ES.ES_REFRESH"), - id: challengeId, - body: { - doc: updatedChallenge, - }, - }); + // Update ES + await esClient.update({ + index: config.get("ES.ES_INDEX"), + type: config.get("ES.OPENSEARCH") == "false" ? config.get("ES.ES_TYPE") : undefined, + refresh: config.get("ES.ES_REFRESH"), + id: challengeId, + body: { + doc: updatedChallenge, + }, + }); - if (updatedChallenge.legacy.selfService) { - const creator = await helper.getMemberByHandle(updatedChallenge.createdBy); - if (sendSubmittedEmail) { - await helper.sendSelfServiceNotification( - constants.SelfServiceNotificationTypes.WORK_REQUEST_SUBMITTED, - [{ email: creator.email }], - { - handle: creator.handle, - workItemName: updatedChallenge.name, - } - ); - } - if (sendActivationEmail) { - await helper.sendSelfServiceNotification( - constants.SelfServiceNotificationTypes.WORK_REQUEST_STARTED, - [{ email: creator.email }], - { - handle: creator.handle, - workItemName: updatedChallenge.name, - workItemUrl: `${config.SELF_SERVICE_APP_URL}/work-items/${updatedChallenge.id}`, - } - ); - } - if (sendCompletedEmail) { - await helper.sendSelfServiceNotification( - constants.SelfServiceNotificationTypes.WORK_COMPLETED, - [{ email: creator.email }], - { - handle: creator.handle, - workItemName: updatedChallenge.name, - workItemUrl: `${config.SELF_SERVICE_APP_URL}/work-items/${updatedChallenge.id}?tab=solutions`, - } - ); - } - if (sendRejectedEmail || cancelReason) { - logger.debug("Should send redirected email"); - await helper.sendSelfServiceNotification( - constants.SelfServiceNotificationTypes.WORK_REQUEST_REDIRECTED, - [{ email: creator.email }], - { - handle: creator.handle, - workItemName: updatedChallenge.name, - } - ); - } + if (updatedChallenge.legacy.selfService) { + const creator = await helper.getMemberByHandle(updatedChallenge.createdBy); + if (sendSubmittedEmail) { + await helper.sendSelfServiceNotification( + constants.SelfServiceNotificationTypes.WORK_REQUEST_SUBMITTED, + [{ email: creator.email }], + { + handle: creator.handle, + workItemName: updatedChallenge.name, + } + ); + } + if (sendActivationEmail) { + await helper.sendSelfServiceNotification( + constants.SelfServiceNotificationTypes.WORK_REQUEST_STARTED, + [{ email: creator.email }], + { + handle: creator.handle, + workItemName: updatedChallenge.name, + workItemUrl: `${config.SELF_SERVICE_APP_URL}/work-items/${updatedChallenge.id}`, + } + ); + } + if (sendCompletedEmail) { + await helper.sendSelfServiceNotification( + constants.SelfServiceNotificationTypes.WORK_COMPLETED, + [{ email: creator.email }], + { + handle: creator.handle, + workItemName: updatedChallenge.name, + workItemUrl: `${config.SELF_SERVICE_APP_URL}/work-items/${updatedChallenge.id}?tab=solutions`, + } + ); + } + if (sendRejectedEmail || cancelReason) { + logger.debug("Should send redirected email"); + await helper.sendSelfServiceNotification( + constants.SelfServiceNotificationTypes.WORK_REQUEST_REDIRECTED, + [{ email: creator.email }], + { + handle: creator.handle, + workItemName: updatedChallenge.name, + } + ); } }