File size: 19,533 Bytes
8133df9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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) };
}