"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.DashboardController = void 0; const common_1 = require("@nestjs/common"); const authenticated_guard_1 = require("../auth/guards/authenticated.guard"); const utils_service_1 = require("../utils/utils.service"); const user_goal_model_1 = require("../models/user-goal.model"); const weight_log_model_1 = require("../models/weight-log.model"); const sequelize_1 = require("@nestjs/sequelize"); let DashboardController = class DashboardController { constructor(utilsService, userGoalModel, weightLogModel) { this.utilsService = utilsService; this.userGoalModel = userGoalModel; this.weightLogModel = weightLogModel; } async getDashboard(req, res) { try { const today = new Date(); const dateStr = today.toISOString().split('T')[0]; const nutrition = await this.utilsService.calculateDailyTotals(req.user.id, dateStr); const water = await this.utilsService.calculateWaterTotal(req.user.id, dateStr); let goals = await this.userGoalModel.findOne({ where: { UserId: req.user.id } }); if (!goals) { goals = await this.userGoalModel.create({ UserId: req.user.id, target_protein_g: 150, target_carbs_g: 200, target_fat_g: 60, target_water_ml: 2000, }); } const weightLogToday = await this.weightLogModel.findOne({ where: { UserId: req.user.id, date: dateStr }, }); const yesterday = new Date(today); yesterday.setDate(yesterday.getDate() - 1); const yesterdayStr = yesterday.toISOString().split('T')[0]; const weightLogYesterday = await this.weightLogModel.findOne({ where: { UserId: req.user.id, date: yesterdayStr }, }); let weightChange = null; if (weightLogToday && weightLogYesterday) { weightChange = weightLogToday.weight_kg - weightLogYesterday.weight_kg; } const remaining = { calories: req.user.target_daily_calories - nutrition.calories, protein: goals.target_protein_g - nutrition.protein, carbs: goals.target_carbs_g - nutrition.carbs, fat: goals.target_fat_g - nutrition.fat, water: goals.target_water_ml - water.total_ml, }; const macro_percentages = this.utilsService.getMacroPercentages(nutrition.protein, nutrition.carbs, nutrition.fat); const suggestions = this.utilsService.suggestFoodsForMacros(remaining.protein, remaining.carbs, remaining.fat); const calorie_trend = await this.utilsService.getCalorieTrend(req.user.id); const weight_trend = await this.utilsService.getWeightTrend(req.user.id); return { user: req.user, current_user: req.user, nutrition, water, goals, weightLogToday, weightChange, remaining, macro_percentages, suggestions, calorie_trend, weight_trend, }; } catch (err) { console.error(err); req.flash('error_msg', 'Error loading dashboard'); return { user: req.user, current_user: req.user, nutrition: { calories: 0, protein: 0, carbs: 0, fat: 0, meals: [] }, water: { total_ml: 0, logs: [] }, goals: null, weightLogToday: null, weightChange: null, remaining: { calories: 0, protein: 0, carbs: 0, fat: 0, water: 0 }, macro_percentages: { protein: 0, carbs: 0, fat: 0 }, suggestions: [], calorie_trend: [], weight_trend: [], }; } } }; exports.DashboardController = DashboardController; __decorate([ (0, common_1.Get)(), (0, common_1.Render)('dashboard'), __param(0, (0, common_1.Req)()), __param(1, (0, common_1.Res)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], DashboardController.prototype, "getDashboard", null); exports.DashboardController = DashboardController = __decorate([ (0, common_1.Controller)('dashboard'), (0, common_1.UseGuards)(authenticated_guard_1.AuthenticatedGuard), __param(1, (0, sequelize_1.InjectModel)(user_goal_model_1.UserGoal)), __param(2, (0, sequelize_1.InjectModel)(weight_log_model_1.WeightLog)), __metadata("design:paramtypes", [utils_service_1.UtilsService, Object, Object]) ], DashboardController); //# sourceMappingURL=dashboard.controller.js.map