feat: enhance pantry management with new features and UI improvements

This commit is contained in:
Nils-Johan Gynther
2026-04-21 16:09:33 +02:00
parent 69f05e6b43
commit 2acf66e4c4
7 changed files with 221 additions and 43 deletions
+21 -2
View File
@@ -86,6 +86,10 @@ export class MealPlanService {
async inventoryCompare(from: string, to: string) {
const entries = await this.findByRange(from, to);
// Hämta pantry-produkter — dessa anses alltid tillgängliga
const pantryItems = await this.prisma.pantryItem.findMany({ select: { productId: true } });
const pantryProductIds = new Set(pantryItems.map((p) => p.productId));
// Aggregera ingredienser per produkt+enhet (skalat per portionsantal)
const map = new Map<string, { productId: number; name: string; required: number; unit: string }>();
for (const entry of entries) {
@@ -112,6 +116,19 @@ export class MealPlanService {
// Kontrollera inventariet för varje ingrediens
const result = await Promise.all(
Array.from(map.values()).map(async (item) => {
// Pantry-varor anses alltid tillgängliga — visa inte i inköpslistan
if (pantryProductIds.has(item.productId)) {
return {
productId: item.productId,
name: item.name,
required: item.required,
unit: item.unit,
available: item.required,
missing: 0,
status: 'pantry' as const,
};
}
const inventoryItems = await this.prisma.inventoryItem.findMany({
where: { productId: item.productId },
});
@@ -125,13 +142,15 @@ export class MealPlanService {
unit: item.unit,
available,
missing: Math.max(0, item.required - available),
status: (available >= item.required ? 'enough' : 'missing') as 'enough' | 'missing',
status: (available >= item.required ? 'enough' : 'missing') as 'enough' | 'missing' | 'pantry',
};
}),
);
const statusOrder = { missing: 0, enough: 1, pantry: 2 };
return result.sort((a, b) => {
if (a.status !== b.status) return a.status === 'missing' ? -1 : 1;
const diff = statusOrder[a.status] - statusOrder[b.status];
if (diff !== 0) return diff;
return a.name.localeCompare(b.name, 'sv');
});
}