Fas 3 steg 3a: cooking_sessions-tabell + lifecycle-endpoints + timeout-jobb, gamla /cook kvar som kortkommando

This commit is contained in:
Sven (AAMOS AI)
2026-08-07 03:14:13 +07:00
parent 4a4f448e1c
commit e9fb4a17d4
17 changed files with 10619 additions and 196 deletions
+255
View File
@@ -0,0 +1,255 @@
import type { FastifyInstance } from "fastify";
import { and, eq, gt, isNull, sql } from "drizzle-orm";
import { schema, markMilestone } from "@app/database";
import { allocateFefo } from "@app/inventory-engine";
import { scaleNutrition } from "@app/nutrition-engine";
import type { Unit } from "@app/shared-types";
import { todayIso, emitEvent } from "./helpers.js";
import { errors } from "./errors.js";
import { loadFullRecipe } from "../routes/recipes.js";
import { MEAL_TYPES } from "@app/shared-types";
export interface CompleteCookingInput {
portionsCooked?: number;
mealBoxPortions?: number;
mealBoxFrozen?: boolean;
mealBoxStorageLocationId?: string;
deductInventory?: boolean;
eaters?: Array<{ userId: string; portionFraction: number }>;
date?: string;
mealType?: string;
inventoryOverrides?: Array<{ canonicalIngredientId: string; quantityUsed: number; unit: string }>;
}
export interface CompleteCookingResult {
ok: boolean;
mealIds: string[];
mealBoxId: string | null;
inventoryDeductions: Array<{ itemId: string; quantity: number; unit: string; name: string }>;
}
/**
* Gemensam kärna för att "jag har lagat". Används av både
* POST /v1/recipes/:id/cook (legacy shortcut) och
* POST /v1/cooking-sessions/:id/complete.
*/
export async function completeCookingSessionCore(
app: FastifyInstance,
session: typeof schema.cookingSessions.$inferSelect,
userId: string,
input: CompleteCookingInput,
correlationId: string,
): Promise<CompleteCookingResult> {
const recipe = await loadFullRecipe(app, session.recipeId);
const householdId = session.householdId;
const portionsCooked = input.portionsCooked ?? session.plannedPortions;
const date = input.date ?? todayIso();
const mealType = (input.mealType ?? session.plannedMealType) as (typeof MEAL_TYPES)[number];
// 1. FEFO-avdrag
const deductions: Array<{ itemId: string; quantity: number; unit: string; name: string }> = [];
if (input.deductInventory !== false) {
const factor = portionsCooked / recipe.portions;
const overrides = new Map(input.inventoryOverrides?.map((o) => [o.canonicalIngredientId, o]) ?? []);
for (const ing of recipe.ingredients) {
if (ing.optional) continue;
const override = overrides.get(ing.canonicalIngredientId);
const requiredQty = override ? override.quantityUsed : ing.quantity * factor;
const requiredUnit = override ? override.unit : ing.unit;
if (requiredQty <= 0) continue;
const stock = await app.db
.select()
.from(schema.inventoryItems)
.where(
and(
eq(schema.inventoryItems.householdId, householdId),
eq(schema.inventoryItems.canonicalIngredientId, ing.canonicalIngredientId),
isNull(schema.inventoryItems.depletedAt),
gt(schema.inventoryItems.quantity, 0),
),
);
if (stock.length === 0) continue;
const [info] = await app.db
.select()
.from(schema.canonicalIngredients)
.where(eq(schema.canonicalIngredients.id, ing.canonicalIngredientId))
.limit(1);
const allocation = allocateFefo(
requiredQty,
requiredUnit as Unit,
stock.map((s) => ({
id: s.id,
canonicalIngredientId: s.canonicalIngredientId,
quantity: s.quantity,
unit: s.unit,
bestBeforeDate: s.bestBeforeDate,
useByDate: s.useByDate,
openedAt: s.openedAt,
frozenAt: s.frozenAt,
thawedAt: s.thawedAt,
purchasedAt: s.purchasedAt,
})),
{ densityGPerMl: info?.densityGPerMl, gramsPerPiece: info?.gramsPerPiece },
);
for (const alloc of allocation.allocations) {
const item = stock.find((s) => s.id === alloc.itemId)!;
const newQty = Math.max(0, Math.round((item.quantity - alloc.quantity) * 1000) / 1000);
await app.db.insert(schema.inventoryTransactions).values({
householdId,
inventoryItemId: alloc.itemId,
cookingSessionId: session.id,
type: "cook_use",
quantityDelta: -(item.quantity - newQty),
unit: item.unit,
refType: "recipe_cook",
refId: session.recipeId,
actorUserId: userId,
});
await app.db
.update(schema.inventoryItems)
.set({
quantity: newQty,
depletedAt: newQty <= 0 ? new Date() : null,
updatedAt: new Date(),
})
.where(eq(schema.inventoryItems.id, alloc.itemId));
deductions.push({
itemId: alloc.itemId,
quantity: alloc.quantity,
unit: alloc.unit,
name: item.displayName,
});
}
}
}
// 2. Måltider
const eaters =
input.eaters && input.eaters.length > 0
? input.eaters
: [{ userId, portionFraction: 1 }];
const mealIds: string[] = [];
for (const eater of eaters) {
const nutrition = scaleNutrition(recipe.nutritionPerPortion, eater.portionFraction);
const [meal] = await app.db
.insert(schema.meals)
.values({
userId: eater.userId,
householdId,
cookingSessionId: session.id,
date,
mealType,
source: "cooked_recipe",
recipeId: session.recipeId,
titleSv: recipe.titleSv,
portionFraction: eater.portionFraction,
nutrition,
nutritionIsEstimate: false,
})
.returning();
mealIds.push(meal!.id);
await emitEvent(app.db, {
type: "MEAL_LOGGED",
payload: { mealId: meal!.id, mealType, kcal: nutrition.kcal, source: "cooked_recipe" },
userId: eater.userId,
householdId,
correlationId,
});
}
// 3. Matlådor
let mealBoxId: string | null = null;
const mealBoxPortions = input.mealBoxPortions ?? 0;
if (mealBoxPortions > 0) {
const locationId =
input.mealBoxStorageLocationId ??
(
await app.db
.select({ id: schema.storageLocations.id })
.from(schema.storageLocations)
.where(
and(
eq(schema.storageLocations.householdId, householdId),
eq(schema.storageLocations.type, input.mealBoxFrozen ? "freezer" : "fridge"),
),
)
.limit(1)
)[0]?.id;
if (!locationId) throw errors.badRequest("Ingen förvaringsplats för matlådor hittades.");
const useByDays = input.mealBoxFrozen ? 90 : 3;
const recommendedUseBy = new Date(Date.parse(date) + useByDays * 86_400_000)
.toISOString()
.slice(0, 10);
const [box] = await app.db
.insert(schema.mealBoxes)
.values({
householdId,
recipeId: session.recipeId,
cookingSessionId: session.id,
titleSv: recipe.titleSv,
portions: mealBoxPortions,
portionsRemaining: mealBoxPortions,
nutritionPerPortion: recipe.nutritionPerPortion,
cookedAt: date,
storageLocationId: locationId,
frozen: input.mealBoxFrozen ?? false,
recommendedUseBy,
})
.returning();
mealBoxId = box!.id;
await emitEvent(app.db, {
type: "MEAL_BOX_CREATED",
payload: { mealBoxId: box!.id, portions: mealBoxPortions },
userId,
householdId,
correlationId,
});
}
// 4. recipe_cooks + statistik
await app.db.insert(schema.recipeCooks).values({
recipeId: session.recipeId,
userId,
householdId,
cookingSessionId: session.id,
portionsCooked,
cookedAt: new Date(),
});
await app.db
.update(schema.recipes)
.set({ cookCount: sql`${schema.recipes.cookCount} + 1` })
.where(eq(schema.recipes.id, session.recipeId));
await emitEvent(app.db, {
type: "RECIPE_COOKED",
payload: { recipeId: session.recipeId, portions: portionsCooked, mealBoxPortions },
userId,
householdId,
correlationId,
});
// 5. Uppdatera session
await app.db
.update(schema.cookingSessions)
.set({
status: "completed",
completedAt: new Date(),
actualPortionsEaten: portionsCooked - mealBoxPortions,
leftoverEstimatePortions: mealBoxPortions,
plannedDeductions: deductions,
updatedAt: new Date(),
})
.where(eq(schema.cookingSessions.id, session.id));
await markMilestone(app.db, householdId, "firstCookingSessionCompletedAt");
if (deductions.length > 0) {
await markMilestone(app.db, householdId, "inventoryUpdatedAfterCookingAt");
}
return { ok: true, mealIds, mealBoxId, inventoryDeductions: deductions };
}
+198
View File
@@ -0,0 +1,198 @@
import type { FastifyInstance } from "fastify";
import { and, eq, lt } from "drizzle-orm";
import { schema, markMilestone } from "@app/database";
import {
cookingSessionStartInputSchema,
cookingSessionCompleteInputSchema,
idParamSchema,
} from "@app/validation";
import { errors, parse } from "../lib/errors.js";
import { requireActiveHousehold, requireMembership } from "../lib/helpers.js";
import {
cookingSessionStarted,
cookingSessionCompleted,
cookingSessionCancelled,
} from "@app/analytics";
import { trackProductAnalytics } from "../lib/helpers.js";
import { completeCookingSessionCore } from "../lib/cooking.js";
import { z } from "zod";
/**
* Cooking Sessions (Fas 3 §6).
*
* - PLANNED reserverar aldrig lager.
* - STARTED har 24 h på sig att complete/cancel (§20 timeout).
* - COMPLETED/CANCELLED är terminala.
* - Gamla POST /v1/recipes/:id/cook finns kvar som kortkommando.
*/
export async function cookingSessionRoutes(app: FastifyInstance) {
const auth = { preHandler: [app.authenticate] };
/** Skapa ny session. Status planned om startNow saknas, annars started. */
app.post("/v1/recipes/:id/cook/start", auth, async (req, reply) => {
const { id } = parse(idParamSchema, req.params);
const input = parse(cookingSessionStartInputSchema, req.body);
const householdId = await requireActiveHousehold(app.db, req.userId);
await requireMembership(app.db, householdId, req.userId);
const [recipe] = await app.db
.select({ id: schema.recipes.id, portions: schema.recipes.portions })
.from(schema.recipes)
.where(eq(schema.recipes.id, id))
.limit(1);
if (!recipe) throw errors.notFound("Receptet finns inte.");
const now = new Date();
const status = input.startNow ? "started" : "planned";
const [session] = await app.db
.insert(schema.cookingSessions)
.values({
recipeId: id,
householdId,
startedByUserId: req.userId,
status,
plannedPortions: input.portions ?? recipe.portions,
plannedMealType: input.mealType ?? "dinner",
startedAt: input.startNow ? now : null,
})
.returning();
await trackProductAnalytics(
app.db,
req.userId,
cookingSessionStarted({
householdId,
properties: {
cookingSessionId: session!.id,
recipeId: id,
status,
plannedPortions: input.portions ?? recipe.portions,
},
}),
);
return reply.status(201).send({ session: session });
});
/** Starta en planned session. */
app.post("/v1/cooking-sessions/:id/start", auth, async (req) => {
const { id } = parse(idParamSchema, req.params);
const session = await getOwnedSession(app, id, req.userId);
if (session.status !== "planned") throw errors.conflict("Sessionen är inte planerad.");
const [updated] = await app.db
.update(schema.cookingSessions)
.set({ status: "started", startedAt: new Date(), updatedAt: new Date() })
.where(eq(schema.cookingSessions.id, id))
.returning();
await trackProductAnalytics(
app.db,
req.userId,
cookingSessionStarted({
householdId: session.householdId,
properties: { cookingSessionId: id, recipeId: session.recipeId, status: "started" },
}),
);
return { session: updated };
});
/** Avbryt session utan att röra lagret. */
app.post("/v1/cooking-sessions/:id/cancel", auth, async (req) => {
const params = z.object({ id: z.uuid() }).parse(req.params);
const body = z.object({ reason: z.string().max(200).optional() }).parse(req.body ?? {});
const session = await getOwnedSession(app, params.id, req.userId);
if (session.status === "completed" || session.status === "cancelled") {
throw errors.conflict("Sessionen är redan avslutad.");
}
const [updated] = await app.db
.update(schema.cookingSessions)
.set({
status: "cancelled",
cancelledAt: new Date(),
cancelReason: body.reason ?? null,
updatedAt: new Date(),
})
.where(eq(schema.cookingSessions.id, params.id))
.returning();
await trackProductAnalytics(
app.db,
req.userId,
cookingSessionCancelled({
householdId: session.householdId,
properties: { cookingSessionId: params.id, recipeId: session.recipeId },
}),
);
return { session: updated };
});
/**
* Complete en session.
* I steg 3a: anropar samma logik som gamla /cook, men länkar allt till cookingSessionId.
*/
app.post("/v1/cooking-sessions/:id/complete", auth, async (req) => {
const { id } = parse(idParamSchema, req.params);
const input = parse(cookingSessionCompleteInputSchema, req.body);
const session = await getOwnedSession(app, id, req.userId);
if (session.status !== "started") {
throw errors.conflict("Sessionen måste vara startad för att avslutas.");
}
const result = await completeCookingSessionCore(app, session, req.userId, input, req.correlationId);
const [updated] = await app.db
.select()
.from(schema.cookingSessions)
.where(eq(schema.cookingSessions.id, id))
.limit(1);
await trackProductAnalytics(
app.db,
req.userId,
cookingSessionCompleted({
householdId: session.householdId,
properties: {
cookingSessionId: id,
recipeId: session.recipeId,
portionsCooked: session.plannedPortions,
mealBoxPortions: input.mealBoxPortions ?? 0,
},
}),
);
return { ...result, session: updated };
});
/** Lista hushållets aktiva sessioner. */
app.get("/v1/cooking-sessions", auth, async (req) => {
const householdId = await requireActiveHousehold(app.db, req.userId);
await requireMembership(app.db, householdId, req.userId);
const sessions = await app.db
.select()
.from(schema.cookingSessions)
.where(
and(
eq(schema.cookingSessions.householdId, householdId),
eq(schema.cookingSessions.status, "started"),
),
)
.orderBy(schema.cookingSessions.startedAt);
return { sessions };
});
}
async function getOwnedSession(app: FastifyInstance, sessionId: string, userId: string) {
const [session] = await app.db
.select()
.from(schema.cookingSessions)
.where(eq(schema.cookingSessions.id, sessionId))
.limit(1);
if (!session) throw errors.notFound("Sessionen finns inte.");
await requireMembership(app.db, session.householdId, userId);
return session;
}
+17 -194
View File
@@ -32,6 +32,7 @@ import {
todayIso,
} from "../lib/helpers.js";
import { requireFeature } from "../lib/entitlements.js";
import { completeCookingSessionCore } from "../lib/cooking.js";
/** Recept: sök, detalj, betyg, favoriter, "jag har lagat", substitutioner, användarrecept. */
export async function recipeRoutes(app: FastifyInstance) {
@@ -415,204 +416,26 @@ export async function recipeRoutes(app: FastifyInstance) {
app.post("/v1/recipes/:id/cook", auth, async (req) => {
const { id } = parse(idParamSchema, req.params);
const input = parse(cookRecipeInputSchema, req.body);
const recipe = await loadFullRecipe(app, id);
await loadFullRecipe(app, id);
const householdId = await requireActiveHousehold(app.db, req.userId);
const date = input.date ?? todayIso();
// 1. Dra lager enligt FEFO (spec §17: prioritera utgångsdatum)
const deductions: Array<{ itemId: string; quantity: number; unit: string; name: string }> = [];
if (input.deductInventory) {
const factor = input.portionsCooked / recipe.portions;
const overrides = new Map(input.inventoryOverrides.map((o) => [o.canonicalIngredientId, o]));
for (const ing of recipe.ingredients) {
if (ing.optional) continue;
const override = overrides.get(ing.canonicalIngredientId);
const requiredQty = override ? override.quantityUsed : ing.quantity * factor;
const requiredUnit = override ? override.unit : ing.unit;
if (requiredQty <= 0) continue;
const stock = await app.db
.select()
.from(schema.inventoryItems)
.where(
and(
eq(schema.inventoryItems.householdId, householdId),
eq(schema.inventoryItems.canonicalIngredientId, ing.canonicalIngredientId),
isNull(schema.inventoryItems.depletedAt),
gt(schema.inventoryItems.quantity, 0),
),
);
if (stock.length === 0) continue;
const [info] = await app.db
.select()
.from(schema.canonicalIngredients)
.where(eq(schema.canonicalIngredients.id, ing.canonicalIngredientId))
.limit(1);
const allocation = allocateFefo(
requiredQty,
requiredUnit,
stock.map((s) => ({
id: s.id,
canonicalIngredientId: s.canonicalIngredientId,
quantity: s.quantity,
unit: s.unit,
bestBeforeDate: s.bestBeforeDate,
useByDate: s.useByDate,
openedAt: s.openedAt,
frozenAt: s.frozenAt,
thawedAt: s.thawedAt,
purchasedAt: s.purchasedAt,
})),
{ densityGPerMl: info?.densityGPerMl, gramsPerPiece: info?.gramsPerPiece },
);
for (const alloc of allocation.allocations) {
const item = stock.find((s) => s.id === alloc.itemId)!;
const newQty = Math.max(0, Math.round((item.quantity - alloc.quantity) * 1000) / 1000);
await app.db.insert(schema.inventoryTransactions).values({
householdId,
inventoryItemId: alloc.itemId,
type: "cook_use",
quantityDelta: -(item.quantity - newQty),
unit: item.unit,
refType: "recipe_cook",
refId: id,
actorUserId: req.userId,
});
await app.db
.update(schema.inventoryItems)
.set({
quantity: newQty,
depletedAt: newQty <= 0 ? new Date() : null,
updatedAt: new Date(),
})
.where(eq(schema.inventoryItems.id, alloc.itemId));
deductions.push({
itemId: alloc.itemId,
quantity: alloc.quantity,
unit: alloc.unit,
name: item.displayName,
});
}
}
}
// 2. Logga måltid per ätare med portionsandel (spec §7: individuellt)
const eaters =
input.eaters.length > 0 ? input.eaters : [{ userId: req.userId, portionFraction: 1 }];
const mealIds: string[] = [];
for (const eater of eaters) {
const nutrition = scaleNutrition(recipe.nutritionPerPortion, eater.portionFraction);
const [meal] = await app.db
.insert(schema.meals)
.values({
userId: eater.userId,
householdId,
date,
mealType: input.mealType,
source: "cooked_recipe",
recipeId: id,
titleSv: recipe.titleSv,
portionFraction: eater.portionFraction,
nutrition,
nutritionIsEstimate: false,
})
.returning();
mealIds.push(meal!.id);
await emitEvent(app.db, {
type: "MEAL_LOGGED",
payload: {
mealId: meal!.id,
mealType: input.mealType,
kcal: nutrition.kcal,
source: "cooked_recipe",
},
userId: eater.userId,
householdId,
correlationId: req.correlationId,
});
}
// 3. Matlådor (spec §24)
let mealBoxId: string | null = null;
if (input.mealBoxPortions > 0) {
const locationId =
input.mealBoxStorageLocationId ??
(
await app.db
.select({ id: schema.storageLocations.id })
.from(schema.storageLocations)
.where(
and(
eq(schema.storageLocations.householdId, householdId),
eq(schema.storageLocations.type, input.mealBoxFrozen ? "freezer" : "fridge"),
),
)
.limit(1)
)[0]?.id;
if (!locationId) throw errors.badRequest("Ingen förvaringsplats för matlådor hittades.");
const useByDays = input.mealBoxFrozen ? 90 : 3;
const recommendedUseBy = new Date(Date.now() + useByDays * 86_400_000)
.toISOString()
.slice(0, 10);
const [box] = await app.db
.insert(schema.mealBoxes)
.values({
householdId,
recipeId: id,
titleSv: recipe.titleSv,
portions: input.mealBoxPortions,
portionsRemaining: input.mealBoxPortions,
nutritionPerPortion: recipe.nutritionPerPortion,
cookedAt: date,
storageLocationId: locationId,
frozen: input.mealBoxFrozen,
recommendedUseBy,
})
.returning();
mealBoxId = box!.id;
await emitEvent(app.db, {
type: "MEAL_BOX_CREATED",
payload: { mealBoxId: box!.id, portions: input.mealBoxPortions },
userId: req.userId,
householdId,
correlationId: req.correlationId,
});
}
// 4. Statistik + event
await app.db.insert(schema.recipeCooks).values({
recipeId: id,
userId: req.userId,
householdId,
portionsCooked: input.portionsCooked,
});
await app.db
.update(schema.recipes)
.set({ cookCount: sql`${schema.recipes.cookCount} + 1` })
.where(eq(schema.recipes.id, id));
await emitEvent(app.db, {
type: "RECIPE_COOKED",
payload: {
// Bakåtkompatibel shortcut: skapa session + complete direkt.
const [session] = await app.db
.insert(schema.cookingSessions)
.values({
recipeId: id,
portions: input.portionsCooked,
mealBoxPortions: input.mealBoxPortions,
},
userId: req.userId,
householdId,
correlationId: req.correlationId,
});
householdId,
startedByUserId: req.userId,
status: "started",
plannedPortions: input.portionsCooked,
plannedMealType: input.mealType,
startedAt: new Date(),
})
.returning();
await markMilestone(app.db, householdId, "firstCookingSessionCompletedAt");
if (input.deductInventory && deductions.length > 0) {
await markMilestone(app.db, householdId, "inventoryUpdatedAfterCookingAt");
}
const result = await completeCookingSessionCore(app, session!, req.userId, input, req.correlationId);
return { ok: true, mealIds, mealBoxId, inventoryDeductions: deductions };
return { sessionId: session!.id, ...result };
});
/** Substitutionsförslag för en ingrediens (spec §20). */
@@ -997,7 +820,7 @@ export async function recipeRoutes(app: FastifyInstance) {
});
}
async function loadFullRecipe(app: FastifyInstance, id: string) {
export async function loadFullRecipe(app: FastifyInstance, id: string) {
const [recipe] = await app.db
.select()
.from(schema.recipes)
+2
View File
@@ -13,6 +13,7 @@ import { inventoryRoutes } from "./routes/inventory.js";
import { scanRoutes } from "./routes/scans.js";
import { scanDiffRoutes } from "./routes/scan-diff.js";
import { recipeRoutes } from "./routes/recipes.js";
import { cookingSessionRoutes } from "./routes/cooking-sessions.js";
import { mealRoutes } from "./routes/meals.js";
import { shoppingRoutes } from "./routes/shopping.js";
import { planningRoutes } from "./routes/planning.js";
@@ -80,6 +81,7 @@ export async function buildServer(config: AppConfig) {
await app.register(scanRoutes);
await app.register(scanDiffRoutes);
await app.register(recipeRoutes);
await app.register(cookingSessionRoutes);
await app.register(mealRoutes);
await app.register(shoppingRoutes);
await app.register(planningRoutes);