Files
recipe-app/backend/dist/recipes/recipe-analysis.service.js
T
Nils-Johan Gynther a68a0ca86f
Test Suite / test (24.15.0) (push) Has been cancelled
feat: add unit mapping functionality
- Added new API path for unit mappings in `api_paths.dart`.
- Implemented `upsertUnitMapping` method in `ImportRepository` to handle unit mapping creation.
- Updated `ReceiptImportTab` to learn and save unit mappings during receipt import.
- Created DTO for unit mapping with validation in `create-unit-mapping.dto.ts`.
- Added SQL migration for `UnitMapping` table creation with necessary constraints.
2026-05-07 10:00:42 +02:00

277 lines
13 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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RecipeAnalysisService = void 0;
const common_1 = require("@nestjs/common");
const prisma_service_1 = require("../prisma/prisma.service");
const units_1 = require("../common/utils/units");
const ai_service_1 = require("../ai/ai.service");
let RecipeAnalysisService = class RecipeAnalysisService {
constructor(prisma, aiService) {
this.prisma = prisma;
this.aiService = aiService;
}
async getAccessibleRecipe(id, userId) {
const recipe = await this.prisma.recipe.findFirst({
where: {
id,
OR: [
{ isPublic: true },
{ ownerId: userId },
{ shares: { some: { userId } } },
],
},
include: {
ingredients: {
include: {
product: true,
},
orderBy: { id: 'asc' },
},
},
});
if (!recipe) {
throw new common_1.NotFoundException(`Recipe with id ${id} not found`);
}
return recipe;
}
calculateAvailableQuantity(inventoryItems, requiredUnit) {
if (!requiredUnit) {
return inventoryItems.reduce((sum, item) => sum + Number(item.quantity ?? 0), 0);
}
const normalizedRequiredUnit = requiredUnit.trim().toLowerCase();
const sameUnit = inventoryItems
.filter((item) => item.unit.trim().toLowerCase() === normalizedRequiredUnit)
.reduce((sum, item) => sum + Number(item.quantity ?? 0), 0);
const converted = inventoryItems
.filter((item) => item.unit.trim().toLowerCase() !== normalizedRequiredUnit)
.reduce((sum, item) => {
if (!(0, units_1.canConvert)(item.unit, requiredUnit))
return sum;
try {
return sum + (0, units_1.convertUnit)(Number(item.quantity ?? 0), item.unit, requiredUnit);
}
catch {
return sum;
}
}, 0);
return sameUnit + converted;
}
async analyzeRecipeIngredients(id, userId) {
const recipe = await this.getAccessibleRecipe(id, userId);
const user = await this.prisma.user.findUnique({
where: { id: userId },
select: { aiEngineEnabled: true },
});
const pantryItems = await this.prisma.pantryItem.findMany({
where: { userId },
select: { productId: true },
});
const pantryProductIds = new Set(pantryItems.map((p) => p.productId));
const userInventory = await this.prisma.inventoryItem.findMany({
where: { product: { ownerId: userId } },
select: { productId: true },
});
const availableProductIds = new Set([
...pantryItems.map((p) => p.productId),
...userInventory.map((i) => i.productId),
]);
const availableProducts = availableProductIds.size > 0
? await this.prisma.product.findMany({
where: { id: { in: Array.from(availableProductIds) }, isActive: true },
select: { id: true, name: true, canonicalName: true },
})
: [];
const ingredients = await Promise.all(recipe.ingredients.map(async (ingredient) => {
const requiredQuantity = Number(ingredient.quantity ?? 0);
const requiredUnit = (ingredient.unit ?? '').trim();
const rawName = (ingredient.rawName ?? '').trim() || 'Okänd ingrediens';
if (!ingredient.productId || !ingredient.product) {
const aiMatches = user?.aiEngineEnabled ? await this.aiService.suggestIngredientMatches(rawName, availableProducts) : [];
const aiBest = aiMatches[0];
if (aiBest) {
const matched = availableProducts.find((p) => p.id === aiBest.productId);
return {
ingredientId: ingredient.id,
rawName,
quantity: requiredQuantity,
unit: requiredUnit,
note: ingredient.note ?? null,
status: 'substitutable',
matchedProductId: aiBest.productId,
matchedProductName: matched?.canonicalName || matched?.name || null,
source: 'ai_match',
availableQuantity: 0,
missingQuantity: requiredQuantity,
};
}
return {
ingredientId: ingredient.id,
rawName,
quantity: requiredQuantity,
unit: requiredUnit,
note: ingredient.note ?? null,
status: 'missing',
matchedProductId: null,
matchedProductName: null,
source: null,
availableQuantity: 0,
missingQuantity: requiredQuantity,
};
}
if (pantryProductIds.has(ingredient.productId)) {
return {
ingredientId: ingredient.id,
rawName,
quantity: requiredQuantity,
unit: requiredUnit,
note: ingredient.note ?? null,
status: 'covered_by_pantry',
matchedProductId: ingredient.productId,
matchedProductName: ingredient.product.canonicalName || ingredient.product.name,
source: 'pantry',
availableQuantity: requiredQuantity,
missingQuantity: 0,
};
}
const inventoryItems = await this.prisma.inventoryItem.findMany({
where: { productId: ingredient.productId, product: { ownerId: userId } },
select: { quantity: true, unit: true },
});
const availableQuantity = this.calculateAvailableQuantity(inventoryItems, requiredUnit);
if (availableQuantity >= requiredQuantity) {
return {
ingredientId: ingredient.id,
rawName,
quantity: requiredQuantity,
unit: requiredUnit,
note: ingredient.note ?? null,
status: 'exact_match',
matchedProductId: ingredient.productId,
matchedProductName: ingredient.product.canonicalName || ingredient.product.name,
source: 'inventory',
availableQuantity,
missingQuantity: 0,
};
}
const alternativeProductIds = Array.isArray(ingredient.alternativeProductIds)
? ingredient.alternativeProductIds.filter((id) => typeof id === 'number')
: [];
for (const altProductId of alternativeProductIds) {
if (pantryProductIds.has(altProductId)) {
const altProduct = await this.prisma.product.findUnique({
where: { id: altProductId },
select: { id: true, name: true, canonicalName: true },
});
return {
ingredientId: ingredient.id,
rawName,
quantity: requiredQuantity,
unit: requiredUnit,
note: ingredient.note ?? null,
status: 'substitutable',
matchedProductId: altProduct?.id ?? altProductId,
matchedProductName: altProduct?.canonicalName || altProduct?.name || null,
source: 'pantry_substitute',
availableQuantity: requiredQuantity,
missingQuantity: 0,
};
}
const altInventoryItems = await this.prisma.inventoryItem.findMany({
where: { productId: altProductId },
select: { quantity: true, unit: true },
});
const altAvailable = this.calculateAvailableQuantity(altInventoryItems, requiredUnit);
if (altAvailable > 0) {
const altProduct = await this.prisma.product.findUnique({
where: { id: altProductId },
select: { id: true, name: true, canonicalName: true },
});
return {
ingredientId: ingredient.id,
rawName,
quantity: requiredQuantity,
unit: requiredUnit,
note: ingredient.note ?? null,
status: 'substitutable',
matchedProductId: altProduct?.id ?? altProductId,
matchedProductName: altProduct?.canonicalName || altProduct?.name || null,
source: 'inventory_substitute',
availableQuantity: altAvailable,
missingQuantity: Math.max(0, requiredQuantity - altAvailable),
};
}
}
const aiSubs = user?.aiEngineEnabled ? await this.aiService.suggestSubstitutions(rawName, availableProducts) : [];
const aiBestSub = aiSubs[0];
if (aiBestSub) {
const aiProduct = availableProducts.find((p) => p.id === aiBestSub.productId);
return {
ingredientId: ingredient.id,
rawName,
quantity: requiredQuantity,
unit: requiredUnit,
note: ingredient.note ?? null,
status: 'substitutable',
matchedProductId: aiBestSub.productId,
matchedProductName: aiProduct?.canonicalName || aiProduct?.name || null,
source: 'ai_substitute',
availableQuantity,
missingQuantity: Math.max(0, requiredQuantity - availableQuantity),
};
}
return {
ingredientId: ingredient.id,
rawName,
quantity: requiredQuantity,
unit: requiredUnit,
note: ingredient.note ?? null,
status: 'missing',
matchedProductId: ingredient.productId,
matchedProductName: ingredient.product.canonicalName || ingredient.product.name,
source: null,
availableQuantity,
missingQuantity: Math.max(0, requiredQuantity - availableQuantity),
};
}));
const summary = {
exactCount: ingredients.filter((i) => i.status === 'exact_match').length,
pantryCount: ingredients.filter((i) => i.status === 'covered_by_pantry').length,
substituteCount: ingredients.filter((i) => i.status === 'substitutable').length,
missingCount: ingredients.filter((i) => i.status === 'missing').length,
};
const shoppingListCandidates = ingredients
.filter((i) => i.status === 'missing')
.map((i) => ({
ingredientId: i.ingredientId,
rawName: i.rawName,
quantity: i.quantity,
unit: i.unit,
missingQuantity: i.missingQuantity,
}));
return {
recipeId: recipe.id,
ingredients,
summary,
shoppingListCandidates,
};
}
async rematchRecipeIngredients(id, userId) {
return this.analyzeRecipeIngredients(id, userId);
}
};
exports.RecipeAnalysisService = RecipeAnalysisService;
exports.RecipeAnalysisService = RecipeAnalysisService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [prisma_service_1.PrismaService,
ai_service_1.AiService])
], RecipeAnalysisService);
//# sourceMappingURL=recipe-analysis.service.js.map