Commit ca0bac72 authored by Sang D NGO's avatar Sang D NGO
Browse files

proposal API

parent ebce8afa
module.exports = {
DEFAULT_OFFSET: 0,
DEFAULT_LIMIT: 20
};
'use strict';
module.exports = function(dependencies) {
const models = require('./db')(dependencies);
const proposal = require('./proposal')(dependencies);
return {
models
models,
proposal
};
};
module.exports = denormalize;
function denormalize(proposal) {
return {
id: proposal.id,
creator: proposal.creator,
type: proposal.type,
formData: proposal.formData,
status: proposal.status,
timestamps: proposal.timestamps,
reviewers: proposal.reviewers
};
}
const { DEFAULT_OFFSET, DEFAULT_LIMIT } = require('../constants');
const denormalize = require('./denormalize');
module.exports = function(dependencies) {
const mongoose = dependencies('db').mongo.mongoose;
const Proposal = mongoose.model('MostProposal');
return {
create,
denormalize,
getById,
list,
updateById
};
function create(proposal) {
return Proposal.create(proposal);
}
function getById(id) {
return Proposal.findOne({ _id: id });
}
function list(options = {}) {
const query = {};
if (options.creator) {
query.creator = options.creator;
}
if (options.status) {
query.status = options.status;
}
return Proposal
.find(query)
.skip(+options.offset || DEFAULT_OFFSET)
.limit(+options.limit || DEFAULT_LIMIT)
.sort({ _id: -1 })
.exec();
}
function updateById(proposalId, modified) {
return Proposal.findOneAndUpdate(
{ _id: proposalId },
{ $set: modified },
{ new: true } // to return updated document
).exec();
}
};
'use strict';
module.exports = function(dependencies, lib, router) {
const authorizationMW = dependencies('authorizationMW');
const exampleController = require('../controllers/example')(dependencies, lib);
router.get('/example', authorizationMW.requiresAPILogin, exampleController.example);
};
......@@ -6,7 +6,7 @@ module.exports = function(dependencies, lib) {
const router = express.Router();
require('./example')(dependencies, lib, router);
require('./proposals')(dependencies, lib, router);
return router;
};
module.exports = function(dependencies, lib) {
const logger = dependencies('logger');
return {
createProposal,
getProposals,
updateProposal
};
function createProposal(req, res) {
const { type, formData, reviewers } = req.body;
lib.proposal.create({
creator: req.user.id,
type,
formData,
reviewers: reviewers ? reviewers.map(reviewer => reviewer.id) : []
})
.then(proposal => {
res.status(201).json(lib.proposal.denormalize(proposal));
})
.catch(err => {
const details = 'Error while creating proposal';
logger.error(details, err);
res.status(500).json({
error: {
code: 500,
message: 'Server Error',
details
}
});
});
}
function getProposals(req, res) {
var listOptions = {
creator: req.user.id,
type: req.query.type,
limit: Number(req.query.limit),
offset: Number(req.query.offset)
};
lib.proposal.list(listOptions)
.then(proposals => proposals.map(lib.proposal.denormalize))
.then(denormalizedProposals => res.status(200).json(denormalizedProposals))
.catch(err => {
const details = 'Error while gettings proposal';
logger.error(details, err);
res.status(500).json({
error: {
code: 500,
message: 'Server Error',
details
}
});
});
}
function updateProposal(req, res) {
var updateFields = {};
if (req.body.status) {
updateFields.status = req.body.status;
}
if (req.body.formData) {
updateFields.formData = req.body.formData;
}
if (req.body.reviewers) {
updateFields.reviewers = req.body.reviewers;
}
lib.proposal.updateById(req.params.proposalId, updateFields)
.then(proposal => res.status(200).json(lib.proposal.denormalize(proposal)))
.catch(err => {
const details = 'Error while updating proposal';
logger.error(details, err);
res.status(500).json({
error: {
code: 500,
message: 'Server Error',
details
}
});
});
}
};
'use strict';
module.exports = function(dependencies, lib, router) {
const authorizationMW = dependencies('authorizationMW');
const helperMW = dependencies('helperMW');
const controller = require('./controller')(dependencies, lib);
const middleware = require('./middleware')(dependencies, lib);
router.get('/proposals',
authorizationMW.requiresAPILogin,
controller.getProposals);
router.post('/proposals',
authorizationMW.requiresAPILogin,
controller.createProposal);
router.post('/proposals/:proposalId',
authorizationMW.requiresAPILogin,
helperMW.checkIdInParams('proposalId', 'Proposal'),
middleware.load,
controller.updateProposal);
};
module.exports = function(dependencies, lib) {
const logger = dependencies('logger');
return {
load
};
function load(req, res, next) {
lib.proposal.getById(req.params.proposalId)
.then(proposal => {
if (proposal) {
req.proposal = proposal;
next();
} else {
res.status(404).json({
error: {
code: 404,
message: 'Not Found',
details: 'Proposal not found'
}
});
}
})
.catch(err => {
const details = 'Error while loading proposal';
logger.error(details, err);
res.status(500).json({
error: {
code: 500,
message: 'Server Error',
details
}
});
});
}
};
......@@ -12,8 +12,9 @@ const myAwesomeModule = new AwesomeModule(AWESOME_MODULE_NAME, {
new Dependency(Dependency.TYPE_NAME, 'linagora.esn.core.logger', 'logger'),
new Dependency(Dependency.TYPE_NAME, 'linagora.esn.core.webserver.wrapper', 'webserver-wrapper'),
new Dependency(Dependency.TYPE_NAME, 'linagora.esn.core.db', 'db'),
new Dependency(Dependency.TYPE_NAME, 'linagora.esn.core.i18n', 'i18n'),
new Dependency(Dependency.TYPE_NAME, 'linagora.esn.core.webserver.middleware.authorization', 'authorizationMW'),
new Dependency(Dependency.TYPE_NAME, 'linagora.esn.core.i18n', 'i18n')
new Dependency(Dependency.TYPE_NAME, 'linagora.esn.core.webserver.middleware.helper', 'helperMW')
],
states: {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment