fipamo/brain/api/content/project.js

181 lines
5.8 KiB
JavaScript
Raw Normal View History

import RightsManager, {
TASK_CREATE,
TASK_UPDATE,
TASK_READ,
TASK_DELETE,
OBJECT_CLIENT_ADMIN,
OBJECT_CLIENT_USER,
OBJECT_PROJECT_CLIENT,
OBJECT_PROJECT_FOLIO,
OBJECT_BOOKMARK,
OBJECT_POST
} from '../../tools/utilities/RightsManager';
var express = require('express');
var router = express.Router();
var multer = require('multer');
var fs = require('fs-extra');
var Models = require('../../models');
var User = require('../../models/User.js');
var uploadPath = "./content/client-images/";
fs.ensureDir(uploadPath, function(err) {
//console.log(err) // => null
// dir has now been created, including the directory it is to be placed in
})
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, uploadPath)
},
filename: function(req, file, cb) {
var splice = file.originalname.split(':');
cb(null, splice[0]);
}
});
var upload = multer({storage: storage}).array('projectImages');
var rightsManager = new RightsManager();
/*
Create New Client Project
*/
router.post('/add', function(req, res, next) {
if (!req.user)
return res.json({message: "You need to be logged in, champ."})
User.findById(req.user._id).then((user) => {
upload(req, res, function(err) {
if (err) {
//console.log('Error in Saving Entry: ' + err);
res.json({message: err});
throw err;
} else {
if (rightsManager.check(user.role, OBJECT_PROJECT_FOLIO, TASK_CREATE)) {
var project = new Project(req.body);
project.owner = user._id;
project.user = [];
project.comments = [];
project.tasks = [];
project.created = new Date().getTime();
project.edited = new Date().getTime();
if (req.files != "") {
project.images = req.files;
} else {
console.log("NOTHING TO SAVE");
}
project.save().then((project) => {
res.json({message: "new client project added"});
}).catch((err) => {
console.log(err);
})
} else {
res.json({message: "Nah. You can't do that. Talk to the admin, sport."});
}
}
});
}).catch((err) => {
console.log(err);
})
});
/*
Get Client Project List
*/
router.get('/', function(req, res, next) {
if (!req.user)
return res.json({message: "You need to be logged in, champ."})
User.findById(req.user._id).then((user) => {
if ( rightsManager.check(user.role, OBJECT_POST, TASK_READ )) {
Project.find({}).then((projects) => {
res.json(projects);
}).then((users) => {
//res.json({message: "got user list"});
}).catch((err) => {
console.log(err);
});
} else {
res.json({message: "Nah. You can't do that. Talk to the admin, sport."});
}
}).catch((err) => {
console.log(err);
})
});
/*
Get Client Project by ID
*/
router.get('/:id', function(req, res, next) {
if (!req.user)
return res.json({message: "You need to be logged in, champ."});
User.findById(req.user._id).then((user) => {
if (RightsManager.check(user.role, [RightsManager.OBJECT_PROJECT_CLIENT], RightsManager.TASK_READ)) {
Project.findById(req.params.id).then((project) => {
res.json(project);
}).catch((err) => {
console.log(err);
});
}
})
});
/*
Update Client Project by ID
*/
router.post('/update/:id', function(req, res, next) {
if (!req.user)
return res.json({message: "You need to be logged in, champ."});
User.findById(req.user._id).then((user) => {
if ( rightsManager.check(user.role, OBJECT_POST, TASK_UPDATE) ) {
upload(req, res, function(err) {
if (err) {
//console.log('Error in Saving Entry: ' + err);
res.json({message: err});
throw err;
} else {
Project.findByIdAndUpdate(req.params.id, req.body).then((project) => {
if (req.files != "") {
project.images = req.files;
} else {
console.log("NOTHING TO SAVE");
}
project.edited = new Date().getTime();
project.save().then((saved) => {
res.json({message: "project updated"});
}).catch((err) => {
console.log(err);
})
}).catch((err) => {
console.log(err);
})
}
});
} else {
res.json({message: "Nah. You can't do that. Talk to the admin, sport."});
}
});
});
/*
Delete Client Project by ID
*/
router.delete('/:id', function(req, res, next) {
if (!req.user)
return res.json({message: "users only, yo. you're not that"});
User.findById(req.user._id).then((user) => {
if ( rightsManager.check(user.role, OBJECT_POST, TASK_DELETE) ) {
Project.findByIdAndRemove(req.params.id).then((project) => {
res.json({message: 'project has been removed'});
}).catch((err) => {
console.log(err);
})
} else {
res.json({message: "Nah. You can't do that. Talk to the admin, sport."});
}
});
});
module.exports = router;