diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..3e14308b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,32 @@ +{ + "version": "0.2.0", + "configurations": [ + + + { + "type": "node", + "request": "launch", + "name": "Mocha Tests", + "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", + "args": [ + "--slow", + "5000", + "--colors", + "${workspaceFolder}/test/**/*.test.js", + ], + "internalConsoleOptions": "openOnSessionStart", + "skipFiles": [ + "/**" + ] + }, + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/src/index.js" + } + ] +} \ No newline at end of file diff --git a/src/support/config.js b/src/support/config.js index 68c87bc4..f9fd90bd 100644 --- a/src/support/config.js +++ b/src/support/config.js @@ -11,16 +11,35 @@ */ import { isArray } from '@adobe/spacecat-shared-utils'; -export const isDigestReport = (conf, alertType) => { - const alertConfig = isArray(conf?.alerts) - ? conf?.alerts.find((alert) => alert.type === alertType) - : {}; - return alertConfig?.byOrg; +export const getAlertConfig = (conf, alertType) => (isArray(conf?.alerts) + ? conf?.alerts.find((alert) => alert.type === alertType) : null); + +// export const isDigestReport = (conf, alertType) => { +// const alertConfig = isArray(conf?.alerts) +// ? conf?.alerts.find((alert) => alert.type === alertType) +// : {}; +// return alertConfig?.byOrg; +// }; + +// used AI just to see if I could get the test to work -- it is still failing +export const isDigestReport = (orgConf, siteConf, alertType) => { + if (getAlertConfig(siteConf, alertType) !== null) { + return getAlertConfig(siteConf, alertType).byOrg; + } + if (getAlertConfig(orgConf, alertType) !== null) { + return getAlertConfig(orgConf, alertType).byOrg; + } + return false; }; -export const hasAlertConfig = (conf, alertType) => isArray(conf?.alerts) - && conf?.alerts.find((alert) => alert.type === alertType); +// TODO replace current implementation with the new one +// export const isDigestReport = (orgConf, siteConf, alertType) => { +// if getAlertConfig for siteConf is not null/undefined return siteAlertConfig.byOrg value +// else return getAlertConfig for orgConf is not null/undefined return orgAlertConfig.byOrg value +// else return false +// }; +export const hasAlertConfig = (conf, alertType) => !!getAlertConfig(conf, alertType); const getSlackContext = (conf, alertType) => { const channel = conf?.slack?.channel; const alertConfig = isArray(conf?.alerts) diff --git a/test/support/config.test.js b/test/support/config.test.js index 0d9c894a..a3cabb25 100644 --- a/test/support/config.test.js +++ b/test/support/config.test.js @@ -70,6 +70,27 @@ describe('config util', () => { expect(slackContext.mentions).to.deep.equal(['slackId2']); }); + it('isDigestReport for valid configs bySite', () => { + const orgConfig = { + alerts: [{ + type: '404', + }], + }; + const siteConfig = { + slack: { + workspace: 'workspace2', + channel: 'channel2', + }, + alerts: [{ + type: '404', + byOrg: false, + mentions: [{ slack: ['slackId2'] }], + }], + }; + const isDigest = isDigestReport(orgConfig, siteConfig, '404'); + expect(isDigest).to.be.false; + }); + it('getSlackContextForAlert returns only slack channel if no alerts are in config', () => { const orgConfig = { slack: {