@managesome/eposmatic-toolkit 0.4.54 → 0.4.56

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 (84) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +91 -0
  2. package/esm/helpers/materialized-reports/index.js +7 -0
  3. package/esm/helpers/materialized-reports/materialized-delivery-wipe-targets.js +18 -0
  4. package/esm/helpers/materialized-reports/materialized-dialer-accumulator-helpers.js +100 -0
  5. package/esm/helpers/materialized-reports/materialized-dialer-close-day-helpers.js +328 -0
  6. package/esm/helpers/materialized-reports/materialized-dialer-compile-helpers.js +180 -0
  7. package/esm/helpers/materialized-reports/materialized-dialer-entry-pages.js +222 -0
  8. package/esm/helpers/materialized-reports/materialized-dialer-key-helpers.js +82 -0
  9. package/esm/helpers/materialized-reports/materialized-dialer-masking.js +54 -0
  10. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
  11. package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +13 -1
  12. package/esm/helpers/materialized-reports/report-platform/definition.js +85 -1
  13. package/esm/helpers/materialized-reports/tenant-dialer-formatter.js +95 -0
  14. package/esm/helpers/reporting-contract/report-contract.js +3 -0
  15. package/esm/schemas/models/index.js +3 -0
  16. package/esm/schemas/models/materialized_report_drilldown_rows.js +1 -0
  17. package/esm/schemas/models/materialized_tenant_dialer_entries.js +65 -0
  18. package/esm/schemas/models/materialized_tenant_dialer_reports.js +66 -0
  19. package/esm/schemas/models/tenant_dialer_events.js +133 -0
  20. package/esm/schemas/types/index.js +3 -0
  21. package/esm/schemas/types/materialized_report_drilldown_rows.js +1 -0
  22. package/esm/schemas/types/materialized_tenant_dialer_entries.js +1 -0
  23. package/esm/schemas/types/materialized_tenant_dialer_reports.js +7 -0
  24. package/esm/schemas/types/tenant_dialer_events.js +1 -0
  25. package/lib/constants/MATERIALIZED_REPORTS.d.ts +72 -2
  26. package/lib/constants/MATERIALIZED_REPORTS.js +93 -1
  27. package/lib/helpers/materialized-reports/index.d.ts +7 -0
  28. package/lib/helpers/materialized-reports/index.js +7 -0
  29. package/lib/helpers/materialized-reports/materialized-delivery-wipe-targets.d.ts +1 -0
  30. package/lib/helpers/materialized-reports/materialized-delivery-wipe-targets.js +19 -0
  31. package/lib/helpers/materialized-reports/materialized-dialer-accumulator-helpers.d.ts +39 -0
  32. package/lib/helpers/materialized-reports/materialized-dialer-accumulator-helpers.js +110 -0
  33. package/lib/helpers/materialized-reports/materialized-dialer-close-day-helpers.d.ts +90 -0
  34. package/lib/helpers/materialized-reports/materialized-dialer-close-day-helpers.js +340 -0
  35. package/lib/helpers/materialized-reports/materialized-dialer-compile-helpers.d.ts +67 -0
  36. package/lib/helpers/materialized-reports/materialized-dialer-compile-helpers.js +183 -0
  37. package/lib/helpers/materialized-reports/materialized-dialer-entry-pages.d.ts +97 -0
  38. package/lib/helpers/materialized-reports/materialized-dialer-entry-pages.js +235 -0
  39. package/lib/helpers/materialized-reports/materialized-dialer-key-helpers.d.ts +51 -0
  40. package/lib/helpers/materialized-reports/materialized-dialer-key-helpers.js +90 -0
  41. package/lib/helpers/materialized-reports/materialized-dialer-masking.d.ts +35 -0
  42. package/lib/helpers/materialized-reports/materialized-dialer-masking.js +57 -0
  43. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
  44. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +13 -1
  45. package/lib/helpers/materialized-reports/report-platform/definition.js +84 -0
  46. package/lib/helpers/materialized-reports/tenant-dialer-formatter.d.ts +91 -0
  47. package/lib/helpers/materialized-reports/tenant-dialer-formatter.js +98 -0
  48. package/lib/helpers/reporting-contract/report-contract.d.ts +1 -0
  49. package/lib/helpers/reporting-contract/report-contract.js +3 -0
  50. package/lib/schemas/models/index.d.ts +3 -0
  51. package/lib/schemas/models/index.js +3 -0
  52. package/lib/schemas/models/materialized_report_drilldown_rows.js +1 -0
  53. package/lib/schemas/models/materialized_tenant_dialer_entries.d.ts +107 -0
  54. package/lib/schemas/models/materialized_tenant_dialer_entries.js +68 -0
  55. package/lib/schemas/models/materialized_tenant_dialer_reports.d.ts +100 -0
  56. package/lib/schemas/models/materialized_tenant_dialer_reports.js +69 -0
  57. package/lib/schemas/models/tenant-activity-logs.d.ts +3 -3
  58. package/lib/schemas/models/tenant_dialer_events.d.ts +130 -0
  59. package/lib/schemas/models/tenant_dialer_events.js +136 -0
  60. package/lib/schemas/models/warehouse-activity-logs.d.ts +3 -3
  61. package/lib/schemas/types/index.d.ts +3 -0
  62. package/lib/schemas/types/index.js +3 -0
  63. package/lib/schemas/types/materialized_report_drilldown_rows.d.ts +1 -1
  64. package/lib/schemas/types/materialized_report_drilldown_rows.js +1 -0
  65. package/lib/schemas/types/materialized_tenant_dialer_entries.d.ts +83 -0
  66. package/lib/schemas/types/materialized_tenant_dialer_entries.js +2 -0
  67. package/lib/schemas/types/materialized_tenant_dialer_reports.d.ts +77 -0
  68. package/lib/schemas/types/materialized_tenant_dialer_reports.js +10 -0
  69. package/lib/schemas/types/tenant_dialer_events.d.ts +35 -0
  70. package/lib/schemas/types/tenant_dialer_events.js +2 -0
  71. package/lib/tests/materialized-reports/materialized-dialer-compile.test.d.ts +1 -0
  72. package/lib/tests/materialized-reports/materialized-dialer-compile.test.js +213 -0
  73. package/lib/tests/materialized-reports/materialized-dialer-formatter.test.d.ts +1 -0
  74. package/lib/tests/materialized-reports/materialized-dialer-formatter.test.js +159 -0
  75. package/lib/tests/materialized-reports/materialized-dialer-plan.test.d.ts +1 -0
  76. package/lib/tests/materialized-reports/materialized-dialer-plan.test.js +223 -0
  77. package/lib/tests/materialized-reports/materialized-tenant-dialer-schema.test.d.ts +1 -0
  78. package/lib/tests/materialized-reports/materialized-tenant-dialer-schema.test.js +183 -0
  79. package/lib/tests/materialized-reports/materialized-wipe-tooling.test.d.ts +1 -0
  80. package/lib/tests/materialized-reports/materialized-wipe-tooling.test.js +37 -0
  81. package/lib/tests/materialized-reports/report-platform/registry-conformance.test.js +16 -3
  82. package/lib/tests/tenant-dialer-events-schema.test.d.ts +1 -0
  83. package/lib/tests/tenant-dialer-events-schema.test.js +191 -0
  84. package/package.json +1 -1
