loopable / web /src /apps /routing /useRoutingCustomers.ts
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
b2aa446 verified
Raw History Blame Contribute Delete
19.5 kB
import { useCallback, useEffect, useState } from "react";
import { TypedRefusal, typedQuery, typedSchema } from "../../typed/client";
import type { DatabaseSchema, QueryRequest, QueryResponse } from "../../typed/client";
import { FILTER_OPS, isFilterGroup } from "../../customer-grid/types";
import type { Field, FilterNode, FilterOp, Row, ViewConfig } from "../../customer-grid/types";
export type RoutingReadScope = Pick<ViewConfig, "filters" | "filterConj" | "sorts">;
export type RoutingProperties = Record<string, { type: readonly string[] }>;
export interface RoutingPage {
fields: Field[];
rows: Row[];
nextCursor: string | null;
limit: number;
}
export interface RoutingPageRequest {
cursor?: string | null;
limit?: number;
search?: string;
searchFields?: string[];
priorCursors?: (string | null)[];
}
/** The JSON types `external_openapi.py::_JSON_TYPE` gives each field type. Unknown types are strings. */
export const ROUTING_JSON_TYPE: Readonly<Record<string, string>> = {
text: "string", status: "string", select: "string", user: "string",
multiselect: "string", phone: "string", email: "string", url: "string",
date: "string", created_time: "string", automation: "string",
json: "string", code: "string", ai_enrich: "string", link: "string",
image: "string", int: "integer", rating: "integer",
currency: "number", pct: "number", metric: "number",
formula: "number", rollup: "number", checkbox: "string",
};
/** Type one database's record properties as `external_openapi.py::_field_schema` does. */
export function routingProperties(schema: DatabaseSchema, database?: string): RoutingProperties {
if ((database !== undefined && 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 Routing database is unavailable.");
}
const properties: RoutingProperties = {};
for (const field of schema.fields) {
if (!field || typeof field.key !== "string" || !field.key) continue;
if (field.type === "multiselect") {
properties[field.key] = { type: ["string", "array", "null"] };
continue;
}
const kind = field.type === "number" ? "number" : field.type === "checkbox" && ["mirror_user_table", "runtime_cache"].includes(schema.source_kind ?? "")
? "boolean" : ROUTING_JSON_TYPE[field.type] ?? "string";
properties[field.key] = { type: [...(kind === "number" || kind === "integer" ? ["number", "string"] : [kind]), "null"] };
}
return properties;
}
async function databaseProperties(database: string, signal: AbortSignal): Promise<RoutingProperties> {
const schema = await typedSchema(database, signal);
return routingProperties(schema, database);
}
function projectRow(record: QueryResponse["records"][number], properties: RoutingProperties): 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 customer 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 = 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 customer value does not match the typed schema.");
}
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 customer value does not match the typed schema.");
}
return row;
}
const FIRST_PAGE_SIZE = 50;
// The QueryRequest per-response maximum. A whole-book walk asks for it on page 1.
const RECORDS_PAGE_SIZE = 500;
function pageCap(value: unknown): number | null {
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : null;
}
/** Negotiate a bounded request size for one first page. The cap lives only in this call. */
async function firstPage(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;
}
const cap = pageCap(response.caps?.max_page_size);
return { response, limit: cap === null ? limit : Math.min(limit, cap) };
}
}
type FilterTree = Extract<NonNullable<QueryRequest["filters"]>, { conj: "and" | "or" }>;
function typedFilters(scope: RoutingReadScope): FilterTree | null {
if (!scope.filters.length) return null;
function node(value: FilterNode, depth: number): unknown {
if (isFilterGroup(value)) {
if (depth >= 3) throw new Error("This Routing 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 Routing 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 FilterTree;
}
/** Read one page of customers through the typed door. */
export async function loadRoutingPage(database: string, scope: RoutingReadScope, signal: AbortSignal,
page: RoutingPageRequest = {}, schemaRead?: RoutingProperties): Promise<RoutingPage> {
const properties = schemaRead ?? await databaseProperties(database, signal);
signal.throwIfAborted();
const filters = typedFilters(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 ?? FIRST_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 firstPage(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) => projectRow(record, properties));
return { fields, rows, nextCursor, limit };
}
export interface RoutingCustomers { fields: Field[]; rows: Row[] }
/** Read every page with one document and one negotiated limit across at most two stale restarts. */
export async function loadRoutingCustomers(database: string, scope: RoutingReadScope, signal: AbortSignal,
query: Pick<RoutingPageRequest, "search" | "searchFields"> = {},
onPage?: (data: RoutingCustomers) => void): Promise<RoutingCustomers> {
const properties = await databaseProperties(database, signal);
signal.throwIfAborted();
const filters = typedFilters(scope);
const search = query.search?.trim();
const searchNodes = search ? [...new Set(query.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: RECORDS_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 })),
};
let limit: number | null = null;
for (let restarts = 0; ; restarts += 1) {
const cursors: (string | null)[] = [null];
let data: RoutingCustomers = { fields: [], rows: [] };
try {
for (;;) {
signal.throwIfAborted();
const cursor = cursors[cursors.length - 1];
let response: QueryResponse;
if (limit === null) {
const first = await firstPage(database, body, signal);
response = first.response;
limit = first.limit;
} else {
response = await typedQuery(database, { ...body, cursor, limit }, signal);
}
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 || cursors.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) => projectRow(record, properties));
data = { fields, rows: [...data.rows, ...rows] };
onPage?.(data);
signal.throwIfAborted();
if (!nextCursor) return data;
cursors.push(nextCursor);
}
} catch (error) {
signal.throwIfAborted();
if (!(error instanceof TypedRefusal) || error.code !== "query_cursor_stale" || restarts >= 2) throw error;
}
}
}
type CustomersState = RoutingCustomers & { key: string; loading: boolean; error: string };
/** Retain rows for the current query while a new walk loads or fails. */
export function useRoutingCustomers(database: string, scope: RoutingReadScope, searchFields: string[] = []) {
const [search, setSearch] = useState("");
const scopeJson = JSON.stringify(scope);
const searchJson = JSON.stringify(searchFields);
const key = `${database}::${scopeJson}::${searchJson}::${search}`;
const [attempt, setAttempt] = useState(0);
const [state, setState] = useState<CustomersState>(() => ({ fields: [], rows: [], key, loading: true, error: "" }));
const retry = useCallback(() => setAttempt((value) => value + 1), []);
useEffect(() => {
const controller = new AbortController();
setState((live) => ({ fields: live.key === key ? live.fields : [], rows: live.key === key ? live.rows : [],
key, loading: true, error: "" }));
void loadRoutingCustomers(database, JSON.parse(scopeJson) as RoutingReadScope, controller.signal,
{ search, searchFields: JSON.parse(searchJson) }, (data) => {
if (!controller.signal.aborted) setState((live) => live.key === key && live.rows.length > data.rows.length
? live : { ...data, key, loading: true, error: "" });
}).then((data) => {
if (!controller.signal.aborted) setState({ ...data, key, loading: false, error: "" });
}).catch(() => {
if (!controller.signal.aborted) setState((live) => ({ ...live, key, loading: false,
error: "Could not load records" }));
});
return () => controller.abort();
}, [database, scopeJson, searchJson, search, key, attempt]);
const live = state.key === key ? state : { fields: [], rows: [] as Row[], key, loading: true, error: "" };
return { fields: live.fields, rows: live.rows, loading: live.loading, error: live.error, retry, search, setSearch };
}
type RoutingSelectionProgress = { rows: Row[]; errors: Record<string, string> };
/** Resolve saved codes in order. A progress callback opts into independent results. */
export async function loadRoutingSelected(database: string, codes: string[], signal: AbortSignal,
onProgress?: (data: RoutingSelectionProgress) => void): Promise<Row[]> {
const key = "pid";
signal.throwIfAborted();
const unique = [...new Set(codes)];
if (!unique.length) return [];
const schema = await typedSchema(database, signal);
const properties = routingProperties(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: RoutingReadScope = { filters: [{ id: "placement", colId: "partner_id", op: "isAnyOf", value: "", values: batch }],
filterConj: "and", sorts: [] };
for (;;) {
const page = await loadRoutingPage(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: RoutingReadScope = { filters: [{ id: "placement", colId: key, op: "eq", value: code }], filterConj: "and", sorts: [] };
const page = await loadRoutingPage(database, scope, signal, {}, properties);
if (page.nextCursor || page.rows.length !== 1 || String(page.rows[0][key]) !== code)
throw new Error("A selected customer 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 useRoutingSelected(database: string, codes: string[], partial = true) {
const codesJson = JSON.stringify([...new Set(codes)].sort());
const requestKey = `${database}::${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 loadRoutingSelected(database, 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 : "Selected customers 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, codesJson, requestKey, partial, attempt]);
return { ...(state.key === requestKey ? state : { rows: [], loading: true, error: "", errors: {} }),
retry: () => setAttempt((value) => value + 1) };
}