Fas 2 steg 2: Inventory Trust Engine - decay-profiler och cache-jobb
This commit is contained in:
@@ -2,6 +2,7 @@ import { createHash, randomBytes, randomUUID } from "node:crypto";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import type { Database } from "@app/database";
|
||||
import { schema } from "@app/database";
|
||||
import type { DecayProfile } from "@app/inventory-engine";
|
||||
import type { EventType } from "@app/shared-types";
|
||||
import type { NewDomainEvent } from "@app/events";
|
||||
import { errors } from "./errors.js";
|
||||
@@ -145,3 +146,18 @@ export async function audit(
|
||||
export function newCorrelationId(): string {
|
||||
return randomUUID();
|
||||
}
|
||||
|
||||
/** Hämta den aktiva trust-decay-profilen. Cachas i praktiken av databasen;
|
||||
* funktionen returnerar alltid en giltig profil (default fallback). */
|
||||
export async function getActiveDecayProfile(db: Database): Promise<DecayProfile> {
|
||||
const [profile] = await db
|
||||
.select({ halfLifeDays: schema.inventoryDecayProfiles.halfLifeDays, staleAfterDays: schema.inventoryDecayProfiles.staleAfterDays })
|
||||
.from(schema.inventoryDecayProfiles)
|
||||
.where(eq(schema.inventoryDecayProfiles.active, true))
|
||||
.orderBy(schema.inventoryDecayProfiles.createdAt)
|
||||
.limit(1);
|
||||
return {
|
||||
halfLifeDays: profile?.halfLifeDays ?? 7,
|
||||
staleAfterDays: profile?.staleAfterDays ?? 30,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
} from "@app/validation";
|
||||
import { classifyExpiry, findDuplicateCandidates, normalizeDelta, computeTrust } from "@app/inventory-engine";
|
||||
import { errors, parse } from "../lib/errors.js";
|
||||
import { emitEvent, requireActiveHousehold, requireMembership } from "../lib/helpers.js";
|
||||
import { emitEvent, getActiveDecayProfile, requireActiveHousehold, requireMembership } from "../lib/helpers.js";
|
||||
|
||||
/**
|
||||
* Food Twin – lagret (spec §8). Transaktionsbaserat: varje förändring skrivs
|
||||
@@ -22,6 +22,7 @@ export async function inventoryRoutes(app: FastifyInstance) {
|
||||
app.get("/v1/inventory", auth, async (req) => {
|
||||
const query = parse(inventoryQuerySchema, req.query);
|
||||
const householdId = await requireActiveHousehold(app.db, req.userId);
|
||||
const decayProfile = await getActiveDecayProfile(app.db);
|
||||
|
||||
const conditions = [
|
||||
eq(schema.inventoryItems.householdId, householdId),
|
||||
@@ -73,11 +74,24 @@ export async function inventoryRoutes(app: FastifyInstance) {
|
||||
storageLocationType: r.locationType,
|
||||
shelfLifeGuidance: r.shelfLife,
|
||||
});
|
||||
const trust = computeTrust(
|
||||
{
|
||||
confidence: r.item.confidence,
|
||||
verifiedByUser: r.item.verifiedByUser,
|
||||
lastVerifiedAt: r.item.lastVerifiedAt,
|
||||
quantity: r.item.quantity,
|
||||
updatedAt: r.item.updatedAt,
|
||||
},
|
||||
new Date(),
|
||||
decayProfile,
|
||||
);
|
||||
return {
|
||||
...r.item,
|
||||
locationName: r.locationName,
|
||||
locationType: r.locationType,
|
||||
expiry,
|
||||
trustState: trust.state,
|
||||
trustScore: trust.score,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -90,6 +104,7 @@ export async function inventoryRoutes(app: FastifyInstance) {
|
||||
/** Varor som bör användas snart – driver "använd först" (spec §4.4, §40). */
|
||||
app.get("/v1/inventory/expiring", auth, async (req) => {
|
||||
const householdId = await requireActiveHousehold(app.db, req.userId);
|
||||
const decayProfile = await getActiveDecayProfile(app.db);
|
||||
const rows = await app.db
|
||||
.select({
|
||||
item: schema.inventoryItems,
|
||||
@@ -114,9 +129,8 @@ export async function inventoryRoutes(app: FastifyInstance) {
|
||||
);
|
||||
|
||||
const withExpiry = rows
|
||||
.map((r) => ({
|
||||
...r.item,
|
||||
expiry: classifyExpiry({
|
||||
.map((r) => {
|
||||
const expiry = classifyExpiry({
|
||||
bestBeforeDate: r.item.bestBeforeDate,
|
||||
useByDate: r.item.useByDate,
|
||||
openedAt: r.item.openedAt,
|
||||
@@ -125,8 +139,25 @@ export async function inventoryRoutes(app: FastifyInstance) {
|
||||
purchasedAt: r.item.purchasedAt,
|
||||
storageLocationType: r.locationType,
|
||||
shelfLifeGuidance: r.shelfLife,
|
||||
}),
|
||||
}))
|
||||
});
|
||||
const trust = computeTrust(
|
||||
{
|
||||
confidence: r.item.confidence,
|
||||
verifiedByUser: r.item.verifiedByUser,
|
||||
lastVerifiedAt: r.item.lastVerifiedAt,
|
||||
quantity: r.item.quantity,
|
||||
updatedAt: r.item.updatedAt,
|
||||
},
|
||||
new Date(),
|
||||
decayProfile,
|
||||
);
|
||||
return {
|
||||
...r.item,
|
||||
expiry,
|
||||
trustState: trust.state,
|
||||
trustScore: trust.score,
|
||||
};
|
||||
})
|
||||
.filter(
|
||||
(i) =>
|
||||
i.expiry.status === "expiring" ||
|
||||
|
||||
Reference in New Issue
Block a user