feat: implement recipe analysis service and data models
Test Suite / test (24.15.0) (push) Has been cancelled
Test Suite / test (24.15.0) (push) Has been cancelled
- Added RecipeAnalysisService to handle recipe ingredient analysis, including methods for checking ingredient availability and calculating quantities. - Introduced new TypeScript definitions for recipe analysis results, including ingredient status and summary. - Created corresponding Dart models for recipe analysis, including RecipeIngredientAnalysis, RecipeAnalysisSummary, and RecipeShoppingCandidate. - Updated Flutter UI to reflect changes in ingredient availability status. - Fixed color opacity issue in recipe image card.
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { canConvert, convertUnit } from '../common/utils/units';
|
||||
|
||||
type AnalysisStatus = 'exact_match' | 'covered_by_pantry' | 'substitutable' | 'missing';
|
||||
|
||||
@Injectable()
|
||||
export class RecipeAnalysisService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
private async getAccessibleRecipe(id: number, userId: number) {
|
||||
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 NotFoundException(`Recipe with id ${id} not found`);
|
||||
}
|
||||
|
||||
return recipe;
|
||||
}
|
||||
|
||||
private calculateAvailableQuantity(
|
||||
inventoryItems: Array<{ quantity: any; unit: string }>,
|
||||
requiredUnit: string,
|
||||
): number {
|
||||
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 (!canConvert(item.unit, requiredUnit)) return sum;
|
||||
try {
|
||||
return sum + convertUnit(Number(item.quantity ?? 0), item.unit, requiredUnit);
|
||||
} catch {
|
||||
return sum;
|
||||
}
|
||||
}, 0);
|
||||
|
||||
return sameUnit + converted;
|
||||
}
|
||||
|
||||
async analyzeRecipeIngredients(id: number, userId: number) {
|
||||
const recipe = await this.getAccessibleRecipe(id, userId);
|
||||
|
||||
const pantryItems = await this.prisma.pantryItem.findMany({
|
||||
where: { userId },
|
||||
select: { productId: true },
|
||||
});
|
||||
const pantryProductIds = new Set(pantryItems.map((p) => p.productId));
|
||||
|
||||
const ingredients = await Promise.all(
|
||||
recipe.ingredients.map(async (ingredient: any) => {
|
||||
const requiredQuantity = Number(ingredient.quantity ?? 0);
|
||||
const requiredUnit = (ingredient.unit ?? '').trim();
|
||||
const rawName = (ingredient.rawName ?? '').trim() || 'Okänd ingrediens';
|
||||
|
||||
if (!ingredient.productId || !ingredient.product) {
|
||||
return {
|
||||
ingredientId: ingredient.id,
|
||||
rawName,
|
||||
quantity: requiredQuantity,
|
||||
unit: requiredUnit,
|
||||
note: ingredient.note ?? null,
|
||||
status: 'missing' as AnalysisStatus,
|
||||
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' as AnalysisStatus,
|
||||
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 },
|
||||
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' as AnalysisStatus,
|
||||
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: any) => 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' as AnalysisStatus,
|
||||
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' as AnalysisStatus,
|
||||
matchedProductId: altProduct?.id ?? altProductId,
|
||||
matchedProductName: altProduct?.canonicalName || altProduct?.name || null,
|
||||
source: 'inventory_substitute',
|
||||
availableQuantity: altAvailable,
|
||||
missingQuantity: Math.max(0, requiredQuantity - altAvailable),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ingredientId: ingredient.id,
|
||||
rawName,
|
||||
quantity: requiredQuantity,
|
||||
unit: requiredUnit,
|
||||
note: ingredient.note ?? null,
|
||||
status: 'missing' as AnalysisStatus,
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { ParseMarkdownDto } from './dto/parse-markdown.dto';
|
||||
import { CurrentUser } from '../auth/decorators/current-user.decorator';
|
||||
import { ShareRecipeDto } from './dto/share-recipe.dto';
|
||||
import { SetRecipeVisibilityDto } from './dto/set-recipe-visibility.dto';
|
||||
import { RecipeAnalysisService } from './recipe-analysis.service';
|
||||
|
||||
class UpdateImageDto {
|
||||
@IsString()
|
||||
@@ -15,7 +16,10 @@ class UpdateImageDto {
|
||||
|
||||
@Controller('recipes')
|
||||
export class RecipesController {
|
||||
constructor(private readonly recipesService: RecipesService) {}
|
||||
constructor(
|
||||
private readonly recipesService: RecipesService,
|
||||
private readonly recipeAnalysisService: RecipeAnalysisService,
|
||||
) {}
|
||||
|
||||
@Post('parse-markdown')
|
||||
parseMarkdown(@Body() dto: ParseMarkdownDto) {
|
||||
@@ -40,6 +44,14 @@ export class RecipesController {
|
||||
return this.recipesService.getInventoryPreview(id, user.userId);
|
||||
}
|
||||
|
||||
@Get(':id/analysis')
|
||||
getRecipeAnalysis(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@CurrentUser() user: { userId: number },
|
||||
) {
|
||||
return this.recipeAnalysisService.analyzeRecipeIngredients(id, user.userId);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
|
||||
@@ -3,10 +3,11 @@ import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { AiModule } from '../ai/ai.module';
|
||||
import { RecipesController } from './recipes.controller';
|
||||
import { RecipesService } from './recipes.service';
|
||||
import { RecipeAnalysisService } from './recipe-analysis.service';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, AiModule],
|
||||
controllers: [RecipesController],
|
||||
providers: [RecipesService],
|
||||
providers: [RecipesService, RecipeAnalysisService],
|
||||
})
|
||||
export class RecipesModule {}
|
||||
@@ -34,6 +34,12 @@ export class RecipesService {
|
||||
throw new NotFoundException(`Recipe with id ${id} not found`);
|
||||
}
|
||||
|
||||
private normalizeIngredientName(value: string): string {
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return trimmed;
|
||||
return `${trimmed.charAt(0).toUpperCase()}${trimmed.slice(1)}`;
|
||||
}
|
||||
|
||||
private async assertProductsActive(productIds: number[]): Promise<void> {
|
||||
if (productIds.length === 0) return;
|
||||
const activeProducts = await this.prisma.product.findMany({
|
||||
@@ -361,7 +367,7 @@ export class RecipesService {
|
||||
ingredients: {
|
||||
create: updateRecipeDto.ingredients.map((ingredient) => ({
|
||||
productId: ingredient.productId ?? null,
|
||||
rawName: ingredient.rawName,
|
||||
rawName: this.normalizeIngredientName(ingredient.rawName),
|
||||
rawLine: ingredient.rawLine ?? null,
|
||||
quantity: ingredient.quantity ?? null,
|
||||
unit: ingredient.unit?.trim() ? ingredient.unit : null,
|
||||
@@ -537,7 +543,7 @@ export class RecipesService {
|
||||
ingredients: {
|
||||
create: createRecipeDto.ingredients.map((ingredient) => ({
|
||||
productId: ingredient.productId ?? null,
|
||||
rawName: ingredient.rawName,
|
||||
rawName: this.normalizeIngredientName(ingredient.rawName),
|
||||
rawLine: ingredient.rawLine ?? null,
|
||||
quantity: ingredient.quantity ?? null,
|
||||
unit: ingredient.unit?.trim() ? ingredient.unit : null,
|
||||
|
||||
Reference in New Issue
Block a user