Refactor code structure for improved readability and maintainability
Test Suite / test (24.15.0) (push) Has been cancelled
Test Suite / test (24.15.0) (push) Has been cancelled
This commit is contained in:
@@ -76,8 +76,11 @@ export class MealPlanService {
|
||||
const entryServings = (entry as any).servings as number | null;
|
||||
const scale = recipeServings && entryServings ? entryServings / recipeServings : 1;
|
||||
for (const ing of entry.recipe.ingredients) {
|
||||
if (!ing.product || !ing.unit) {
|
||||
continue;
|
||||
}
|
||||
const key = `${ing.product.id}-${ing.unit}`;
|
||||
const qty = Number(ing.quantity) * scale;
|
||||
const qty = Number(ing.quantity ?? 0) * scale;
|
||||
const existing = map.get(key);
|
||||
if (existing) {
|
||||
existing.quantity += qty;
|
||||
|
||||
@@ -131,6 +131,9 @@ export class RecipesService {
|
||||
};
|
||||
}
|
||||
|
||||
const requiredUnit = (ingredient.unit ?? '').trim();
|
||||
const requiredQuantity = Number(ingredient.quantity ?? 0);
|
||||
|
||||
// Täcks ingrediensen av pantry (inkl. alternativ)?
|
||||
const coveredByPantry =
|
||||
pantryProductIds.has(ingredient.productId) ||
|
||||
@@ -144,11 +147,11 @@ export class RecipesService {
|
||||
ingredientId: ingredient.id,
|
||||
productId: ingredient.productId,
|
||||
productName: ingredient.product.canonicalName || ingredient.product.name,
|
||||
requiredQuantity: Number(ingredient.quantity),
|
||||
requiredUnit: ingredient.unit,
|
||||
requiredQuantity,
|
||||
requiredUnit,
|
||||
note: ingredient.note,
|
||||
availableQuantity: Number(ingredient.quantity),
|
||||
availableUnit: ingredient.unit,
|
||||
availableQuantity: requiredQuantity,
|
||||
availableUnit: requiredUnit,
|
||||
matchingInventoryItems: [],
|
||||
otherInventoryItems: [],
|
||||
status: 'enough' as const,
|
||||
@@ -173,7 +176,10 @@ export class RecipesService {
|
||||
|
||||
// Hitta inventory-poster med samma enhet
|
||||
const sameUnitItems = inventoryItems.filter(
|
||||
(item: any) => item.unit.trim().toLowerCase() === ingredient.unit.trim().toLowerCase(),
|
||||
(item: any) =>
|
||||
requiredUnit
|
||||
? item.unit.trim().toLowerCase() === requiredUnit.toLowerCase()
|
||||
: true,
|
||||
);
|
||||
const availableSameUnit = sameUnitItems.reduce(
|
||||
(sum: number, item: any) => sum + Number(item.quantity),
|
||||
@@ -182,7 +188,10 @@ export class RecipesService {
|
||||
|
||||
// Hitta inventory-poster med annan enhet och konvertera (endast viktbaserade enheter)
|
||||
const otherUnitItems = inventoryItems.filter(
|
||||
(item: any) => item.unit.trim().toLowerCase() !== ingredient.unit.trim().toLowerCase(),
|
||||
(item: any) =>
|
||||
requiredUnit
|
||||
? item.unit.trim().toLowerCase() !== requiredUnit.toLowerCase()
|
||||
: false,
|
||||
);
|
||||
let availableOtherUnit = 0;
|
||||
|
||||
@@ -192,7 +201,7 @@ export class RecipesService {
|
||||
const convertedQuantity = convertUnit(
|
||||
Number(item.quantity),
|
||||
item.unit,
|
||||
ingredient.unit,
|
||||
requiredUnit,
|
||||
);
|
||||
availableOtherUnit += convertedQuantity;
|
||||
} catch {
|
||||
@@ -204,7 +213,7 @@ export class RecipesService {
|
||||
const totalAvailable = availableSameUnit + availableOtherUnit;
|
||||
let status: 'enough' | 'missing' | 'unit_mismatch';
|
||||
|
||||
if (totalAvailable >= Number(ingredient.quantity)) {
|
||||
if (totalAvailable >= requiredQuantity) {
|
||||
status = 'enough';
|
||||
} else if (availableSameUnit === 0 && availableOtherUnit > 0) {
|
||||
status = 'unit_mismatch';
|
||||
@@ -216,11 +225,11 @@ export class RecipesService {
|
||||
ingredientId: ingredient.id,
|
||||
productId: ingredient.productId,
|
||||
productName: ingredient.product.canonicalName || ingredient.product.name,
|
||||
requiredQuantity: Number(ingredient.quantity),
|
||||
requiredUnit: ingredient.unit,
|
||||
requiredQuantity,
|
||||
requiredUnit,
|
||||
note: ingredient.note,
|
||||
availableQuantity: totalAvailable,
|
||||
availableUnit: ingredient.unit,
|
||||
availableUnit: requiredUnit,
|
||||
matchingInventoryItems: sameUnitItems.map((item: any) => ({
|
||||
id: item.id,
|
||||
quantity: item.quantity,
|
||||
@@ -231,11 +240,11 @@ export class RecipesService {
|
||||
})),
|
||||
otherInventoryItems: otherUnitItems.map((item: any) => {
|
||||
// Kolla om konvertering är möjlig (samma enhetskategori)
|
||||
const canConvertUnits = canConvert(item.unit, ingredient.unit);
|
||||
const canConvertUnits = requiredUnit ? canConvert(item.unit, requiredUnit) : false;
|
||||
let convertedQuantity = 0;
|
||||
if (canConvertUnits) {
|
||||
try {
|
||||
convertedQuantity = convertUnit(Number(item.quantity), item.unit, ingredient.unit);
|
||||
convertedQuantity = convertUnit(Number(item.quantity), item.unit, requiredUnit);
|
||||
} catch {
|
||||
convertedQuantity = 0;
|
||||
}
|
||||
@@ -252,7 +261,7 @@ export class RecipesService {
|
||||
}),
|
||||
status,
|
||||
fromPantry: false,
|
||||
missingQuantity: status === 'missing' ? Math.max(0, Number(ingredient.quantity) - totalAvailable) : 0,
|
||||
missingQuantity: status === 'missing' ? Math.max(0, requiredQuantity - totalAvailable) : 0,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user