@managesome/eposmatic-toolkit 0.4.4 → 0.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/esm/constants/REPORT_CALENDAR.js +47 -0
  2. package/esm/constants/index.js +1 -0
  3. package/esm/helpers/materialized-reports/index.js +2 -0
  4. package/esm/helpers/materialized-reports/materialized-reports-read-helpers.js +20 -21
  5. package/esm/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +281 -0
  6. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +473 -0
  7. package/esm/helpers/materialized-reports/tenant-sales-overview-formatter.js +126 -7
  8. package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +47 -1
  9. package/esm/helpers/order/generate-order.js +11 -0
  10. package/esm/helpers/store/common-order-actions.js +7 -3
  11. package/esm/helpers/types/index.js +1 -0
  12. package/esm/helpers/types/materialized-reports-apis.js +1 -1
  13. package/esm/helpers/types/report-calendar.js +1 -0
  14. package/lib/constants/REPORT_CALENDAR.d.ts +31 -0
  15. package/lib/constants/REPORT_CALENDAR.js +50 -0
  16. package/lib/constants/index.d.ts +1 -0
  17. package/lib/constants/index.js +1 -0
  18. package/lib/helpers/materialized-reports/index.d.ts +2 -0
  19. package/lib/helpers/materialized-reports/index.js +2 -0
  20. package/lib/helpers/materialized-reports/materialized-reports-read-helpers.d.ts +20 -21
  21. package/lib/helpers/materialized-reports/materialized-reports-read-helpers.js +20 -21
  22. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.d.ts +58 -0
  23. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +286 -0
  24. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +25 -0
  25. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +479 -0
  26. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.d.ts +24 -0
  27. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.js +127 -7
  28. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +47 -1
  29. package/lib/helpers/order/generate-order.d.ts +2 -0
  30. package/lib/helpers/order/generate-order.js +13 -0
  31. package/lib/helpers/store/common-order-actions.js +7 -3
  32. package/lib/helpers/types/index.d.ts +1 -0
  33. package/lib/helpers/types/index.js +1 -0
  34. package/lib/helpers/types/materialized-reports-apis.d.ts +117 -25
  35. package/lib/helpers/types/materialized-reports-apis.js +1 -0
  36. package/lib/helpers/types/report-calendar.d.ts +20 -0
  37. package/lib/helpers/types/report-calendar.js +2 -0
  38. package/lib/schemas/models/internal_invoices.d.ts +3 -3
  39. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +6 -6
  40. package/lib/schemas/models/orders.d.ts +692 -692
  41. package/lib/schemas/models/tenant_purchase_orders.d.ts +90 -90
  42. package/lib/schemas/models/tenants.d.ts +9 -9
  43. package/lib/schemas/models/warehouse_communications.d.ts +3 -3
  44. package/lib/schemas/models/warehouse_internal_movements.d.ts +48 -48
  45. package/lib/schemas/models/warehouse_purchase_order_rfqs.d.ts +63 -63
  46. package/lib/schemas/models/warehouse_purchase_orders.d.ts +96 -96
  47. package/lib/schemas/models/warehouse_sale_invoices.d.ts +3 -3
  48. package/lib/schemas/models/warehouse_sale_order_rfqs.d.ts +69 -69
  49. package/lib/schemas/models/warehouse_sale_orders.d.ts +45 -45
  50. package/lib/schemas/models/warehouses.d.ts +6 -6
  51. package/lib/tests/materialized-reports/materialized-reports-scope-group-helpers.test.d.ts +1 -0
  52. package/lib/tests/materialized-reports/materialized-reports-scope-group-helpers.test.js +275 -0
  53. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.d.ts +1 -0
  54. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.js +166 -0
  55. package/lib/tests/materialized-reports/tenant-sales-overview-formatter-aggregated.test.d.ts +1 -0
  56. package/lib/tests/materialized-reports/tenant-sales-overview-formatter-aggregated.test.js +143 -0
  57. package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +22 -0
  58. package/lib/tests/num-of-guests-limit.test.d.ts +1 -0
  59. package/lib/tests/num-of-guests-limit.test.js +76 -0
  60. package/package.json +1 -1
@@ -241,6 +241,50 @@ function finalizeMetrics(metrics) {
241
241
  }
242
242
  return finalized;
243
243
  }
