- 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
215 lines
8.4 KiB
JavaScript
215 lines
8.4 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.NutritionService = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const sequelize_1 = require("@nestjs/sequelize");
|
|
const config_1 = require("@nestjs/config");
|
|
const sequelize_2 = require("sequelize");
|
|
const axios_1 = require("axios");
|
|
const food_item_model_1 = require("../models/food-item.model");
|
|
const api_cache_model_1 = require("../models/api-cache.model");
|
|
let NutritionService = class NutritionService {
|
|
constructor(configService, foodItemModel, apiCacheModel) {
|
|
this.configService = configService;
|
|
this.foodItemModel = foodItemModel;
|
|
this.apiCacheModel = apiCacheModel;
|
|
this.apiKey = this.configService.get('API_NINJAS_KEY');
|
|
this.baseUrl = 'https://api.api-ninjas.com/v1/nutrition';
|
|
this.headers = { 'X-Api-Key': this.apiKey };
|
|
this.cacheDurationDays = 30;
|
|
}
|
|
async searchFood(query) {
|
|
const cached = await this._getFromCache(query);
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
try {
|
|
if (!this.apiKey) {
|
|
console.warn('API_NINJAS_KEY is not set');
|
|
return [];
|
|
}
|
|
const response = await axios_1.default.get(this.baseUrl, {
|
|
headers: this.headers,
|
|
params: { query: query },
|
|
timeout: 10000,
|
|
});
|
|
if (response.status === 200) {
|
|
const data = response.data;
|
|
await this._saveToCache(query, 'api_ninjas', data);
|
|
return this._parseApiResponse(data);
|
|
}
|
|
else {
|
|
console.error(`API Error: ${response.status}`);
|
|
return [];
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.error(`API Request failed: ${error.message}`);
|
|
return [];
|
|
}
|
|
}
|
|
async _getFromCache(query) {
|
|
const cacheEntry = await this.apiCacheModel.findOne({
|
|
where: { query: query.toLowerCase() },
|
|
});
|
|
if (cacheEntry) {
|
|
const age = (new Date().getTime() - new Date(cacheEntry.cached_at).getTime()) / (1000 * 60 * 60 * 24);
|
|
if (age < this.cacheDurationDays) {
|
|
const data = JSON.parse(cacheEntry.response_json);
|
|
return this._parseApiResponse(data);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
async _saveToCache(query, source, data) {
|
|
try {
|
|
let cacheEntry = await this.apiCacheModel.findOne({
|
|
where: { query: query.toLowerCase() },
|
|
});
|
|
if (cacheEntry) {
|
|
cacheEntry.response_json = JSON.stringify(data);
|
|
cacheEntry.cached_at = new Date();
|
|
await cacheEntry.save();
|
|
}
|
|
else {
|
|
await this.apiCacheModel.create({
|
|
query: query.toLowerCase(),
|
|
api_source: source,
|
|
response_json: JSON.stringify(data),
|
|
cached_at: new Date(),
|
|
});
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.error(`Cache save failed: ${error.message}`);
|
|
}
|
|
}
|
|
_parseApiResponse(data) {
|
|
return data.map((item) => ({
|
|
name: (item.name || '').replace(/\b\w/g, (l) => l.toUpperCase()),
|
|
calories: item.calories || 0,
|
|
protein_g: item.protein_g || 0,
|
|
carbs_g: item.carbohydrates_total_g || 0,
|
|
fat_g: item.fat_total_g || 0,
|
|
fiber_g: item.fiber_g || 0,
|
|
sugar_g: item.sugar_g || 0,
|
|
sodium_mg: item.sodium_mg || 0,
|
|
serving_size_g: item.serving_size_g || 100,
|
|
source: 'api_ninjas',
|
|
}));
|
|
}
|
|
async saveFoodToDb(foodData) {
|
|
try {
|
|
const existing = await this.foodItemModel.findOne({
|
|
where: {
|
|
name: foodData.name,
|
|
source: foodData.source || 'api',
|
|
},
|
|
});
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
const food = await this.foodItemModel.create({
|
|
name: foodData.name,
|
|
calories: foodData.calories,
|
|
protein_g: foodData.protein_g || 0,
|
|
carbs_g: foodData.carbs_g || 0,
|
|
fat_g: foodData.fat_g || 0,
|
|
fiber_g: foodData.fiber_g || 0,
|
|
sugar_g: foodData.sugar_g || 0,
|
|
sodium_mg: foodData.sodium_mg || 0,
|
|
serving_size_g: foodData.serving_size_g || 100,
|
|
serving_description: foodData.serving_description || '1 serving',
|
|
source: foodData.source || 'api',
|
|
api_data: JSON.stringify(foodData),
|
|
});
|
|
return food;
|
|
}
|
|
catch (error) {
|
|
console.error(`Error saving food to DB: ${error.message}`);
|
|
return null;
|
|
}
|
|
}
|
|
async searchAllSources(query) {
|
|
const results = [];
|
|
const filipinoFoods = await this.foodItemModel.findAll({
|
|
where: {
|
|
is_filipino: true,
|
|
[sequelize_2.Op.or]: [
|
|
{ name: { [sequelize_2.Op.like]: `%${query}%` } },
|
|
{ name_tagalog: { [sequelize_2.Op.like]: `%${query}%` } },
|
|
],
|
|
},
|
|
limit: 5,
|
|
});
|
|
for (const food of filipinoFoods) {
|
|
results.push({
|
|
id: food.id,
|
|
name: food.name,
|
|
name_tagalog: food.name_tagalog,
|
|
calories: food.calories,
|
|
protein_g: food.protein_g,
|
|
carbs_g: food.carbs_g,
|
|
fat_g: food.fat_g,
|
|
serving_description: food.serving_description,
|
|
source: 'filipino',
|
|
category: food.category,
|
|
});
|
|
}
|
|
const otherFoods = await this.foodItemModel.findAll({
|
|
where: {
|
|
is_filipino: false,
|
|
name: { [sequelize_2.Op.like]: `%${query}%` },
|
|
},
|
|
limit: 5,
|
|
});
|
|
for (const food of otherFoods) {
|
|
results.push({
|
|
id: food.id,
|
|
name: food.name,
|
|
calories: food.calories,
|
|
protein_g: food.protein_g,
|
|
carbs_g: food.carbs_g,
|
|
fat_g: food.fat_g,
|
|
serving_description: food.serving_description,
|
|
source: food.source,
|
|
});
|
|
}
|
|
if (results.length < 3 && this.apiKey) {
|
|
const apiResults = await this.searchFood(query);
|
|
for (const foodData of apiResults.slice(0, 5)) {
|
|
results.push({
|
|
name: foodData.name,
|
|
calories: foodData.calories,
|
|
protein_g: foodData.protein_g,
|
|
carbs_g: foodData.carbs_g || 0,
|
|
fat_g: foodData.fat_g || 0,
|
|
serving_description: '100g',
|
|
source: 'api_ninjas',
|
|
raw_data: foodData,
|
|
});
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
};
|
|
exports.NutritionService = NutritionService;
|
|
exports.NutritionService = NutritionService = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__param(1, (0, sequelize_1.InjectModel)(food_item_model_1.FoodItem)),
|
|
__param(2, (0, sequelize_1.InjectModel)(api_cache_model_1.APICache)),
|
|
__metadata("design:paramtypes", [config_1.ConfigService, Object, Object])
|
|
], NutritionService);
|
|
//# sourceMappingURL=nutrition.service.js.map
|