Skip to content

Standard lint #67

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 4 commits into from
Oct 25, 2022
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
53 changes: 25 additions & 28 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
version: 2
defaults: &defaults
docker:
- image: circleci/python:2.7-stretch-browsers
docker:
- image: circleci/python:2.7-stretch-browsers
install_dependency: &install_dependency
name: Installation of build and deployment dependencies.
command: |
sudo apt install jq
sudo pip install awscli --upgrade
sudo pip install docker-compose
name: Installation of build and deployment dependencies.
command: |
sudo apt install jq
sudo pip install awscli --upgrade
sudo pip install docker-compose

install_deploysuite: &install_deploysuite
name: Installation of install_deploysuite.
#Git Clone -change back to v1.3 or latest once counter var is generalized.
command: |
git clone --branch master https://github.com/topcoder-platform/tc-deploy-scripts ../buildscript
cp ./../buildscript/master_deploy.sh .
cp ./../buildscript/buildenv.sh .
cp ./../buildscript/awsconfiguration.sh .
name: Installation of install_deploysuite.
#Git Clone -change back to v1.3 or latest once counter var is generalized.
command: |
git clone --branch master https://github.com/topcoder-platform/tc-deploy-scripts ../buildscript
cp ./../buildscript/master_deploy.sh .
cp ./../buildscript/buildenv.sh .
cp ./../buildscript/awsconfiguration.sh .

restore_cache_settings_for_build: &restore_cache_settings_for_build
key: circleci-tc-email-service-{{ checksum "package-lock.json" }}

Expand All @@ -27,9 +27,8 @@ save_cache_settings: &save_cache_settings
- node_modules

run_build: &run_build
name: Build of Docker image
command: ./build.sh

name: Build of Docker image
command: ./build.sh

jobs:
# Build & Deploy against development backend rer
Expand All @@ -45,14 +44,13 @@ jobs:
- save_cache: *save_cache_settings
- deploy:
name: Running Masterscript - deploy tc-email-service
command: |
command: |
./awsconfiguration.sh DEV
source awsenvconf
./buildenv.sh -e DEV -b dev-tc-email-service-deployvar
source buildenvvar
./master_deploy.sh -d ECS -e DEV -t latest -s dev-global-appvar,dev-tc-email-service-appvar -i tc-email-service -p FARGATE


./master_deploy.sh -d ECS -e DEV -t latest -s dev-global-appvar,dev-tc-email-service-appvar -i tc-email-service -p FARGATE

"build-prod":
<<: *defaults
steps:
Expand All @@ -70,21 +68,20 @@ jobs:
source awsenvconf
./buildenv.sh -e PROD -b prod-tc-email-service-deployvar
source buildenvvar
./master_deploy.sh -d ECS -e PROD -t latest -s prod-global-appvar,prod-tc-email-service-appvar -i tc-email-service -p FARGATE
./master_deploy.sh -d ECS -e PROD -t latest -s prod-global-appvar,prod-tc-email-service-appvar -i tc-email-service


workflows:
version: 2
build:
jobs:
# Development builds are executed on "develop" branch only.
- "build-dev":
context : org-global
context: org-global
filters:
branches:
only: ['dev', 'feature/decouple-sendgridtemplateid']
only: ["dev", "feature/decouple-sendgridtemplateid"]
- "build-prod":
context : org-global
context: org-global
filters:
branches:
only: master
9 changes: 0 additions & 9 deletions .eslintrc.js

This file was deleted.

21 changes: 11 additions & 10 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ module.exports = {
dialect: 'postgres',
logging: false,
dialectOptions: {
ssl: process.env.DATABASE_SSL != null,
ssl: process.env.DATABASE_SSL != null
},
pool: {
max: 5,
min: 0,
idle: 10000,
},
idle: 10000
}
},
DISABLE_LOGGING: process.env.DISABLE_LOGGING || 'false',