244
+ /** True for a number that is zero (or broken beyond being distinguishable). */
245
+ const isZeroNumber = (value) => !value;
246
+ const isZeroPaymentMetric = (payment) => isZeroNumber(payment.count) &&
247
+ isZeroNumber(payment.gross_amount) &&
248
+ isZeroNumber(payment.amount_due) &&
249
+ isZeroNumber(payment.amount_paid) &&
250
+ isZeroNumber(payment.amount_refunded) &&
251
+ isZeroNumber(payment.card_charges);
252
+ const isZeroOrderReportMetric = (orders) => isZeroNumber(orders.count) &&
253
+ isZeroNumber(orders.gross_amount) &&
254
+ isZeroNumber(orders.discount_amount) &&
255
+ isZeroNumber(orders.discount_by_type.special_discount) &&
256
+ isZeroNumber(orders.discount_by_type.loyalty_discount) &&
257
+ isZeroNumber(orders.discount_by_type.voucher_discount) &&
258
+ isZeroNumber(orders.gst_amount) &&
259
+ isZeroNumber(orders.tax_amount) &&
260
+ isZeroNumber(orders.tip_amount) &&
261
+ isZeroNumber(orders.tax_invoice_provider_charges) &&
262
+ isZeroNumber(orders.delivery_charges);
263
+ /**
264
+ * Reconcile-time cleanup: subtracting a day can zero a key out while leaving
265
+ * it behind — a "zombie" key that would make accumulated period documents
266
+ * diverge from day / hour reads (which simply lose the key). Compile output
267
+ * never carries all-zero keys, so pruning only removes reconcile artifacts.
268
+ * A key whose payments are all zero-valued counts as empty (subtraction can
269
+ * leave zero-count payment entries behind).
270
+ */
271
+ const pruneZeroReportKeys = (metrics) => {
272
+ const pruned = {};
273
+ for (const [key, metric] of Object.entries(metrics)) {
274
+ const totalSales = metric[REPORT_SALE_TYPE_ENUM.TOTAL_SALES];
275
+ const amountCollected = metric[REPORT_SALE_TYPE_ENUM.AMOUNT_COLLECTED];
276
+ const creditBills = metric[REPORT_SALE_TYPE_ENUM.CREDIT_BILLS];
277
+ const isZero = isZeroOrderReportMetric(totalSales.orders) &&
278
+ totalSales.payments.every(isZeroPaymentMetric) &&
279
+ isZeroOrderReportMetric(amountCollected.orders) &&
280
+ (amountCollected.payments ?? []).every(isZeroPaymentMetric) &&
281
+ creditBills.issued.bills.length === 0 &&
282
+ creditBills.received.bills.length === 0;
283
+ if (!isZero)
284
+ pruned[key] = metric;
285
+ }
286
+ return pruned;
287
+ };
244
288
  /**
245
289
  * Deep plain-value conversion for mongoose-hydrated values: subdocuments
246
290
  * (`toObject()`), nested `Map`s and document arrays. Plain JSON values are
@@ -862,7 +906,9 @@ export function updateMaterializedTenantSalesOverviewReport(existing, updates, o
862
906
  }
863
907
  return {
864
908
  ...existing,
865
- sale_type_metrics: finalizeMetrics(metrics),
909
+ // Reconcile artifacts: a fully-subtracted key is pruned so accumulated
910
+ // period documents keep the same key set as day / hour reads.
911
+ sale_type_metrics: pruneZeroReportKeys(finalizeMetrics(metrics)),
866
912
  // Version metric writes AND ledger-changing received-payment patches (both
867
913
  // append a history row via the caller). No-op patches bump nothing, so
868
914
  // replays stay churn-free.
@@ -1,5 +1,15 @@
1
1
  import Mongoose from 'mongoose';
2
2
  import { PAYMENT_STATUS_ENUM, PAYMENT_TYPE_ENUM } from '../../constants';
3
+ // num_of_guests is persisted as a BSON int32 (orders collection validator: bsonType 'int', minimum 1).
4
+ // Corrupt client values (e.g. 3257330333) serialize as a double and make the whole order write fail
5
+ // document validation — clamp to a sane range on every normalization pass.
6
+ export const MAX_NUM_OF_GUESTS = 1000;
7
+ export function getValidNumOfGuests(num_of_guests) {
8
+ const numOfGuests = Number(num_of_guests);
9
+ if (!Number.isFinite(numOfGuests) || numOfGuests < 1)
10
+ return 1;
11
+ return Math.min(Math.floor(numOfGuests), MAX_NUM_OF_GUESTS);
12
+ }
3
13
  export function generateOrderDineinDetails(dinein_details) {
4
14
  if (!dinein_details)
5
15
  return null;
@@ -7,6 +17,7 @@ export function generateOrderDineinDetails(dinein_details) {
7
17
  ...dinein_details,
8
18
  floor_id: new Mongoose.Types.ObjectId(dinein_details.floor_id),
9
19
  table_id: new Mongoose.Types.ObjectId(dinein_details.table_id),
20
+ num_of_guests: getValidNumOfGuests(dinein_details.num_of_guests),
10
21
  reservation_time: new Date(dinein_details.reservation_time),
11
22
  start_time: new Date(dinein_details.start_time),
12
23
  };
@@ -1,5 +1,6 @@
1
1
  import ObjectID from 'bson-objectid';
2
2
  import { ORDER_TYPE_ENUM, SPLIT_BILL_TYPE_ENUM } from '../../constants';
3
+ import { getValidNumOfGuests } from '../order/generate-order';
3
4
  import { convertPriceFromCents, convertPriceToCents, parseNum } from '../utils';
4
5
  import { calculateCartOrderInfoAndSyncData, getCartItemsObj } from './cart-actions';
5
6
  /* ======================>Order Object<====================== */
