"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MealsController = void 0; const common_1 = require("@nestjs/common"); const sequelize_1 = require("@nestjs/sequelize"); const authenticated_guard_1 = require("../auth/guards/authenticated.guard"); const utils_service_1 = require("../utils/utils.service"); const nutrition_service_1 = require("./nutrition.service"); const meal_model_1 = require("../models/meal.model"); const meal_food_model_1 = require("../models/meal-food.model"); const food_item_model_1 = require("../models/food-item.model"); const water_log_model_1 = require("../models/water-log.model"); const weight_log_model_1 = require("../models/weight-log.model"); let MealsController = class MealsController { constructor(utilsService, nutritionService, mealModel, mealFoodModel, foodItemModel, waterLogModel, weightLogModel) { this.utilsService = utilsService; this.nutritionService = nutritionService; this.mealModel = mealModel; this.mealFoodModel = mealFoodModel; this.foodItemModel = foodItemModel; this.waterLogModel = waterLogModel; this.weightLogModel = weightLogModel; } addMealPage(req, res) { res.render('add_meal', { today: new Date().toISOString().split('T')[0] }); } async addMeal(req, res, body) { try { let { date, meal_type, time, 'food_id[]': foodIds, 'quantity[]': quantities } = body; if (!foodIds) { req.flash('error_msg', 'Please add at least one food item'); return res.redirect('/add-meal'); } if (!Array.isArray(foodIds)) { foodIds = [foodIds]; quantities = [quantities]; } const meal = await this.mealModel.create({ UserId: req.user.id, date: date || new Date(), meal_type, time: time || null, }); for (let i = 0; i < foodIds.length; i++) { const foodId = foodIds[i]; const quantity = quantities[i]; if (foodId && quantity) { const food = await this.foodItemModel.findByPk(foodId); if (food) { await this.mealFoodModel.create({ MealId: meal.id, FoodItemId: foodId, quantity: quantity, calories_consumed: food.calories * quantity, protein_consumed: food.protein_g * quantity, carbs_consumed: food.carbs_g * quantity, fat_consumed: food.fat_g * quantity, }); } } } await this.utilsService.updateDailySummary(req.user.id, date); req.flash('success_msg', 'Meal added successfully!'); res.redirect('/dashboard'); } catch (err) { console.error(err); req.flash('error_msg', 'Error adding meal'); res.redirect('/add-meal'); } } async searchFood(query) { if (!query || query.length < 2) { return []; } return this.nutritionService.searchAllSources(query); } async addFood(body) { try { const food = await this.nutritionService.saveFoodToDb(body); if (food) { return { success: true, food_id: food.id, name: food.name, }; } else { return { success: false }; } } catch (err) { console.error(err); return { success: false }; } } async addWater(req, res, body) { try { const amountMl = parseInt(body.amount_ml) || 250; const date = body.date || new Date().toISOString().split('T')[0]; await this.waterLogModel.create({ UserId: req.user.id, date: date, amount_ml: amountMl, time: new Date().toISOString(), }); await this.utilsService.updateDailySummary(req.user.id, date); req.flash('success_msg', `Added ${amountMl}ml of water!`); res.redirect('/dashboard'); } catch (err) { console.error(err); req.flash('error_msg', 'Error adding water'); res.redirect('/dashboard'); } } async addWeight(req, res, body) { try { const weightKg = parseFloat(body.weight_kg); const date = body.date || new Date().toISOString().split('T')[0]; const weightLog = await this.weightLogModel.findOne({ where: { UserId: req.user.id, date: date }, }); if (weightLog) { weightLog.weight_kg = weightKg; weightLog.time = new Date().toISOString(); await weightLog.save(); } else { await this.weightLogModel.create({ UserId: req.user.id, date: date, weight_kg: weightKg, time: new Date().toISOString(), }); } await this.utilsService.updateDailySummary(req.user.id, date); res.redirect('/dashboard'); } catch (err) { console.error(err); req.flash('error_msg', 'Error adding weight'); res.redirect('/dashboard'); } } }; exports.MealsController = MealsController; __decorate([ (0, common_1.Get)('add-meal'), __param(0, (0, common_1.Req)()), __param(1, (0, common_1.Res)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", void 0) ], MealsController.prototype, "addMealPage", null); __decorate([ (0, common_1.Post)('add-meal'), __param(0, (0, common_1.Req)()), __param(1, (0, common_1.Res)()), __param(2, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object, Object]), __metadata("design:returntype", Promise) ], MealsController.prototype, "addMeal", null); __decorate([ (0, common_1.Get)('api/search-food'), __param(0, (0, common_1.Query)('q')), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], MealsController.prototype, "searchFood", null); __decorate([ (0, common_1.Post)('api/add-food'), __param(0, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], MealsController.prototype, "addFood", null); __decorate([ (0, common_1.Post)('add-water'), __param(0, (0, common_1.Req)()), __param(1, (0, common_1.Res)()), __param(2, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object, Object]), __metadata("design:returntype", Promise) ], MealsController.prototype, "addWater", null); __decorate([ (0, common_1.Post)('add-weight'), __param(0, (0, common_1.Req)()), __param(1, (0, common_1.Res)()), __param(2, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object, Object]), __metadata("design:returntype", Promise) ], MealsController.prototype, "addWeight", null); exports.MealsController = MealsController = __decorate([ (0, common_1.Controller)(), (0, common_1.UseGuards)(authenticated_guard_1.AuthenticatedGuard), __param(2, (0, sequelize_1.InjectModel)(meal_model_1.Meal)), __param(3, (0, sequelize_1.InjectModel)(meal_food_model_1.MealFood)), __param(4, (0, sequelize_1.InjectModel)(food_item_model_1.FoodItem)), __param(5, (0, sequelize_1.InjectModel)(water_log_model_1.WaterLog)), __param(6, (0, sequelize_1.InjectModel)(weight_log_model_1.WeightLog)), __metadata("design:paramtypes", [utils_service_1.UtilsService, nutrition_service_1.NutritionService, Object, Object, Object, Object, Object]) ], MealsController); //# sourceMappingURL=meals.controller.js.map