diff --git a/.circleci/config.yml b/.circleci/config.yml index 2297526..1628f9a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -102,7 +102,7 @@ workflows: context : org-global filters: branches: - only: [dev, 'hotfix/V5-API-Standards'] + only: [dev, 'hotfix/V5-API-Standards', 'v5-upgrade'] - "build-prod": context : org-global filters: diff --git a/README.md b/README.md index 808a611..affbd38 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ tc-notifications (as a standard nodejs app) provides generic framework around no 5. Either add deployment for this new notification consumer/processor in existing deployment script (if you want to host the processor as separate service in the same ECS cluster) or write a new script if you want to keep the deployment separate. ## Dependencies -- nodejs https://nodejs.org/en/ (v6+, if newer version of node is used, e.g. v10, then it needs to install extra lib `npm i natives@1.1.6` to support the gulp build) +- nodejs https://nodejs.org/en/ (v6+, if newer version of node is used, e.g. v10, then it needs to install extra lib `npm i natives@1.1.6` to support the gulp build) - Heroku Toolbelt https://toolbelt.heroku.com - git - PostgreSQL 9.5 @@ -50,8 +50,8 @@ The following parameters can be set in config files or in env variables: if not provided, then SSL connection is not used, direct insecure connection is used; if provided, it can be either path to private key file or private key content - **Topcoder API** - - `TC_API_V3_BASE_URL`: the TopCoder API V3 base URL - - `TC_API_V4_BASE_URL`: the TopCoder API V4 base URL + - `TC_API_V3_BASE_URL`: the TopCoder API V3 base URL + - `TC_API_V4_BASE_URL`: the TopCoder API V4 base URL - `TC_API_V5_BASE_URL`: the TopCoder API V5 base URL - **Notifications API** - `API_CONTEXT_PATH`: path to serve API on @@ -61,16 +61,16 @@ The following parameters can be set in config files or in env variables: - `TOKEN_CACHE_TIME`: time period of the cached token - `AUTH0_CLIENT_ID`: auth0 client id - `AUTH0_CLIENT_SECRET`: auth0 client secret - - `AUTH0_PROXY_SERVER_URL`: auth0 proxy server URL -- **Consumer handlers** - - `KAFKA_CONSUMER_HANDLERS`: mapping from consumer topic to handlers -- **Email notification** - - `ENV`: used to construct email category - - `ENABLE_EMAILS`: whether to enable email notifications - - `ENABLE_DEV_MODE`: whether to enable dev mode - - `DEV_MODE_EMAIL`: recipient email used in dev mode - - `DEFAULT_REPLY_EMAIL`: default reply email - + - `AUTH0_PROXY_SERVER_URL`: auth0 proxy server URL +- **Consumer handlers** + - `KAFKA_CONSUMER_HANDLERS`: mapping from consumer topic to handlers +- **Email notification** + - `ENV`: used to construct email category + - `ENABLE_EMAILS`: whether to enable email notifications + - `ENABLE_DEV_MODE`: whether to enable dev mode + - `DEV_MODE_EMAIL`: recipient email used in dev mode + - `DEFAULT_REPLY_EMAIL`: default reply email + ### Connect notification server Configuration for the connect notification server is at `connect/config.js`. @@ -137,6 +137,7 @@ You may reuse it during review. - start local PostgreSQL db, create an empty database, update the config/default.js DATABASE_URL param to point to the db - install dependencies `npm i` - run code lint check `npm run lint` +- init DB `npm run reset:db` - start connect notification server `npm start` - the app is running at `http://localhost:4000`, it also starts Kafka consumer to listen for events and save unroll-ed notifications to db diff --git a/connect/config.js b/connect/config.js index f2c414a..48751c7 100644 --- a/connect/config.js +++ b/connect/config.js @@ -6,6 +6,7 @@ module.exports = { // TC API related variables TC_API_V3_BASE_URL: process.env.TC_API_V3_BASE_URL || 'https://api.topcoder-dev.com/v3', TC_API_V4_BASE_URL: process.env.TC_API_V4_BASE_URL || 'https://api.topcoder-dev.com/v4', + TC_API_V5_BASE_URL: process.env.TC_API_V5_BASE_URL || 'https://api.topcoder-dev.com/v5', MESSAGE_API_BASE_URL: process.env.MESSAGE_API_BASE_URL || 'https://api.topcoder-dev.com/v5', // id of the BOT user which creates post with various events in discussions diff --git a/connect/service.js b/connect/service.js index aae9f38..e6f734c 100644 --- a/connect/service.js +++ b/connect/service.js @@ -19,17 +19,17 @@ const getProject = (projectId) => ( M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET) .then((token) => ( request - .get(`${config.TC_API_V4_BASE_URL}/projects/${projectId}`) + .get(`${config.TC_API_V5_BASE_URL}/projects/${projectId}`) .set('accept', 'application/json') .set('authorization', `Bearer ${token}`) .then((res) => { - if (!_.get(res, 'body.result.success')) { + const project = res.body; + if (!project) { throw new Error(`Failed to get project details of project id: ${projectId}`); } - const project = _.get(res, 'body.result.content'); return project; }).catch((err) => { - const errorDetails = _.get(err, 'response.body.result.content.message'); + const errorDetails = _.get(err, 'response.body.message'); throw new Error( `Failed to get project details of project id: ${projectId}.` + (errorDetails ? ' Server response: ' + errorDetails : '') @@ -244,17 +244,17 @@ const getPhase = (projectId, phaseId) => ( M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET) .then((token) => ( request - .get(`${config.TC_API_V4_BASE_URL}/projects/${projectId}/phases/${phaseId}`) + .get(`${config.TC_API_V5_BASE_URL}/projects/${projectId}/phases/${phaseId}`) .set('accept', 'application/json') .set('authorization', `Bearer ${token}`) .then((res) => { - if (!_.get(res, 'body.result.success')) { + const project = res.body; + if (!project) { throw new Error(`Failed to get phase details of project id: ${projectId}, phase id: ${phaseId}`); } - const project = _.get(res, 'body.result.content'); return project; }).catch((err) => { - const errorDetails = _.get(err, 'response.body.result.content.message'); + const errorDetails = _.get(err, 'response.body.message'); throw new Error( `Failed to get phase details of project id: ${projectId}, phase id: ${phaseId}.` + (errorDetails ? ' Server response: ' + errorDetails : '') diff --git a/package.json b/package.json index 3789de8..5ed55b2 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "lint:fix": "eslint *.js --fix src config test connect", "postinstall": "npm run build", "build": "gulp build", - "watch": "gulp watch" + "watch": "gulp watch", + "reset:db": "node tools/resetDb.js" }, "author": "TCSCODER", "license": "none", diff --git a/tools/resetDb.js b/tools/resetDb.js new file mode 100644 index 0000000..c393234 --- /dev/null +++ b/tools/resetDb.js @@ -0,0 +1,20 @@ +/** + * Script for initializing/resetting DB. + * + * WARNING + * It recreates DB schema causing removing existent data if any. + */ +const notificationServer = require('../index'); + +console.info('Initializing db...'); + +notificationServer + .initDatabase() + .then(() => { + console.info('Database was successfully initialized.'); + process.exit(); + }) + .catch((err) => { + console.error('Error initializing DB', err); + process.exit(1); + }); \ No newline at end of file