@@ -177,7 +178,7 @@ export function getOrderWithPrice(order, toCents) {
177
178
  ? {
178
179
  dinein_details: {
179
180
  ...order.dinein_details,
180
- num_of_guests: order.dinein_details.num_of_guests || 1,
181
+ num_of_guests: getValidNumOfGuests(order.dinein_details.num_of_guests),
181
182
  },
182
183
  }
183
184
  : {}),
@@ -235,9 +236,12 @@ function getCommonOptionalBoolFields(order, partial) {
235
236
  }
236
237
  function getCommonOptionalNullFields(order, partial) {
237
238
  const defaultValue = partial ? undefined : null;
239
+ const dineinDetails = order['dinein_details'];
238
240
  return {
239
241
  delivery_details: order['delivery_details'] || defaultValue,
240
- dinein_details: order['dinein_details'] || defaultValue,
242
+ dinein_details: dineinDetails != null
243
+ ? { ...dineinDetails, num_of_guests: getValidNumOfGuests(dineinDetails.num_of_guests) }
244
+ : defaultValue,
241
245
  takeaway_details: order['takeaway_details'] || defaultValue,
242
246
  staff_details: order['staff_details'] || defaultValue,
243
247
  order_taker_details: order['order_taker_details'] || defaultValue,
@@ -509,7 +513,7 @@ function syncDineinCoursesOnSplit(dinein_details, swapedItemIdsMap, totalChecks,
509
513
  }
510
514
  const result = {
511
515
  ...dinein_details,
512
- num_of_guests,
516
+ num_of_guests: getValidNumOfGuests(num_of_guests),
513
517
  courses: newCourses,
514
518
  };
515
519
  return result;
@@ -18,6 +18,7 @@ export * from './marketing';
18
18
  export * from './materialized-reports-apis';
19
19
  export * from './order-engine';
20
20
  export * from './pra-generate-invoice';
21
+ export * from './report-calendar';
21
22
  export * from './report-webview';
22
23
  export * from './reports';
23
24
  export * from './smartpay-apis';
@@ -1 +1 @@
1
- export {};
1
+ import { REPORT_SALE_TYPE_ENUM } from '../../constants';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,31 @@
1
+ export declare enum CALENDAR_MODE_ENUM {
2
+ LIVE = "LIVE",
3
+ HISTORICAL = "HISTORICAL"
4
+ }
5
+ export declare const ALL_CALENDAR_MODE_ENUMS: CALENDAR_MODE_ENUM[];
6
+ export declare enum CALENDAR_VIEW_MODE_ENUM {
7
+ DATE_RANGE = "DATE_RANGE",
8
+ MULTI_DATE = "MULTI_DATE",
9
+ DATE_RANGE_TIME_WINDOW = "DATE_RANGE_TIME_WINDOW",
10
+ WEEK = "WEEK",
11
+ DIFFERENT_WEEKS = "DIFFERENT_WEEKS",
12
+ WEEK_OF_YEAR = "WEEK_OF_YEAR",
13
+ DIFFERENT_WEEKS_OF_YEAR = "DIFFERENT_WEEKS_OF_YEAR",
14
+ MONTH = "MONTH",
15
+ DIFFERENT_MONTHS = "DIFFERENT_MONTHS",
16
+ QUARTER = "QUARTER",
17
+ DIFFERENT_QUARTERS = "DIFFERENT_QUARTERS",
18
+ SEMESTER = "SEMESTER",
19
+ DIFFERENT_SEMESTERS = "DIFFERENT_SEMESTERS",
20
+ YEAR = "YEAR",
21
+ DIFFERENT_YEARS = "DIFFERENT_YEARS"
22
+ }
23
+ export declare const ALL_CALENDAR_VIEW_MODE_ENUMS: CALENDAR_VIEW_MODE_ENUM[];
24
+ /**
25
+ * Version of the `IReportCalendarSelection` spec carried by a materialized
26
+ * report READ body (`selection.spec_version`). Bump on any meaning-changing
27
+ * reshape of the selection model — the read API rejects unknown versions
28
+ * with a 400, so rolling deploys (old frontend → new backend) fail loudly
29
+ * instead of decoding a stale shape.
30
+ */
31
+ export declare const MATERIALIZED_REPORT_SELECTION_SPEC_VERSION = 1;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ // Report Calendar selection model constants (see the Report Calendar
3
+ // concept doc §5). The calendar produces an `IReportCalendarSelection`;
4
+ // every materialized report read API decodes it server-side with the
5
+ // toolkit's `buildMaterializedReportScopeGroupsFromSelection` — the
6
+ // selection model is the SINGLE shared contract between the calendar UI and
7
+ // the read services.
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.MATERIALIZED_REPORT_SELECTION_SPEC_VERSION = exports.ALL_CALENDAR_VIEW_MODE_ENUMS = exports.CALENDAR_VIEW_MODE_ENUM = exports.ALL_CALENDAR_MODE_ENUMS = exports.CALENDAR_MODE_ENUM = void 0;
10
+ var CALENDAR_MODE_ENUM;
11
+ (function (CALENDAR_MODE_ENUM) {
12
+ CALENDAR_MODE_ENUM["LIVE"] = "LIVE";
13
+ CALENDAR_MODE_ENUM["HISTORICAL"] = "HISTORICAL";
14
+ })(CALENDAR_MODE_ENUM || (exports.CALENDAR_MODE_ENUM = CALENDAR_MODE_ENUM = {}));
15
+ exports.ALL_CALENDAR_MODE_ENUMS = Object.values(CALENDAR_MODE_ENUM);
16
+ // Historical selection modes (doc §4.2) — default = DATE_RANGE.
17
+ // Every materialized scope context is selectable: day/hour ranges, calendar
18
+ // weeks (Sun–Sat), ISO weeks of year (Mon–Sun), months, quarters, semesters
19
+ // and years — contiguous ranges plus discrete (DIFFERENT_*) picks.
20
+ // Two refinements apply to ANY historical view (they are modifiers, not
21
+ // modes): the time-of-day window (hour-level, own `localHour`
22
+ // materialization) and the weekday filter (`day_index`, applied by the
23
+ // decoder).
24
+ var CALENDAR_VIEW_MODE_ENUM;
25
+ (function (CALENDAR_VIEW_MODE_ENUM) {
26
+ CALENDAR_VIEW_MODE_ENUM["DATE_RANGE"] = "DATE_RANGE";
27
+ CALENDAR_VIEW_MODE_ENUM["MULTI_DATE"] = "MULTI_DATE";
28
+ CALENDAR_VIEW_MODE_ENUM["DATE_RANGE_TIME_WINDOW"] = "DATE_RANGE_TIME_WINDOW";
29
+ CALENDAR_VIEW_MODE_ENUM["WEEK"] = "WEEK";
30
+ CALENDAR_VIEW_MODE_ENUM["DIFFERENT_WEEKS"] = "DIFFERENT_WEEKS";
31
+ CALENDAR_VIEW_MODE_ENUM["WEEK_OF_YEAR"] = "WEEK_OF_YEAR";
32
+ CALENDAR_VIEW_MODE_ENUM["DIFFERENT_WEEKS_OF_YEAR"] = "DIFFERENT_WEEKS_OF_YEAR";
33
+ CALENDAR_VIEW_MODE_ENUM["MONTH"] = "MONTH";
34
+ CALENDAR_VIEW_MODE_ENUM["DIFFERENT_MONTHS"] = "DIFFERENT_MONTHS";
35
+ CALENDAR_VIEW_MODE_ENUM["QUARTER"] = "QUARTER";
36
+ CALENDAR_VIEW_MODE_ENUM["DIFFERENT_QUARTERS"] = "DIFFERENT_QUARTERS";
37
+ CALENDAR_VIEW_MODE_ENUM["SEMESTER"] = "SEMESTER";
38
+ CALENDAR_VIEW_MODE_ENUM["DIFFERENT_SEMESTERS"] = "DIFFERENT_SEMESTERS";
39
+ CALENDAR_VIEW_MODE_ENUM["YEAR"] = "YEAR";
40
+ CALENDAR_VIEW_MODE_ENUM["DIFFERENT_YEARS"] = "DIFFERENT_YEARS";
41
+ })(CALENDAR_VIEW_MODE_ENUM || (exports.CALENDAR_VIEW_MODE_ENUM = CALENDAR_VIEW_MODE_ENUM = {}));
42
+ exports.ALL_CALENDAR_VIEW_MODE_ENUMS = Object.values(CALENDAR_VIEW_MODE_ENUM);
43
+ /**
44
+ * Version of the `IReportCalendarSelection` spec carried by a materialized
45
+ * report READ body (`selection.spec_version`). Bump on any meaning-changing
46
+ * reshape of the selection model — the read API rejects unknown versions
47
+ * with a 400, so rolling deploys (old frontend → new backend) fail loudly
48
+ * instead of decoding a stale shape.
49
+ */
50
+ exports.MATERIALIZED_REPORT_SELECTION_SPEC_VERSION = 1;
@@ -11,6 +11,7 @@ export * from './MATERIALIZED_REPORTS';
11
11
  export * from './NOTIFICATIONS';
12
12
  export * from './ORDER';
13
13
  export * from './PRA_ENUMS';
14
+ export * from './REPORT_CALENDAR';
14
15
  export * from './REPORT_WEBVIEW';
15
16
  export * from './RFQ_GO';
16
17
  export * from './SMARTPAY_ENUMS';
@@ -27,6 +27,7 @@ __exportStar(require("./MATERIALIZED_REPORTS"), exports);
27
27
  __exportStar(require("./NOTIFICATIONS"), exports);
28
28
  __exportStar(require("./ORDER"), exports);
29
29
  __exportStar(require("./PRA_ENUMS"), exports);
30
+ __exportStar(require("./REPORT_CALENDAR"), exports);
30
31
  __exportStar(require("./REPORT_WEBVIEW"), exports);
31
32
  __exportStar(require("./RFQ_GO"), exports);
32
33
  __exportStar(require("./SMARTPAY_ENUMS"), exports);
@@ -1,5 +1,7 @@
1
1
  export * from './key-helpers';
2
2
  export * from './materialized-reports-read-helpers';
3
+ export * from './materialized-reports-scope-group-helpers';
4
+ export * from './materialized-reports-selection-helpers';
3
5
  export * from './materialized-tenant-reports-iterations-helpers';
4
6
  export * from './scope-helpers';
5
7
  export * from './tenant-sales-overview-close-day-helpers';
@@ -16,6 +16,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./key-helpers"), exports);
18
18
  __exportStar(require("./materialized-reports-read-helpers"), exports);
19
+ __exportStar(require("./materialized-reports-scope-group-helpers"), exports);
20
+ __exportStar(require("./materialized-reports-selection-helpers"), exports);
19
21
  __exportStar(require("./materialized-tenant-reports-iterations-helpers"), exports);
20
22
  __exportStar(require("./scope-helpers"), exports);
21
23
  __exportStar(require("./tenant-sales-overview-close-day-helpers"), exports);
@@ -4,15 +4,16 @@ import { IMaterializedTenantSalesOverviewReport } from '../../schemas/types/mate
4
4
  * Read-side helpers for materialized-report documents — the frontend-facing
5
5
  * half of the query story (see `MATERIALIZED_REPORTS.md` "Time scopes").
6
6
  *
7
- * The ops-service read API takes `{ scope_context, scope_field | from/to }`:
8
- * - contiguous selections go through `from`/`to` directly (callers can use
9
- * `getMaterializedScopeSpan` / `getMaterializedHourScopeSpan` to derive the
10
- * span of a set of keys);
11
- * - discrete selections fetch the min..max SPAN and keep the exact keys with
12
- * `filterMaterializedTenantSalesOverviewReportsByScopeFields`;
7
+ * The ops-service read API takes `{ scope_context, scope_field }` where
8
+ * `scope_field` is an EXACT value or an `$in` list of exact values — the
9
+ * request itself never carries ranges:
10
+ * - every selection (contiguous periods, discrete picks, hour windows,
11
+ * weekday filters) is compiled by the caller into the exact value list it
12
+ * wants;
13
13
  * - recurring hour windows (e.g. 6–8pm on every day of a range) expand to the
14
- * exact hour labels with `getMaterializedHourWindowScopeFields` (span-fetch
15
- * the full days first, then filter to the labels).
14
+ * exact hour labels with `getMaterializedHourWindowScopeFields`;
15
+ * - `getMaterializedScopeSpan` / `getMaterializedHourScopeSpan` remain as
16
+ * min..max bounds utilities for key sets (not part of the read path).
16
17
  *
17
18
  * Validation convention: helpers return `null` instead of throwing — `null`
18
19
  * means the caller passed invalid data.
@@ -46,12 +47,9 @@ export declare function getMaterializedBusinessHourIndex(hour: number): number |
46
47
  */
47
48
  export declare function getMaterializedHourWindowScopeFields(businessDays: string[], fromHour: number, toHour: number): string[] | null;
48
49
  /**
49
- * Full-day span of a list of business days for `localHour` reads: the
50
- * inclusive `{ from: '<first> 00', to: '<last> 23' }` range covers ALL 24
51
- * hour buckets of every label day in between (a label day's buckets are
52
- * `'D 00'`…`'D 23'`, spanning the post-midnight hours too). Filter the
53
- * fetched documents afterwards with `getMaterializedHourWindowScopeFields` /
54
- * `filterMaterializedTenantSalesOverviewReportsByScopeFields`.
50
+ * Min..max `localHour` bounds of a list of business days:
51
+ * `{ from: '<first> 00', to: '<last> 23' }`. A bounds utility for key sets —
52
+ * the READ API takes exact values / `$in` lists, never ranges.
55
53
  *
56
54
  * Returns `null` (never throws) for a malformed list.
57
55
  */
@@ -60,11 +58,11 @@ export declare function getMaterializedHourScopeSpan(businessDays: string[]): {
60
58
  to: string;
61
59
  } | null;
62
60
  /**
63
- * Inclusive min..max span of exact non-hour scope values — the fetch range
64
- * for discrete selections (all non-hour scope fields are fixed-width, so
65
- * lexicographic order equals chronological order). Returns `null` (never
66
- * throws) for the hour scope (use `getMaterializedHourScopeSpan`) or a
67
- * malformed list.
61
+ * Inclusive min..max bounds of exact non-hour scope values (all non-hour
62
+ * scope fields are fixed-width, so lexicographic order equals chronological
63
+ * order). A bounds utility for key sets — the READ API takes exact values /
64
+ * `$in` lists, never ranges. Returns `null` (never throws) for the hour
65
+ * scope or a malformed list.
68
66
  */
69
67
  export declare function getMaterializedScopeSpan(scope_context: MATERIALIZED_REPORT_SCOPE_ENUM, scopeFields: string[]): {
70
68
  from: string;
@@ -72,8 +70,9 @@ export declare function getMaterializedScopeSpan(scope_context: MATERIALIZED_REP
72
70
  } | null;
73
71
  /**
74
72
  * Keeps only the documents whose `scope_field` is one of the requested keys —
75
- * the client-side refinement for discrete selections fetched as a span.
76
- * Returns `null` (never throws) for malformed input; unknown keys simply match
73
+ * a client-side refinement utility for arbitrary document sets (the read API
74
+ * returns exactly the requested values, so it needs no refinement). Returns
75
+ * `null` (never throws) for malformed input; unknown keys simply match
77
76
  * nothing (absence = zero).
78
77
  */
79
78
  export declare function filterMaterializedTenantSalesOverviewReportsByScopeFields(docs: IMaterializedTenantSalesOverviewReport[], scopeFields: string[]): IMaterializedTenantSalesOverviewReport[] | null;
@@ -11,15 +11,16 @@ const scope_helpers_1 = require("./scope-helpers");
11
11
  * Read-side helpers for materialized-report documents — the frontend-facing
12
12
  * half of the query story (see `MATERIALIZED_REPORTS.md` "Time scopes").
13
13
  *
14
- * The ops-service read API takes `{ scope_context, scope_field | from/to }`:
15
- * - contiguous selections go through `from`/`to` directly (callers can use
16
- * `getMaterializedScopeSpan` / `getMaterializedHourScopeSpan` to derive the
17
- * span of a set of keys);
18
- * - discrete selections fetch the min..max SPAN and keep the exact keys with
19
- * `filterMaterializedTenantSalesOverviewReportsByScopeFields`;
14
+ * The ops-service read API takes `{ scope_context, scope_field }` where
15
+ * `scope_field` is an EXACT value or an `$in` list of exact values — the
16
+ * request itself never carries ranges:
17
+ * - every selection (contiguous periods, discrete picks, hour windows,
18
+ * weekday filters) is compiled by the caller into the exact value list it
19
+ * wants;
20
20
  * - recurring hour windows (e.g. 6–8pm on every day of a range) expand to the
21
- * exact hour labels with `getMaterializedHourWindowScopeFields` (span-fetch
22
- * the full days first, then filter to the labels).
21
+ * exact hour labels with `getMaterializedHourWindowScopeFields`;
22
+ * - `getMaterializedScopeSpan` / `getMaterializedHourScopeSpan` remain as
23
+ * min..max bounds utilities for key sets (not part of the read path).
23
24
  *
24
25
  * Validation convention: helpers return `null` instead of throwing — `null`
25
26
  * means the caller passed invalid data.
@@ -101,12 +102,9 @@ function getMaterializedHourWindowScopeFields(businessDays, fromHour, toHour) {
101
102
  return labels;
102
103
  }
103
104
  /**
104
- * Full-day span of a list of business days for `localHour` reads: the
105
- * inclusive `{ from: '<first> 00', to: '<last> 23' }` range covers ALL 24
106
- * hour buckets of every label day in between (a label day's buckets are
107
- * `'D 00'`…`'D 23'`, spanning the post-midnight hours too). Filter the
108
- * fetched documents afterwards with `getMaterializedHourWindowScopeFields` /
109
- * `filterMaterializedTenantSalesOverviewReportsByScopeFields`.
105
+ * Min..max `localHour` bounds of a list of business days:
106
+ * `{ from: '<first> 00', to: '<last> 23' }`. A bounds utility for key sets —
107
+ * the READ API takes exact values / `$in` lists, never ranges.
110
108
  *
111
109
  * Returns `null` (never throws) for a malformed list.
112
110
  */
@@ -123,11 +121,11 @@ function getMaterializedHourScopeSpan(businessDays) {
123
121
  return { from: `${sorted[0]} 00`, to: `${sorted[sorted.length - 1]} 23` };
124
122
  }
125
123
  /**
126
- * Inclusive min..max span of exact non-hour scope values — the fetch range
127
- * for discrete selections (all non-hour scope fields are fixed-width, so
128
- * lexicographic order equals chronological order). Returns `null` (never
129
- * throws) for the hour scope (use `getMaterializedHourScopeSpan`) or a
130
- * malformed list.
124
+ * Inclusive min..max bounds of exact non-hour scope values (all non-hour
125
+ * scope fields are fixed-width, so lexicographic order equals chronological
126
+ * order). A bounds utility for key sets — the READ API takes exact values /
127
+ * `$in` lists, never ranges. Returns `null` (never throws) for the hour
128
+ * scope or a malformed list.
131
129
  */
132
130
  function getMaterializedScopeSpan(scope_context, scopeFields) {
133
131
  if (scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR) {
@@ -146,8 +144,9 @@ function getMaterializedScopeSpan(scope_context, scopeFields) {
146
144
  }
147
145
  /**
148
146
  * Keeps only the documents whose `scope_field` is one of the requested keys —
149
- * the client-side refinement for discrete selections fetched as a span.
150
- * Returns `null` (never throws) for malformed input; unknown keys simply match
147
+ * a client-side refinement utility for arbitrary document sets (the read API
148
+ * returns exactly the requested values, so it needs no refinement). Returns
149
+ * `null` (never throws) for malformed input; unknown keys simply match
151
150
  * nothing (absence = zero).
152
151
  */
153
152
  function filterMaterializedTenantSalesOverviewReportsByScopeFields(docs, scopeFields) {
@@ -0,0 +1,58 @@
1
+ import { IGetMaterializedReportScopeGroup } from '../types/materialized-reports-apis';
2
+ /**
3
+ * Smart scope-group compiler for materialized-report reads — turns a
4
+ * selection into the FEWEST exact-scope groups the read needs
5
+ * (`IGetMaterializedReportScopeGroup[]`, the `scope_groups` body field):
6
+ *
7
+ * - **compaction**: a contiguous day set is folded into the largest EXACT
8
+ * periods (year → semester → quarter → month → week) before falling back
9
+ * to `localDate` values — e.g. Sep 1–30 becomes ONE `localMonth: ['2026-09']`
10
+ * instead of 30 day values (one document instead of 30);
11
+ * - **recurring window**: the same hours on every selected day → a single
12
+ * `localHour` group (the dedicated time-of-day-window read);
13
+ * - **span window**: from `x:00` on the first day CONTINUOUSLY through
14
+ * `y:00` on the last day — boundary days carry only their partial hour
15
+ * labels (business-day continuous: the start day runs to its 06:00
16
+ * boundary including the post-midnight tail, the end day starts at 06:00),
17
+ * the full days in between compact through the same period logic. What the
18
+ * span does NOT read (absent documents) counts as zero, and groups NEVER
19
+ * overlap — the backend simply unions them.
20
+ *
21
+ * `compactMaterializedReportDayKeys` is exported for direct testing.
22
+ *
23
+ * Returns `null` (never throws) for malformed input — `null` means the
24
+ * caller passed invalid data.
25
+ */
26
+ /** Window refinement kinds (see the module docblock). */
27
+ export declare const MATERIALIZED_REPORT_WINDOW_KIND: {
28
+ readonly RECURRING: "RECURRING";
29
+ readonly SPAN: "SPAN";
30
+ };
31
+ export type TMaterializedReportWindowKind = (typeof MATERIALIZED_REPORT_WINDOW_KIND)[keyof typeof MATERIALIZED_REPORT_WINDOW_KIND];
32
+ export interface IMaterializedReportScopeSelectionSpec {
33
+ /**
34
+ * Effective business-day keys ('YYYY-MM-DD'), sorted or not — deduped
35
+ * here. A SPAN window reads the CONTINUOUS interval first-day →
36
+ * last-day: non-contiguous days would silently read the gap days as
37
+ * full middle days, so a gapped set returns `null`.
38
+ */
39
+ days: string[];
40
+ /** Optional hour window refinement. `toHour` is end-EXCLUSIVE (0 = midnight). */
41
+ window?: {
42
+ fromHour: number;
43
+ toHour: number;
44
+ kind: TMaterializedReportWindowKind;
45
+ } | null;
46
+ }
47
+ /**
48
+ * Folds a contiguous day set into the largest EXACT periods (year →
49
+ * semester → quarter → month → week) + `localDate` leftovers — never
50
+ * partial coverage: a period compacts only when the WHOLE period is inside
51
+ * a contiguous run. Returns `null` for malformed/empty input.
52
+ */
53
+ export declare function compactMaterializedReportDayKeys(days: string[]): IGetMaterializedReportScopeGroup[] | null;
54
+ /**
55
+ * Builds the read's scope groups from a selection spec (see the module
56
+ * docblock). Returns `null` for malformed input or an unservable window.
57
+ */
58
+ export declare function buildMaterializedReportScopeGroups(spec: IMaterializedReportScopeSelectionSpec): IGetMaterializedReportScopeGroup[] | null;