Node.js
If you (or your company) needs a freelance worker for a project, Awesome Dude is the right choice for you. I develop ES6/7 full-stack
so if you want an API fixed up, give me a heads up and I'll take care of it. Delivery in a git repository and/or deploying to production.
class MySQLHandler {
static connection(connectionController, database) {
let conn;
// vervangen met process.env.NODE_ENV
switch (connectionController) {
case 'dev':
conn = {
host: 'localhost',
user: 'yourusername',
password: 'yourpassword',
database,
};
break;
case 'production':
conn = {
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASS,
database,
};
break;
default:
conn = {
host: 'localhost',
user: 'yourusername',
password: 'yourpassword',
database,
};
}
return mysql.createConnection(conn);
}
static getDatabase(controller) {
let response;
switch (controller) {
case 'General':
response = process.env.MYSQL_DB_GENERAL;
break;
case 'StockInfo':
response = process.env.MYSQL_DB_STOCKINFO;
break;
default:
Promise.reject(new productionErrors.ConnectionError({
message: 'Error: Code 418 Oops, something went wrong in the mysql handler.',
}));
}
return response;
}
static async insert(sql, databaseController) {
// const sql = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
let con;
try {
const databaseName = this.getDatabase(databaseController);
con = this.connection('production', databaseName);
} catch (error) {
throw error;
}
con.connect((err) => {
if (err) throw new productionErrors.ConnectionError(err);
});
con.query(sql, ((err2, result) => {
if (err2) throw new productionErrors.MySQLError(`SQL Message: ${err2.sqlMessage} - SQL: ${err2.sql}`);
console.log('1 record inserted');
return result;
}));
}
static async insertMany(databaseController, table, rowNames, values) {
// const values = [
// ['John', 'Highway 71'],
// ['Peter', 'Lowstreet 4'],
// ['Amy', 'Apple st 652'],
// ['Hannah', 'Mountain 21'],
// ['Michael', 'Valley 345'],
// ['Sandy', 'Ocean blvd 2'],
// ['Betty', 'Green Grass 1'],
// ['Richard', 'Sky st 331'],
// ['Susan', 'One way 98'],
// ['Vicky', 'Yellow Garden 2'],
// ['Ben', 'Park Lane 38'],
// ['William', 'Central st 954'],
// ['Chuck', 'Main Road 989'],
// ['Viola', 'Sideway 1633']
// ];
let con;
const sql = `INSERT INTO ${table} (${rowNames}) VALUES ?`;
try {
const databaseName = this.getDatabase(databaseController);
con = this.connection('production', databaseName);
} catch (err) {
log.error(err.message, 'mysql.handler.js - Function insertMany - Setting up the database connection');
Promise.reject(err);
}
con.connect((err2) => {
if (err2) {
log.error(err2.message, 'mysql.handler.js - Function insertMany - Connecting to the database');
Promise.reject(err2);
}
});
return new Promise((resolve, reject) => {
con.query(sql, [values], ((err3, result) => {
if (err3) reject(err3);
if (result === undefined) reject(new Error('MySQL result undefined'));
if (result !== undefined) {
// log.info(`Number of records inserted: ${result.affectedRows}`);
con.end();
resolve(`Number of records inserted: ${result.affectedRows}`);
}
reject(new Error('Oops, something went wrong with the MySQL Handler'));
}));
});
}
static async update(sql, databaseController) {
const databaseName = this.getDatabase(databaseController);
const con = this.connection('production', databaseName);
con.connect((err) => {
if (err) throw new productionErrors.ConnectionError(err);
// const sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley 345'";
});
con.query(sql, ((err2, result) => {
if (err2) throw new productionErrors.MySQLError(`SQL Message: ${err2.sqlMessage} - SQL: ${err2.sql}`);
console.log(`${result.affectedRows} record(s) updated`);
return `${result.affectedRows} record(s) updated`;
}));
}
static async delete(sql, databaseController) {
let con;
try {
const databaseName = this.getDatabase(databaseController);
con = this.connection('production', databaseName);
} catch (error) {
log.error(error.message, 'mysql.handler.js - Method delete - Setting up the database connection');
Promise.reject(error);
}
con.connect((err) => {
if (err) {
log.error(err.message, 'mysql.handler.js - Method delete - Connecting to the database');
Promise.reject(new productionErrors.ConnectionError(err.message));
}
// const sql = "DELETE FROM customers WHERE address = 'Mountain 21'";
});
return new Promise((resolve, reject) => {
con.query(sql, ((err2, result) => {
if (err2) reject(new productionErrors.MySQLError(`SQL Message: ${err2.sqlMessage} - SQL: ${err2.sql}`));
if (result === undefined || result === null) reject(new productionErrors.MySQLError('MySQL result undefined'));
if (result !== undefined) {
con.end();
resolve(`Number of records deleted: ${result.affectedRows}`);
}
reject(new productionErrors.MySQLError('Oops, something went wrong with the MySQL Handler'));
}));
});
}
static async deleteMany(sql, values, databaseController) {
let con;
try {
const databaseName = this.getDatabase(databaseController);
con = this.connection('production', databaseName);
} catch (error) {
Promise.reject(error);
}
con.connect((err) => {
if (err) {
log.error(err.message, 'mysql.handler.js - Method delete - Connecting to the database');
Promise.reject(new productionErrors.ConnectionError(err.message));
}
// const sql = "DELETE FROM customers WHERE address = 'Mountain 21'";
});
return new Promise((resolve, reject) => {
con.query(sql, values, ((err2, result) => {
if (err2) reject(new productionErrors.MySQLError(`SQL Message: ${err2.sqlMessage} - SQL: ${err2.sql}`));
if (result === undefined || result === null) reject(new productionErrors.MySQLError('MySQL result undefined'));
if (result !== undefined) {
con.end();
resolve(`Number of records deleted: ${result.affectedRows}`);
}
reject(new productionErrors.MySQLError('Oops, something went wrong with the MySQL Handler'));
}));
});
}
static async selectSafe(sql, values, databaseController) {
let con;
try {
const databaseName = this.getDatabase(databaseController);
con = this.connection('production', databaseName);
} catch (error) {
Promise.reject(error);
}
con.connect((err) => {
if (err) {
log.error(err.message, 'mysql.handler.js - Method select safe - Connecting to the database');
Promise.reject(new productionErrors.ConnectionError(err.message));
}
// const sql = "DELETE FROM customers WHERE address = 'Mountain 21'";
});
return new Promise((resolve, reject) => {
con.query(sql, values, ((err2, result) => {
if (err2) reject(new productionErrors.MySQLError(`SQL Message: ${err2.sqlMessage} - SQL: ${err2.sql}`));
if (result === undefined || result === null) reject(new productionErrors.MySQLError('MySQL result undefined'));
if (result !== undefined) {
con.end();
resolve(result);
}
reject(new productionErrors.MySQLError('Oops, something went wrong with the MySQL Handler'));
}));
});
}
static async select(sql, databaseController) {
let con;
try {
const databaseName = this.getDatabase(databaseController);
con = this.connection('production', databaseName);
} catch (error) {
throw error;
}
return new Promise((resolve, reject) => {
con.connect((err) => {
if (err) reject(err);
});
con.query(sql, ((err2, result) => {
if (err2) reject(new productionErrors.MySQLError(`SQL Message: ${err2.sqlMessage} - SQL: ${err2.sql}`));
// console.log(result);
resolve(result);
}));
});
}
}
module.exports = {
MySQLHandler,
};
const cronJobGetBalanceSheets = async () => {
log.info('Start getting the balance sheets', 'cronJob.iex.controller.js - Function cronJobGetBalanceSheets');
const [error, availableStock] = await promiseHandler(stockListHelper('balanceSheetStatements'));
if (error) log.error(error.message, 'cronJob.iex.controller.js - Function cronJobGetBalanceSheets - Getting the available stock symbols with the stock list helper', error.stack !== undefined ? error.stack : '');
if (availableStock) {
let balanceSheetArray = [];
let result;
let error3;
let countResultSets = 0;
const sanitizedAvailableStock = [];
const countTotal = availableStock.length;
for (let i = 0; i < countTotal; i += 1) {
if (availableStock[i].symbol !== null) {
const { symbol } = availableStock[i];
sanitizedAvailableStock.push({ symbol });
}
}
const countSanitized = sanitizedAvailableStock.length;
const results = [];
let countForSlack = 0;
// eslint-disable-next-line consistent-return
for (let i = 0; i < countSanitized; i += 1) {
// availableStock.forEach(async (element, i) => {
if (sanitizedAvailableStock[i].symbol !== null) {
countForSlack += 1;
const { symbol } = sanitizedAvailableStock[i];
// eslint-disable-next-line no-await-in-loop
const [error2, apiResult] = await promiseHandler(axios.get(`https://cloud.iexapis.com/stable/stock/${symbol}/balance-sheet?period=annual&last=${Config.cronJobGetStocksLast}&token=${process.env.IEX_TOKEN}`));
if (error2) log.error(error2.message, 'cronJob.iex.controller.js - Function cronJobGetBalanceSheets - Getting the balance sheets for loop -> API Call');
if (apiResult !== undefined && apiResult !== null
&& apiResult.status === 200) countResultSets += 1;
if (apiResult !== null && apiResult !== undefined
&& apiResult.status === 200 && apiResult.data !== null) {
balanceSheetArray.push(apiResult.data);
}
if (countForSlack === 1000) {
SlackLogger.info(`Total getting the balance sheets to gather: ${countSanitized} Current: ${countResultSets}`);
countForSlack = 0;
}
if (balanceSheetArray.length === Number(process.env.SCRAPING_ITERATIONS_FOR_DELAY)) {
[error3, result] =
// eslint-disable-next-line no-await-in-loop
await promiseHandler(IEXController.saveBalanceSheets(balanceSheetArray));
if (error3) {
log.error(error3.message, 'cronJob.iex.controller.js - Function cronJobGetBalanceSheets - Promise saveBalanceSheets', error3.stack !== undefined ? error3.stack : '');
}
if (result) {
results.push(result);
}
balanceSheetArray = [];
// eslint-disable-next-line no-await-in-loop
await delay(process.env.SCRAPING_DELAY_MS);
}
if (i === countSanitized - 1) {
[error3, result] =
// eslint-disable-next-line no-await-in-loop
await promiseHandler(IEXController.saveBalanceSheets(balanceSheetArray));
if (error3) log.error(error3.message, 'cronJob.iex.controller.js - Function cronJobGetBalanceSheets - Promise saveBalanceSheets', error3.stack !== undefined ? error3.stack : '');
if (result !== undefined) results.push(result);
resetStatus('cronStatus');
ResultLogger.write(results, 'info', 'cronJob.iex.controller.js - Function cronJobGetBalanceSheets');
log.info(`Finished getting the balance sheets to the database. Date: ${DateTime.convertDateTime(new Date())}`, 'cronJob.iex.controller.js - Function cronJobGetBalanceSheets');
}
}
}
}
};
const fs = require('fs');
const zlib = require('zlib');
const {
writeJSONFileSync,
readJSONSync,
checkIfFileExist,
DateTime,
} = require('../helpers/general.helper');
const fileName = 'application.log';
const maxFileSize = 1024 * 1024 * 1.5; // 1.5 MB
class logger {
static getNewDate() {
return new Date();
}
static loggingObject(message, status, discription, stack) {
return {
env: 'Production',
lastUpdate: this.getNewDate(),
message,
stack,
status,
discription,
};
}
static loggingNewFile() {
return {
env: 'Production',
lastUpdate: this.getNewDate(),
message: 'Created new file',
stack: '',
status: 'info',
discription: 'logger.helper.js -> loggingNewFile',
};
}
static getLoggingFile() {
return readJSONSync(fileName);
}
static writeLoggingFile(json) {
return writeJSONFileSync(fileName, json);
}
static fileSize() {
const stats = fs.statSync(`logs/${fileName}.json`);
const fileSizeInBytes = stats.size;
return fileSizeInBytes;
}
static logStart() {
const array = [];
array.push(this.loggingNewFile());
this.writeLoggingFile(array);
}
static write(message, status, discription, stack = '') {
if (!checkIfFileExist(`logs/${fileName}.json`)) {
this.logStart();
} else {
// eslint-disable-next-line no-lonely-if
if (this.fileSize() > maxFileSize) {
const stream = fs.createReadStream(`logs/${fileName}.json`).pipe(zlib.createGzip())
.pipe(fs.createWriteStream(`logs/${fileName}-${DateTime.convertDateTime(new Date(), 'YYYY-MM-DD HH-mm')}.json.gz`));
stream.on('finish', () => {
this.logStart();
const getLoggingEntries = this.getLoggingFile();
getLoggingEntries.push(this.loggingObject(message, status, discription, stack));
this.writeLoggingFile(getLoggingEntries);
});
} else {
const getLoggingEntries = this.getLoggingFile();
getLoggingEntries.push(this.loggingObject(message, status, discription, stack));
this.writeLoggingFile(getLoggingEntries);
}
}
}
}
module.exports = {
logger,
};
/* eslint-disable no-restricted-globals */
const fs = require('fs');
const moment = require('moment-timezone');
const { log } = require('../bunyan_logger');
const sortOnDashNR = (a, b) => {
const AA = a.clientID.split('-');
const BB = b.clientID.split('-');
if (parseInt(AA[1], 10) === parseInt(BB[1], 10)) {
return 0;
}
return (parseInt(AA[1], 10) < parseInt(BB[1], 10)) ? -1 : 1;
};
const createRandomString = (stringLength) => {
let text = '';
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789#$%@*@$%&*';
for (let i = 0; i < stringLength; i += 1) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
const escapeRegExp = string => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
const sortArrayAlphabetically = (array, sortBy, sortOrder) => (sortOrder ?
array.sort((a, b) => a[sortBy] > b[sortBy]) :
array.sort((a, b) => a[sortBy] < b[sortBy]));
const writeJSONFile = (nameFile, json) => new Promise((resolve, reject) => {
fs.writeFile(`logs/${nameFile}.json`, JSON.stringify(json, null, 4), (err) => {
if (err) {
reject(err);
}
resolve();
});
});
const writeJSONFileSync = (fileName, json) => {
try {
fs.writeFileSync(`logs/${fileName}.json`, JSON.stringify(json));
} catch (error) {
log.error(error.message, 'general.helper.js - writeJSONFileSync', error.stack !== undefined ? error.stack : '');
}
};
const readJSONFile = fileName => new Promise((resolve, reject) => {
fs.readFile(`logs/${fileName}.json`, ((err, data) => {
if (err) reject(err);
resolve(JSON.parse(data));
}));
});
const readJSONFileAsync = fileName => new Promise((resolve, reject) => {
fs.readFile(`logs/${fileName}.json`, ((err, data) => {
if (err) reject(err);
resolve(JSON.parse(data));
}));
});
const readJSONSync = fileName => JSON.parse(fs.readFileSync(`./logs/${fileName}.json`, 'utf8'));
// eslint-disable-next-line consistent-return
const checkIfFileExist = (path) => {
try {
if (fs.existsSync(path)) {
return true;
}
} catch (err) {
return false;
}
};
const castValue = (payload, identifier) => {
// const value = payload.trim;
let response;
try {
switch (identifier) {
case 'string':
response = String(payload);
break;
case 'number':
if (isNaN(payload)) {
response = payload;
} else {
response = Number(payload);
}
break;
case 'date':
if (payload !== null && payload !== '0000-00-00') {
response = new Date(payload).toISOString().slice(0, 19).replace('T', ' ');
} else {
response = null;
}
break;
case 'boolean':
response = Boolean(payload);
break;
default:
response = String(payload);
}
} catch (e) {
throw e;
}
return response;
};
class DateTime {
static getDutchDateTime() {
return moment.tz(new Date(), 'Europe/Amsterdam').format('YYYY-MM-DD HH:mm');
}
static convertDateTime(timeToConvert, format = 'YYYY-MM-DD HH:mm', timezone = 'Europe/Amsterdam') {
return moment.tz(timeToConvert, timezone).format(format);
}
}
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const uniqueArray = array => new Promise((resolve) => {
const response = [];
array.forEach((element, i) => {
const checkArray = value => value.symbol !== element.symbol;
if (response.filter(checkArray)) response.push(element);
if (array.length - 1 === i) resolve(response);
});
});
module.exports = {
sortOnDashNR,
createRandomString,
escapeRegExp,
sortArrayAlphabetically,
writeJSONFile,
writeJSONFileSync,
readJSONFile,
readJSONFileAsync,
readJSONSync,
checkIfFileExist,
castValue,
DateTime,
delay,
uniqueArray,
};