fixed mail service
This commit is contained in:
parent
3ff29845d4
commit
63d14acc3c
44
src/db.ts
44
src/db.ts
@ -7,7 +7,7 @@ import ChangelogModel from './models/changelog';
|
|||||||
// database urls, prod db url is retrieved automatically
|
// database urls, prod db url is retrieved automatically
|
||||||
const TESTING_URL = 'mongodb://localhost/dfopdb_test';
|
const TESTING_URL = 'mongodb://localhost/dfopdb_test';
|
||||||
const DEV_URL = 'mongodb://localhost/dfopdb';
|
const DEV_URL = 'mongodb://localhost/dfopdb';
|
||||||
const debugging = true;
|
const debugging = false;
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== 'production' && debugging) {
|
if (process.env.NODE_ENV !== 'production' && debugging) {
|
||||||
mongoose.set('debug', true); // enable mongoose debug
|
mongoose.set('debug', true); // enable mongoose debug
|
||||||
@ -19,7 +19,8 @@ export default class db {
|
|||||||
mode: null,
|
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
|
if (this.state.db) return done(); // db is already connected
|
||||||
|
|
||||||
// find right connection url
|
// find right connection url
|
||||||
@ -43,9 +44,14 @@ export default class db {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// connect to 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);
|
if (err) done(err);
|
||||||
});
|
}).then(() => {});
|
||||||
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
|
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
|
||||||
mongoose.connection.on('connected', () => { // evaluation connection behaviour on prod
|
mongoose.connection.on('connected', () => { // evaluation connection behaviour on prod
|
||||||
if (process.env.NODE_ENV !== 'test') { // Do not interfere with testing
|
if (process.env.NODE_ENV !== 'test') { // Do not interfere with testing
|
||||||
@ -63,7 +69,7 @@ export default class db {
|
|||||||
mongoose.connection.close(() => {
|
mongoose.connection.close(() => {
|
||||||
console.info('Mongoose default connection disconnected through app termination');
|
console.info('Mongoose default connection disconnected through app termination');
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
}).then(() => {});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
mongoose.connection.once('open', () => {
|
mongoose.connection.once('open', () => {
|
||||||
@ -79,14 +85,15 @@ export default class db {
|
|||||||
console.info(process.env.NODE_ENV === 'test' ? '' : `Disconnected from database`);
|
console.info(process.env.NODE_ENV === 'test' ? '' : `Disconnected from database`);
|
||||||
this.state.db = 0;
|
this.state.db = 0;
|
||||||
done();
|
done();
|
||||||
});
|
}).then(() => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
static getState () {
|
static getState () {
|
||||||
return this.state;
|
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
|
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
|
this.state.db.db.listCollections().toArray((err, collections) => { // get list of all collections
|
||||||
if (collections.length === 0) { // there are no collections to drop
|
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
|
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();
|
return done();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,8 +134,8 @@ export default class db {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// changelog entry
|
// changelog entry, expects (req, this (from query helper)) or (req, collection, conditions, data)
|
||||||
static log(req, thisOrCollection, conditions = null, data = null) { // expects (req, this (from query helper)) or (req, collection, conditions, data)
|
static log(req, thisOrCollection, conditions = null, data = null) {
|
||||||
if (! (conditions || data)) { // (req, this)
|
if (! (conditions || data)) { // (req, this)
|
||||||
data = thisOrCollection._update ? _.cloneDeep(thisOrCollection._update) : {}; // replace undefined with {}
|
data = thisOrCollection._update ? _.cloneDeep(thisOrCollection._update) : {}; // replace undefined with {}
|
||||||
Object.keys(data).forEach(key => {
|
Object.keys(data).forEach(key => {
|
||||||
@ -136,12 +144,24 @@ export default class db {
|
|||||||
delete data[key];
|
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);
|
if (err) console.error(err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else { // (req, collection, conditions, data)
|
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);
|
if (err) console.error(err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ export default class Mail{
|
|||||||
static mailPass: string;
|
static mailPass: string;
|
||||||
|
|
||||||
static init() {
|
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.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.username = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.username;
|
||||||
this.auth.password = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.password;
|
this.auth.password = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.password;
|
||||||
@ -48,6 +48,7 @@ export default class Mail{
|
|||||||
auth: this.auth
|
auth: this.auth
|
||||||
});
|
});
|
||||||
}).then(() => { // init done successfully
|
}).then(() => { // init done successfully
|
||||||
|
console.info('Mail service established successfully');
|
||||||
this.send('lukas.veit@bosch.com', 'Mail Service started', new Date().toString());
|
this.send('lukas.veit@bosch.com', 'Mail Service started', new Date().toString());
|
||||||
}).catch(err => { // anywhere an error occurred
|
}).catch(err => { // anywhere an error occurred
|
||||||
console.error(`Mail init error: ${err.request.method} ${err.request.path}: ${err.response.status}`,
|
console.error(`Mail init error: ${err.request.method} ${err.request.path}: ${err.response.status}`,
|
||||||
|
@ -6,6 +6,7 @@ import helmet from 'helmet';
|
|||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import api from './api';
|
import api from './api';
|
||||||
import db from './db';
|
import db from './db';
|
||||||
|
import Mail from './helpers/mail';
|
||||||
|
|
||||||
// TODO: check header, also in UI
|
// TODO: check header, also in UI
|
||||||
|
|
||||||
@ -17,9 +18,11 @@ console.info(process.env.NODE_ENV === 'production' ?
|
|||||||
// mongodb connection
|
// mongodb connection
|
||||||
db.connect();
|
db.connect();
|
||||||
|
|
||||||
|
// mail service
|
||||||
|
Mail.init();
|
||||||
|
|
||||||
// create Express app
|
// create Express app
|
||||||
const app = express();
|
const app = express();
|
||||||
app.disable('x-powered-by');
|
|
||||||
|
|
||||||
// get port from environment, defaults to 3000
|
// get port from environment, defaults to 3000
|
||||||
const port = process.env.PORT || 3000;
|
const port = process.env.PORT || 3000;
|
||||||
|
Reference in New Issue
Block a user