import DateUtils from '../../tools/utilities/DateUtils'; 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'); const dateUtil = new DateUtils() var uploadPath = "./content/folio-images/" + dateUtil.getDate('year', new Date()) + "/" + dateUtil.getDate('month', new Date()); 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('folioImages'); /* Create New Folio 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, [RightsManager.OBJECT_PROJECT_FOLIO], RightsManager.TASK_CREATE)) { var project = new Project(req.body); 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 Folio Project List */ router.get('/', function (req, res, next) { Project.find({}).then((projects) => { var folioArray = []; for (var i = 0; i < projects.length; i++) { var imgURL = null; if (projects[i].images.length != 0) imgURL = String(projects[i].images[0].path).substring(8, String(projects[i].images[0].path).length); var folioItem = { id: projects[i]._id, title: projects[i].title, url: projects[i].url, tools: projects[i].tools, description: projects[i].description, type: projects[i].type, img_url: imgURL, img_full_url: "http://formless.local/" + imgURL } folioArray[i] = folioItem; } res.json(folioArray); }) }); /* Get Folio Project by ID */ router.get('/:id', function (req, res, next) { Project.findById(req.params.id).then((project) => { res.json(project); }).catch((err) => { console.log(err); }); }); /* Update Folio Project by ID */ router.post('/update/:id', function (req, res, next) { if (!req.session.user) return res.json({ message: "You need to be logged in, champ." }); Models.User.findById(req.session.user.id).then((user) => { if (RightsManager.check(user.role, [RightsManager.OBJECT_PROJECT_FOLIO], RightsManager.TASK_UPDATE)) { upload(req, res, function (err) { if (err) { //console.log('Error in Saving Entry: ' + err); res.json({ message: err }); throw err; } else { Models.FolioProject.findOne({where:{id: req.params.id}}).then((project) => { if (req.files != "") { project.images = req.files; } else { console.log("NOTHING TO SAVE"); } console.log(req.body); project.update(req.body).then(updated => { res.json({ message: "project updated" }); }).catch(err => { console.log(err) }); /** 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 Folio 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, [RightsManager.OBJECT_PROJECT_FOLIO], RightsManager.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." }); } }); }); router.post('/sort', function (req, res, next) { var sortList = req.body; var self = this; User.findById(req.user._id).then((user) => { if (RightsManager.check(user.role, [RightsManager.OBJECT_PROJECT_FOLIO], RightsManager.TASK_UPDATE)) { var clean = true; for (var i = 0; i < sortList.length; i++) { Project.findByIdAndUpdate(sortList[i].sortID, { sortIndex: sortList[i].sortIndex }).then((project) => { //console.log("SORTED") //res.json({message: "sorted"}) }).catch((err) => { //res.json({message: "sorted", error:err}) }) //res.json({message:'projects updated'}) } } else { res.json({ message: "Nah. You can't do that. Talk to the admin, sport." }); } }); }); module.exports = router;