Fas 2 steg 2: Inventory Trust Engine - decay-profiler och cache-jobb

This commit is contained in:
Sven (AAMOS AI)
2026-08-06 23:25:32 +07:00
parent 8ae4bf6ec3
commit bcf932d2ff
17 changed files with 18296 additions and 61 deletions
+16
View File
@@ -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,
};
}