Expand All @@ -29,8 +29,9 @@ module.exports = {

KAFKA_GROUP_ID: process.env.KAFKA_GROUP_ID,
KAFKA_CLIENT_CERT: process.env.KAFKA_CLIENT_CERT ? process.env.KAFKA_CLIENT_CERT.replace('\\n', '\n') : null,
KAFKA_CLIENT_CERT_KEY: process.env.KAFKA_CLIENT_CERT_KEY ?
process.env.KAFKA_CLIENT_CERT_KEY.replace('\\n', '\n') : null,
KAFKA_CLIENT_CERT_KEY: process.env.KAFKA_CLIENT_CERT_KEY
? process.env.KAFKA_CLIENT_CERT_KEY.replace('\\n', '\n')
: null,

// mapping from event type to sendgrid email template id
TEMPLATE_MAP: process.env.TEMPLATE_MAP,
Expand All @@ -41,16 +42,16 @@ module.exports = {
EMAIL_MAX_ERRORS: process.env.EMAIL_MAX_ERRORS || 2,
EMAIL_PAUSE_TIME: process.env.EMAIL_PAUSE_TIME || 30,

//in every 2 minutes will retry for failed status
// in every 2 minutes will retry for failed status
EMAIL_RETRY_SCHEDULE: process.env.EMAIL_RETRY_SCHEDULE || '0 */2 * * * *',
//wont't retry failed emails older than this time (msec)
// wont't retry failed emails older than this time (msec)
EMAIL_RETRY_MAX_AGE: process.env.EMAIL_RETRY_MAX_AGE || 1000 * 60 * 60 * 24,

API_CONTEXT_PATH: process.env.API_CONTEXT_PATH || '/v5/email',

PAYLOAD_SENDGRID_TEMPLATE_KEY: process.env.PAYLOAD_SENDGRID_TEMPLATE_KEY || 'sendgrid_template_id',
//Tracing information
// Tracing information
APM_OTLP_TRACE_EXPORTER_URL: process.env.APM_OTLP_TRACE_EXPORTER_URL || '',
APM_SERVICE_NAME: process.env.APM_SERVICE_NAME || 'tc-email-service-svc',
APM_TRACER_NAME: process.env.APM_TRACER_NAME || 'tc-email-service-svc',
};
APM_TRACER_NAME: process.env.APM_TRACER_NAME || 'tc-email-service-svc'
}
4 changes: 2 additions & 2 deletions config/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
*/
module.exports = {
LOG_LEVEL: process.env.LOG_LEVEL || 'debug',
PORT: process.env.PORT || 4000,
};
PORT: process.env.PORT || 4000
}
4 changes: 2 additions & 2 deletions config/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
module.exports = {
LOG_LEVEL: process.env.LOG_LEVEL || 'error',
PORT: process.env.PORT || 4000,
DISABLE_LOGGING: process.env.DISABLE_LOGGING || 'true',
};
DISABLE_LOGGING: process.env.DISABLE_LOGGING || 'true'
}
4 changes: 2 additions & 2 deletions config/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
*/
module.exports = {
LOG_LEVEL: process.env.LOG_LEVEL || 'debug',
PORT: process.env.PORT || 4000,
};
PORT: process.env.PORT || 4000
}
39 changes: 19 additions & 20 deletions connect/connectEmailServer.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
/**
* This is TopCoder connect email server.
*/
const _ = require('lodash');
const config = require('config');
const emailServer = require('../index');
const service = require('./service');
const logger = require('../src/common/logger');
const _ = require('lodash')
const config = require('config')
const emailServer = require('../index')
const service = require('./service')
const logger = require('../src/common/logger')

// set configuration for the server, see ../config/default.js for available config parameters
// setConfig should be called before initDatabase and start functions
emailServer.setConfig({ LOG_LEVEL: config.LOG_LEVEL });
emailServer.setConfig({ LOG_LEVEL: config.LOG_LEVEL })

// add topic handlers,
// handler is used build a notification list for a message of a topic,
// it is defined as: function(topic, message),
// the topic is topic name,
// the message is JSON event message,
const handler = async (topic, message) => {
let templateId = config.TEMPLATE_MAP[topic];
templateId = _.get(message, config.PAYLOAD_SENDGRID_TEMPLATE_KEY, templateId);
let templateId = config.TEMPLATE_MAP[topic]
templateId = _.get(message, config.PAYLOAD_SENDGRID_TEMPLATE_KEY, templateId)
if (!templateId) {
return { success: false, error: `Template not found for topic ${topic}` };
return { success: false, error: `Template not found for topic ${topic}` }
}

try {
await service.sendEmail(templateId, message)
return { success: true };
return { success: true }
} catch (err) {
logger.error("Error occurred in sendgrid api calling:", err);
return { success: false, error: err };
logger.error('Error occurred in sendgrid api calling:', err)
return { success: false, error: err }
}

};
}

