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:
+6
-54
@@ -19,11 +19,13 @@ const ai_service_1 = require("../ai/ai.service");
|
||||
const download_image_1 = require("../common/utils/download-image");
|
||||
const recipe_parser_1 = require("../common/utils/recipe-parser");
|
||||
const units_1 = require("../common/utils/units");
|
||||
const recipe_matching_service_1 = require("./recipe-matching.service");
|
||||
const IMAGE_DEST_DIR = process.env.IMAGE_DEST_DIR || '/app/recipe-images';
|
||||
let RecipesService = RecipesService_1 = class RecipesService {
|
||||
constructor(prisma, aiService) {
|
||||
constructor(prisma, aiService, recipeMatchingService) {
|
||||
this.prisma = prisma;
|
||||
this.aiService = aiService;
|
||||
this.recipeMatchingService = recipeMatchingService;
|
||||
this.logger = new common_1.Logger(RecipesService_1.name);
|
||||
}
|
||||
throwRecipeNotFound(id) {
|
||||
@@ -608,59 +610,8 @@ Regler:
|
||||
where: { isActive: true },
|
||||
select: { id: true, name: true, canonicalName: true, normalizedName: true },
|
||||
});
|
||||
const normalize = (s) => s.toLowerCase().trim().replace(/[^a-zåäö0-9\s]/gi, '').replace(/\s+/g, ' ');
|
||||
const levenshtein = (a, b) => {
|
||||
const m = a.length;
|
||||
const n = b.length;
|
||||
const dp = Array.from({ length: m + 1 }, (_, i) => Array.from({ length: n + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)));
|
||||
for (let i = 1; i <= m; i++) {
|
||||
for (let j = 1; j <= n; j++) {
|
||||
dp[i][j] =
|
||||
a[i - 1] === b[j - 1]
|
||||
? dp[i - 1][j - 1]
|
||||
: 1 + Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]);
|
||||
}
|
||||
}
|
||||
return dp[m][n];
|
||||
};
|
||||
const ingredientsWithSuggestions = parsed.ingredients.map((ingredient) => {
|
||||
const alternatives = ingredient.alternatives?.length > 1
|
||||
? ingredient.alternatives
|
||||
: [ingredient.rawName];
|
||||
const scoreProduct = (query) => allProducts
|
||||
.map((product) => {
|
||||
const targetName = normalize(product.canonicalName || product.name);
|
||||
const targetNormalized = normalize(product.normalizedName);
|
||||
if (targetNormalized === query || targetName === query) {
|
||||
return { product, score: 100 };
|
||||
}
|
||||
if (targetName.includes(query) || query.includes(targetName)) {
|
||||
return { product, score: 70 };
|
||||
}
|
||||
const dist = levenshtein(query, targetName);
|
||||
const maxLen = Math.max(query.length, targetName.length);
|
||||
const similarity = maxLen === 0 ? 100 : Math.round((1 - dist / maxLen) * 100);
|
||||
return { product, score: similarity };
|
||||
})
|
||||
.filter((s) => s.score >= 40)
|
||||
.sort((a, b) => b.score - a.score)
|
||||
.slice(0, 5);
|
||||
const seenIds = new Set();
|
||||
const scored = alternatives
|
||||
.flatMap((alt) => scoreProduct(normalize(alt)))
|
||||
.filter((s) => {
|
||||
if (seenIds.has(s.product.id))
|
||||
return false;
|
||||
seenIds.add(s.product.id);
|
||||
return true;
|
||||
})
|
||||
.sort((a, b) => b.score - a.score)
|
||||
.slice(0, 5)
|
||||
.map((s) => ({
|
||||
productId: s.product.id,
|
||||
productName: s.product.canonicalName || s.product.name,
|
||||
score: s.score,
|
||||
}));
|
||||
const scored = this.recipeMatchingService.buildIngredientSuggestions(ingredient.rawName, ingredient.alternatives, allProducts);
|
||||
return {
|
||||
rawName: ingredient.rawName,
|
||||
rawLine: ingredient.rawName,
|
||||
@@ -683,6 +634,7 @@ exports.RecipesService = RecipesService;
|
||||
exports.RecipesService = RecipesService = RecipesService_1 = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__metadata("design:paramtypes", [prisma_service_1.PrismaService,
|
||||
ai_service_1.AiService])
|
||||
ai_service_1.AiService,
|
||||
recipe_matching_service_1.RecipeMatchingService])
|
||||
], RecipesService);
|
||||
//# sourceMappingURL=recipes.service.js.map
|
||||
Reference in New Issue
Block a user