feat: add rematch functionality for recipe ingredients and enhance inventory management
Test Suite / test (24.15.0) (push) Has been cancelled
Test Suite / test (24.15.0) (push) Has been cancelled
- Added a new API path for rematching recipe ingredients in `api_paths.dart`. - Implemented a manual product creation dialog in `inventory_screen.dart` to allow users to create new products directly. - Integrated the rematch functionality in `recipe_repository.dart` to handle rematching of recipe ingredients. - Updated the recipe detail screen to include a button for triggering the rematch process. - Introduced a new `RecipeMatchingService` in the backend to handle ingredient matching logic. - Added database migration to include `aiEngineEnabled` column in the User table. Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
+61
-2
@@ -13,9 +13,11 @@ 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) {
|
||||
constructor(prisma, aiService) {
|
||||
this.prisma = prisma;
|
||||
this.aiService = aiService;
|
||||
}
|
||||
async getAccessibleRecipe(id, userId) {
|
||||
const recipe = await this.prisma.recipe.findFirst({
|
||||
@@ -65,16 +67,51 @@ let RecipeAnalysisService = class RecipeAnalysisService {
|
||||
}
|
||||
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({
|
||||
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,
|
||||
@@ -172,6 +209,24 @@ let RecipeAnalysisService = class RecipeAnalysisService {
|
||||
};
|
||||
}
|
||||
}
|
||||
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,
|
||||
@@ -208,10 +263,14 @@ let RecipeAnalysisService = class RecipeAnalysisService {
|
||||
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])
|
||||
__metadata("design:paramtypes", [prisma_service_1.PrismaService,
|
||||
ai_service_1.AiService])
|
||||
], RecipeAnalysisService);
|
||||
//# sourceMappingURL=recipe-analysis.service.js.map
|
||||
Reference in New Issue
Block a user