obrand-admin-server/server/app/newcompanies.js
2016-04-06 17:02:43 +01:00

58 lines
1.6 KiB
JavaScript

/*jshint node:true*/
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');
// 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) => {
'use strict';
dbAccounts.makeMemberOf(uid, cid)
.catch(function(err) {
console.error(err);
});
});
newcompaniesRouter.get('/', function(req, res) {
res.send({
newcompanies: []
});
});
newcompaniesRouter.post('/', function(req, res) {
'use strict';
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);
};