fsanyoto commited on
Commit
925e33e
·
verified ·
1 Parent(s): 8133df9

Prune files not in the deploy set (stale build context)

Browse files
platform/core/app_list_query.py DELETED
@@ -1,606 +0,0 @@
1
- """Compile PostgreSQL App list pages and retain an offline Grid evaluator.
2
-
3
- PUBLIC production API: compile_list_query(source_sql, columns, *, identity_sql,
4
- source_params=(), filter=None, sort=None, offset=0, limit=200, today=None,
5
- default_order_sql=None, clamp_offset=False) -> ListQuerySQL(ctes, params).
6
- The compiler does no I/O. Values use psycopg %s bindings. Never pass request SQL.
7
-
8
- source_sql is a trusted SELECT over the FULL permitted, searched source, without
9
- paging or a row cap. It becomes relation `s`. Column `sql` expressions,
10
- identity_sql and default_order_sql reference `s`. Identity must be unique,
11
- non-null and ordered consistently. Reserve the `_alq_` column/CTE prefix.
12
- Use a native numeric SQL expression for numeric fields, text for choice fields,
13
- and date/timestamp or ISO text for dates. Numeric comparisons use unrounded Grid
14
- values. Multiselect needs sql_shape='csv' and SQL normalization to Grid's
15
- comma-joined representation. Arrays, JSON and grouped ranks are refused.
16
-
17
- The result's ctes have no WITH prefix. Append them to caller CTEs. `_alq_bounds`
18
- has (total, page_offset), including empty results. `_alq_page` has (item_id,
19
- row_number), at most limit rows, ordered by row_number. Params follow textual
20
- CTE order, starting with source_params. Projection params follow plan.params.
21
- Only scalar identities/columns enter these CTEs. Project payloads AFTER `_alq_page`.
22
- No connection, grant, search, cursor or response policy moves into this module.
23
- default_order_sql preserves the route's order when sort is absent or empty.
24
- Explicit sorts replace it and always append identity ASC. Blanks sort last.
25
- clamp_offset=True preserves last-page snapping used by Restock and Aged Stock.
26
- Ranks use all non-rank conditions as their domain, matching filter_eval.
27
-
28
- Exact bounded Aged Stock amendment (inside its existing tenant-bound items):
29
- keep PAGE_SQL's doc/items/searched/filtered CTEs and their bound parameters.
30
- Add scalar Grid fields to searched, e.g. e.value->>'owner' AS owner.
31
- Replace bounds/ranked/page/projected and the final SELECT with this composition::
32
-
33
- fields = [{"key": "owner", "label": "Owner", "type": "user", "sql": "s.owner"}]
34
- plan = compile_list_query("SELECT * FROM filtered", fields,
35
- identity_sql="s.iid", filter=filter, sort=sort, offset=start, limit=limit,
36
- today=tenant_today, clamp_offset=True,
37
- default_order_sql="s.report DESC, CASE s.action WHEN 'Remove' THEN 0 "
38
- "WHEN 'Price down' THEN 1 WHEN 'Push with ads' THEN 2 ELSE 3 END, "
39
- 'COALESCE(s.stake, 0) DESC NULLS LAST, s.sku COLLATE "C"')
40
- sql = prefix_through_filtered + ", " + plan.ctes + ", projected AS (" \
41
- "SELECT items.v->p.item_id AS v, p.row_number FROM _alq_page p " \
42
- "CROSS JOIN items) SELECT jsonb_build_object(" \
43
- "'meta', (SELECT value - 'items' FROM doc), " \
44
- "'total', (SELECT total FROM _alq_bounds), " \
45
- "'offset', (SELECT page_offset FROM _alq_bounds), " \
46
- "'items', COALESCE((SELECT jsonb_agg(v - 'history' - 'fee_history' - 'fees' " \
47
- "ORDER BY row_number) FROM projected), '[]'::jsonb))"
48
- result = _select(st, sql, lambda key: (*prefix_params(key), *plan.params))
49
-
50
- prefix_params retains every existing scope/search binding through filtered.
51
- Keep the Next tier conditional default expressions and bind their flags inside
52
- source_sql/source_params, or choose trusted SQL expressions from next_tier.
53
- Keep existing page-size validation, cursor response and metadata handling.
54
- Serve list_columns(fields) only after the existing route grant checks.
55
- Restock follows the same pattern and retains its searched counts projection.
56
- The other four list owners supply their own full permitted source and projection.
57
- """
58
-
59
- import json
60
- import math
61
- import re
62
- from collections.abc import Mapping
63
- from dataclasses import dataclass
64
- from functools import cmp_to_key
65
-
66
- from core import windows
67
- from core.external_tools import FILTER_OPS
68
- from core.filter_contract import MAX_FILTER_VALUES
69
- from harness import filter_eval
70
- from harness.filter_sql import MULTI_VALUE_OPS, RANK_OPS, is_rule_active, to_num
71
-
72
-
73
- _EMPTY_OPS = frozenset({"isEmpty", "isNotEmpty"})
74
- _COMPARISONS = frozenset({"eq", "neq", "gt", "gte", "lt", "lte"})
75
- _TEXT_OPS = frozenset({"contains", "doesNotContain", "eq", "neq"})
76
- _NUMERIC = filter_eval.NUMERIC_TYPES
77
- _DATES = filter_eval.DATE_TYPES
78
- _TYPES = _NUMERIC | _DATES | {
79
- "text", "status", "select", "multiselect", "user", "checkbox", "json",
80
- }
81
- _LEAF_KEYS = frozenset({
82
- "id", "colId", "op", "value", "value2", "values", "rhs",
83
- "dateMode", "dateWindow", "rankScope",
84
- })
85
- _NUMBER = re.compile(r"[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\Z")
86
- # Match aios_grid.clean_filter_tree bounds, refusing excess conditions instead of truncating them.
87
- _MAX_DEPTH = 3
88
- _MAX_NODES = 100
89
- _MAX_SIBLINGS = 50
90
- _MAX_JSON = 65536
91
-
92
-
93
- def _fail(message):
94
- raise ValueError(message)
95
-
96
-
97
- def _pairs(pairs):
98
- result = {}
99
- for key, value in pairs:
100
- if key in result:
101
- _fail("Query JSON contains a repeated key.")
102
- result[key] = value
103
- return result
104
-
105
-
106
- def _decode(value, name, default):
107
- if value is None:
108
- return default
109
- if isinstance(value, str):
110
- if len(value) > _MAX_JSON:
111
- _fail(f"The {name} query is too large.")
112
- try:
113
- return json.loads(value, object_pairs_hook=_pairs,
114
- parse_constant=lambda _: _fail("Query numbers must be finite."))
115
- except (ValueError, RecursionError) as exc:
116
- raise ValueError(f"The {name} query must be valid JSON.") from exc
117
- return value
118
-
119
-
120
- def _column_map(columns):
121
- if isinstance(columns, Mapping):
122
- fields = []
123
- for key, value in columns.items():
124
- if not isinstance(value, Mapping) or value.get("key", key) != key:
125
- _fail("Each column needs a matching key and type.")
126
- fields.append({**value, "key": key})
127
- elif isinstance(columns, (list, tuple)):
128
- fields = columns
129
- else:
130
- _fail("Columns must be a field list or mapping.")
131
- result = {}
132
- for field in fields:
133
- if not isinstance(field, Mapping):
134
- _fail("Each column needs a key and type.")
135
- key, kind = field.get("key"), field.get("type")
136
- if not isinstance(key, str) or not key or key in result:
137
- _fail("Column keys must be unique nonempty strings.")
138
- if not isinstance(kind, str) or kind not in _TYPES:
139
- _fail(f"Column {key} has an unsupported type.")
140
- result[key] = dict(field)
141
- return result
142
-
143
-
144
- def list_columns(columns):
145
- """Return explicit Grid builder fields for an authenticated route's columns response."""
146
- public = {"key", "label", "type", "options", "description", "width"}
147
- return [{key: value for key, value in field.items() if key in public}
148
- for field in _column_map(columns).values()]
149
-
150
-
151
- def _column(key, columns):
152
- if not isinstance(key, str) or key not in columns:
153
- _fail("The query names an unknown column.")
154
- return columns[key]
155
-
156
-
157
- def _scalar(value):
158
- if type(value) not in (str, int, float) or isinstance(value, float) and not math.isfinite(value):
159
- _fail("Filter values must be text or finite numbers.")
160
-
161
-
162
- def _number(value):
163
- _scalar(value)
164
- try:
165
- number = float(value)
166
- except (ValueError, OverflowError) as exc:
167
- raise ValueError("This filter needs a finite numeric value.") from exc
168
- if not _NUMBER.fullmatch(str(value).strip()) or not math.isfinite(number):
169
- _fail("This filter needs a finite numeric value.")
170
- return number
171
-
172
-
173
- def _day(value):
174
- if not isinstance(value, str) or windows.ending_date(value) is None:
175
- _fail("Date filters need an ISO calendar date.")
176
-
177
-
178
- def _date_operand(node, today):
179
- op = node["op"]
180
- if op == "within":
181
- spec = node.get("dateWindow")
182
- if not isinstance(spec, dict) or set(spec) - {"kind", "n", "from", "to"}:
183
- _fail("A date window needs a known kind and bounds.")
184
- kind = spec.get("kind")
185
- if not isinstance(kind, str) or kind not in windows.WINDOW_KINDS:
186
- _fail("A date window needs a known kind.")
187
- allowed = {"kind"}
188
- if kind in windows.N_KINDS:
189
- allowed.add("n")
190
- n = _number(spec.get("n"))
191
- if n != int(n) or not 1 <= n <= windows.MAX_N:
192
- _fail("The date window size is out of range.")
193
- if kind == "custom":
194
- allowed |= {"from", "to"}
195
- for key in ("from", "to"):
196
- if spec.get(key) not in (None, ""):
197
- _day(spec[key])
198
- if set(spec) - allowed:
199
- _fail("The date window has fields for another kind.")
200
- if windows.normalize(spec) is None:
201
- _fail("The date window is incomplete.")
202
- if today is None:
203
- _fail("Date windows need the tenant's today date.")
204
- try:
205
- windows.resolve(spec, today)
206
- except (ValueError, OverflowError) as exc:
207
- raise ValueError("The date window is out of range.") from exc
208
- elif op == "between":
209
- _day(node.get("value"))
210
- _day(node.get("value2"))
211
- else:
212
- mode = node.get("dateMode", "exact")
213
- if not isinstance(mode, str) or mode not in windows.ANCHOR_MODES:
214
- _fail("The date anchor is unknown.")
215
- if mode != "exact" and today is None:
216
- _fail("Relative dates need the tenant's today date.")
217
- if mode == "exact":
218
- _day(node.get("value"))
219
- try:
220
- anchor = windows.resolve_anchor(mode, node.get("value"), today or "1970-01-01")
221
- except (ValueError, OverflowError) as exc:
222
- raise ValueError("The date anchor is out of range.") from exc
223
- if anchor is None:
224
- _fail("The date anchor is incomplete.")
225
-
226
-
227
- def _leaf(node, columns, today):
228
- if set(node) - _LEAF_KEYS:
229
- _fail("The filter has unsupported fields.")
230
- field = _column(node.get("colId"), columns)
231
- op, kind = node.get("op"), field["type"]
232
- if not isinstance(op, str) or op not in FILTER_OPS:
233
- _fail("The filter operator is unknown.")
234
- if "id" in node and not isinstance(node["id"], str):
235
- _fail("Filter ids must be text.")
236
- for key in ("value", "value2"):
237
- if key in node:
238
- _scalar(node[key])
239
- allowed = _EMPTY_OPS
240
- if kind in _NUMERIC:
241
- allowed |= _COMPARISONS | RANK_OPS | {"between", "isAnyOf", "isNoneOf"}
242
- elif kind in _DATES:
243
- allowed |= _COMPARISONS | {"between", "within"}
244
- elif kind not in {"checkbox", "json"}:
245
- allowed |= _TEXT_OPS | MULTI_VALUE_OPS
246
- if op not in allowed:
247
- _fail("The operator does not apply to this column type.")
248
- if (("dateWindow" in node and op != "within") or ("dateMode" in node and (
249
- kind not in _DATES or op not in _COMPARISONS))):
250
- _fail("Date options do not apply to this filter.")
251
- if "values" in node and op not in MULTI_VALUE_OPS:
252
- _fail("A value list needs a membership operator.")
253
- if "rankScope" in node:
254
- if op not in RANK_OPS or node["rankScope"] not in ("global", {"mode": "global"}):
255
- _fail("App lists support global ranking only.")
256
- if "rhs" in node:
257
- rhs = node["rhs"]
258
- if (not isinstance(rhs, dict) or set(rhs) != {"kind", "colId"}
259
- or rhs.get("kind") != "field" or op not in _COMPARISONS | _TEXT_OPS):
260
- _fail("The filter needs a supported field comparison.")
261
- other = _column(rhs.get("colId"), columns)["type"]
262
- if ((kind in _NUMERIC) != (other in _NUMERIC)
263
- or (kind in _DATES) != (other in _DATES)):
264
- _fail("Compared fields need compatible types.")
265
- elif op in MULTI_VALUE_OPS:
266
- values = node.get("values")
267
- if (not isinstance(values, list) or not 1 <= len(values) <= MAX_FILTER_VALUES
268
- or any(not isinstance(v, str) or not v.strip() or len(v.strip()) > 120 for v in values)):
269
- _fail("Membership filters need a nonempty bounded list of text values.")
270
- if kind in _NUMERIC:
271
- for value in values:
272
- _number(value)
273
- elif op in RANK_OPS:
274
- if op not in {"aboveAvg", "belowAvg"}:
275
- number = _number(node.get("value"))
276
- upper = {"inQuartile": 4, "inDecile": 10, "inTopPct": 100, "inBottomPct": 100}.get(op, 10000)
277
- if number != int(number) or not 1 <= number <= upper:
278
- _fail("The rank operand is out of range.")
279
- elif op not in _EMPTY_OPS:
280
- if kind in _DATES:
281
- _date_operand(node, today)
282
- elif kind in _NUMERIC:
283
- _number(node.get("value"))
284
- if op == "between":
285
- _number(node.get("value2"))
286
- elif not isinstance(node.get("value"), str) or not node["value"]:
287
- _fail("This filter needs a nonempty text value.")
288
- if not is_rule_active(node, columns):
289
- _fail("The filter is incomplete.")
290
- return dict(node)
291
-
292
-
293
- def _filter_tree(value, columns, today):
294
- raw = _decode(value, "filter", [])
295
- if isinstance(raw, list):
296
- raw = {"nodes": raw}
297
- if not isinstance(raw, dict) or set(raw) - {"conj", "nodes"} or "nodes" not in raw:
298
- _fail("A filter needs nodes and an optional conjunction.")
299
- count = 0
300
-
301
- def group(node, key, depth):
302
- nonlocal count
303
- if depth > _MAX_DEPTH:
304
- _fail("The filter is nested too deeply.")
305
- conj, nodes = node.get("conj", "and"), node.get(key)
306
- if conj not in ("and", "or") or not isinstance(nodes, list):
307
- _fail("A filter group needs nodes and a conjunction of and or or.")
308
- if len(nodes) > _MAX_SIBLINGS:
309
- _fail("The filter group has too many conditions.")
310
- children = []
311
- for child in nodes:
312
- count += 1
313
- if count > _MAX_NODES or not isinstance(child, dict):
314
- _fail("The filter has too many nodes or a malformed node.")
315
- if "children" in child:
316
- if set(child) - {"conj", "children"} or not child["children"]:
317
- _fail("Nested filter groups need children and a conjunction.")
318
- children.append(group(child, "children", depth + 1))
319
- else:
320
- children.append(_leaf(child, columns, today))
321
- return {"conj": conj, key: children}
322
-
323
- return group(raw, "nodes", 1)
324
-
325
-
326
- def _sort_rules(value, columns):
327
- rules = _decode(value, "sort", [])
328
- if not isinstance(rules, list):
329
- _fail("Sort must be a list of column and direction pairs.")
330
- seen, result = set(), []
331
- for rule in rules:
332
- if not isinstance(rule, dict) or set(rule) != {"colId", "dir"}:
333
- _fail("Each sort needs colId and dir.")
334
- key = rule["colId"]
335
- _column(key, columns)
336
- if rule["dir"] not in ("asc", "desc") or key in seen:
337
- _fail("Sort directions must be asc or desc with unique columns.")
338
- if columns[key]["type"] == "json":
339
- _fail("JSON columns cannot be sorted.")
340
- seen.add(key)
341
- result.append(dict(rule))
342
- return result
343
-
344
-
345
- def _eval_key(key):
346
- return "field:" + key
347
-
348
-
349
- def _eval_tree(node):
350
- result = dict(node)
351
- for key in ("nodes", "children"):
352
- if key in result:
353
- result[key] = [_eval_tree(child) for child in result[key]]
354
- if "colId" in result:
355
- result["colId"] = _eval_key(result["colId"])
356
- if "rhs" in result:
357
- result["rhs"] = {**result["rhs"], "colId": _eval_key(result["rhs"]["colId"])}
358
- return result
359
-
360
-
361
- def _compare(left, right, sorts, columns):
362
- for rule in sorts:
363
- key = rule["colId"]
364
- a, b = left.get(key), right.get(key)
365
- a_blank, b_blank = filter_eval._is_blank(a), filter_eval._is_blank(b)
366
- if a_blank != b_blank:
367
- return 1 if a_blank else -1
368
- if a_blank:
369
- continue
370
- kind = columns[key]["type"]
371
- if kind in _NUMERIC:
372
- a, b = to_num(a), to_num(b)
373
- elif kind in _DATES:
374
- a, b = str(a)[:10], str(b)[:10]
375
- else:
376
- a, b = filter_eval._as_choice_text(a), filter_eval._as_choice_text(b)
377
- order = (a > b) - (a < b)
378
- if order:
379
- return order if rule["dir"] == "asc" else -order
380
- return 0
381
-
382
-
383
- def apply_list_query(rows, columns, *, filter=None, sort=None, offset=0, limit=200, today=None):
384
- """Evaluate complete offline fixtures, never a production PostgreSQL request path."""
385
- if type(offset) is not int or offset < 0 or type(limit) is not int or limit < 1:
386
- _fail("Paging needs a nonnegative offset and a positive limit.")
387
- if today is not None:
388
- _day(today)
389
- fields = _column_map(columns)
390
- tree = _filter_tree(filter, fields, today)
391
- sorts = _sort_rules(sort, fields)
392
- source = list(rows)
393
- if any(not isinstance(row, Mapping) for row in source):
394
- _fail("App list rows must be mappings.")
395
- selected = source
396
- if tree["nodes"]:
397
- # Separate evaluator identities from real columns, including a visible pid column.
398
- eval_fields = {_eval_key(key): {**field, "key": _eval_key(key)} for key, field in fields.items()}
399
- eval_rows = [{"pid": index, **{_eval_key(key): row.get(key) for key in fields}}
400
- for index, row in enumerate(source)]
401
- pids = filter_eval.visible_pids(_eval_tree(tree), eval_rows, eval_fields,
402
- filter_eval.EvalCtx(today=today))
403
- selected = [source[index] for index in pids]
404
- if sorts:
405
- selected = sorted(selected, key=cmp_to_key(lambda a, b: _compare(a, b, sorts, fields)))
406
- total = len(selected)
407
- more = offset + limit < total
408
- return {"items": selected[offset:offset + limit], "total": total, "offset": offset,
409
- "limit": limit, "hasMore": more, "nextOffset": offset + limit if more else None}
410
-
411
-
412
- @dataclass(frozen=True)
413
- class ListQuerySQL:
414
- """Public PostgreSQL CTEs and positional bindings for a caller-owned bounded projection."""
415
-
416
- ctes: str
417
- params: tuple
418
-
419
-
420
- _SQL_CMP = {"eq": "=", "neq": "<>", "lt": "<", "lte": "<=", "gt": ">", "gte": ">="}
421
- _SQL_WS = "".join(chr(i) for i in (*range(9, 14), *range(28, 33), 133, 160, 5760,
422
- *range(8192, 8203), 8232, 8233, 8239, 8287, 12288))
423
-
424
-
425
- def _sql_trim(expr):
426
- return f"TRIM(BOTH '{_SQL_WS}' FROM CAST({expr} AS TEXT))"
427
-
428
-
429
- def _sql_blank(expr):
430
- return f"({expr} IS NULL OR {_sql_trim(expr)} IN ('', '(none)'))"
431
-
432
-
433
- def _sql_value(expr, kind):
434
- if kind in _NUMERIC:
435
- return f"COALESCE(CAST({expr} AS DOUBLE PRECISION), 0)"
436
- if kind in _DATES:
437
- return f"COALESCE(SUBSTR(CAST({expr} AS TEXT), 1, 10), '')"
438
- return f"LOWER(COALESCE(CAST({expr} AS TEXT), ''))"
439
-
440
-
441
- def _sql_leaf(node, fields, refs, params, today):
442
- key, op = node["colId"], node["op"]
443
- kind, raw = fields[key]["type"], refs[key]
444
- value = _sql_value(raw, kind)
445
-
446
- def bind(operand):
447
- params.append(operand)
448
- return "%s"
449
-
450
- if op in _EMPTY_OPS:
451
- blank = _sql_blank(raw)
452
- return blank if op == "isEmpty" else f"(NOT {blank})"
453
- if op in MULTI_VALUE_OPS:
454
- values = [v.strip() for v in node["values"]]
455
- if op in {"isAnyOf", "isNoneOf"}:
456
- parts = [f"{value} = {bind(_number(v))}" if kind in _NUMERIC
457
- else f"{value} = LOWER(CAST({bind(v)} AS TEXT))" for v in values]
458
- else:
459
- # PostgreSQL unnest replaces DuckDB's list_transform/list_contains dialect.
460
- parts = [f"EXISTS (SELECT 1 FROM unnest(string_to_array({value}, ',')) "
461
- f"AS m(member) WHERE {_sql_trim('m.member')} = LOWER(CAST({bind(v)} AS TEXT)))"
462
- for v in values]
463
- joined = (" AND " if op == "hasAllOf" else " OR ").join(parts)
464
- return f"({'NOT ' if op in {'isNoneOf', 'hasNoneOf'} else ''}({joined}))"
465
- if op == "between":
466
- if kind in _NUMERIC:
467
- lo, hi = sorted((_number(node["value"]), _number(node["value2"])))
468
- else:
469
- lo, hi = sorted((node["value"], node["value2"]))
470
- a, b = bind(lo), bind(hi)
471
- if kind in _DATES:
472
- a, b = f"CAST({a} AS TEXT)", f"CAST({b} AS TEXT)"
473
- return f"({value} >= {a} AND {value} <= {b})"
474
- if op == "within":
475
- lo, hi = windows.resolve(node["dateWindow"], today)
476
- parts = [f"{value} <> ''"]
477
- if lo is not None:
478
- parts.append(f"{value} >= CAST({bind(lo)} AS TEXT)")
479
- if hi is not None:
480
- parts.append(f"{value} <= CAST({bind(hi)} AS TEXT)")
481
- return "(" + " AND ".join(parts) + ")"
482
- if "rhs" in node:
483
- other = _sql_value(refs[node["rhs"]["colId"]], kind)
484
- elif kind in _NUMERIC:
485
- other = bind(_number(node["value"]))
486
- elif kind in _DATES:
487
- anchor = windows.resolve_anchor(node.get("dateMode", "exact"), node.get("value"),
488
- today or "1970-01-01")
489
- other = f"CAST({bind(anchor)} AS TEXT)"
490
- else:
491
- other = f"LOWER(CAST({bind(node['value'])} AS TEXT))"
492
- if op in {"contains", "doesNotContain"}:
493
- return f"(strpos({value}, {other}) {'>' if op == 'contains' else '='} 0)"
494
- return f"({value} {_SQL_CMP[op]} {other})"
495
-
496
-
497
- def _sql_tree(node, fields, refs, params, today, ranks=None, residue=False):
498
- children = node.get("nodes", node.get("children"))
499
- if children is not None:
500
- parts = [_sql_tree(child, fields, refs, params, today, ranks, residue) for child in children]
501
- parts = [part for part in parts if part is not None]
502
- return ("(" + (" OR " if node.get("conj") == "or" else " AND ").join(parts) + ")"
503
- if parts else None)
504
- if node["op"] in RANK_OPS:
505
- if residue:
506
- return None
507
- return f"(_alq_id IN (SELECT item_id FROM {ranks[id(node)]}))"
508
- return _sql_leaf(node, fields, refs, params, today)
509
-
510
-
511
- def _sql_rank_nodes(node):
512
- for child in node.get("nodes", node.get("children", ())):
513
- if "children" in child:
514
- yield from _sql_rank_nodes(child)
515
- elif child["op"] in RANK_OPS:
516
- yield child
517
-
518
-
519
- def compile_list_query(source_sql, columns, *, identity_sql, source_params=(), filter=None,
520
- sort=None, offset=0, limit=200, today=None, default_order_sql=None,
521
- clamp_offset=False):
522
- """Compile validated Grid values into a full-scope PostgreSQL identity page with total count."""
523
- if type(offset) is not int or offset < 0 or type(limit) is not int or limit < 1:
524
- _fail("Paging needs a nonnegative offset and a positive limit.")
525
- if type(clamp_offset) is not bool:
526
- _fail("Offset clamping must be a boolean.")
527
- if today is not None:
528
- _day(today)
529
- for sql in (source_sql, identity_sql):
530
- if not isinstance(sql, str) or not sql.strip():
531
- _fail("A trusted source SELECT and unique row identity SQL are required.")
532
- if default_order_sql is not None and (
533
- not isinstance(default_order_sql, str) or not default_order_sql.strip()):
534
- _fail("Default ordering must be trusted SQL or None.")
535
- fields = _column_map(columns)
536
- tree = _filter_tree(filter, fields, today)
537
- sorts = _sort_rules(sort, fields)
538
- refs, selections = {}, []
539
- for index, (key, field) in enumerate(fields.items()):
540
- expr, shape = field.get("sql"), field.get("sql_shape", "scalar")
541
- if (not isinstance(expr, str) or not expr.strip() or field["type"] == "json"
542
- or not isinstance(shape, str) or shape not in {"scalar", "csv"}
543
- or (field["type"] == "multiselect" and shape != "csv")
544
- or (field["type"] != "multiselect" and shape != "scalar")
545
- or field.get("aggregate")):
546
- _fail("SQL columns need typed scalar SQL, or explicit csv SQL for multiselect.")
547
- refs[key] = f"_alq_c{index}"
548
- selections.append(f"({expr}) AS {refs[key]}")
549
- params = list(source_params)
550
- if not sorts and default_order_sql:
551
- selections.append("row_number() OVER (ORDER BY " + default_order_sql
552
- + f", {identity_sql} ASC) AS _alq_default_n")
553
- select = ", ".join([f"({identity_sql}) AS _alq_id", *selections])
554
- ctes = [f"_alq_source AS MATERIALIZED (SELECT {select} FROM ({source_sql}) AS s)"]
555
- rank_nodes = tuple(_sql_rank_nodes(tree))
556
- ranks = {}
557
- if rank_nodes:
558
- predicate = _sql_tree(tree, fields, refs, params, today, residue=True) or "TRUE"
559
- ctes.append(f"_alq_rank_domain AS MATERIALIZED (SELECT * FROM _alq_source WHERE {predicate})")
560
- for index, node in enumerate(rank_nodes):
561
- name, raw, op = f"_alq_rank_{index}", refs[node["colId"]], node["op"]
562
- ranks[id(node)] = name
563
- numeric = f"CAST({raw} AS DOUBLE PRECISION)"
564
- # Rank blanks are absent. Comparison blanks still coerce to zero.
565
- stats = (f"SELECT _alq_id AS item_id, {numeric} AS v, "
566
- f"row_number() OVER (ORDER BY {numeric} ASC, _alq_id ASC) AS a, "
567
- f"row_number() OVER (ORDER BY {numeric} DESC, _alq_id ASC) AS d, "
568
- f"count(*) OVER () AS n, avg({numeric}) OVER () AS mean "
569
- f"FROM _alq_rank_domain WHERE {raw} IS NOT NULL")
570
- if op in {"aboveAvg", "belowAvg"}:
571
- predicate = f"v {'>' if op == 'aboveAvg' else '<'} mean"
572
- else:
573
- params.append(int(_number(node["value"])))
574
- if op in {"topN", "bottomN"}:
575
- predicate = f"{'d' if op == 'topN' else 'a'} <= %s"
576
- elif op in {"inTopPct", "inBottomPct"}:
577
- predicate = f"{'d' if op == 'inTopPct' else 'a'} <= CEIL(n * CAST(%s AS NUMERIC) / 100)"
578
- else:
579
- buckets = 4 if op == "inQuartile" else 10
580
- predicate = f"n >= {buckets} AND FLOOR((a - 1) * {buckets} / CAST(n AS NUMERIC)) + 1 = %s"
581
- ctes.append(f"{name} AS (SELECT item_id FROM ({stats}) AS r WHERE {predicate})")
582
- predicate = _sql_tree(tree, fields, refs, params, today, ranks) or "TRUE"
583
- ctes.append(f"_alq_filtered AS MATERIALIZED (SELECT * FROM _alq_source WHERE {predicate})")
584
- terms = []
585
- for rule in sorts:
586
- raw, kind = refs[rule["colId"]], fields[rule["colId"]]["type"]
587
- terms += [f"CASE WHEN {_sql_blank(raw)} THEN 1 ELSE 0 END ASC",
588
- f"CASE WHEN {_sql_blank(raw)} THEN NULL ELSE {_sql_value(raw, kind)} END "
589
- f"{rule['dir'].upper()}"]
590
- if not sorts and default_order_sql:
591
- terms.append("s._alq_default_n ASC")
592
- terms.append("s._alq_id ASC")
593
- if clamp_offset:
594
- page_offset = ("CASE WHEN count(*) = 0 THEN 0 ELSE LEAST(CAST(%s AS BIGINT), "
595
- "CAST(FLOOR((count(*) - 1) / CAST(%s AS NUMERIC)) AS BIGINT) * %s) END")
596
- params.extend((offset, limit, limit))
597
- else:
598
- page_offset = "CAST(%s AS BIGINT)"
599
- params.append(offset)
600
- ctes += [f"_alq_bounds AS (SELECT count(*) AS total, {page_offset} AS page_offset FROM _alq_filtered)",
601
- "_alq_ordered AS (SELECT s._alq_id AS item_id, row_number() OVER (ORDER BY "
602
- + ", ".join(terms) + ") AS row_number FROM _alq_filtered AS s)",
603
- "_alq_page AS (SELECT item_id, row_number FROM _alq_ordered ORDER BY row_number "
604
- "LIMIT %s OFFSET (SELECT page_offset FROM _alq_bounds))"]
605
- params.append(limit)
606
- return ListQuerySQL(",\n".join(ctes), tuple(params))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
platform/core/test_accounts.py DELETED
@@ -1,398 +0,0 @@
1
- """Test accounts: an exact copy of one member's permissions that takes no seat and sends nothing.
2
-
3
- S79-T63 (QI34, rulings R28 and R42). The copy carries the source's role, business units, modules,
4
- agent, permission block (row filters and field walls stay locks, rule 3) and every share entry
5
- that names the source. It never carries a password, MFA, email, WhatsApp, avatar or epoch.
6
- Creation is all or nothing: a failure removes every part already written.
7
- """
8
- import copy
9
- import datetime as _dt
10
- import re
11
-
12
- import core.shares as shares
13
- import core.store as store
14
- import core.users as users
15
-
16
- PREFIX = 'test-'
17
- #: `routes_admin._UNAME_RE` admits 2 to 32 characters; a generated name stays inside it.
18
- _NAME_MAX = 32
19
- _RETRIES = 3
20
-
21
- #: The record fields a copy takes verbatim from its source.
22
- COPIED_FIELDS = ('role', 'bus', 'modules', 'agent', 'perms', 'perms_v')
23
-
24
-
25
- class CopyRefused(ValueError):
26
- """The request names a source or target this door does not copy or remove."""
27
-
28
- def __init__(self, code, message, status=400):
29
- self.code, self.status = code, status
30
- super().__init__(message)
31
-
32
-
33
- class CopyFailed(RuntimeError):
34
- """A copy or removal stopped part way. `leftovers` names what cleanup could not remove."""
35
-
36
- def __init__(self, cause, leftovers=()):
37
- self.cause, self.leftovers = cause, list(leftovers)
38
- super().__init__(cause)
39
-
40
-
41
- def is_test(record):
42
- """Whether this stored record is a copied test account."""
43
- return users.is_test_account(record)
44
-
45
-
46
- def _tenant_of(record):
47
- return str((record or {}).get('tenant') or 'royal-imports').strip().lower()
48
-
49
-
50
- def paid_seats(registry, tenant):
51
- """Active accounts in `tenant` that take a paid seat. A test account takes none (R42)."""
52
- want = str(tenant or '').strip().lower()
53
- return sum(1 for record in (registry or {}).values()
54
- if isinstance(record, dict) and record.get('active', True)
55
- and _tenant_of(record) == want and not is_test(record))
56
-
57
-
58
- def test_username(source, registry):
59
- """`test-<source>` cut to fit the username rule, with a numeric suffix on a collision."""
60
- base = re.sub(r'[^a-z0-9._-]+', '-', str(source or '').strip().lower()).strip('.-_')
61
- stem = (PREFIX + base)[:_NAME_MAX].rstrip('.-_')
62
- taken = set(registry or {})
63
- if stem not in taken:
64
- return stem
65
- for n in range(2, 1000):
66
- suffix = f'-{n}'
67
- candidate = stem[:_NAME_MAX - len(suffix)].rstrip('.-_') + suffix
68
- if candidate not in taken:
69
- return candidate
70
- raise CopyRefused('no_free_username', 'No free name is left for another test account.', 409)
71
-
72
-
73
- class _Doc:
74
- """A read-only store over one already-read shares document."""
75
-
76
- def __init__(self, doc):
77
- self._doc = doc if isinstance(doc, dict) else {}
78
-
79
- def get(self, name, *args, **kwargs):
80
- return self._doc if name == shares.SHARES_KEY else None
81
-
82
-
83
- class _Batch:
84
- """Stage `shares.set_grants` writes in memory, then replay them all in ONE store update.
85
-
86
- Every staged closure keeps its `expect` check, so the replay refuses if any record changed.
87
- """
88
-
89
- def __init__(self, st, doc):
90
- self._st, self._doc, self._fns = st, copy.deepcopy(doc or {}), []
91
-
92
- def get(self, name, *args, **kwargs):
93
- if name != shares.SHARES_KEY:
94
- raise RuntimeError(f'a staged grant write cannot read {name!r}')
95
- return self._doc
96
-
97
- def update(self, name, fn, flush='sync'):
98
- if name != shares.SHARES_KEY:
99
- raise RuntimeError(f'a staged grant write cannot write {name!r}')
100
- self._doc = fn(self._doc)
101
- self._fns.append(fn)
102
- return self._doc
103
-
104
- def commit(self):
105
- if not self._fns:
106
- return
107
- fns = list(self._fns)
108
-
109
- def replay(data):
110
- data = data if isinstance(data, dict) else {}
111
- for fn in fns:
112
- data = fn(data)
113
- return data
114
-
115
- self._st.update(shares.SHARES_KEY, replay, flush='sync')
116
-
117
-
118
- def _read_shares(st):
119
- """The tenant's shares document, read strictly: an unreadable bucket stops the copy."""
120
- doc = st.get(shares.SHARES_KEY, fresh=True)
121
- if doc is None:
122
- return {}
123
- if not isinstance(doc, dict):
124
- raise RuntimeError('the sharing records could not be read')
125
- return doc
126
-
127
-
128
- def grants_naming(username, doc):
129
- """`[{kind, oid, role}]` for every share entry that names `username`, from one document."""
130
- username = str(username or '').strip().lower()
131
- found = []
132
- view = _Doc(doc)
133
- for kind in shares.KINDS:
134
- for oid, record in sorted(shares.grants_bulk(kind, st=view).items()):
135
- for entry in record['entries']:
136
- if entry['user'] == username:
137
- found.append({'kind': kind, 'oid': oid, 'role': entry['role']})
138
- return found
139
-
140
-
141
- def _check_source(source, registry, tenant):
142
- uname = str(source or '').strip().lower()
143
- record = (registry or {}).get(uname)
144
- if not uname or not isinstance(record, dict) or _tenant_of(record) != tenant:
145
- raise CopyRefused('no_such_user', f'No account named {uname or "(blank)"}.', 404)
146
- if is_test(record):
147
- raise CopyRefused('source_is_test', 'A test account cannot be copied. Copy a member.')
148
- if str(record.get('role') or 'user').strip().lower() != 'user':
149
- raise CopyRefused('source_is_admin',
150
- 'An administrator cannot be copied. Copy a member account.')
151
- if not record.get('active', True):
152
- raise CopyRefused('source_inactive', 'This account is turned off. Turn it on first.')
153
- return uname, record
154
-
155
-
156
- def plan(source, registry, *, tenant, st):
157
- """What a copy of `source` would carry, read now: the access fields and every share entry."""
158
- tenant = str(tenant or '').strip().lower()
159
- uname, record = _check_source(source, registry, tenant)
160
- access = {key: copy.deepcopy(record[key]) for key in COPIED_FIELDS if key in record}
161
- return {'source': uname,
162
- 'sourceName': str(record.get('name') or uname),
163
- 'username': test_username(uname, registry),
164
- 'name': f'Test copy of {record.get("name") or uname}',
165
- 'access': access,
166
- 'grants': grants_naming(uname, _read_shares(st))}
167
-
168
-
169
- def _password(username, name):
170
- for _ in range(8):
171
- candidate = users.generate_password()
172
- if not users.password_failures(candidate, username=username, name=name):
173
- return candidate
174
- raise RuntimeError('could not generate a password for the test account')
175
-
176
-
177
- def copy_grants(username, grants, *, st, granter):
178
- """Add `username` beside each listed share entry, same role, in one store update."""
179
- batch = _Batch(st, _read_shares(st))
180
- for grant in grants:
181
- current = shares.grants(grant['kind'], grant['oid'], st=batch)
182
- if any(entry['user'] == username for entry in current['entries']):
183
- continue
184
- entries = current['entries'] + [{'user': username, 'role': grant['role']}]
185
- shares.set_grants(grant['kind'], grant['oid'], entries, st=batch,
186
- granter=granter, expect=current['entries'])
187
- batch.commit()
188
-
189
-
190
- def remove_grants(username, *, st, granter):
191
- """Remove every share entry naming `username`, retrying if a record changes meanwhile."""
192
- for attempt in range(_RETRIES):
193
- doc = _read_shares(st)
194
- batch = _Batch(st, doc)
195
- for grant in grants_naming(username, doc):
196
- current = shares.grants(grant['kind'], grant['oid'], st=batch)
197
- entries = [entry for entry in current['entries'] if entry['user'] != username]
198
- shares.set_grants(grant['kind'], grant['oid'], entries, st=batch,
199
- granter=granter, expect=current['entries'])
200
- try:
201
- batch.commit()
202
- return
203
- except shares.GrantsChanged:
204
- if attempt == _RETRIES - 1:
205
- raise
206
-
207
-
208
- def _fresh_registry():
209
- registry = store.get('users', fresh=True)
210
- if not isinstance(registry, dict):
211
- raise RuntimeError('the account registry could not be read')
212
- return registry
213
-
214
-
215
- def _confirm(username, plan_, marker, st):
216
- record = _fresh_registry().get(username) or {}
217
- if record.get(users.TEST_ACCOUNT_KEY) != marker:
218
- raise RuntimeError('the account was not saved')
219
- if any(record.get(key) != plan_['access'].get(key) for key in plan_['access']):
220
- raise RuntimeError('the copied permissions were not saved')
221
- held = {(g['kind'], g['oid'], g['role']) for g in grants_naming(username, _read_shares(st))}
222
- missing = [g for g in plan_['grants'] if (g['kind'], g['oid'], g['role']) not in held]
223
- if missing:
224
- raise RuntimeError(f'{len(missing)} shared items were not saved')
225
-
226
-
227
- def _cleanup(username, tenant, marker, st, granter, steps):
228
- """Undo every part of a copy. Returns the names of the parts that could not be removed."""
229
- leftovers = []
230
- for name, _do, undo in reversed(list(steps)):
231
- try:
232
- undo(username)
233
- except Exception: # noqa: BLE001
234
- leftovers.append(name)
235
- try:
236
- remove_grants(username, st=st, granter=granter)
237
- except Exception: # noqa: BLE001
238
- leftovers.append('shared items')
239
- try:
240
- users.delete_test_account(username, tenant=tenant, marker=marker)
241
- left = _fresh_registry().get(username)
242
- if is_test(left) and left.get(users.TEST_ACCOUNT_KEY) == marker:
243
- leftovers.append('sign in')
244
- except Exception: # noqa: BLE001
245
- leftovers.append('sign in')
246
- return leftovers
247
-
248
-
249
- def _cause(exc):
250
- text = str(exc).strip() or type(exc).__name__
251
- return text[:1].upper() + text[1:]
252
-
253
-
254
- def create(source, *, admin, tenant, st, steps=(), now=None):
255
- """Copy `source` into a new test account. Returns `{username, password, plan}`.
256
-
257
- `steps` are `(name, do(username, source), undo(username))` for grant stores outside core.
258
- Any failure removes what was written and raises `CopyFailed`.
259
- """
260
- tenant = str(tenant or '').strip().lower()
261
- admin = str(admin or '').strip().lower()
262
- for attempt in range(_RETRIES):
263
- plan_ = plan(source, _fresh_registry(), tenant=tenant, st=st)
264
- username = plan_['username']
265
- marker = {'copied_from': plan_['source'], 'created_by': admin,
266
- 'created_at': (now or _dt.datetime.now(_dt.timezone.utc)).isoformat(
267
- timespec='seconds')}
268
- password = _password(username, plan_['name'])
269
- try:
270
- users.create_test_account(username, password, name=plan_['name'],
271
- access=plan_['access'], marker=marker, tenant=tenant)
272
- except users.TestAccountExists:
273
- if attempt == _RETRIES - 1:
274
- raise CopyFailed('Another account took the test account name. Try again.')
275
- continue
276
- except Exception as exc: # noqa: BLE001
277
- # The write may have landed before it reported failure: clean up as for any step.
278
- leftovers = _cleanup(username, tenant, marker, st, admin, ())
279
- raise CopyFailed(_cause(exc), leftovers) from exc
280
- try:
281
- copy_grants(username, plan_['grants'], st=st, granter=admin)
282
- for _name, do, _undo in steps:
283
- do(username, plan_['source'])
284
- _confirm(username, plan_, marker, st)
285
- except Exception as exc: # noqa: BLE001
286
- leftovers = _cleanup(username, tenant, marker, st, admin, steps)
287
- raise CopyFailed(_cause(exc), leftovers) from exc
288
- return {'username': username, 'password': password, 'plan': plan_}
289
- raise CopyFailed('The test account could not be created. Try again.')
290
-
291
-
292
- def remove(username, *, tenant, st, granter, steps=()):
293
- """Delete a test account and every grant naming it. Refuses a real account."""
294
- tenant = str(tenant or '').strip().lower()
295
- uname = str(username or '').strip().lower()
296
- record = _fresh_registry().get(uname)
297
- if not isinstance(record, dict) or _tenant_of(record) != tenant:
298
- raise CopyRefused('no_such_user', f'No account named {uname or "(blank)"}.', 404)
299
- if not is_test(record):
300
- raise CopyRefused('not_a_test_account',
301
- 'This is a real account. Only a test account can be removed here.')
302
- leftovers = _cleanup(uname, tenant, record.get(users.TEST_ACCOUNT_KEY), st, granter, steps)
303
- if leftovers:
304
- raise CopyFailed('Some parts are still saved', leftovers)
305
-
306
-
307
- # ------------------------------------------------------------------ preview
308
- def _label(key, defs):
309
- import core.perms as perms
310
- import core.registry as registry
311
- definition = (defs or {}).get(key) or {}
312
- # The editor's surface rows (Assistant, Agents, AI apps (MCP)) are named by perms, not the registry.
313
- surfaces = {m['key']: m['label'] for m in perms.governable_surface_modules()}
314
- return str(definition.get('label') or (registry.BY_KEY.get(key) or {}).get('label')
315
- or surfaces.get(key) or key)
316
-
317
-
318
- def _plural(n, one, many):
319
- return f'{n} {one if n == 1 else many}'
320
-
321
-
322
- def preview(plan_, *, st=None, limits=None):
323
- """The contract body for GET .../test-account/preview, built from `plan`.
324
-
325
- `st` is a definitions store (`user_tables.lend_defs`) for the database lock checks.
326
- `limits` is `perm_scope.scope_limits` for the source; it names each row limit, business units included.
327
- """
328
- from core import user_tables
329
- access = plan_['access']
330
- try:
331
- defs = user_tables.all_defs(st=st, schema_only=True) or {}
332
- except Exception: # noqa: BLE001
333
- defs = {}
334
- block = access.get('perms') if isinstance(access.get('perms'), dict) else {}
335
- locks, reached = [], set()
336
- said = {}
337
- for limit in limits or ():
338
- text = str(limit.get('text') or '').strip().rstrip('.')
339
- if limit.get('database') and text:
340
- said[str(limit['database'])] = text
341
- for key in sorted(said):
342
- locks.append({'kind': 'row_scope', 'label': f'{_label(key, defs)}: {said[key]}'})
343
- for key, entry in sorted(block.items()):
344
- if not isinstance(entry, dict) or not entry.get('access', True):
345
- continue
346
- reached.add(key)
347
- if entry.get('filter') and key not in said:
348
- locks.append({'kind': 'row_scope', 'label': f'Rows are limited in {_label(key, defs)}'})
349
- hidden = [f for f in entry.get('hiddenFields') or () if f]
350
- if hidden:
351
- locks.append({'kind': 'field_wall',
352
- 'label': f'{_plural(len(hidden), "field is", "fields are")} hidden in '
353
- f'{_label(key, defs)}'})
354
- if not access.get('perms_v') and not said:
355
- bus = access.get('bus', 'all')
356
- if bus != 'all' and isinstance(bus, list):
357
- names = ', '.join(users.BU_LABELS.get(b, str(b)) for b in bus)
358
- locks.append({'kind': 'row_scope', 'label': f'Rows are limited to {names}'})
359
- if access.get('agent'):
360
- locks.append({'kind': 'row_scope', 'label': "Rows are limited to one salesperson's book"})
361
- for grant in plan_['grants']:
362
- if grant['kind'] == 'cohort':
363
- scope, _cid = shares.split_cohort_oid(grant['oid'])
364
- where = _label(scope, defs) if scope else 'a database'
365
- locks.append({'kind': 'locked_view', 'label': f'A locked view in {where}'})
366
- if grant['kind'] == 'database':
367
- reached.add(grant['oid'])
368
- for key in sorted(k for k in reached if str(k).startswith(user_tables.KEY_PREFIX)):
369
- definition = defs.get(key)
370
- if not isinstance(definition, dict):
371
- continue
372
- if not user_tables.records_mutable(key, st=st):
373
- locks.append({'kind': 'locked_database',
374
- 'label': f'{_label(key, defs)} is locked for record changes'})
375
- preset = [f for f in definition.get('fields') or ()
376
- if isinstance(f, dict) and isinstance(f.get('automation'), dict)
377
- and f['automation'].get('preset') is True
378
- and not user_tables.preset_editable(f)]
379
- if preset:
380
- locks.append({'kind': 'preset_field',
381
- 'label': f'{_plural(len(preset), "pre-set field", "pre-set fields")} '
382
- f'in {_label(key, defs)}'})
383
- grants = []
384
- for grant in plan_['grants']:
385
- row = dict(grant)
386
- if grant['kind'] == 'database':
387
- row['label'] = _label(grant['oid'], defs)
388
- grants.append(row)
389
- database_access = [{'key': key, 'label': _label(key, defs),
390
- 'access': bool(entry.get('access', True))}
391
- for key, entry in sorted(block.items()) if isinstance(entry, dict)]
392
- return {'source': {'username': plan_['source'], 'name': plan_['sourceName']},
393
- 'testUsername': plan_['username'],
394
- 'copy': {'role': access.get('role', 'user'), 'bus': access.get('bus', 'all'),
395
- 'buLabels': users.allowed_bus_labels(access),
396
- 'modules': access.get('modules', 'all'), 'agent': access.get('agent'),
397
- 'grants': grants, 'databaseAccess': database_access, 'locks': locks},
398
- 'counts': {'grants': len(grants), 'locks': len(locks)}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
web/src/apps/SkuListTools.tsx DELETED
@@ -1,85 +0,0 @@
1
- import { useMemo } from "react";
2
- import { Funnel, SortAscending } from "@phosphor-icons/react";
3
- import type { Field, FilterTree, SortSpec } from "../customer-grid/types";
4
- import { FilterBuilderPanel, Popover, SortBuilder, countConditions } from "../filter-kit";
5
- import type { FilterBuilderPanelProps } from "../filter-kit";
6
- import "../styles/sdlc81/sku-list-tools.css";
7
-
8
- export interface SkuListQuery {
9
- filters: FilterTree;
10
- sorts: SortSpec;
11
- }
12
-
13
- /** Use on query changes to preserve search and scope while restarting server paging. */
14
- export function skuListQueryParams(query: SkuListQuery, base?: URLSearchParams): URLSearchParams {
15
- const params = new URLSearchParams(base);
16
- params.set("filter", JSON.stringify(query.filters));
17
- params.set("sort", JSON.stringify(query.sorts));
18
- params.delete("cursor");
19
- params.set("offset", "0");
20
- return params;
21
- }
22
-
23
- export interface SkuListToolsProps {
24
- /** Authenticated column vocabulary. Never infer fields or choices from the loaded page. */
25
- fields: Field[];
26
- query: SkuListQuery;
27
- /** Refetch the server list with the new query. Rows and paging remain the host's responsibility. */
28
- onChange: (query: SkuListQuery) => void;
29
- /** Full matching server count. Null means no successful answer yet. */
30
- total: number | null;
31
- loading?: boolean;
32
- error?: boolean;
33
- canRead?: boolean;
34
- unavailable?: boolean;
35
- onRetry?: () => void;
36
- database?: string | null;
37
- statusValues?: FilterBuilderPanelProps["statusValues"];
38
- userOptions?: FilterBuilderPanelProps["userOptions"];
39
- today?: string;
40
- }
41
-
42
- /** Grid controls for a server list. This component accepts no record collection. */
43
- export function SkuListTools({
44
- fields, query, onChange, total, loading = false, error = false, canRead = true,
45
- unavailable = false, onRetry, database = null, statusValues, userOptions, today,
46
- }: SkuListToolsProps) {
47
- const fieldByKey = useMemo(() => new Map(fields.map((field) => [field.key, field])), [fields]);
48
- const filtersReady = fields.length > 0;
49
- const showCount = canRead && !unavailable && !loading && !error && total !== null;
50
- const message = !canRead ? "You do not have access to this list"
51
- : unavailable ? "Products are unavailable"
52
- : loading ? "Loading products"
53
- : error ? "Could not load products"
54
- : !filtersReady ? "Filters are unavailable"
55
- : total === 0 ? "No matches" : null;
56
-
57
- return (
58
- <div className="sku-list-tools" aria-busy={loading}>
59
- <div className="sku-list-tools__bar" role="toolbar" aria-label="Product list tools">
60
- {canRead && !unavailable && filtersReady && <>
61
- <Popover label="Filter" icon={<Funnel size={16} />} active={countConditions(query.filters.nodes) > 0}
62
- tone="filter" wide>
63
- {() => <FilterBuilderPanel fields={fields} filters={query.filters}
64
- onChange={(filters) => onChange({ ...query, filters })} database={database}
65
- statusValues={statusValues} userOptions={userOptions} today={today} />}
66
- </Popover>
67
- <Popover label="Sort" icon={<SortAscending size={16} />} active={query.sorts.length > 0}
68
- tone="sort" wide>
69
- {() => <SortBuilder fields={fields} fieldByKey={fieldByKey} sorts={query.sorts}
70
- onSorts={(sorts) => onChange({ ...query, sorts })} filters={query.filters}
71
- onFilters={(filters) => onChange({ ...query, filters })} grouped={false} />}
72
- </Popover>
73
- </>}
74
- <span className="sku-list-tools__count" role="status" aria-live="polite" aria-atomic="true">
75
- {showCount ? `${total.toLocaleString()} ${total === 1 ? "product" : "products"}` : ""}
76
- </span>
77
- </div>
78
- {message && <div className="sku-list-tools__message" role={error && canRead ? "alert" : "status"}>
79
- <span>{message}</span>
80
- {error && canRead && !unavailable && !loading && onRetry
81
- && <button type="button" className="sku-list-tools__retry" onClick={onRetry}>Retry</button>}
82
- </div>}
83
- </div>
84
- );
85
- }