Fix meal service
This commit is contained in:
parent
cffb99ed7d
commit
46c531099f
@ -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
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -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);
|
||||
|
@ -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,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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');
|
||||
|
@ -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,
|
||||
},
|
||||
];
|
||||
|
||||
|
@ -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');
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -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),
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user