loopable / web /src /customer-grid /catalogPlacements.ts
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
85f794d verified
Raw History Blame Contribute Delete
14.2 kB
/**
* The host-owned Catalog reader: one typed page read and the saved-placement read.
*
* The Table catalog mode and the Catalog App both read through this module, so removing the
* Catalog App leaves the Table mode whole. Nothing here keeps module state: a negotiated page size
* lives in the caller's own page state.
*/
import { useEffect, useState } from "react";
import { TypedRefusal, typedQuery, typedSchema } from "../typed/client";
import type { DatabaseSchema, QueryRequest, QueryResponse } from "../typed/client";
import { FILTER_OPS, isFilterGroup } from "./types";
import type { Field, FilterNode, FilterOp, Row, ViewConfig } from "./types";
export type CatalogReadScope = Pick<ViewConfig, "filters" | "filterConj" | "sorts">;
export type CatalogProperties = Record<string, { type?: string | readonly string[] }>;
export interface CatalogData {
fields: Field[];
rows: Row[];
allRows: Row[];
returned: number;
nextCursor: string | null;
limit: number;
}
export interface CatalogPageRequest {
cursor?: string | null;
limit?: number;
search?: string;
searchFields?: string[];
priorCursors?: (string | null)[];
}
export const CATALOG_PAGE_SIZE = 50;
/** The typed value kinds each schema field may carry. */
export function catalogProperties(schema: DatabaseSchema, database: string): CatalogProperties {
if (schema?.database !== database || !Array.isArray(schema.fields) || schema.fields.some((field) =>
!field || typeof field.key !== "string" || !field.key || typeof field.type !== "string")) {
throw new Error("The Catalog database is unavailable.");
}
return Object.fromEntries(schema.fields.map((field) => {
const numeric = ["number", "int", "rating", "currency", "pct", "metric", "formula", "rollup"].includes(field.type);
const boolean = field.type === "checkbox" && ["mirror_user_table", "runtime_cache"].includes(String(schema.source_kind));
return [field.key, { type: field.type === "multiselect" ? ["string", "array", "null"]
: numeric ? ["number", "string", "null"] : [boolean ? "boolean" : "string", "null"] }];
}));
}
async function databaseProperties(database: string, signal: AbortSignal): Promise<CatalogProperties> {
signal.throwIfAborted();
const schema = await typedSchema(database, signal);
signal.throwIfAborted();
return catalogProperties(schema, database);
}
/** Project one typed record onto the schema's fields, refusing a value of the wrong kind. */
export function projectCatalogRow(record: QueryResponse["records"][number], properties: CatalogProperties): Row {
const pid = typeof record.pid === "number" ? record.pid : Number(record.pid);
if (record.pid == null || record.pid === "" || !Number.isSafeInteger(pid)) {
throw new Error("A Catalog record has an invalid identifier.");
}
const row: Row = { pid };
for (const [key, property] of Object.entries(properties)) {
if (key === "pid" || !(key in record)) continue;
const value = record[key];
const types = typeof property.type === "string" ? [property.type] : property.type ?? [];
const kind = value === null ? "null" : Array.isArray(value) ? "array" : typeof value;
if (!types.includes(kind) && !["string", "number", "null"].includes(kind) &&
!(kind === "number" && types.includes("integer") && Number.isInteger(value))) {
throw new Error("A Catalog value does not match the typed document.");
}
if (Array.isArray(value) && types.includes("array") && value.every(item => typeof item === "string"))
row[key] = value.join(", ");
else if (typeof value === "boolean") row[key] = String(value);
else if (value === null || typeof value === "string" || typeof value === "number") row[key] = value;
else throw new Error("A Catalog value does not match the typed document.");
}
return row;
}
function pageCap(value: unknown): number | null {
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : null;
}
/** Negotiate a bounded request size for a first page. A cursor page keeps the size it returns. */
export async function firstCatalogPage(database: string, body: QueryRequest, signal: AbortSignal) {
let limit = body.limit;
for (let attempt = 0; ; attempt += 1) {
signal.throwIfAborted();
let response: QueryResponse;
try {
response = await typedQuery(database, { ...body, limit, cursor: null }, signal);
} catch (error) {
const details = error instanceof TypedRefusal && error.code === "query_limit_invalid" &&
typeof error.details === "object" && error.details !== null
? error.details as Record<string, unknown> : null;
const cap = pageCap(details?.limit);
if (attempt || cap === null || cap >= limit) throw error;
limit = cap;
continue;
}
return { response, limit };
}
}
export type CatalogFilterTree = Extract<NonNullable<QueryRequest["filters"]>, { conj: "and" | "or" }>;
/** Translate a View's filters to the typed door's tree, at most three levels deep. */
export function catalogFilters(scope: CatalogReadScope): CatalogFilterTree | null {
if (!scope.filters.length) return null;
function node(value: FilterNode, depth: number): unknown {
if (isFilterGroup(value)) {
if (depth >= 3) throw new Error("This Catalog filter is nested too deeply. Use at most three levels.");
return { conj: value.conj, children: value.children.map((child) => node(child, depth + 1)) };
}
if (!FILTER_OPS.includes(value.op as FilterOp) || (value.rhs && value.rhs.kind !== "field")) {
throw new Error("This Catalog filter cannot be used. Choose a column condition.");
}
const { colId, op, id, rhs, value: operand, value2, values } = value;
return { colId, op, id, rhs, value: operand, value2, values };
}
return { conj: scope.filterConj ?? "and", nodes: scope.filters.map((value) => node(value, 1)) } as CatalogFilterTree;
}
/** Read one typed page. A first page negotiates its size; a cursor page keeps the given size. */
export async function loadCatalogData(database: string, scope: CatalogReadScope, signal: AbortSignal,
page: CatalogPageRequest = {}, schemaRead?: CatalogProperties): Promise<CatalogData> {
const properties = schemaRead ?? await databaseProperties(database, signal);
signal.throwIfAborted();
const filters = catalogFilters(scope);
const search = page.search?.trim();
const searchNodes = search ? [...new Set(page.searchFields)].filter(Boolean).map((colId) =>
({ colId, op: "contains", value: search })) : [];
if (search && !searchNodes.length) throw new Error("Choose a search field.");
const body: QueryRequest = {
limit: page.limit ?? CATALOG_PAGE_SIZE,
filters: searchNodes.length ? { conj: "and", nodes: [
...(filters ? [{ conj: filters.conj, children: filters.nodes }] : []),
{ conj: "or", children: searchNodes },
] } as QueryRequest["filters"] : filters,
sorts: scope.sorts.map((sort) => ({ field: sort.colId, direction: sort.dir })),
};
const cursor = page.cursor ?? null;
const { response, limit } = cursor === null ? await firstCatalogPage(database, body, signal)
: { response: await typedQuery(database, { ...body, cursor }, signal), limit: body.limit };
signal.throwIfAborted();
if (response.database !== database || !Array.isArray(response.records) || !Array.isArray(response.fields) ||
response.page?.returned !== response.records.length || response.records.length > limit ||
typeof response.page.has_more !== "boolean") {
throw new Error("The App page could not be confirmed.");
}
const nextCursor = response.page.has_more ? response.page.next_cursor : null;
if (response.page.has_more && (typeof nextCursor !== "string" || !nextCursor || nextCursor === cursor ||
page.priorCursors?.includes(nextCursor) || !response.page.returned)) {
throw new Error("The App cursor did not advance. Retry the request.");
}
const fields = response.fields.filter((field) => Object.hasOwn(properties, field.key)).map<Field>((field) => ({
...field, label: field.label ?? field.key, type: field.type as Field["type"],
source: field.source === "odoo" ? "odoo" : "overlay",
}));
const rows = response.records.map((record) => projectCatalogRow(record, properties));
return { fields, rows, allRows: rows, returned: response.page.returned, nextCursor, limit };
}
type PlacementProgress = { rows: Row[]; errors: Record<string, string> };
/** Resolve saved codes in order. A progress callback opts into independent results. */
export async function loadCatalogPlacements(database: string, key: string, codes: string[], signal: AbortSignal,
onProgress?: (data: PlacementProgress) => void): Promise<Row[]> {
signal.throwIfAborted();
const unique = [...new Set(codes)];
if (!unique.length) return [];
const schema = await typedSchema(database, signal);
const properties = catalogProperties(schema, database);
signal.throwIfAborted();
const rows: (Row | undefined)[] = new Array(unique.length);
const errors: Record<string, string> = {};
const resolved = () => rows.filter((row): row is Row => row !== undefined);
const batchIdentities = database === "customer_data" && key === "pid" && schema.fields.some((field) =>
field.key === "partner_id" && field.source === "odoo" && ["int", "number"].includes(field.type)) &&
unique.every((code) => /^[1-9]\d*$/.test(code) && Number.isSafeInteger(Number(code)));
if (batchIdentities) {
for (let offset = 0; offset < unique.length; offset += 100) {
signal.throwIfAborted();
const batch = unique.slice(offset, offset + 100);
try {
const requested = new Set(batch);
const found = new Map<string, Row>();
const cursors: (string | null)[] = [null];
let limit = 100;
const scope: CatalogReadScope = { filters: [{ id: "placement", colId: "partner_id", op: "isAnyOf", value: "", values: batch }],
filterConj: "and", sorts: [] };
for (;;) {
const page = await loadCatalogData(database, scope, signal,
{ cursor: cursors[cursors.length - 1], limit, priorCursors: [...cursors] }, properties);
limit = page.limit;
for (const row of page.rows) {
const code = String(row.pid);
if (!requested.has(code) || String(row.partner_id) !== code || found.has(code)) {
throw new Error("A selected customer batch could not be confirmed.");
}
found.set(code, row);
}
if (!page.nextCursor) break;
cursors.push(page.nextCursor);
}
for (const [index, code] of batch.entries()) {
const row = found.get(code);
if (row) rows[offset + index] = row;
else {
const message = "A selected customer could not be confirmed.";
if (!onProgress) throw new Error(message);
errors[code] = message;
}
}
} catch (error: unknown) {
signal.throwIfAborted();
if (!onProgress) throw error;
for (const code of batch) errors[code] = error instanceof Error ? error.message : "The selected record could not be loaded.";
}
signal.throwIfAborted();
onProgress?.({ rows: resolved(), errors: { ...errors } });
}
signal.throwIfAborted();
return resolved();
}
let next = 0;
let rejected = false;
await Promise.all(Array.from({ length: Math.min(6, unique.length) }, async () => {
while (next < unique.length && !rejected) {
signal.throwIfAborted();
const index = next++;
const code = unique[index];
try {
const scope: CatalogReadScope = { filters: [{ id: "placement", colId: key, op: "eq", value: code }], filterConj: "and", sorts: [] };
const page = await loadCatalogData(database, scope, signal, {}, properties);
if (page.nextCursor || page.rows.length !== 1 || String(page.rows[0][key]) !== code) throw new Error(key === "pid"
? "A selected customer could not be confirmed." : `The saved product ${code} could not be confirmed.`);
rows[index] = page.rows[0];
} catch (error: unknown) {
signal.throwIfAborted();
if (!onProgress) { rejected = true; throw error; }
errors[code] = error instanceof Error ? error.message : "The selected record could not be loaded.";
}
signal.throwIfAborted();
onProgress?.({ rows: resolved(), errors: { ...errors } });
}
}));
signal.throwIfAborted();
return resolved();
}
export function useCatalogPlacements(database: string, key: string, codes: string[], partial = false) {
const codesJson = JSON.stringify([...new Set(codes)].sort());
const requestKey = `${database}::${key}::${codesJson}::${partial}`;
const [attempt, setAttempt] = useState(0);
const [state, setState] = useState({ key: "", rows: [] as Row[], loading: true, error: "", errors: {} as Record<string, string> });
useEffect(() => {
const controller = new AbortController();
setState({ key: requestKey, rows: [], loading: true, error: "", errors: {} });
void loadCatalogPlacements(database, key, JSON.parse(codesJson), controller.signal, partial ? (data) => {
if (!controller.signal.aborted) setState({ ...data, key: requestKey, loading: true, error: "" });
} : undefined).then((rows) => {
if (!controller.signal.aborted) setState((live) => ({ ...live, key: requestKey, rows, loading: false, error: "" }));
}).catch((error: unknown) => {
if (!controller.signal.aborted) setState((live) => {
const message = error instanceof Error ? error.message : "Saved products could not be loaded.";
return { key: requestKey, rows: partial ? live.rows : [], loading: false, error: message,
errors: partial ? Object.fromEntries((JSON.parse(codesJson) as string[]).map((code) => [code, message])) : {} };
});
});
return () => controller.abort();
}, [database, key, codesJson, requestKey, partial, attempt]);
return { ...(state.key === requestKey ? state : { rows: [], loading: true, error: "", errors: {} }),
retry: () => setAttempt((value) => value + 1) };
}