// init all events
_.keys(config.TEMPLATE_MAP).forEach((eventType) => {
emailServer.addTopicHandler(eventType, handler);
});
emailServer.addTopicHandler(eventType, handler)
})

// init database, it will clear and re-create all tables
emailServer
Expand All @@ -47,9 +46,9 @@ emailServer
emailServer.start()
})
.catch((e) => {
logger.error('Error occurred in starting email server:', e);
process.exit(1);
}); // eslint-disable-line no-console
logger.error('Error occurred in starting email server:', e)
process.exit(1)
}) // eslint-disable-line no-console

// if no need to init database, then directly start the server:
// emailServer.start()
// emailServer.start()
52 changes: 26 additions & 26 deletions connect/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
* This is TopCoder connect email service.
*/

const sgMail = require('@sendgrid/mail');
const config = require('config');
const logger = require('../src/common/logger');
const sgMail = require('@sendgrid/mail')
const config = require('config')
const logger = require('../src/common/logger')

// set api key for SendGrid email client
sgMail.setApiKey(config.SENDGRID_API_KEY);
sgMail.setApiKey(config.SENDGRID_API_KEY)

const sendEmail = async (templateId, message) => { // send email
const span = await logger.startSpan('sendEmail');
const span = await logger.startSpan('sendEmail')
let msg = {}
const from = message.from ? message.from : config.EMAIL_FROM;
const replyTo = message.replyTo ? message.replyTo : config.EMAIL_FROM;
const substitutions = message.data;
const categories = message.categories ? message.categories : [];
const to = message.recipients;
const cc = message.cc ? message.cc : [];
const bcc = message.bcc ? message.bcc : [];
const sendAt = message.sendAt ? message.sendAt : undefined;
const from = message.from ? message.from : config.EMAIL_FROM
const replyTo = message.replyTo ? message.replyTo : config.EMAIL_FROM
const substitutions = message.data
const categories = message.categories ? message.categories : []
const to = message.recipients
const cc = message.cc ? message.cc : []
const bcc = message.bcc ? message.bcc : []
const sendAt = message.sendAt ? message.sendAt : undefined
const personalizations = message.personalizations ? message.personalizations : undefined
const attachments = message.attachments ? message.attachments : [];
const attachments = message.attachments ? message.attachments : []

if (message.version && message.version == "v3") {
if (message.version && message.version === 'v3') {
msg = {
to,
templateId,
Expand All @@ -36,7 +36,7 @@ const sendEmail = async (templateId, message) => { // send email
bcc,
attachments,
sendAt
};
}
} else {
msg = {
to,
Expand All @@ -47,23 +47,23 @@ const sendEmail = async (templateId, message) => { // send email
replyTo,
categories,
cc,
bcc,
};
bcc
}
}
logger.info(`Sending email with templateId: ${templateId} and message: ${JSON.stringify(msg)}`);
logger.info(`Sending email with templateId: ${templateId} and message: ${JSON.stringify(msg)}`)
try {
await logger.endSpan(span);
const sgSpan = await logger.startSpan('sendgrid');
await logger.endSpan(span)
const sgSpan = await logger.startSpan('sendgrid')
const result = await sgMail.send(msg)
await logger.endSpan(sgSpan);
logger.info(`Email sent successfully with result: ${JSON.stringify(result)} \n ${JSON.stringify(msg)}`);
await logger.endSpan(sgSpan)
logger.info(`Email sent successfully with result: ${JSON.stringify(result)} \n ${JSON.stringify(msg)}`)
return result
} catch (err) {
logger.error(`Error occurred in sendgrid api calling: ${err}`);
logger.error(`Error occurred in sendgrid api calling: ${err}`)
throw err
}
}
module.exports = {
sendEmail,
sendEmail
}
logger.buildService(module.exports)
logger.buildService(module.exports)
Loading