Fix meal service
This commit is contained in:
parent
cffb99ed7d
commit
46c531099f
@ -1,5 +1,5 @@
|
|||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const { GenderSeeder, UnitSeeder, ActivitySeeder, ProductSeeder, GoalSeeder } = require('./src/seeders');
|
const { ProductSeeder } = require('./src/seeders');
|
||||||
|
|
||||||
const config = require('./src/config/config');
|
const config = require('./src/config/config');
|
||||||
|
|
||||||
@ -11,11 +11,7 @@ const mongoURL = config.mongoose.url
|
|||||||
* @type {Object}
|
* @type {Object}
|
||||||
*/
|
*/
|
||||||
module.exports.seedersList = {
|
module.exports.seedersList = {
|
||||||
GoalSeeder,
|
ProductSeeder,
|
||||||
GenderSeeder,
|
|
||||||
UnitSeeder,
|
|
||||||
ActivitySeeder,
|
|
||||||
// ProductSeeder,
|
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* Connect to mongodb implementation
|
* Connect to mongodb implementation
|
||||||
|
@ -5,7 +5,7 @@ const catchAsync = require('../utils/catchAsync');
|
|||||||
const { mealService } = require('../services');
|
const { mealService } = require('../services');
|
||||||
|
|
||||||
const createMeal = catchAsync(async (req, res) => {
|
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);
|
const meal = await mealService.createMeal(req.body);
|
||||||
res.status(httpStatus.CREATED).send(meal);
|
res.status(httpStatus.CREATED).send(meal);
|
||||||
});
|
});
|
||||||
@ -13,7 +13,7 @@ const createMeal = catchAsync(async (req, res) => {
|
|||||||
const getMeals = catchAsync(async (req, res) => {
|
const getMeals = catchAsync(async (req, res) => {
|
||||||
const filter = pick(req.query, ['label']);
|
const filter = pick(req.query, ['label']);
|
||||||
const options = pick(req.query, ['sortBy', 'limit', 'page']);
|
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);
|
res.send(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -26,10 +26,15 @@ const getMeal = catchAsync(async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const updateMeal = 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);
|
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) => {
|
const deleteMeal = catchAsync(async (req, res) => {
|
||||||
await mealService.deleteMealById(req.params.mealId);
|
await mealService.deleteMealById(req.params.mealId);
|
||||||
res.status(httpStatus.NO_CONTENT).send();
|
res.status(httpStatus.NO_CONTENT).send();
|
||||||
@ -40,5 +45,6 @@ module.exports = {
|
|||||||
getMeals,
|
getMeals,
|
||||||
getMeal,
|
getMeal,
|
||||||
updateMeal,
|
updateMeal,
|
||||||
|
updateMealProducts,
|
||||||
deleteMeal,
|
deleteMeal,
|
||||||
};
|
};
|
||||||
|
@ -10,7 +10,7 @@ const createProduct = catchAsync(async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const getProducts = 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 options = pick(req.query, ['sortBy', 'limit', 'page']);
|
||||||
const result = await productService.queryProducts(filter, options);
|
const result = await productService.queryProducts(filter, options);
|
||||||
res.send(result);
|
res.send(result);
|
||||||
|
@ -3,7 +3,7 @@ const { toJSON, paginate } = require('./plugins');
|
|||||||
|
|
||||||
const productSchema = mongoose.Schema(
|
const productSchema = mongoose.Schema(
|
||||||
{
|
{
|
||||||
name: {
|
label: {
|
||||||
type: String,
|
type: String,
|
||||||
unique: true,
|
unique: true,
|
||||||
trim: true,
|
trim: true,
|
||||||
@ -26,10 +26,11 @@ const productSchema = mongoose.Schema(
|
|||||||
type: Number,
|
type: Number,
|
||||||
unique: true,
|
unique: true,
|
||||||
index: true,
|
index: true,
|
||||||
|
required: true,
|
||||||
},
|
},
|
||||||
unit: {
|
unit: {
|
||||||
type: mongoose.SchemaTypes.ObjectId,
|
type: String,
|
||||||
ref: 'Unit',
|
enum: ['g', 'ml'],
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
servingCapacity: {
|
servingCapacity: {
|
||||||
@ -54,6 +55,7 @@ const productSchema = mongoose.Schema(
|
|||||||
},
|
},
|
||||||
salt: {
|
salt: {
|
||||||
type: Number,
|
type: Number,
|
||||||
|
default: 0,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -3,7 +3,8 @@ const authRoute = require('./auth.route');
|
|||||||
const userRoute = require('./user.route');
|
const userRoute = require('./user.route');
|
||||||
const profileRoute = require('./profile.route');
|
const profileRoute = require('./profile.route');
|
||||||
const docsRoute = require('./docs.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();
|
const router = express.Router();
|
||||||
|
|
||||||
@ -11,6 +12,7 @@ router.use('/auth', authRoute);
|
|||||||
router.use('/users', userRoute);
|
router.use('/users', userRoute);
|
||||||
router.use('/profiles', profileRoute);
|
router.use('/profiles', profileRoute);
|
||||||
router.use('/docs', docsRoute);
|
router.use('/docs', docsRoute);
|
||||||
router.use('/meals', meal);
|
router.use('/products', productsRoute);
|
||||||
|
router.use('/meals', mealRoute);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
@ -14,7 +14,8 @@ router
|
|||||||
router
|
router
|
||||||
.route('/:mealId')
|
.route('/:mealId')
|
||||||
.get(auth(), validate(mealValidation.getMeal), mealController.getMeal)
|
.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);
|
.delete(auth(), validate(mealValidation.deleteMeal), mealController.deleteMeal);
|
||||||
|
|
||||||
module.exports = router;
|
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.ProductSeeder = require('./product.seeder');
|
||||||
module.exports.GoalSeeder = require('./goal.seeder');
|
|
||||||
|
@ -3,15 +3,32 @@ const { Product } = require('../models');
|
|||||||
|
|
||||||
const data = [
|
const data = [
|
||||||
{
|
{
|
||||||
|
label: 'cappy - multiwitamina',
|
||||||
|
brand: 'capy',
|
||||||
|
verified: true,
|
||||||
|
eco: false,
|
||||||
barcode: 5449000097750,
|
barcode: 5449000097750,
|
||||||
name: 'cappy - multiwitamina',
|
unit: 'ml',
|
||||||
capacity: 1,
|
servingCapacity: 1000,
|
||||||
kcal: 45,
|
calories: 45,
|
||||||
fat: 0,
|
fat: 0,
|
||||||
carbohydrates: 10.7,
|
carbohydrates: 10.7,
|
||||||
protein: 0,
|
protein: 0,
|
||||||
salt: 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.tokenService = require('./token.service');
|
||||||
module.exports.userService = require('./user.service');
|
module.exports.userService = require('./user.service');
|
||||||
module.exports.profileService = require('./profile.service');
|
module.exports.profileService = require('./profile.service');
|
||||||
|
module.exports.productService = require('./product.service');
|
||||||
module.exports.mealService = require('./meal.service');
|
module.exports.mealService = require('./meal.service');
|
||||||
|
@ -20,7 +20,7 @@ const getMealByUserId = async (userId) => {
|
|||||||
return Meal.findOne({ userId });
|
return Meal.findOne({ userId });
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateProfileById = async (mealId, updateBody) => {
|
const updateMealById = async (mealId, updateBody) => {
|
||||||
const meal = await getMealById(mealId);
|
const meal = await getMealById(mealId);
|
||||||
if (!meal) {
|
if (!meal) {
|
||||||
throw new ApiError(httpStatus.NOT_FOUND, 'Meal not found');
|
throw new ApiError(httpStatus.NOT_FOUND, 'Meal not found');
|
||||||
@ -30,6 +30,16 @@ const updateProfileById = async (mealId, updateBody) => {
|
|||||||
return meal;
|
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 deleteMealById = async (mealId) => {
|
||||||
const meal = await getMealById(mealId);
|
const meal = await getMealById(mealId);
|
||||||
if (!meal) {
|
if (!meal) {
|
||||||
@ -44,6 +54,7 @@ module.exports = {
|
|||||||
queryMeals,
|
queryMeals,
|
||||||
getMealById,
|
getMealById,
|
||||||
getMealByUserId,
|
getMealByUserId,
|
||||||
updateProfileById,
|
updateMealById,
|
||||||
|
updateMealProductsById,
|
||||||
deleteMealById,
|
deleteMealById,
|
||||||
};
|
};
|
||||||
|
@ -9,16 +9,15 @@ const products = Joi.object().keys({
|
|||||||
|
|
||||||
const createMeal = {
|
const createMeal = {
|
||||||
body: Joi.object().keys({
|
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'),
|
label: Joi.string().required().valid('breakfast', 'lunch', 'dinner', 'supper', 'snack I', 'snack II', 'snack III'),
|
||||||
products: Joi.array().items(products).required(),
|
products: Joi.array().items(products).required(),
|
||||||
date: Joi.date().required(),
|
date: Joi.date(),
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
const getMeals = {
|
const getMeals = {
|
||||||
query: Joi.object().keys({
|
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(),
|
sortBy: Joi.string(),
|
||||||
limit: Joi.number().integer(),
|
limit: Joi.number().integer(),
|
||||||
page: Joi.number().integer(),
|
page: Joi.number().integer(),
|
||||||
@ -45,6 +44,17 @@ const updateMeal = {
|
|||||||
.min(1),
|
.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 = {
|
const deleteMeal = {
|
||||||
params: Joi.object().keys({
|
params: Joi.object().keys({
|
||||||
mealId: Joi.string().custom(objectId).required(),
|
mealId: Joi.string().custom(objectId).required(),
|
||||||
@ -56,5 +66,6 @@ module.exports = {
|
|||||||
getMeals,
|
getMeals,
|
||||||
getMeal,
|
getMeal,
|
||||||
updateMeal,
|
updateMeal,
|
||||||
|
updateMealProducts,
|
||||||
deleteMeal,
|
deleteMeal,
|
||||||
};
|
};
|
||||||
|
@ -6,21 +6,21 @@ const createProduct = {
|
|||||||
barcode: Joi.number(),
|
barcode: Joi.number(),
|
||||||
verified: Joi.boolean(),
|
verified: Joi.boolean(),
|
||||||
eco: Joi.boolean(),
|
eco: Joi.boolean(),
|
||||||
name: Joi.string().required(),
|
label: Joi.string().required(),
|
||||||
servingCapacity: Joi.number().required(),
|
servingCapacity: Joi.number().required(),
|
||||||
calories: Joi.number().required(),
|
calories: Joi.number().required(),
|
||||||
fat: Joi.number().required(),
|
fat: Joi.number().required(),
|
||||||
carbohydrates: Joi.number().required(),
|
carbohydrates: Joi.number().required(),
|
||||||
protein: Joi.number().required(),
|
protein: Joi.number().required(),
|
||||||
salt: Joi.number(),
|
salt: Joi.number(),
|
||||||
unit: Joi.string().custom(objectId).required(),
|
unit: Joi.string().required().valid('g', 'ml'),
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
const getProducts = {
|
const getProducts = {
|
||||||
query: Joi.object().keys({
|
query: Joi.object().keys({
|
||||||
barcode: Joi.number(),
|
barcode: Joi.number(),
|
||||||
name: Joi.string(),
|
label: Joi.string(),
|
||||||
verified: Joi.boolean(),
|
verified: Joi.boolean(),
|
||||||
eco: Joi.boolean(),
|
eco: Joi.boolean(),
|
||||||
sortBy: Joi.string(),
|
sortBy: Joi.string(),
|
||||||
@ -44,14 +44,14 @@ const updateProduct = {
|
|||||||
barcode: Joi.number(),
|
barcode: Joi.number(),
|
||||||
verified: Joi.boolean(),
|
verified: Joi.boolean(),
|
||||||
eco: Joi.boolean(),
|
eco: Joi.boolean(),
|
||||||
name: Joi.string().required(),
|
label: Joi.string().required(),
|
||||||
servingCapacity: Joi.number().required(),
|
servingCapacity: Joi.number().required(),
|
||||||
calories: Joi.number().required(),
|
calories: Joi.number().required(),
|
||||||
fat: Joi.number().required(),
|
fat: Joi.number().required(),
|
||||||
carbohydrates: Joi.number().required(),
|
carbohydrates: Joi.number().required(),
|
||||||
protein: Joi.number().required(),
|
protein: Joi.number().required(),
|
||||||
salt: Joi.number(),
|
salt: Joi.number(),
|
||||||
unit: Joi.string().custom(objectId).required(),
|
unit: Joi.string().required().valid('g', 'ml'),
|
||||||
})
|
})
|
||||||
.min(1),
|
.min(1),
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user