Skip to content

fix: validate date #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2023
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
12 changes: 7 additions & 5 deletions src/common/challenge-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
152 changes: 75 additions & 77 deletions src/services/ChallengeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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,
}
);
}
}

Expand Down