@@ -0,0 +1,97 @@
1
+ import { MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
2
+ import { IMaterializedTenantDialerEntry } from '../../schemas/types/materialized_tenant_dialer_entries';
3
+ /**
4
+ * Dialer ENTRY PAGES — the page identity of `materialized_tenant_dialer_entries`.
5
+ *
6
+ * ## One page per source day
7
+ *
8
+ * | scope | page |
9
+ * | --- | --- |
10
+ * | `localDate` | `'all'` (the scope is already one day) |
11
+ * | `localWeek` | `<source localDate>` |
12
+ * | `localWeekOfYear` | `<source localDate>` |
13
+ * | `localMonth` | `<source localDate>` |
14
+ *
15
+ * There is NO quarter / semester / year entry document. A month is covered by
16
+ * its 28-31 pages.
17
+ *
18
+ * ## Deterministic `#N` sub-pages, only when needed
19
+ *
20
+ * An oversized page splits into `page#0`, `page#1`, … . A page that fits keeps
21
+ * its ORIGINAL identity (no suffix), so existing pages are untouched until they
22
+ * actually grow, and a page is never renumbered. The sub-pages are disjoint:
23
+ * every row appears exactly once, and the reader unions them.
24
+ *
25
+ * ## Broad selections
26
+ *
27
+ * A details reader resolves its exact selected DAYS into either the single day
28
+ * group (one day) or the set of MONTH groups that contain them; rows are then
29
+ * filtered to the exact days via `local_date`. A year is 12 monthly scopes, not
30
+ * a refusal.
31
+ */
32
+ /** True when a scope context carries entry pages (day + the three period scopes). */
33
+ export declare function isMaterializedDialerEntryScope(scope: unknown): boolean;
34
+ /** The `page` value a compiled entry document of `scopeContext` must carry. */
35
+ export declare function dialerEntryPageOf(scopeContext: unknown, localDate: string): string | null;
36
+ /** The unsuffixed source-day page (`all` / `YYYY-MM-DD`). */
37
+ export declare function dialerEntryPageBase(page: unknown): string;
38
+ /** True when a stored page belongs to the source day. */
39
+ export declare function isDialerEntryPageForSourceDay(scopeContext: unknown, page: unknown, localDate: string): boolean;
40
+ /** The deterministic identity of one chunk. */
41
+ export declare function materializedDialerEntrySubPage(basePage: string, index: number): string;
42
+ /** Stable identity string of one entry page (tests / diagnostics / map keys). */
43
+ export declare function materializedDialerEntryPageKey(ref: {
44
+ scope_context: unknown;
45
+ scope_field: unknown;
46
+ page: unknown;
47
+ }): string;
48
+ /**
49
+ * Splits ONE source-day entry page when it exceeds the budget.
50
+ *
51
+ * Rows are walked in their stored (deterministic) key/row order and packed until
52
+ * the next row would cross the budget. The chunks are `page#0`, `page#1`, …; a
53
+ * page that fits keeps its original identity (no suffix). Throws when a single
54
+ * row cannot fit, because silently dropping it would understate the statement.
55
+ */
56
+ export declare function splitMaterializedDialerEntryPage(doc: IMaterializedTenantDialerEntry, maxBytes?: number): IMaterializedTenantDialerEntry[];
57
+ /**
58
+ * Unions all pages of ONE source day (the base page or its `#N` sub-pages) into
59
+ * one logical document. Rows are disjoint by construction; the `event_id`
60
+ * dedupe makes a duplicated page harmless instead of doubling a row.
61
+ */
62
+ export declare function mergeMaterializedDialerEntryPages(pages: readonly IMaterializedTenantDialerEntry[] | null | undefined, identity?: {
63
+ scope_context: IMaterializedTenantDialerEntry['scope_context'];
64
+ scope_field: string;
65
+ page: string;
66
+ } | null): IMaterializedTenantDialerEntry | null;
67
+ /** One entry-page identity: the scope, its value, and the source-day page. */
68
+ export interface IMaterializedDialerEntryPageRef {
69
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM;
70
+ scope_field: string;
71
+ page: string;
72
+ }
73
+ /**
74
+ * The BASE page refs belonging to ONE source day. The close reader expands each
75
+ * ref to the base plus any deterministic `#N` sub-pages; no other day's page is
76
+ * ever fetched.
77
+ *
78
+ * Returns `null` for a malformed date.
79
+ */
80
+ export declare function buildMaterializedDialerEntryPageRefs(localDate: string): IMaterializedDialerEntryPageRef[] | null;
81
+ /** A decoded scope group, structurally identical to the read API's group shape. */
82
+ export interface IDialerScopeGroup {
83
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM;
84
+ scope_field: string[];
85
+ }
86
+ /** The distinct `YYYY-MM` scope values of a day set, ascending. */
87
+ export declare function dialerMonthScopeFieldsOfDays(days: readonly string[]): string[];
88
+ /**
89
+ * Resolves the EXACT selected days into the scope groups a DIALER DETAILS read
90
+ * uses:
91
+ *
92
+ * - one day → the `localDate` page (`page='all'`) — the direct path;
93
+ * - many days → the `localMonth` groups that contain them. The reader scans the
94
+ * pages of those months and filters rows to the exact days, so a quarter /
95
+ * semester / year / custom multi-month range is served instead of refused.
96
+ */
97
+ export declare function buildMaterializedDialerDetailsScopeGroups(days: readonly string[]): IDialerScopeGroup[] | null;
@@ -0,0 +1,235 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isMaterializedDialerEntryScope = isMaterializedDialerEntryScope;
4
+ exports.dialerEntryPageOf = dialerEntryPageOf;
5
+ exports.dialerEntryPageBase = dialerEntryPageBase;
6
+ exports.isDialerEntryPageForSourceDay = isDialerEntryPageForSourceDay;
7
+ exports.materializedDialerEntrySubPage = materializedDialerEntrySubPage;
8
+ exports.materializedDialerEntryPageKey = materializedDialerEntryPageKey;
9
+ exports.splitMaterializedDialerEntryPage = splitMaterializedDialerEntryPage;
10
+ exports.mergeMaterializedDialerEntryPages = mergeMaterializedDialerEntryPages;
11
+ exports.buildMaterializedDialerEntryPageRefs = buildMaterializedDialerEntryPageRefs;
12
+ exports.dialerMonthScopeFieldsOfDays = dialerMonthScopeFieldsOfDays;
13
+ exports.buildMaterializedDialerDetailsScopeGroups = buildMaterializedDialerDetailsScopeGroups;
14
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
15
+ const materialized_document_size_1 = require("./materialized-document-size");
16
+ const scope_helpers_1 = require("./scope-helpers");
17
+ /**
18
+ * Dialer ENTRY PAGES — the page identity of `materialized_tenant_dialer_entries`.
19
+ *
20
+ * ## One page per source day
21
+ *
22
+ * | scope | page |
23
+ * | --- | --- |
24
+ * | `localDate` | `'all'` (the scope is already one day) |
25
+ * | `localWeek` | `<source localDate>` |
26
+ * | `localWeekOfYear` | `<source localDate>` |
27
+ * | `localMonth` | `<source localDate>` |
28
+ *
29
+ * There is NO quarter / semester / year entry document. A month is covered by
30
+ * its 28-31 pages.
31
+ *
32
+ * ## Deterministic `#N` sub-pages, only when needed
33
+ *
34
+ * An oversized page splits into `page#0`, `page#1`, … . A page that fits keeps
35
+ * its ORIGINAL identity (no suffix), so existing pages are untouched until they
36
+ * actually grow, and a page is never renumbered. The sub-pages are disjoint:
37
+ * every row appears exactly once, and the reader unions them.
38
+ *
39
+ * ## Broad selections
40
+ *
41
+ * A details reader resolves its exact selected DAYS into either the single day
42
+ * group (one day) or the set of MONTH groups that contain them; rows are then
43
+ * filtered to the exact days via `local_date`. A year is 12 monthly scopes, not
44
+ * a refusal.
45
+ */
46
+ /** True when a scope context carries entry pages (day + the three period scopes). */
47
+ function isMaterializedDialerEntryScope(scope) {
48
+ return MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_ENTRY_SCOPES.includes(String(scope));
49
+ }
50
+ /** The `page` value a compiled entry document of `scopeContext` must carry. */
51
+ function dialerEntryPageOf(scopeContext, localDate) {
52
+ const scope = String(scopeContext);
53
+ if (!isMaterializedDialerEntryScope(scope))
54
+ return null;
55
+ if (scope === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE) {
56
+ return MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_ENTRY_PAGE_ALL;
57
+ }
58
+ if (typeof localDate !== 'string' || localDate === '')
59
+ return null;
60
+ return localDate;
61
+ }
62
+ const SUB_PAGE_PATTERN = /^(.*)#(\d+)$/;
63
+ /** The unsuffixed source-day page (`all` / `YYYY-MM-DD`). */
64
+ function dialerEntryPageBase(page) {
65
+ const text = String(page ?? '');
66
+ const match = SUB_PAGE_PATTERN.exec(text);
67
+ return match == null ? text : match[1];
68
+ }
69
+ /** True when a stored page belongs to the source day. */
70
+ function isDialerEntryPageForSourceDay(scopeContext, page, localDate) {
71
+ const base = dialerEntryPageOf(scopeContext, localDate);
72
+ if (base == null)
73
+ return false;
74
+ return dialerEntryPageBase(page) === base;
75
+ }
76
+ /** The deterministic identity of one chunk. */
77
+ function materializedDialerEntrySubPage(basePage, index) {
78
+ return `${basePage}#${Math.max(0, Math.floor(index))}`;
79
+ }
80
+ /** Stable identity string of one entry page (tests / diagnostics / map keys). */
81
+ function materializedDialerEntryPageKey(ref) {
82
+ return `${String(ref?.scope_context)}|${String(ref?.scope_field)}|${String(ref?.page)}`;
83
+ }
84
+ const countRows = (sections) => {
85
+ let rows = 0;
86
+ for (const section of Object.values(sections ?? {}))
87
+ rows += section?.rows?.length ?? 0;
88
+ return rows;
89
+ };
90
+ /**
91
+ * Splits ONE source-day entry page when it exceeds the budget.
92
+ *
93
+ * Rows are walked in their stored (deterministic) key/row order and packed until
94
+ * the next row would cross the budget. The chunks are `page#0`, `page#1`, …; a
95
+ * page that fits keeps its original identity (no suffix). Throws when a single
96
+ * row cannot fit, because silently dropping it would understate the statement.
97
+ */
98
+ function splitMaterializedDialerEntryPage(doc, maxBytes = MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_ENTRY_MAX_BSON_BYTES) {
99
+ if (doc == null)
100
+ return [];
101
+ if ((0, materialized_document_size_1.materializedDocumentBsonBytes)(doc) <= maxBytes)
102
+ return [doc];
103
+ const sections = doc.dialer_entries ?? null;
104
+ if (sections == null || countRows(sections) === 0)
105
+ return [doc];
106
+ const basePage = dialerEntryPageBase(doc.page);
107
+ const envelope = { ...doc, page: basePage, dialer_entries: {} };
108
+ const envelopeBytes = (0, materialized_document_size_1.materializedDocumentBsonBytes)(envelope);
109
+ const chunks = [];
110
+ let current = {};
111
+ let currentBytes = envelopeBytes;
112
+ let currentRows = 0;
113
+ const flush = () => {
114
+ if (currentRows > 0)
115
+ chunks.push(current);
116
+ current = {};
117
+ currentBytes = envelopeBytes;
118
+ currentRows = 0;
119
+ };
120
+ for (const key of Object.keys(sections)) {
121
+ for (const row of sections[key]?.rows ?? []) {
122
+ const rowBytes = (0, materialized_document_size_1.materializedDocumentBsonBytes)(row) + key.length + 24;
123
+ if (currentRows > 0 && currentBytes + rowBytes > maxBytes - 512)
124
+ flush();
125
+ if (current[key] == null)
126
+ current[key] = { rows: [] };
127
+ current[key].rows.push(row);
128
+ currentBytes += rowBytes;
129
+ currentRows += 1;
130
+ if (currentBytes > maxBytes) {
131
+ throw new Error(`Dialer entry page ${String(doc.scope_context)}:${String(doc.scope_field)}:${basePage} ` +
132
+ 'cannot split below the budget (one row is too large)');
133
+ }
134
+ }
135
+ }
136
+ flush();
137
+ return chunks.map((chunk, index) => {
138
+ const page = {
139
+ ...doc,
140
+ page: materializedDialerEntrySubPage(basePage, index),
141
+ dialer_entries: chunk,
142
+ };
143
+ if ((0, materialized_document_size_1.materializedDocumentBsonBytes)(page) > maxBytes) {
144
+ throw new Error(`Dialer entry sub-page ${String(doc.scope_context)}:${String(doc.scope_field)}:${page.page} ` +
145
+ 'exceeds the entry-page budget');
146
+ }
147
+ return page;
148
+ });
149
+ }
150
+ /**
151
+ * Unions all pages of ONE source day (the base page or its `#N` sub-pages) into
152
+ * one logical document. Rows are disjoint by construction; the `event_id`
153
+ * dedupe makes a duplicated page harmless instead of doubling a row.
154
+ */
155
+ function mergeMaterializedDialerEntryPages(pages, identity) {
156
+ const list = (pages ?? []).filter((page) => page != null);
157
+ if (list.length === 0)
158
+ return null;
159
+ const sections = {};
160
+ const seen = new Set();
161
+ for (const page of list) {
162
+ for (const [key, section] of Object.entries(page.dialer_entries ?? {})) {
163
+ for (const row of section?.rows ?? []) {
164
+ const rowIdentity = String(row.event_id ?? '');
165
+ if (rowIdentity !== '') {
166
+ if (seen.has(rowIdentity))
167
+ continue;
168
+ seen.add(rowIdentity);
169
+ }
170
+ if (sections[key] == null)
171
+ sections[key] = { rows: [] };
172
+ sections[key].rows.push(row);
173
+ }
174
+ }
175
+ }
176
+ const first = list[0];
177
+ return {
178
+ ...first,
179
+ ...(identity ?? {}),
180
+ dialer_entries: Object.keys(sections).length === 0 ? null : sections,
181
+ };
182
+ }
183
+ /**
184
+ * The BASE page refs belonging to ONE source day. The close reader expands each
185
+ * ref to the base plus any deterministic `#N` sub-pages; no other day's page is
186
+ * ever fetched.
187
+ *
188
+ * Returns `null` for a malformed date.
189
+ */
190
+ function buildMaterializedDialerEntryPageRefs(localDate) {
191
+ const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(localDate);
192
+ if (fields == null)
193
+ return null;
194
+ const refs = [];
195
+ for (const scope of MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_ENTRY_SCOPES) {
196
+ const scopeField = fields[String(scope)];
197
+ const page = dialerEntryPageOf(scope, localDate);
198
+ if (typeof scopeField !== 'string' || scopeField === '' || page == null)
199
+ return null;
200
+ refs.push({ scope_context: scope, scope_field: scopeField, page });
201
+ }
202
+ return refs;
203
+ }
204
+ /** The distinct `YYYY-MM` scope values of a day set, ascending. */
205
+ function dialerMonthScopeFieldsOfDays(days) {
206
+ const months = new Set();
207
+ for (const day of days ?? []) {
208
+ const match = /^(\d{4})-(\d{2})-\d{2}$/.exec(String(day));
209
+ if (match == null)
210
+ continue;
211
+ months.add(`${match[1]}-${match[2]}`);
212
+ }
213
+ return [...months].sort();
214
+ }
215
+ /**
216
+ * Resolves the EXACT selected days into the scope groups a DIALER DETAILS read
217
+ * uses:
218
+ *
219
+ * - one day → the `localDate` page (`page='all'`) — the direct path;
220
+ * - many days → the `localMonth` groups that contain them. The reader scans the
221
+ * pages of those months and filters rows to the exact days, so a quarter /
222
+ * semester / year / custom multi-month range is served instead of refused.
223
+ */
224
+ function buildMaterializedDialerDetailsScopeGroups(days) {
225
+ const unique = [...new Set((days ?? []).map((day) => String(day)))].sort();
226
+ if (unique.length === 0)
227
+ return null;
228
+ if (unique.length === 1) {
229
+ return [{ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE, scope_field: [unique[0]] }];
230
+ }
231
+ const months = dialerMonthScopeFieldsOfDays(unique);
232
+ if (months.length === 0)
233
+ return null;
234
+ return [{ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH, scope_field: months }];
235
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Dialer key grammar: `<ACTION>:<POS_ID>:<STAFF_ID>`.
3
+ *
4
+ * The three components are the report's filter dimensions. The writer and the
5
+ * reader BOTH use these helpers — keys are never built or parsed ad hoc, so the
6
+ * live compile, the close-day fold and every filtered read cannot drift.
7
+ *
8
+ * `STAFF_ID` is nullable in the source and a dimension may be blank in a
9
+ * corrupt row; a missing component is encoded as `UNKNOWN` rather than omitted,
10
+ * so the event still contributes to the totals (an event with no terminal is
11
+ * still an event).
12
+ */
13
+ /** Filters a dialer read may apply — one set per key component. */
14
+ export interface IDialerKeyFilters {
15
+ /** ACTION component (`INCOMING_CALL`, …). */
16
+ actions?: string[] | null;
17
+ /** POS_ID component. */
18
+ pos_ids?: string[] | null;
19
+ /** STAFF_ID component. */
20
+ staff_ids?: string[] | null;
21
+ }
22
+ /** The parsed components of one dialer key. */
23
+ export interface IDialerKeyParts {
24
+ action: string;
25
+ pos_id: string;
26
+ staff_id: string;
27
+ }
28
+ /** Normalizes ONE component: missing/blank → `UNKNOWN`, separator → dash. */
29
+ export declare function normalizeDialerKeyComponent(value: unknown): string;
30
+ /** Builds `<ACTION>:<POS_ID>:<STAFF_ID>`. */
31
+ export declare function buildMaterializedDialerKey(action: unknown, posId: unknown, staffId: unknown): string;
32
+ /** Splits a key back into its parts, or `null` when it is malformed. */
33
+ export declare function parseMaterializedDialerKey(key: unknown): IDialerKeyParts | null;
34
+ /** True when NO filter dimension is set (the key passes unchanged). */
35
+ export declare function isDialerKeyFilterEmpty(filters?: IDialerKeyFilters | null): boolean;
36
+ /**
37
+ * Exact component-level filter match — the SINGLE filter semantics shared by
38
+ * the live compile, the historical counter fold and the details page.
39
+ *
40
+ * `UNKNOWN` is an ordinary component value: filtering on it selects the rows
41
+ * that genuinely have no staff/terminal, never "everything the filter
42
+ * excluded". A malformed key is never hidden by a filter.
43
+ */
44
+ export declare function dialerKeyPassesFilters(key: unknown, filters?: IDialerKeyFilters | null): boolean;
45
+ /**
46
+ * Anchored MongoDB-key regex for a dialer filter, or `null` when no dimension
47
+ * is set. Each component is either an exact escaped value or a wildcard that
48
+ * cannot contain the separator, so a filter can never match a key PREFIX (the
49
+ * `ALL` action must not match `CALL_ANSWERED`).
50
+ */
51
+ export declare function buildDialerKeyFilterRegex(filters?: IDialerKeyFilters | null): RegExp | null;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizeDialerKeyComponent = normalizeDialerKeyComponent;
4
+ exports.buildMaterializedDialerKey = buildMaterializedDialerKey;
5
+ exports.parseMaterializedDialerKey = parseMaterializedDialerKey;
6
+ exports.isDialerKeyFilterEmpty = isDialerKeyFilterEmpty;
7
+ exports.dialerKeyPassesFilters = dialerKeyPassesFilters;
8
+ exports.buildDialerKeyFilterRegex = buildDialerKeyFilterRegex;
9
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
10
+ /** Normalizes ONE component: missing/blank → `UNKNOWN`, separator → dash. */
11
+ function normalizeDialerKeyComponent(value) {
12
+ if (value == null)
13
+ return MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_UNKNOWN_TOKEN;
14
+ const text = String(value).trim();
15
+ if (text === '')
16
+ return MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_UNKNOWN_TOKEN;
17
+ // The separator must never appear inside a component or the key is
18
+ // ambiguous; collapsing it keeps the row instead of dropping it.
19
+ return text.split(MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_KEY_SEPARATOR).join('-');
20
+ }
21
+ /** Builds `<ACTION>:<POS_ID>:<STAFF_ID>`. */
22
+ function buildMaterializedDialerKey(action, posId, staffId) {
23
+ return (normalizeDialerKeyComponent(action) +
24
+ MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_KEY_SEPARATOR +
25
+ normalizeDialerKeyComponent(posId) +
26
+ MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_KEY_SEPARATOR +
27
+ normalizeDialerKeyComponent(staffId));
28
+ }
29
+ /** Splits a key back into its parts, or `null` when it is malformed. */
30
+ function parseMaterializedDialerKey(key) {
31
+ if (key == null)
32
+ return null;
33
+ const parts = String(key).split(MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_KEY_SEPARATOR);
34
+ if (parts.length !== 3)
35
+ return null;
36
+ const [action, pos_id, staff_id] = parts;
37
+ if (action === '' || pos_id === '' || staff_id === '')
38
+ return null;
39
+ return { action, pos_id, staff_id };
40
+ }
41
+ /** True when NO filter dimension is set (the key passes unchanged). */
42
+ function isDialerKeyFilterEmpty(filters) {
43
+ if (filters == null)
44
+ return true;
45
+ return ((filters.actions == null || filters.actions.length === 0) &&
46
+ (filters.pos_ids == null || filters.pos_ids.length === 0) &&
47
+ (filters.staff_ids == null || filters.staff_ids.length === 0));
48
+ }
49
+ /**
50
+ * Exact component-level filter match — the SINGLE filter semantics shared by
51
+ * the live compile, the historical counter fold and the details page.
52
+ *
53
+ * `UNKNOWN` is an ordinary component value: filtering on it selects the rows
54
+ * that genuinely have no staff/terminal, never "everything the filter
55
+ * excluded". A malformed key is never hidden by a filter.
56
+ */
57
+ function dialerKeyPassesFilters(key, filters) {
58
+ if (isDialerKeyFilterEmpty(filters))
59
+ return true;
60
+ const parsed = parseMaterializedDialerKey(key);
61
+ if (parsed == null)
62
+ return true;
63
+ if (filters?.actions != null && filters.actions.length > 0 && !filters.actions.includes(parsed.action)) {
64
+ return false;
65
+ }
66
+ if (filters?.pos_ids != null && filters.pos_ids.length > 0 && !filters.pos_ids.includes(parsed.pos_id)) {
67
+ return false;
68
+ }
69
+ if (filters?.staff_ids != null && filters.staff_ids.length > 0 && !filters.staff_ids.includes(parsed.staff_id)) {
70
+ return false;
71
+ }
72
+ return true;
73
+ }
74
+ const escapeRegExp = (value) => value.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
75
+ /**
76
+ * Anchored MongoDB-key regex for a dialer filter, or `null` when no dimension
77
+ * is set. Each component is either an exact escaped value or a wildcard that
78
+ * cannot contain the separator, so a filter can never match a key PREFIX (the
79
+ * `ALL` action must not match `CALL_ANSWERED`).
80
+ */
81
+ function buildDialerKeyFilterRegex(filters) {
82
+ if (isDialerKeyFilterEmpty(filters))
83
+ return null;
84
+ const component = (values) => {
85
+ if (values == null || values.length === 0)
86
+ return '[^:]+';
87
+ return '(?:' + values.map((value) => escapeRegExp(String(value))).join('|') + ')';
88
+ };
89
+ return new RegExp('^' + component(filters?.actions) + ':' + component(filters?.pos_ids) + ':' + component(filters?.staff_ids) + '$');
90
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Phone masking for the Dialer report.
3
+ *
4
+ * ## Why mask at all
5
+ *
6
+ * A dialer event is a personal call record: the caller number is customer PII
7
+ * and the staff number is employee PII. The materialized report is a long-lived
8
+ * read model served to any operator with report access, so the RAW source
9
+ * numbers must never enter it. Masking happens ONCE, at COMPILE time: stored
10
+ * rows and every API response derived from them carry only masked values.
11
+ *
12
+ * ## The rule
13
+ *
14
+ * - keep the last FOUR digits (enough for an operator to correlate a caller
15
+ * across events of the same call, which is all the page needs);
16
+ * - replace EVERY other digit with `*`;
17
+ * - keep a leading `+` when the source carries one, and DROP every other
18
+ * separator (spaces/dashes) so the mask cannot leak the number's grouping.
19
+ *
20
+ * Examples:
21
+ * `+923001234567` → `+********4567`
22
+ * `0300-1234567` → `*******4567`
23
+ * `null` → `null`
24
+ * `"12"` → `**` (nothing is exposed for a number shorter than 5 digits)
25
+ *
26
+ * The function is TOTAL and deterministic: it never throws, so a malformed
27
+ * source value can never fail a close.
28
+ */
29
+ /**
30
+ * Masks one phone number (see the header for the exact rule).
31
+ *
32
+ * Returns `null` for `null` / empty input, so a missing source number stays
33
+ * "unmeasured" rather than becoming the string `"null"`.
34
+ */
35
+ export declare function maskDialerPhoneNumber(value: unknown): string | null;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ /**
3
+ * Phone masking for the Dialer report.
4
+ *
5
+ * ## Why mask at all
6
+ *
7
+ * A dialer event is a personal call record: the caller number is customer PII
8
+ * and the staff number is employee PII. The materialized report is a long-lived
9
+ * read model served to any operator with report access, so the RAW source
10
+ * numbers must never enter it. Masking happens ONCE, at COMPILE time: stored
11
+ * rows and every API response derived from them carry only masked values.
12
+ *
13
+ * ## The rule
14
+ *
15
+ * - keep the last FOUR digits (enough for an operator to correlate a caller
16
+ * across events of the same call, which is all the page needs);
17
+ * - replace EVERY other digit with `*`;
18
+ * - keep a leading `+` when the source carries one, and DROP every other
19
+ * separator (spaces/dashes) so the mask cannot leak the number's grouping.
20
+ *
21
+ * Examples:
22
+ * `+923001234567` → `+********4567`
23
+ * `0300-1234567` → `*******4567`
24
+ * `null` → `null`
25
+ * `"12"` → `**` (nothing is exposed for a number shorter than 5 digits)
26
+ *
27
+ * The function is TOTAL and deterministic: it never throws, so a malformed
28
+ * source value can never fail a close.
29
+ */
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.maskDialerPhoneNumber = maskDialerPhoneNumber;
32
+ const DIGIT_PATTERN = /\d/g;
33
+ /**
34
+ * Masks one phone number (see the header for the exact rule).
35
+ *
36
+ * Returns `null` for `null` / empty input, so a missing source number stays
37
+ * "unmeasured" rather than becoming the string `"null"`.
38
+ */
39
+ function maskDialerPhoneNumber(value) {
40
+ if (value == null)
41
+ return null;
42
+ const raw = String(value).trim();
43
+ if (raw === '')
44
+ return null;
45
+ const digits = raw.match(DIGIT_PATTERN) ?? [];
46
+ if (digits.length === 0) {
47
+ // No number to mask: do not echo arbitrary text (it may itself be PII).
48
+ return '****';
49
+ }
50
+ if (digits.length <= 4) {
51
+ // Too short to reveal ANY digit: the last-four rule would expose half of it.
52
+ return '*'.repeat(digits.length);
53
+ }
54
+ const visible = digits.slice(-4).join('');
55
+ const masked = '*'.repeat(digits.length - 4) + visible;
56
+ return raw.startsWith('+') ? `+${masked}` : masked;
57
+ }
@@ -9,6 +9,8 @@ const materialized_tenant_delivery_entries_1 = require("../../schemas/models/mat
9
9
  const materialized_tenant_delivery_geo_cells_1 = require("../../schemas/models/materialized_tenant_delivery_geo_cells");
10
10
  const materialized_tenant_delivery_geo_scopes_1 = require("../../schemas/models/materialized_tenant_delivery_geo_scopes");
11
11
  const materialized_tenant_delivery_reports_1 = require("../../schemas/models/materialized_tenant_delivery_reports");
12
+ const materialized_tenant_dialer_entries_1 = require("../../schemas/models/materialized_tenant_dialer_entries");
13
+ const materialized_tenant_dialer_reports_1 = require("../../schemas/models/materialized_tenant_dialer_reports");
12
14
  const materialized_tenant_discount_entries_1 = require("../../schemas/models/materialized_tenant_discount_entries");
13
15
  const materialized_tenant_discount_reports_1 = require("../../schemas/models/materialized_tenant_discount_reports");
14
16
  const materialized_tenant_discount_sources_1 = require("../../schemas/models/materialized_tenant_discount_sources");
@@ -46,6 +48,8 @@ const INDEXES = {
46
48
  [MATERIALIZED_REPORTS_1.MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_COLLECTION_NAME]: materialized_tenant_delivery_geo_scopes_1.MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_INDEX_OPTIONS,
47
49
  [materialized_tenant_delivery_entries_1.MATERIALIZED_TENANT_DELIVERY_ENTRIES_COLLECTION_NAME]: materialized_tenant_delivery_entries_1.MATERIALIZED_TENANT_DELIVERY_ENTRIES_INDEX_OPTIONS,
48
50
  [materialized_tenant_delivery_reports_1.MATERIALIZED_TENANT_DELIVERY_REPORTS_COLLECTION_NAME]: materialized_tenant_delivery_reports_1.MATERIALIZED_TENANT_DELIVERY_REPORTS_INDEX_OPTIONS,
51
+ [materialized_tenant_dialer_reports_1.MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_NAME]: materialized_tenant_dialer_reports_1.MATERIALIZED_TENANT_DIALER_REPORTS_INDEX_OPTIONS,
52
+ [materialized_tenant_dialer_entries_1.MATERIALIZED_TENANT_DIALER_ENTRIES_COLLECTION_NAME]: materialized_tenant_dialer_entries_1.MATERIALIZED_TENANT_DIALER_ENTRIES_INDEX_OPTIONS,
49
53
  [materialized_report_drilldown_rows_1.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME]: materialized_report_drilldown_rows_1.MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS,
50
54
  [materialized_report_drilldown_rows_1.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME]: materialized_report_drilldown_rows_1.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS,
51
55
  [tenant_calendar_1.TENANT_CALENDAR_COLLECTION_NAME]: tenant_calendar_1.TENANT_CALENDAR_INDEX_OPTIONS,
@@ -66,6 +66,14 @@ const ROW_SPECS = {
66
66
  orderId: (row) => row.order_id,
67
67
  cursorId: (row) => row.payment_id,
68
68
  },
69
+ [MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DIALER_ENTRIES]: {
70
+ rowsKey: 'dialer_entries',
71
+ // `created_at` is an ISO string on the dialer row; `toDate` accepts both.
72
+ time: (row) => row.created_at,
73
+ // Dialer rows are EVENT rows, not order rows (like cash ledger rows).
74
+ orderId: () => '',
75
+ cursorId: (row) => row.event_id,
76
+ },
69
77
  };
70
78
  /** The nested map key an entry collection uses for its row sections. */
71
79
  const getDrilldownRowsKey = (reportType) => ROW_SPECS[reportType].rowsKey;
@@ -109,7 +117,11 @@ function buildMaterializedDrilldownRows(reportType, document, sourceLocalDate =
109
117
  // Discounts are served only when the row carries its kind; the nested
110
118
  // collector applies the same requirement before it can be viewed.
111
119
  const kindMissing = reportType === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES && asString(row.kind) === '';
112
- const orderMissing = reportType !== MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES && orderId === '';
120
+ // Cash and Dialer rows are ledger/event rows and have no `order_id`; every
121
+ // other report's identity includes the order it belongs to.
122
+ const orderMissing = reportType !== MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES &&
123
+ reportType !== MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DIALER_ENTRIES &&
124
+ orderId === '';
113
125
  if (time == null || cursorId === '' || kindMissing || orderMissing) {
114
126
  skipped += 1;
115
127
  continue;