-
Notifications
You must be signed in to change notification settings - Fork 6
[PROD RELEASE] - Fixes #72
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
Changes from all commits
2e5bd08
41b4c68
d1161be
6d39268
f7ee6df
23c3a60
c17d651
95fd2e6
57c5063
4ecb6bf
e814a6d
b39f104
b1812d7
1bd719a
b235f71
eaca715
aa80953
629b0b2
979eb82
867eec4
1c0edd7
0fc71dd
48aac90
c35a6d1
001601c
cc56bd8
4d2ae69
1487ca8
52f4318
aa5e5a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,7 +91,7 @@ workflows: | |
| only: | ||
| - develop | ||
| - security | ||
| - PM-3327 | ||
| - PM-3351 | ||
|
|
||
| - "build-qa": | ||
| context: org-global | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -70,15 +70,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| app.use( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cors({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| origin: (origin, callback) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!origin) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("No origin - probably curl or server to server request"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // disable cors if service to service request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback(null, false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback(null, '*') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| origin: "*", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Check warningCode scanning / CodeQL Permissive CORS configuration Medium
CORS Origin allows broad access due to
permissive or user controlled value Error loading related location Loading
Copilot AutofixAI 3 months ago In general, the problem should be fixed by replacing the permissive For this specific code, the best fix without changing existing functionality too much is to move from
All changes will be confined to
Suggested changeset
1
app.js
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exposedHeaders: [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "X-Prev-Page", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "X-Next-Page", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,4 +133,6 @@ module.exports = { | |
| RESOURCES_DB_SCHEMA: process.env.RESOURCES_DB_SCHEMA || "resources", | ||
| REVIEW_DB_SCHEMA: process.env.REVIEW_DB_SCHEMA || "reviews", | ||
| CHALLENGE_SERVICE_PRISMA_TIMEOUT: process.env.CHALLENGE_SERVICE_PRISMA_TIMEOUT ? parseInt(process.env.CHALLENGE_SERVICE_PRISMA_TIMEOUT, 10) : 10000, | ||
| CHALLENGE_URL: process.env.CHALLENGE_URL || 'https://www.topcoder-dev.com/challenges' , | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| PHASE_CHANGE_SENDGRID_TEMPLATE_ID: process.env.PHASE_CHANGE_SENDGRID_TEMPLATE_ID || "", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ALTER TABLE "Challenge" | ||
| ADD COLUMN "funChallenge" BOOLEAN NOT NULL DEFAULT false; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- CreateIndex | ||
| CREATE INDEX "ChallengeTerm_challengeId_idx" ON "ChallengeTerm"("challengeId"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1428,22 +1428,48 @@ function sumOfPrizes(prizes) { | |
| } | ||
|
|
||
| /** | ||
| * Get group by id | ||
| * @param {String} groupId the group id | ||
| * Get group by id, with oldId fallback for backward compatibility. | ||
| * @param {String} groupId the group id or oldId | ||
| * @returns {Promise<Object>} the group | ||
| */ | ||
| async function getGroupById(groupId) { | ||
| const normalizedGroupId = _.toString(groupId || "").trim(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [💡 |
||
| if (!normalizedGroupId) { | ||
| return; | ||
| } | ||
|
|
||
| const token = await m2mHelper.getM2MToken(); | ||
| const requestHeaders = { Authorization: `Bearer ${token}` }; | ||
| try { | ||
| const result = await axios.get(`${config.GROUPS_API_URL}/${groupId}`, { | ||
| headers: { Authorization: `Bearer ${token}` }, | ||
| const result = await axios.get(`${config.GROUPS_API_URL}/${encodeURIComponent(normalizedGroupId)}`, { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| headers: requestHeaders, | ||
| }); | ||
| return result.data; | ||
| } catch (err) { | ||
| if (err.response.status === HttpStatus.NOT_FOUND) { | ||
| return; | ||
| const status = _.get(err, "response.status"); | ||
| if (status !== HttpStatus.NOT_FOUND) { | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| const result = await axios.get(config.GROUPS_API_URL, { | ||
| headers: requestHeaders, | ||
| params: { | ||
| page: 1, | ||
| perPage: 1, | ||
| oldId: normalizedGroupId, | ||
| }, | ||
| }); | ||
| const groups = _.get(result, "data", []); | ||
| if (groups.length > 0) { | ||
| return groups[0]; | ||
| } | ||
| } catch (err) { | ||
| const status = _.get(err, "response.status"); | ||
| if (status !== HttpStatus.NOT_FOUND) { | ||
| throw err; | ||
| } | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1638,6 +1664,72 @@ async function sendSelfServiceNotification(type, recipients, data) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Build payload for phase change email notification | ||
| * @param {String} challenge Id | ||
| * @param {String} challenge name | ||
| * @param {String} challenge phase name | ||
| * @param {String} operation to be performed on the phase - open | close | reopen | ||
| * @param {String|Date} at - The date/time when the phase opened/closed | ||
| */ | ||
| function buildPhaseChangeEmailData({ challengeId, challengeName, phaseName, operation, at }) { | ||
| const isOpen = operation === 'open' || operation === 'reopen'; | ||
| const isClose = operation === 'close'; | ||
|
|
||
| return { | ||
| challengeURL: `${config.CHALLENGE_URL}/${challengeId}`, | ||
| challengeName, | ||
| phaseOpen: isOpen ? phaseName : null, | ||
| phaseOpenDate: isOpen ? at : null, | ||
| phaseClose: isClose ? phaseName : null, | ||
| phaseCloseDate: isClose ? at : null, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Send phase change notification | ||
| * @param {String} type the notification type | ||
| * @param {Array} recipients the array of recipients emails | ||
| * @param {Object} data the data | ||
| */ | ||
| async function sendPhaseChangeNotification(type, recipients, data) { | ||
| try { | ||
| const settings = constants.PhaseChangeNotificationSettings?.[type]; | ||
|
|
||
| if (!settings) { | ||
| logger.debug(`sendPhaseChangeNotification: unknown type ${type}`); | ||
| return; | ||
| } | ||
|
|
||
| if (!settings.sendgridTemplateId) { | ||
| logger.debug( | ||
| `sendPhaseChangeNotification: sendgridTemplateId not configured for type ${type}` | ||
| ); | ||
| return; | ||
| } | ||
| const safeRecipients = Array.isArray(recipients) ? recipients.filter(Boolean) : []; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
|
|
||
| if (!safeRecipients.length) { | ||
| logger.debug(`sendPhaseChangeNotification: no recipients for type ${type}`); | ||
| return; | ||
| } | ||
|
|
||
| await postBusEvent('external.action.email', | ||
| { | ||
| from: config.EMAIL_FROM, | ||
| replyTo: config.EMAIL_FROM, | ||
| recipients: safeRecipients, | ||
| data: data, | ||
| sendgrid_template_id: settings.sendgridTemplateId, | ||
| version: 'v3', | ||
| }, | ||
| ); | ||
| } catch (e) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| logger.debug(`Failed to post notification ${type}: ${e.message}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Submit a request to zendesk | ||
| * @param {Object} request the request | ||
|
|
@@ -1756,6 +1848,8 @@ module.exports = { | |
| setToInternalCache, | ||
| flushInternalCache, | ||
| removeNullProperties, | ||
| buildPhaseChangeEmailData, | ||
| sendPhaseChangeNotification | ||
| }; | ||
|
|
||
| logger.buildService(module.exports); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -813,9 +813,74 @@ async function partiallyUpdateChallengePhase(currentUser, challengeId, id, data) | |
| _.assignIn({ id: result.id }, data) | ||
| ); | ||
| await postChallengeUpdatedNotification(challengeId); | ||
|
|
||
| // send notification logic | ||
| try { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| const shouldNotifyClose = Boolean(isClosingPhase); | ||
| const shouldNotifyOpen = Boolean(isOpeningPhase); // includes reopen | ||
|
|
||
| if (shouldNotifyClose || shouldNotifyOpen) { | ||
| // Single template - single type | ||
| const notificationType = "PHASE_CHANGE"; | ||
|
|
||
| const operation = shouldNotifyClose | ||
| ? "close" | ||
| : (isReopeningPhase ? "reopen" : "open"); | ||
|
|
||
| const at = shouldNotifyClose | ||
| ? (result.actualEndDate || new Date().toISOString()) | ||
| : (result.actualStartDate || new Date().toISOString()); | ||
|
|
||
| // fetch challenge name | ||
| const challenge = await prisma.challenge.findUnique({ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| where: { id: challengeId }, | ||
| select: { name: true }, | ||
| }); | ||
|
|
||
| const challengeName = challenge?.name; | ||
|
|
||
| // build recipients | ||
| const resources = await helper.getChallengeResources(challengeId); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
|
|
||
| const recipients = Array.from( | ||
| new Set( | ||
| (resources || []) | ||
| .map(r => r?.email || r?.memberEmail) | ||
| .filter(Boolean) | ||
| .map(e => String(e).trim().toLowerCase()) | ||
| ) | ||
| ); | ||
|
|
||
| if (!recipients.length) { | ||
| logger.debug( | ||
| `phase change notification skipped: no recipients for challenge ${challengeId}` | ||
| ); | ||
| return _.omit(result, constants.auditFields); | ||
| } | ||
|
|
||
| // build payload that matches the SendGrid HTML template | ||
| const phaseName = result.name || data.name || challengePhase.name; | ||
|
|
||
| const payload = helper.buildPhaseChangeEmailData({ | ||
| challengeId, | ||
| challengeName, | ||
| phaseName, | ||
| operation, | ||
| at, | ||
| }); | ||
|
|
||
| await helper.sendPhaseChangeNotification(notificationType, recipients, payload); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| } | ||
| } catch (e) { | ||
| logger.debug( | ||
| `phase change notification failed for challenge ${challengeId}, phase ${id}: ${e.message}` | ||
| ); | ||
| } | ||
|
|
||
| return _.omit(result, constants.auditFields); | ||
| } | ||
|
|
||
|
|
||
| partiallyUpdateChallengePhase.schema = { | ||
| currentUser: Joi.any(), | ||
| challengeId: Joi.id(), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[❗❗
correctness]Ensure that
config.PHASE_CHANGE_SENDGRID_TEMPLATE_IDis always defined and valid. If this value can be undefined or incorrect, it may lead to runtime errors when sending notifications.