diff --git a/config/globalConfig.js b/config/globalConfig.js index 2799a38b81ccfd11fce3d3f322e8898fd126b3d8..08dc9f6f44b1b69f8b08de232ecc992091d45e09 100644 --- a/config/globalConfig.js +++ b/config/globalConfig.js @@ -1,9 +1,41 @@ +/** + * Contans some of the constants, and settings of the whole webapp + */ const config = { - useAuthentication: false, - avarageCalorieIntake: 2000 //Kcal -}; + useAuthentication: false, //for debug purpose, if set to false authentication wont be used + + avarageCalorieIntake: 2000, // Avarage calorie intake of a human adult, given in kcal + + FoodWarningTime: 180, // sets how many days before the expiration of a storage item should a warning text be visible + + WarningClasses: { + none: { + + message: 'Nincs étel a raktárban', + class: 'text-secondary' + + }, + + good: { + message: '-', + class: 'text-secondary' + }, + + warning: { + message: 'Fél éven belűl romlandó étel', + class: 'text-warning' + }, + + danger: { + message: 'Romlott étel ', + class: 'text-danger' + } + + } + +} diff --git a/middleware/bunker/getBunker.js b/middleware/bunker/getBunker.js index 0e40dc0c779506f596174734e01b7ac07e7f4a67..7429841de70784568ba872bcfdd1580a08a07773 100644 --- a/middleware/bunker/getBunker.js +++ b/middleware/bunker/getBunker.js @@ -1,5 +1,5 @@ -const moment = require('moment'); const requireOption = require('../common/requireOption'); +const getTextClassFromDate = require('../common/TextClass') /** * Gets bunker information from bunkerid param, puts info in res.locals.bunker * -- if no bunker found, redirects to /bunkers @@ -17,7 +17,7 @@ module.exports = function (objectrepository) { if (!bunker) return res.redirect('/bunkers'); for (let item of bunker.stock) - item.style = getStockStyle(item.expirationDate); + item.style = getTextClassFromDate(item.expirationDate).class; res.locals.bunker = bunker; @@ -28,15 +28,3 @@ module.exports = function (objectrepository) { }; } - -function getStockStyle (lasts) { - const today = moment(); - let diff = -today.diff(lasts,'days'); - - console.log('ITEM:',diff); - - if(diff > 180) return 'text-success'; - else if(diff <=180 && diff > 0) return 'text-warning'; - else return 'text-danger'; - -} diff --git a/middleware/bunker/getBunkerList.js b/middleware/bunker/getBunkerList.js index a706a60d71377c7bc4f8299004e4126e23092db7..9c834ebef5d5b5d3e9955b91e56b39fb4e8bdd74 100644 --- a/middleware/bunker/getBunkerList.js +++ b/middleware/bunker/getBunkerList.js @@ -1,6 +1,6 @@ -const moment = require('moment'); -const requireOption = require('../common/requireOption'); +const requireOption = require('../common/requireOption'); +const getTextClassFromDate = require('../common/TextClass') /** *Gets list of bunkers, puts list on res.locals.bunkers @@ -14,7 +14,7 @@ module.exports = function (objectrepository) { BunkerModel.find({}).exec() .then(bunkers =>{ for(let bunker of bunkers) - bunker.warning = GetWarningLabel(bunker.nextExpDate); + bunker.warning = getTextClassFromDate(bunker.nextExpDate); res.locals.bunkers = bunkers; return next(); }) @@ -22,35 +22,3 @@ module.exports = function (objectrepository) { }; } - - -//TODO: Ezt szebben meg kéne oldani -function GetWarningLabel(nextExpireDate) { - - if(!nextExpireDate) return { - message: 'Nincs étel a raktárban', - class: 'text-secondary' - }; - - const today = new moment(); - let diff = -today.diff(nextExpireDate,'days'); - if( diff > 180){ - return { - message: '-', - class: 'text-secondary' - } - } - else if (diff <= 180 && diff > 0){ - return { - message: 'Fél éven belűl romlandó étel', - class: 'text-warning' - } - } - else{ - return { - message: 'Romlott étel ', - class: 'text-danger' - } - } - -} \ No newline at end of file diff --git a/middleware/bunker/saveBunker.js b/middleware/bunker/saveBunker.js index 1b37ff2d39f5bb9fd4ba333491a9aaf6aa7adfe0..5531964633bd5a4b205c1f7a92834751c9b66f4c 100644 --- a/middleware/bunker/saveBunker.js +++ b/middleware/bunker/saveBunker.js @@ -1,4 +1,4 @@ - +const hasAllOptions = require('../common/hasAllOptions'); const requireOption = require('../common/requireOption'); /** @@ -14,20 +14,12 @@ module.exports = function (objectrepository) { return function (req, res, next) { - if (typeof req.body.name === 'undefined' || - typeof req.body.adress === 'undefined' || - typeof req.body.capacity === 'undefined'){ - - return next(); - } - - + if(!hasAllOptions(req.body,['name','adress','capacity']) ) return next(); + if (req.body.name === '' || req.body.adress === '' || req.body.capacity === '' ){ - res.locals.warning = 'Töltsd ki az összes mezőt'; - return next(); } @@ -43,7 +35,6 @@ module.exports = function (objectrepository) { res.locals.bunker.adress = req.body.adress res.locals.bunker.capacity = req.body.capacity - res.locals.bunker.save(err =>{ if(err) return next(err); diff --git a/middleware/common/TextClass.js b/middleware/common/TextClass.js new file mode 100644 index 0000000000000000000000000000000000000000..3a97db821437d386674035eb455c2d5709bf6136 --- /dev/null +++ b/middleware/common/TextClass.js @@ -0,0 +1,25 @@ + +const moment = require('moment'); +const config = require('../../config/globalConfig'); + + +/** + * Calculates how many days are between the actual date, and the given date in param, + * and assigns a warning class based on the date. + * @param {Date to check} date + */ +function getTextClassFromDate(date) { + if(!date) return config.WarningClasses.none; + + const today = moment(); + const Expirationday = moment(date); + let diff = Expirationday.diff(today,'days'); + + + if(diff < 0 ) return config.WarningClasses.danger; + else if (diff >= 0 && diff <= 180) return config.WarningClasses.warning; + else return config.WarningClasses.good; + +} + +module.exports = getTextClassFromDate; \ No newline at end of file diff --git a/middleware/common/hasAllOptions.js b/middleware/common/hasAllOptions.js new file mode 100644 index 0000000000000000000000000000000000000000..cca2f607031f2e21b08d90bff423c0c5bbc78e9e --- /dev/null +++ b/middleware/common/hasAllOptions.js @@ -0,0 +1,16 @@ +/** + * + * @param {Object to check} object + * @param {List of properties} propertyArray + */ +function hasAllOptions(object, propertyArray) { + if(typeof object === 'undefined' || !Array.isArray(propertyArray)) return false; + + for(property of propertyArray){ + if(typeof object[property] === 'undefined') return false; + } + + return true; +} + +module.exports = hasAllOptions; \ No newline at end of file diff --git a/middleware/food/getFood.js b/middleware/food/getFood.js index c67788708608fe7a7b55f3bcfe85c952bfec9214..d07d09108730a08860c07e1712b0831b24d5a6f2 100644 --- a/middleware/food/getFood.js +++ b/middleware/food/getFood.js @@ -1,4 +1,5 @@ const requireOption = require('../common/requireOption'); + /** * Get food from database, using foodid param * -- if no food found, redirects to '/foods' @@ -10,7 +11,7 @@ module.exports = function (objectrepository) { FoodModel.findOne({_id:req.params.foodid},(err,food)=>{ if(err) return next(err); - if(!food) return res.redirect('foods'); + if(!food) return res.redirect('/foods'); res.locals.food = food; return next(); diff --git a/middleware/food/saveFood.js b/middleware/food/saveFood.js index 160f64772f4cbab1cffe3a0b2a917098e6e1371f..76befcc0f9a2f818c71915b64081511c61a74207 100644 --- a/middleware/food/saveFood.js +++ b/middleware/food/saveFood.js @@ -1,4 +1,7 @@ const requireOption = require('../common/requireOption'); +const hasAllOptions = require('../common/hasAllOptions'); + + /** * Creates/Updates food entry in database using foodid param */ @@ -8,23 +11,16 @@ module.exports = function (objectrepository) { return function (req, res, next) { - if (typeof req.body.name === 'undefined' || - typeof req.body.kcal === 'undefined' || - typeof req.body.lasts === 'undefined'){ - return next(); - } + if (!hasAllOptions(req.body, ['name', 'kcal', 'lasts'])) return next(); - if (req.body.name === '' || + if (req.body.name === '' || req.body.kcal === '' || - req.body.lasts === '' ){ - + req.body.lasts === '') { res.locals.warning = 'Töltsd ki az összes mezőt'; - return next(); } - - if(typeof res.locals.food === 'undefined'){ + if (typeof res.locals.food === 'undefined') { res.locals.food = new FoodModel(); } @@ -32,13 +28,10 @@ module.exports = function (objectrepository) { res.locals.food.kcal = req.body.kcal; res.locals.food.lasts = req.body.lasts; + res.locals.food.save(err => { + if (err) return next(err); - res.locals.food.save(err=>{ - if(err) return next(err); - - return res.redirect('/foods'); + return res.redirect('/foods'); }) - }; - - }; \ No newline at end of file +}; \ No newline at end of file diff --git a/middleware/storage/addStorageItem.js b/middleware/storage/addStorageItem.js index e7c6a95bd5c3e8fb9555f726637abff972636d29..bc8969a84b3d3929bf832a79c21e31bdf2b11e92 100644 --- a/middleware/storage/addStorageItem.js +++ b/middleware/storage/addStorageItem.js @@ -1,8 +1,10 @@ const requireOption = require('../common/requireOption'); +const hasAllOptions = require('../common/hasAllOptions'); const moment = require('moment'); /** * Ads item to a storage of a bunker + * IMPORTANT: It only saves tthe storage item to database, saving the bunker model needs to be called in next middleware, after updateting the statisticks */ module.exports = function (objectrepository) { @@ -12,12 +14,9 @@ module.exports = function (objectrepository) { return function (req, res, next) { - if (typeof req.body.type === 'undefined' || - typeof req.body.dop === 'undefined' || - typeof req.body.quantity === 'undefined'){ - return next(); - } + if(!hasAllOptions(req.body,['type','dop','quantity'])) return next(); + if (req.body.type === '' || req.body.dop === '' || req.body.quantity === '' ){ diff --git a/middleware/storage/deleteStorageItem.js b/middleware/storage/deleteStorageItem.js index 626a5c2e1a737ddeaf8bd30830752b2576b45e29..5746fe07eb9db599c1c97c1bc3e1bbe611a2fc2d 100644 --- a/middleware/storage/deleteStorageItem.js +++ b/middleware/storage/deleteStorageItem.js @@ -1,6 +1,7 @@ const requireOption = require('../common/requireOption'); /** * Deletes item from the storage of a bunker using itemid param + * IMPORTANT: It only deletes storage item to database, saving the bunker model needs to be called in next middleware, after updateting the statisticks */ module.exports = function (objectrepository) { const StorageItemModel = requireOption(objectrepository, 'StorageItemModel'); diff --git a/middleware/storage/updateBunkerStats.js b/middleware/storage/updateBunkerStats.js index 4b04c2b75d540a3ffde58ab24eeccccaaf680e75..086db7f0e713b6d755c93fae8e2cb6d428c21e5d 100644 --- a/middleware/storage/updateBunkerStats.js +++ b/middleware/storage/updateBunkerStats.js @@ -1,18 +1,15 @@ const moment = require('moment'); -const requireOption = require('../common/requireOption'); const globalConfig = require('../../config/globalConfig'); +const requireOption = require('../common/requireOption'); + /** - * Deletes item from the storage of a bunker using itemid param + * Updates the bunkers statistics, like estimated days or next expiration date */ module.exports = function (objectrepository) { const BunkerModel = requireOption(objectrepository, 'BunkerModel'); return function (req, res, next) { - - if(typeof res.locals.bunker === 'undefined') return next('Wrong usage of updateBunkerStatsMW'); - - let sumCalories = 0; let expDates = [];