Skip to content
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
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules
dist
tests
coverage
18 changes: 2 additions & 16 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
{
"extends": [
"airbnb-base",
"plugin:flowtype/recommended"
"@comicrelief/eslint-config"
],
"parser": "babel-eslint",
"plugins": [
"flowtype"
],
"rules": {
"no-param-reassign": "off",
"react/destructuring-assignment": "off",
"flowtype/no-types-missing-file-annotation": "off",
"max-len": "off",
"import/no-cycle": "off",
"no-else-return": "off",
"class-methods-use-this": "off",
"consistent-return": "off"
}
"parser": "@babel/eslint-parser"
}
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Lambda wrapper for all Comic Relief Serverless Projects",
"main": "dist/index.js",
"scripts": {
"lint": "yarn eslint .",
"test": "nyc mocha \"tests/unit/**/*.js\"",
"build": "babel src --out-dir dist --copy-files",
"prepublish": "yarn snyk-protect; yarn build",
Expand All @@ -14,18 +15,25 @@
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/eslint-parser": "^7.11.4",
"@babel/node": "^7.0.0",
"@babel/plugin-syntax-flow": "^7.0.0",
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@comicrelief/eslint-config": "^1.0.6",
"babel-eslint": "^10.1.0",
"babel-plugin-istanbul": "^6.0.0",
"eslint": "^7.5.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-flowtype": "^5.2.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-sonarjs": "^0.5.0",
"eslint-plugin-unicorn": "^21.0.0",
"mocha": "^8.0.1",
"nyc": "^15.1.0",
"prettier": "^2.1.0",
"semantic-release": "^17.1.1",
"serverless-mocha-plugin": "^1.12.0",
"sinon": "^9.0.2",
Expand Down
5 changes: 3 additions & 2 deletions src/DependencyInjection/DependencyInjection.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class DependencyInjection {
*/
get(definition) {
if (typeof this.dependencies[definition] === 'undefined') {
throw Error(`${definition} does not exist in di container`);
throw new TypeError(`${definition} does not exist in di container`);
}

return this.dependencies[definition];
Expand Down Expand Up @@ -69,7 +69,8 @@ export default class DependencyInjection {
getConfiguration(definition = null) {
if (definition !== null && typeof this.configuration[definition] === 'undefined') {
return null;
} else if (typeof this.configuration[definition] !== 'undefined') {
}
if (typeof this.configuration[definition] !== 'undefined') {
return this.configuration[definition];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Model/CloudEvent.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class CloudEventModel extends Model {
this.eventType = '';
this.source = '';
this.eventID = UUID();
this.eventTime = (new Date()).toISOString();
this.eventTime = new Date().toISOString();
this.extensions = {};
this.contentType = 'application/json';
this.data = {};
Expand Down
1 change: 1 addition & 0 deletions src/Model/Model.model.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable class-methods-use-this */
import validate from 'validate.js/validate';

export default class Model {
Expand Down
57 changes: 38 additions & 19 deletions src/Model/SQS/MarketingPreference.model.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable sonarjs/cognitive-complexity */
import validate from 'validate.js';
import Model from '../Model.model';
import requestConstraints from './MarketingPreference.constraints.json';
Expand Down Expand Up @@ -445,14 +446,12 @@ export default class MarketingPreference extends Model {
* @returns {boolean}
*/
isPermissionSet() {
return (this.getPermissionEmail() !== null
&& this.getPermissionEmail() !== '')
|| (this.getPermissionPost() !== null
&& this.getPermissionPost() !== '')
|| (this.getPermissionPhone() !== null
&& this.getPermissionPhone() !== '')
|| (this.getPermissionSMS() !== null
&& this.getPermissionSMS() !== '');
return (
(this.getPermissionEmail() !== null && this.getPermissionEmail() !== '') ||
(this.getPermissionPost() !== null && this.getPermissionPost() !== '') ||
(this.getPermissionPhone() !== null && this.getPermissionPhone() !== '') ||
(this.getPermissionSMS() !== null && this.getPermissionSMS() !== '')
);
}

/**
Expand All @@ -462,24 +461,44 @@ export default class MarketingPreference extends Model {
validate() {
return new Promise((resolve, reject) => {
const requestConstraintsClone = { ...requestConstraints };
if ((this.getPermissionEmail() !== null
&& this.getPermissionEmail() !== ''
&& this.getPermissionEmail() !== '0'
&& this.getPermissionEmail() !== 0)
|| (this.getEmail())) {
if (
(this.getPermissionEmail() !== null &&
this.getPermissionEmail() !== '' &&
this.getPermissionEmail() !== '0' &&
this.getPermissionEmail() !== 0) ||
this.getEmail()
) {
if (this.getEmail()) {
requestConstraintsClone.email = { email: true };
} else {
requestConstraintsClone.email = { presence: { allowEmpty: false }, email: true };
}
}
// Update constraints if fields are not empty
requestConstraintsClone.firstname = this.getFirstName() !== null && this.getFirstName() !== '' ? { format: { pattern: "[a-zA-Z.'-_ ]+", flags: 'i', message: 'can only contain alphabetical characters' } } : '';
requestConstraintsClone.lastname = this.getLastName() !== null && this.getLastName() !== '' ? { format: { pattern: "[a-zA-Z.'-_ ]+", flags: 'i', message: 'can only contain alphabetical characters' } } : '';
requestConstraintsClone.phone = this.getPhone() !== null && this.getPhone() !== '' ? { format: { pattern: '[0-9 ]+', flags: 'i', message: 'can only contain numerical characters' } } : '';
requestConstraintsClone.mobile = this.getMobile() !== null && this.getMobile() !== '' ? { format: { pattern: '[0-9 ]+', flags: 'i', message: 'can only contain numerical characters' } } : '';
requestConstraintsClone.address1 = this.getAddress1() !== null && this.getAddress1() !== '' ? { format: { pattern: "[a-zA-Z.'-_& ]+", flags: 'i', message: 'can only contain alphanumeric characters and . \' - _ &' } } : '';
requestConstraintsClone.country = this.getCountry() !== null && this.getCountry() !== '' ? { format: { pattern: "[a-zA-Z.'-_& ]+", flags: 'i', message: 'can only contain alphabetical characters and . \' - _ &' } } : '';
requestConstraintsClone.firstname =
this.getFirstName() !== null && this.getFirstName() !== ''
? { format: { pattern: "[a-zA-Z.'-_ ]+", flags: 'i', message: 'can only contain alphabetical characters' } }
: '';
requestConstraintsClone.lastname =
this.getLastName() !== null && this.getLastName() !== ''
? { format: { pattern: "[a-zA-Z.'-_ ]+", flags: 'i', message: 'can only contain alphabetical characters' } }
: '';
requestConstraintsClone.phone =
this.getPhone() !== null && this.getPhone() !== ''
? { format: { pattern: '[0-9 ]+', flags: 'i', message: 'can only contain numerical characters' } }
: '';
requestConstraintsClone.mobile =
this.getMobile() !== null && this.getMobile() !== ''
? { format: { pattern: '[0-9 ]+', flags: 'i', message: 'can only contain numerical characters' } }
: '';
requestConstraintsClone.address1 =
this.getAddress1() !== null && this.getAddress1() !== ''
? { format: { pattern: "[a-zA-Z.'-_& ]+", flags: 'i', message: "can only contain alphanumeric characters and . ' - _ &" } }
: '';
requestConstraintsClone.country =
this.getCountry() !== null && this.getCountry() !== ''
? { format: { pattern: "[a-zA-Z.'-_& ]+", flags: 'i', message: "can only contain alphabetical characters and . ' - _ &" } }
: '';

const validation = validate(this.getEntityMappings(), requestConstraintsClone);

Expand Down
2 changes: 1 addition & 1 deletion src/Model/Status.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class StatusModel extends Model {
*/
setStatus(status: string) {
if (typeof STATUS_TYPES[status] === 'undefined') {
throw new Error(`${StatusModel.name} - ${status} is not a valid status type`);
throw new TypeError(`${StatusModel.name} - ${status} is not a valid status type`);
}

this.status = status;
Expand Down
52 changes: 27 additions & 25 deletions src/Service/Logger.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ export const logger = Winston.createLogger({
replacer: (key, value) => {
if (value instanceof Buffer) {
return value.toString('base64');
} else if (value instanceof Error) {
}
if (value instanceof Error) {
const error = {};

Object.getOwnPropertyNames(value).forEach(((objectKey) => {
Object.getOwnPropertyNames(value).forEach((objectKey) => {
error[objectKey] = value[objectKey];
}));
});

return error;
}

return value;
},
}),
})
),
transports: [
new Winston.transports.Console(),
],
transports: [new Winston.transports.Console()],
});

// Instantiate the sentry client
const sentryIsAvailable = typeof process.env.RAVEN_DSN !== 'undefined' && (typeof process.env.RAVEN_DSN === 'string' && process.env.RAVEN_DSN !== 'undefined');
const sentryIsAvailable =
typeof process.env.RAVEN_DSN !== 'undefined' && typeof process.env.RAVEN_DSN === 'string' && process.env.RAVEN_DSN !== 'undefined';

if (sentryIsAvailable) {
Sentry.init({
Expand All @@ -49,10 +49,12 @@ export default class LoggerService extends DependencyAwareClass {
constructor(di: DependencyInjection) {
super(di);
this.sentry = null;
this.logger = logger;

const container = this.getContainer();
const event = container.getEvent();
const context = container.getContext();
const isOffline = !Object.prototype.hasOwnProperty.call(context, 'invokedFunctionArn') || context.invokedFunctionArn.indexOf('offline') !== -1;
const isOffline = !Object.prototype.hasOwnProperty.call(context, 'invokedFunctionArn') || context.invokedFunctionArn.includes('offline');

// Set sentry client context
if (sentryIsAvailable && isOffline === false) {
Expand Down Expand Up @@ -87,11 +89,11 @@ export default class LoggerService extends DependencyAwareClass {
}

if (
typeof process.env.EPSAGON_TOKEN === 'string'
&& process.env.EPSAGON_TOKEN !== 'undefined'
&& typeof process.env.EPSAGON_SERVICE_NAME === 'string'
&& process.env.EPSAGON_SERVICE_NAME !== 'undefined'
&& error instanceof Error
typeof process.env.EPSAGON_TOKEN === 'string' &&
process.env.EPSAGON_TOKEN !== 'undefined' &&
typeof process.env.EPSAGON_SERVICE_NAME === 'string' &&
process.env.EPSAGON_SERVICE_NAME !== 'undefined' &&
error instanceof Error
) {
Epsagon.setError(error);
}
Expand All @@ -114,7 +116,7 @@ export default class LoggerService extends DependencyAwareClass {
* @param message string
*/
info(message) {
logger.log('info', message);
this.logger.log('info', message);
}

/**
Expand All @@ -124,16 +126,16 @@ export default class LoggerService extends DependencyAwareClass {
*/
label(descriptor, silent = false) {
if (
typeof process.env.EPSAGON_TOKEN === 'string'
&& process.env.EPSAGON_TOKEN !== 'undefined'
&& typeof process.env.EPSAGON_SERVICE_NAME === 'string'
&& process.env.EPSAGON_SERVICE_NAME !== 'undefined'
typeof process.env.EPSAGON_TOKEN === 'string' &&
process.env.EPSAGON_TOKEN !== 'undefined' &&
typeof process.env.EPSAGON_SERVICE_NAME === 'string' &&
process.env.EPSAGON_SERVICE_NAME !== 'undefined'
) {
Epsagon.label(descriptor);
}

if (silent === false) {
logger.log('info', `label - ${descriptor}`);
this.logger.log('info', `label - ${descriptor}`);
}
}

Expand All @@ -145,16 +147,16 @@ export default class LoggerService extends DependencyAwareClass {
*/
metric(descriptor, stat, silent = false) {
if (
typeof process.env.EPSAGON_TOKEN === 'string'
&& process.env.EPSAGON_TOKEN !== 'undefined'
&& typeof process.env.EPSAGON_SERVICE_NAME === 'string'
&& process.env.EPSAGON_SERVICE_NAME !== 'undefined'
typeof process.env.EPSAGON_TOKEN === 'string' &&
process.env.EPSAGON_TOKEN !== 'undefined' &&
typeof process.env.EPSAGON_SERVICE_NAME === 'string' &&
process.env.EPSAGON_SERVICE_NAME !== 'undefined'
) {
Epsagon.label(descriptor, stat);
}

if (silent === false) {
logger.log('info', `metric - ${descriptor} - ${stat}`);
this.logger.log('info', `metric - ${descriptor} - ${stat}`);
}
}
}
Loading