2016-03-31 13:24:21 +00:00
|
|
|
/*jshint node:true*/
|
2016-04-06 16:02:43 +00:00
|
|
|
|
2016-03-31 13:24:21 +00:00
|
|
|
var db = require('../units/db-connector').dbConnection;
|
|
|
|
var dbCompany = require('../units/db-company')(db);
|
|
|
|
var dbAccounts = require('../units/db-accounts')(db);
|
|
|
|
var $U = require('md-utils');
|
|
|
|
var events = require('events');
|
2016-04-06 16:02:43 +00:00
|
|
|
|
2016-03-31 13:24:21 +00:00
|
|
|
// Create an eventEmitter object
|
|
|
|
var eventEmitter = new events.EventEmitter();
|
|
|
|
|
|
|
|
module.exports = function(app) {
|
|
|
|
var express = require('express');
|
|
|
|
var newcompaniesRouter = express.Router();
|
|
|
|
|
|
|
|
eventEmitter.on('makeMemberOf', (uid, cid) => {
|
2016-04-06 16:02:43 +00:00
|
|
|
'use strict';
|
2016-03-31 13:24:21 +00:00
|
|
|
dbAccounts.makeMemberOf(uid, cid)
|
|
|
|
.catch(function(err) {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
newcompaniesRouter.get('/', function(req, res) {
|
|
|
|
res.send({
|
|
|
|
newcompanies: []
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
newcompaniesRouter.post('/', function(req, res) {
|
2016-04-06 16:02:43 +00:00
|
|
|
'use strict';
|
2016-03-31 13:24:21 +00:00
|
|
|
var updateData = $U.unDashObject(req.body.data.attributes);
|
|
|
|
if (/Bearer .+/.test(req.headers.authorization)) {
|
|
|
|
dbCompany.addNewCompany(updateData)
|
|
|
|
.then(function(d) {
|
|
|
|
updateData.cid = d.cid;
|
|
|
|
let response = {
|
|
|
|
data: {
|
|
|
|
type: 'newcompany', id: d.cid, attributes: updateData
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
eventEmitter.emit('makeMemberOf', updateData.uid, d.cid);
|
|
|
|
res.status(200).send(response);
|
|
|
|
})
|
|
|
|
.catch(function(e) {
|
|
|
|
console.error(e);
|
|
|
|
res.status(401).end();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use('/api/newcompanies', require('body-parser').json({type: 'application/vnd.api+json'}));
|
|
|
|
|
|
|
|
app.use('/api/newcompanies', newcompaniesRouter);
|
|
|
|
};
|