Fas 2 steg 3: Household Trust Score (admin + i18n status i appen)
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { and, desc, eq, ilike, sql } from "drizzle-orm";
|
||||
import { and, desc, eq, ilike, isNull, sql } from "drizzle-orm";
|
||||
import { schema } from "@app/database";
|
||||
import { z } from "zod";
|
||||
import { adminGrantInputSchema, totpCodeInputSchema } from "@app/validation";
|
||||
import { errors, parse } from "../lib/errors.js";
|
||||
import { audit } from "../lib/helpers.js";
|
||||
import { generateTotpSecret, otpauthUrl, verifyTotp } from "../lib/totp.js";
|
||||
import { householdTrustScore } from "@app/inventory-engine";
|
||||
import { BRAND } from "@app/shared-types";
|
||||
|
||||
/** Adminpanelens API (spec §57). Alla anrop kräver admin-roll och auditloggas. */
|
||||
@@ -167,6 +168,58 @@ export async function adminRoutes(app: FastifyInstance) {
|
||||
return sub;
|
||||
});
|
||||
|
||||
// --- Household trust score (Fas 2 §5.3) ---
|
||||
app.get("/admin/v1/households/:id/trust", admin, async (req) => {
|
||||
const params = z.object({ id: z.uuid() }).parse(req.params);
|
||||
|
||||
const items = await app.db
|
||||
.select()
|
||||
.from(schema.inventoryItems)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.inventoryItems.householdId, params.id),
|
||||
isNull(schema.inventoryItems.depletedAt),
|
||||
),
|
||||
);
|
||||
|
||||
const thirtyDaysAgo = new Date(Date.now() - 30 * 86_400_000);
|
||||
const txStats = await app.db
|
||||
.select({
|
||||
total: sql<number>`count(*)`,
|
||||
adjustments: sql<number>`count(*) FILTER (WHERE ${schema.inventoryTransactions.type} = 'adjust')`,
|
||||
})
|
||||
.from(schema.inventoryTransactions)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.inventoryTransactions.householdId, params.id),
|
||||
sql`${schema.inventoryTransactions.createdAt} >= ${thirtyDaysAgo}`,
|
||||
),
|
||||
);
|
||||
|
||||
const result = householdTrustScore(
|
||||
{
|
||||
items: items.map((i) => ({
|
||||
confidence: i.confidence,
|
||||
verifiedByUser: i.verifiedByUser,
|
||||
lastVerifiedAt: i.lastVerifiedAt,
|
||||
quantity: i.quantity,
|
||||
updatedAt: i.updatedAt,
|
||||
depletedAt: i.depletedAt,
|
||||
})),
|
||||
correctionCount30d: Number(txStats[0]?.adjustments ?? 0),
|
||||
transactionCount30d: Number(txStats[0]?.total ?? 0),
|
||||
},
|
||||
new Date(),
|
||||
);
|
||||
|
||||
return {
|
||||
householdId: params.id,
|
||||
score: result.score,
|
||||
status: result.status,
|
||||
itemCount: items.length,
|
||||
};
|
||||
});
|
||||
|
||||
// --- Jobb & systemhälsa (spec §57–58) ---
|
||||
app.get("/admin/v1/jobs/overview", admin, async () => {
|
||||
const scanStats = await app.db
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { and, desc, eq, gt, ilike, isNull, or } from "drizzle-orm";
|
||||
import { and, desc, eq, gt, ilike, isNull, or, sql } from "drizzle-orm";
|
||||
import { schema } from "@app/database";
|
||||
import {
|
||||
createInventoryItemInputSchema,
|
||||
@@ -8,9 +8,20 @@ import {
|
||||
inventoryTransactionInputSchema,
|
||||
updateInventoryItemInputSchema,
|
||||
} from "@app/validation";
|
||||
import { classifyExpiry, findDuplicateCandidates, normalizeDelta, computeTrust } from "@app/inventory-engine";
|
||||
import {
|
||||
classifyExpiry,
|
||||
findDuplicateCandidates,
|
||||
normalizeDelta,
|
||||
computeTrust,
|
||||
householdTrustScore,
|
||||
} from "@app/inventory-engine";
|
||||
import { errors, parse } from "../lib/errors.js";
|
||||
import { emitEvent, getActiveDecayProfile, 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
|
||||
@@ -98,7 +109,34 @@ export async function inventoryRoutes(app: FastifyInstance) {
|
||||
const filtered = query.expiryStatus
|
||||
? items.filter((i) => i.expiry.status === query.expiryStatus)
|
||||
: items;
|
||||
return { items: filtered };
|
||||
|
||||
const thirtyDaysAgo = new Date(Date.now() - 30 * 86_400_000);
|
||||
const txStats = await app.db
|
||||
.select({
|
||||
total: sql<number>`count(*)`,
|
||||
adjustments: sql<number>`count(*) FILTER (WHERE ${schema.inventoryTransactions.type} = 'adjust')`,
|
||||
})
|
||||
.from(schema.inventoryTransactions)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.inventoryTransactions.householdId, householdId),
|
||||
sql`${schema.inventoryTransactions.createdAt} >= ${thirtyDaysAgo}`,
|
||||
),
|
||||
);
|
||||
|
||||
const householdTrust = householdTrustScore(
|
||||
{
|
||||
items: rows.map((r) => r.item),
|
||||
correctionCount30d: Number(txStats[0]?.adjustments ?? 0),
|
||||
transactionCount30d: Number(txStats[0]?.total ?? 0),
|
||||
},
|
||||
new Date(),
|
||||
);
|
||||
|
||||
return {
|
||||
items: filtered,
|
||||
trustStatus: householdTrust.status,
|
||||
};
|
||||
});
|
||||
|
||||
/** Varor som bör användas snart – driver "använd först" (spec §4.4, §40). */
|
||||
|
||||
Reference in New Issue
Block a user