- Add tsconfig.json for TypeScript compilation with declaration and source map generation - Generate .d.ts declaration files for all modules, services, controllers, and models - Update package.json with NestJS dependencies and TypeScript development tools - Include database files in the distribution output for persistence
150 lines
6.1 KiB
JavaScript
150 lines
6.1 KiB
JavaScript
"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.MealPlannerService = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const sequelize_1 = require("@nestjs/sequelize");
|
|
const meal_plan_model_1 = require("../models/meal-plan.model");
|
|
const planned_food_model_1 = require("../models/planned-food.model");
|
|
const food_item_model_1 = require("../models/food-item.model");
|
|
const utils_service_1 = require("../utils/utils.service");
|
|
let MealPlannerService = class MealPlannerService {
|
|
constructor(mealPlanModel, plannedFoodModel, foodItemModel, utilsService) {
|
|
this.mealPlanModel = mealPlanModel;
|
|
this.plannedFoodModel = plannedFoodModel;
|
|
this.foodItemModel = foodItemModel;
|
|
this.utilsService = utilsService;
|
|
}
|
|
async getWeeklyMealPlans(userId) {
|
|
const today = new Date();
|
|
const day = today.getDay();
|
|
const diff = today.getDate() - day + (day === 0 ? -6 : 1);
|
|
const startDate = new Date(today);
|
|
startDate.setDate(diff);
|
|
const dates = [];
|
|
for (let i = 0; i < 7; i++) {
|
|
const d = new Date(startDate);
|
|
d.setDate(startDate.getDate() + i);
|
|
dates.push(d);
|
|
}
|
|
const mealPlans = {};
|
|
for (const d of dates) {
|
|
const dateStr = d.toISOString().split('T')[0];
|
|
const plans = await this.mealPlanModel.findAll({
|
|
where: { UserId: userId, date: dateStr },
|
|
include: [
|
|
{
|
|
model: planned_food_model_1.PlannedFood,
|
|
include: [food_item_model_1.FoodItem],
|
|
},
|
|
],
|
|
});
|
|
mealPlans[dateStr] = plans.map((p) => {
|
|
let calories = 0, protein = 0, carbs = 0, fat = 0;
|
|
const foods = [];
|
|
if (p.plannedFoods) {
|
|
p.plannedFoods.forEach((pf) => {
|
|
if (pf.foodItem) {
|
|
const q = pf.quantity || 1;
|
|
calories += pf.foodItem.calories * q;
|
|
protein += pf.foodItem.protein_g * q;
|
|
carbs += pf.foodItem.carbs_g * q;
|
|
fat += pf.foodItem.fat_g * q;
|
|
foods.push({
|
|
name: pf.foodItem.name,
|
|
quantity: q,
|
|
});
|
|
}
|
|
});
|
|
}
|
|
return {
|
|
id: p.id,
|
|
meal_type: p.meal_type,
|
|
is_completed: p.is_completed,
|
|
foods: foods,
|
|
totals: {
|
|
calories,
|
|
protein,
|
|
carbs,
|
|
fat,
|
|
},
|
|
};
|
|
});
|
|
}
|
|
return {
|
|
dates: dates.map((d) => d.toISOString().split('T')[0]),
|
|
mealPlans,
|
|
today: new Date().toISOString().split('T')[0],
|
|
};
|
|
}
|
|
async autoGenerate(userId, date) {
|
|
return this.utilsService.generateDailyMealPlan(userId, date);
|
|
}
|
|
async addMealPlan(userId, data) {
|
|
let { date, meal_type, 'food_id[]': foodIds, 'quantity[]': quantities } = data;
|
|
if (!foodIds) {
|
|
throw new Error('Please add at least one food item');
|
|
}
|
|
if (!Array.isArray(foodIds)) {
|
|
foodIds = [foodIds];
|
|
quantities = [quantities];
|
|
}
|
|
let mealPlan = await this.mealPlanModel.findOne({
|
|
where: {
|
|
UserId: userId,
|
|
date: date,
|
|
meal_type: meal_type,
|
|
},
|
|
});
|
|
if (!mealPlan) {
|
|
mealPlan = await this.mealPlanModel.create({
|
|
UserId: userId,
|
|
date: date,
|
|
meal_type: meal_type,
|
|
});
|
|
}
|
|
for (let i = 0; i < foodIds.length; i++) {
|
|
const foodId = foodIds[i];
|
|
const quantity = quantities[i];
|
|
if (foodId && quantity) {
|
|
await this.plannedFoodModel.create({
|
|
MealPlanId: mealPlan.id,
|
|
FoodItemId: foodId,
|
|
quantity: quantity,
|
|
});
|
|
}
|
|
}
|
|
return mealPlan;
|
|
}
|
|
async toggleComplete(userId, planId) {
|
|
const plan = await this.mealPlanModel.findOne({
|
|
where: { id: planId, UserId: userId },
|
|
});
|
|
if (plan) {
|
|
plan.is_completed = !plan.is_completed;
|
|
await plan.save();
|
|
return plan;
|
|
}
|
|
return null;
|
|
}
|
|
};
|
|
exports.MealPlannerService = MealPlannerService;
|
|
exports.MealPlannerService = MealPlannerService = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__param(0, (0, sequelize_1.InjectModel)(meal_plan_model_1.MealPlan)),
|
|
__param(1, (0, sequelize_1.InjectModel)(planned_food_model_1.PlannedFood)),
|
|
__param(2, (0, sequelize_1.InjectModel)(food_item_model_1.FoodItem)),
|
|
__metadata("design:paramtypes", [Object, Object, Object, utils_service_1.UtilsService])
|
|
], MealPlannerService);
|
|
//# sourceMappingURL=meal-planner.service.js.map
|