diff --git a/md-seed-config.js b/md-seed-config.js index adc28c9..e2837c9 100644 --- a/md-seed-config.js +++ b/md-seed-config.js @@ -1,5 +1,5 @@ const mongoose = require('mongoose'); -const { GenderSeeder, UnitSeeder, ActivitySeeder, ProductSeeder, GoalSeeder } = require('./src/seeders'); +const { ProductSeeder } = require('./src/seeders'); const config = require('./src/config/config'); @@ -11,11 +11,7 @@ const mongoURL = config.mongoose.url * @type {Object} */ module.exports.seedersList = { - GoalSeeder, - GenderSeeder, - UnitSeeder, - ActivitySeeder, - // ProductSeeder, + ProductSeeder, }; /** * Connect to mongodb implementation diff --git a/src/controllers/meal.controller.js b/src/controllers/meal.controller.js index f1b83b0..cca61cc 100644 --- a/src/controllers/meal.controller.js +++ b/src/controllers/meal.controller.js @@ -5,7 +5,7 @@ const catchAsync = require('../utils/catchAsync'); const { mealService } = require('../services'); const createMeal = catchAsync(async (req, res) => { - req.body.userId = req.user._id; + req.body.user = req.user._id; const meal = await mealService.createMeal(req.body); res.status(httpStatus.CREATED).send(meal); }); @@ -13,7 +13,7 @@ const createMeal = catchAsync(async (req, res) => { const getMeals = catchAsync(async (req, res) => { const filter = pick(req.query, ['label']); const options = pick(req.query, ['sortBy', 'limit', 'page']); - const result = await mealService.queryProfiles(filter, options); + const result = await mealService.queryMeals(filter, options); res.send(result); }); @@ -26,10 +26,15 @@ const getMeal = catchAsync(async (req, res) => { }); const updateMeal = catchAsync(async (req, res) => { - const meal = await mealService.getMealById(req.params.mealId, req.body); + const meal = await mealService.updateMealById(req.params.mealId, req.body); res.send(meal); }); +const updateMealProducts = catchAsync(async (req, res) => { + const meal = await mealService.updateMealProductsById(req.params.mealId, req.body); + res.send(meal); +}) + const deleteMeal = catchAsync(async (req, res) => { await mealService.deleteMealById(req.params.mealId); res.status(httpStatus.NO_CONTENT).send(); @@ -40,5 +45,6 @@ module.exports = { getMeals, getMeal, updateMeal, + updateMealProducts, deleteMeal, }; diff --git a/src/controllers/product.controller.js b/src/controllers/product.controller.js index e2a93f1..7236a64 100644 --- a/src/controllers/product.controller.js +++ b/src/controllers/product.controller.js @@ -10,7 +10,7 @@ const createProduct = catchAsync(async (req, res) => { }); const getProducts = catchAsync(async (req, res) => { - const filter = pick(req.query, ['name', 'barcode']); + const filter = pick(req.query, ['label', 'barcode']); const options = pick(req.query, ['sortBy', 'limit', 'page']); const result = await productService.queryProducts(filter, options); res.send(result); diff --git a/src/models/product.model.js b/src/models/product.model.js index 586a230..ddd53b8 100644 --- a/src/models/product.model.js +++ b/src/models/product.model.js @@ -3,7 +3,7 @@ const { toJSON, paginate } = require('./plugins'); const productSchema = mongoose.Schema( { - name: { + label: { type: String, unique: true, trim: true, @@ -26,10 +26,11 @@ const productSchema = mongoose.Schema( type: Number, unique: true, index: true, + required: true, }, unit: { - type: mongoose.SchemaTypes.ObjectId, - ref: 'Unit', + type: String, + enum: ['g', 'ml'], required: true, }, servingCapacity: { @@ -54,6 +55,7 @@ const productSchema = mongoose.Schema( }, salt: { type: Number, + default: 0, }, }, { diff --git a/src/routes/v1/index.js b/src/routes/v1/index.js index 4b91406..c494af8 100644 --- a/src/routes/v1/index.js +++ b/src/routes/v1/index.js @@ -3,7 +3,8 @@ const authRoute = require('./auth.route'); const userRoute = require('./user.route'); const profileRoute = require('./profile.route'); const docsRoute = require('./docs.route'); -const meal = require('./meal.route'); +const productsRoute = require('./product.route'); +const mealRoute = require('./meal.route'); const router = express.Router(); @@ -11,6 +12,7 @@ router.use('/auth', authRoute); router.use('/users', userRoute); router.use('/profiles', profileRoute); router.use('/docs', docsRoute); -router.use('/meals', meal); +router.use('/products', productsRoute); +router.use('/meals', mealRoute); module.exports = router; diff --git a/src/routes/v1/meal.route.js b/src/routes/v1/meal.route.js index a2f41b1..e460bbd 100644 --- a/src/routes/v1/meal.route.js +++ b/src/routes/v1/meal.route.js @@ -14,7 +14,8 @@ router router .route('/:mealId') .get(auth(), validate(mealValidation.getMeal), mealController.getMeal) - .patch(auth(), validate(mealValidation.updateMeal), mealController.updateMeal) + .put(auth(), validate(mealValidation.updateMeal), mealController.updateMeal) + .patch(auth(), validate(mealValidation.updateMealProducts), mealController.updateMealProducts) .delete(auth(), validate(mealValidation.deleteMeal), mealController.deleteMeal); module.exports = router; diff --git a/src/seeders/index.js b/src/seeders/index.js index b80c65f..7474f61 100644 --- a/src/seeders/index.js +++ b/src/seeders/index.js @@ -1,5 +1 @@ -module.exports.ActivitySeeder = require('./activity.seeder'); -module.exports.UnitSeeder = require('./unit.seeder'); -module.exports.GenderSeeder = require('./gender.seeder'); module.exports.ProductSeeder = require('./product.seeder'); -module.exports.GoalSeeder = require('./goal.seeder'); diff --git a/src/seeders/product.seeder.js b/src/seeders/product.seeder.js index 3902128..87d3c40 100644 --- a/src/seeders/product.seeder.js +++ b/src/seeders/product.seeder.js @@ -3,15 +3,32 @@ const { Product } = require('../models'); const data = [ { + label: 'cappy - multiwitamina', + brand: 'capy', + verified: true, + eco: false, barcode: 5449000097750, - name: 'cappy - multiwitamina', - capacity: 1, - kcal: 45, + unit: 'ml', + servingCapacity: 1000, + calories: 45, fat: 0, carbohydrates: 10.7, protein: 0, salt: 0, - unit: '5fca7e39283d432b84c8e039', + }, + { + label: 'star - chipsy o smaku smietana i cebula', + brand: 'star', + verified: true, + eco: false, + barcode: 5900259073143, + unit: 'g', + servingCapacity: 130, + calories: 540, + fat: 32, + carbohydrates: 54, + protein: 6.2, + salt: 1.4, }, ]; diff --git a/src/services/index.js b/src/services/index.js index 323d29e..8042030 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -3,4 +3,5 @@ module.exports.emailService = require('./email.service'); module.exports.tokenService = require('./token.service'); module.exports.userService = require('./user.service'); module.exports.profileService = require('./profile.service'); +module.exports.productService = require('./product.service'); module.exports.mealService = require('./meal.service'); diff --git a/src/services/meal.service.js b/src/services/meal.service.js index 00676d7..83fc177 100644 --- a/src/services/meal.service.js +++ b/src/services/meal.service.js @@ -20,7 +20,7 @@ const getMealByUserId = async (userId) => { return Meal.findOne({ userId }); }; -const updateProfileById = async (mealId, updateBody) => { +const updateMealById = async (mealId, updateBody) => { const meal = await getMealById(mealId); if (!meal) { throw new ApiError(httpStatus.NOT_FOUND, 'Meal not found'); @@ -30,6 +30,16 @@ const updateProfileById = async (mealId, updateBody) => { return meal; }; +const updateMealProductsById = async (mealId, updateBody) => { + const meal = await getMealById(mealId); + if (!meal) { + throw new ApiError(httpStatus.NOT_FOUND, 'Meal not found'); + } + meal.products.push(...updateBody.products); + await meal.save(); + return meal; +}; + const deleteMealById = async (mealId) => { const meal = await getMealById(mealId); if (!meal) { @@ -44,6 +54,7 @@ module.exports = { queryMeals, getMealById, getMealByUserId, - updateProfileById, + updateMealById, + updateMealProductsById, deleteMealById, }; diff --git a/src/validations/meal.validation.js b/src/validations/meal.validation.js index 5552d30..0819926 100644 --- a/src/validations/meal.validation.js +++ b/src/validations/meal.validation.js @@ -9,16 +9,15 @@ const products = Joi.object().keys({ const createMeal = { body: Joi.object().keys({ - user: Joi.string().custom(objectId).required(), label: Joi.string().required().valid('breakfast', 'lunch', 'dinner', 'supper', 'snack I', 'snack II', 'snack III'), products: Joi.array().items(products).required(), - date: Joi.date().required(), + date: Joi.date(), }), }; const getMeals = { query: Joi.object().keys({ - label: Joi.string().required().valid('breakfast', 'lunch', 'dinner', 'supper', 'snack I', 'snack II', 'snack III'), + label: Joi.string().valid('breakfast', 'lunch', 'dinner', 'supper', 'snack I', 'snack II', 'snack III'), sortBy: Joi.string(), limit: Joi.number().integer(), page: Joi.number().integer(), @@ -45,6 +44,17 @@ const updateMeal = { .min(1), }; +const updateMealProducts = { + params: Joi.object().keys({ + mealId: Joi.required().custom(objectId), + }), + body: Joi.object() + .keys({ + products: Joi.array().items(products).required(), + }) + .min(1), +}; + const deleteMeal = { params: Joi.object().keys({ mealId: Joi.string().custom(objectId).required(), @@ -56,5 +66,6 @@ module.exports = { getMeals, getMeal, updateMeal, + updateMealProducts, deleteMeal, }; diff --git a/src/validations/product.validation.js b/src/validations/product.validation.js index 17a1916..c6e9c6d 100644 --- a/src/validations/product.validation.js +++ b/src/validations/product.validation.js @@ -6,21 +6,21 @@ const createProduct = { barcode: Joi.number(), verified: Joi.boolean(), eco: Joi.boolean(), - name: Joi.string().required(), + label: Joi.string().required(), servingCapacity: Joi.number().required(), calories: Joi.number().required(), fat: Joi.number().required(), carbohydrates: Joi.number().required(), protein: Joi.number().required(), salt: Joi.number(), - unit: Joi.string().custom(objectId).required(), + unit: Joi.string().required().valid('g', 'ml'), }), }; const getProducts = { query: Joi.object().keys({ barcode: Joi.number(), - name: Joi.string(), + label: Joi.string(), verified: Joi.boolean(), eco: Joi.boolean(), sortBy: Joi.string(), @@ -44,14 +44,14 @@ const updateProduct = { barcode: Joi.number(), verified: Joi.boolean(), eco: Joi.boolean(), - name: Joi.string().required(), + label: Joi.string().required(), servingCapacity: Joi.number().required(), calories: Joi.number().required(), fat: Joi.number().required(), carbohydrates: Joi.number().required(), protein: Joi.number().required(), salt: Joi.number(), - unit: Joi.string().custom(objectId).required(), + unit: Joi.string().required().valid('g', 'ml'), }) .min(1), };