Fas 2 steg 1: Inventory Trust Engine - trust states pa inventory_items
This commit is contained in:
@@ -8,7 +8,7 @@ import {
|
||||
inventoryTransactionInputSchema,
|
||||
updateInventoryItemInputSchema,
|
||||
} from "@app/validation";
|
||||
import { classifyExpiry, findDuplicateCandidates, normalizeDelta } from "@app/inventory-engine";
|
||||
import { classifyExpiry, findDuplicateCandidates, normalizeDelta, computeTrust } from "@app/inventory-engine";
|
||||
import { errors, parse } from "../lib/errors.js";
|
||||
import { emitEvent, requireActiveHousehold, requireMembership } from "../lib/helpers.js";
|
||||
|
||||
@@ -186,6 +186,16 @@ export async function inventoryRoutes(app: FastifyInstance) {
|
||||
})),
|
||||
);
|
||||
|
||||
const now = new Date();
|
||||
const confidence = input.source === "manual_search" || input.source === "free_text" ? 1 : 0.9;
|
||||
const trustState = computeTrust({
|
||||
confidence,
|
||||
verifiedByUser: true,
|
||||
lastVerifiedAt: now,
|
||||
quantity: input.quantity,
|
||||
updatedAt: now,
|
||||
}).state;
|
||||
|
||||
const [item] = await app.db
|
||||
.insert(schema.inventoryItems)
|
||||
.values({
|
||||
@@ -206,9 +216,10 @@ export async function inventoryRoutes(app: FastifyInstance) {
|
||||
frozenAt: input.frozenAt ?? null,
|
||||
priceMinor: input.priceMinor ?? null,
|
||||
source: input.source,
|
||||
confidence: input.source === "manual_search" || input.source === "free_text" ? 1 : 0.9,
|
||||
confidence,
|
||||
verifiedByUser: true,
|
||||
lastVerifiedAt: new Date(),
|
||||
lastVerifiedAt: now,
|
||||
trustState,
|
||||
})
|
||||
.returning();
|
||||
|
||||
@@ -268,6 +279,15 @@ export async function inventoryRoutes(app: FastifyInstance) {
|
||||
updates.quantity = quantity;
|
||||
}
|
||||
|
||||
const trustState = computeTrust({
|
||||
confidence: updates.confidence as number | undefined ?? item.confidence,
|
||||
verifiedByUser: (updates.verifiedByUser as boolean | undefined) ?? item.verifiedByUser,
|
||||
lastVerifiedAt: (updates.lastVerifiedAt as Date | null | undefined) ?? item.lastVerifiedAt,
|
||||
quantity: (updates.quantity as number | undefined) ?? item.quantity,
|
||||
updatedAt: updates.updatedAt as Date,
|
||||
}).state;
|
||||
updates.trustState = trustState;
|
||||
|
||||
const [row] = await app.db
|
||||
.update(schema.inventoryItems)
|
||||
.set(updates)
|
||||
@@ -311,12 +331,22 @@ export async function inventoryRoutes(app: FastifyInstance) {
|
||||
valueMinor,
|
||||
});
|
||||
|
||||
const updatedAt = new Date();
|
||||
const trustState = computeTrust({
|
||||
confidence: item.confidence,
|
||||
verifiedByUser: item.verifiedByUser,
|
||||
lastVerifiedAt: item.lastVerifiedAt,
|
||||
quantity: newQuantity,
|
||||
updatedAt,
|
||||
}).state;
|
||||
|
||||
const [updated] = await app.db
|
||||
.update(schema.inventoryItems)
|
||||
.set({
|
||||
quantity: newQuantity,
|
||||
depletedAt: newQuantity <= 0 ? new Date() : null,
|
||||
updatedAt: new Date(),
|
||||
trustState,
|
||||
updatedAt,
|
||||
})
|
||||
.where(eq(schema.inventoryItems.id, id))
|
||||
.returning();
|
||||
|
||||
Reference in New Issue
Block a user