Archived
2

fixed mail service

This commit is contained in:
VLE2FE 2020-08-07 08:37:25 +02:00
parent 3ff29845d4
commit 63d14acc3c
3 changed files with 38 additions and 14 deletions

View File

@ -7,7 +7,7 @@ import ChangelogModel from './models/changelog';
// database urls, prod db url is retrieved automatically
const TESTING_URL = 'mongodb://localhost/dfopdb_test';
const DEV_URL = 'mongodb://localhost/dfopdb';
const debugging = true;
const debugging = false;
if (process.env.NODE_ENV !== 'production' && debugging) {
mongoose.set('debug', true); // enable mongoose debug
@ -19,7 +19,8 @@ export default class db {
mode: null,
};
static connect (mode = '', done: Function = () => {}) { // set mode to test for unit/integration tests, otherwise skip parameters. done is also only needed for testing
// set mode to test for unit/integration tests, otherwise skip parameters. done is also only needed for testing
static connect (mode = '', done: Function = () => {}) {
if (this.state.db) return done(); // db is already connected
// find right connection url
@ -43,9 +44,14 @@ export default class db {
}
// connect to db
mongoose.connect(connectionString, {useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, connectTimeoutMS: 10000}, err => {
mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
connectTimeoutMS: 10000
}, err => {
if (err) done(err);
});
}).then(() => {});
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.on('connected', () => { // evaluation connection behaviour on prod
if (process.env.NODE_ENV !== 'test') { // Do not interfere with testing
@ -63,7 +69,7 @@ export default class db {
mongoose.connection.close(() => {
console.info('Mongoose default connection disconnected through app termination');
process.exit(0);
});
}).then(() => {});
}
});
mongoose.connection.once('open', () => {
@ -79,14 +85,15 @@ export default class db {
console.info(process.env.NODE_ENV === 'test' ? '' : `Disconnected from database`);
this.state.db = 0;
done();
});
}).then(() => {});
}
static getState () {
return this.state;
}
static drop (done: Function = () => {}) { // drop all collections of connected db (only dev and test for safety reasons ;)
// drop all collections of connected db (only dev and test for safety reasons)
static drop (done: Function = () => {}) {
if (!this.state.db || this.state.mode === 'prod') return done(); // no db connection or prod db
this.state.db.db.listCollections().toArray((err, collections) => { // get list of all collections
if (collections.length === 0) { // there are no collections to drop
@ -106,7 +113,8 @@ export default class db {
}
static loadJson (json, done: Function = () => {}) { // insert given JSON data into db, uses core mongodb methods
if (!this.state.db || !json.hasOwnProperty('collections') || json.collections.length === 0) { // no db connection or nothing to load
// no db connection or nothing to load
if (!this.state.db || !json.hasOwnProperty('collections') || json.collections.length === 0) {
return done();
}
@ -126,8 +134,8 @@ export default class db {
});
}
// changelog entry
static log(req, thisOrCollection, conditions = null, data = null) { // expects (req, this (from query helper)) or (req, collection, conditions, data)
// changelog entry, expects (req, this (from query helper)) or (req, collection, conditions, data)
static log(req, thisOrCollection, conditions = null, data = null) {
if (! (conditions || data)) { // (req, this)
data = thisOrCollection._update ? _.cloneDeep(thisOrCollection._update) : {}; // replace undefined with {}
Object.keys(data).forEach(key => {
@ -136,12 +144,24 @@ export default class db {
delete data[key];
}
});
new ChangelogModel({action: req.method + ' ' + req.url, collectionName: thisOrCollection._collection.collectionName, conditions: thisOrCollection._conditions, data: data, user_id: req.authDetails.id ? req.authDetails.id : null}).save(err => {
new ChangelogModel({
action: req.method + ' ' + req.url,
collectionName: thisOrCollection._collection.collectionName,
conditions: thisOrCollection._conditions,
data: data,
user_id: req.authDetails.id ? req.authDetails.id : null
}).save(err => {
if (err) console.error(err);
});
}
else { // (req, collection, conditions, data)
new ChangelogModel({action: req.method + ' ' + req.url, collectionName: thisOrCollection, conditions: conditions, data: data, user_id: req.authDetails.id ? req.authDetails.id : null}).save(err => {
new ChangelogModel({
action: req.method + ' ' + req.url,
collectionName: thisOrCollection,
conditions: conditions,
data: data,
user_id: req.authDetails.id ? req.authDetails.id : null
}).save(err => {
if (err) console.error(err);
});
}

View File

@ -10,7 +10,7 @@ export default class Mail{
static mailPass: string;
static init() {
this.mailPass = Array(64).map(() => Math.floor(Math.random() * 10)).join('');
this.mailPass = Array(64).fill(0).map(() => Math.floor(Math.random() * 10)).join('');
this.uri = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.uri;
this.auth.username = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.username;
this.auth.password = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.password;
@ -48,6 +48,7 @@ export default class Mail{
auth: this.auth
});
}).then(() => { // init done successfully
console.info('Mail service established successfully');
this.send('lukas.veit@bosch.com', 'Mail Service started', new Date().toString());
}).catch(err => { // anywhere an error occurred
console.error(`Mail init error: ${err.request.method} ${err.request.path}: ${err.response.status}`,

View File

@ -6,6 +6,7 @@ import helmet from 'helmet';
import cors from 'cors';
import api from './api';
import db from './db';
import Mail from './helpers/mail';
// TODO: check header, also in UI
@ -17,9 +18,11 @@ console.info(process.env.NODE_ENV === 'production' ?
// mongodb connection
db.connect();
// mail service
Mail.init();
// create Express app
const app = express();
app.disable('x-powered-by');
// get port from environment, defaults to 3000
const port = process.env.PORT || 3000;