@managesome/eposmatic-toolkit 0.4.55 → 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.
- package/esm/constants/MATERIALIZED_REPORTS.js +91 -0
- package/esm/helpers/materialized-reports/index.js +7 -0
- package/esm/helpers/materialized-reports/materialized-dialer-accumulator-helpers.js +100 -0
- package/esm/helpers/materialized-reports/materialized-dialer-close-day-helpers.js +328 -0
- package/esm/helpers/materialized-reports/materialized-dialer-compile-helpers.js +180 -0
- package/esm/helpers/materialized-reports/materialized-dialer-entry-pages.js +222 -0
- package/esm/helpers/materialized-reports/materialized-dialer-key-helpers.js +82 -0
- package/esm/helpers/materialized-reports/materialized-dialer-masking.js +54 -0
- package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
- package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +13 -1
- package/esm/helpers/materialized-reports/report-platform/definition.js +85 -1
- package/esm/helpers/materialized-reports/tenant-dialer-formatter.js +95 -0
- package/esm/helpers/reporting-contract/report-contract.js +3 -0
- package/esm/schemas/models/index.js +2 -0
- package/esm/schemas/models/materialized_report_drilldown_rows.js +1 -0
- package/esm/schemas/models/materialized_tenant_dialer_entries.js +65 -0
- package/esm/schemas/models/materialized_tenant_dialer_reports.js +66 -0
- package/esm/schemas/types/index.js +2 -0
- package/esm/schemas/types/materialized_report_drilldown_rows.js +1 -0
- package/esm/schemas/types/materialized_tenant_dialer_entries.js +1 -0
- package/esm/schemas/types/materialized_tenant_dialer_reports.js +7 -0
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +72 -2
- package/lib/constants/MATERIALIZED_REPORTS.js +93 -1
- package/lib/helpers/materialized-reports/index.d.ts +7 -0
- package/lib/helpers/materialized-reports/index.js +7 -0
- package/lib/helpers/materialized-reports/materialized-dialer-accumulator-helpers.d.ts +39 -0
- package/lib/helpers/materialized-reports/materialized-dialer-accumulator-helpers.js +110 -0
- package/lib/helpers/materialized-reports/materialized-dialer-close-day-helpers.d.ts +90 -0
- package/lib/helpers/materialized-reports/materialized-dialer-close-day-helpers.js +340 -0
- package/lib/helpers/materialized-reports/materialized-dialer-compile-helpers.d.ts +67 -0
- package/lib/helpers/materialized-reports/materialized-dialer-compile-helpers.js +183 -0
- package/lib/helpers/materialized-reports/materialized-dialer-entry-pages.d.ts +97 -0
- package/lib/helpers/materialized-reports/materialized-dialer-entry-pages.js +235 -0
- package/lib/helpers/materialized-reports/materialized-dialer-key-helpers.d.ts +51 -0
- package/lib/helpers/materialized-reports/materialized-dialer-key-helpers.js +90 -0
- package/lib/helpers/materialized-reports/materialized-dialer-masking.d.ts +35 -0
- package/lib/helpers/materialized-reports/materialized-dialer-masking.js +57 -0
- package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
- package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +13 -1
- package/lib/helpers/materialized-reports/report-platform/definition.js +84 -0
- package/lib/helpers/materialized-reports/tenant-dialer-formatter.d.ts +91 -0
- package/lib/helpers/materialized-reports/tenant-dialer-formatter.js +98 -0
- package/lib/helpers/reporting-contract/report-contract.d.ts +1 -0
- package/lib/helpers/reporting-contract/report-contract.js +3 -0
- package/lib/schemas/models/index.d.ts +2 -0
- package/lib/schemas/models/index.js +2 -0
- package/lib/schemas/models/materialized_report_drilldown_rows.js +1 -0
- package/lib/schemas/models/materialized_tenant_dialer_entries.d.ts +107 -0
- package/lib/schemas/models/materialized_tenant_dialer_entries.js +68 -0
- package/lib/schemas/models/materialized_tenant_dialer_reports.d.ts +100 -0
- package/lib/schemas/models/materialized_tenant_dialer_reports.js +69 -0
- package/lib/schemas/models/tenant_dialer_events.d.ts +3 -3
- package/lib/schemas/types/index.d.ts +2 -0
- package/lib/schemas/types/index.js +2 -0
- package/lib/schemas/types/materialized_report_drilldown_rows.d.ts +1 -1
- package/lib/schemas/types/materialized_report_drilldown_rows.js +1 -0
- package/lib/schemas/types/materialized_tenant_dialer_entries.d.ts +83 -0
- package/lib/schemas/types/materialized_tenant_dialer_entries.js +2 -0
- package/lib/schemas/types/materialized_tenant_dialer_reports.d.ts +77 -0
- package/lib/schemas/types/materialized_tenant_dialer_reports.js +10 -0
- package/lib/tests/materialized-reports/materialized-dialer-compile.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-dialer-compile.test.js +213 -0
- package/lib/tests/materialized-reports/materialized-dialer-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-dialer-formatter.test.js +159 -0
- package/lib/tests/materialized-reports/materialized-dialer-plan.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-dialer-plan.test.js +223 -0
- package/lib/tests/materialized-reports/materialized-tenant-dialer-schema.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-tenant-dialer-schema.test.js +183 -0
- package/lib/tests/materialized-reports/report-platform/registry-conformance.test.js +16 -3
- package/package.json +1 -1
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { MATERIALIZED_DIALER_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DIALER_ENTRY_SCOPES, MATERIALIZED_DIALER_REPORT_MAX_BSON_BYTES, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { MATERIALIZED_TENANT_DIALER_ENTRIES_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_dialer_entries';
|
|
3
|
+
import { MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION as REPORT_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_dialer_reports';
|
|
4
|
+
import { MATERIALIZED_DIALER_REPORT_KEY_FILTERS, } from '../../schemas/types/materialized_tenant_dialer_reports';
|
|
5
|
+
import { addDialerEvent, dialerHourKey, freshDialerCounters } from './materialized-dialer-accumulator-helpers';
|
|
6
|
+
import { dialerEntryPageOf, splitMaterializedDialerEntryPage } from './materialized-dialer-entry-pages';
|
|
7
|
+
import { buildMaterializedDialerKey } from './materialized-dialer-key-helpers';
|
|
8
|
+
import { maskDialerPhoneNumber } from './materialized-dialer-masking';
|
|
9
|
+
import { assertMaterializedDocumentSize } from './materialized-document-size';
|
|
10
|
+
import { getDayIndexFromLocalDate, getMaterializedPeriodScopeFields, parseMaterializedLocalDate, } from './scope-helpers';
|
|
11
|
+
const toDate = (value) => {
|
|
12
|
+
if (value == null)
|
|
13
|
+
return null;
|
|
14
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
15
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
16
|
+
};
|
|
17
|
+
/** Builds one statement row from an attributed event (phones masked). */
|
|
18
|
+
function buildDialerEntryRow(event, localDate, localHour) {
|
|
19
|
+
return {
|
|
20
|
+
event_id: String(event._id ?? ''),
|
|
21
|
+
request_id: String(event.request_id ?? ''),
|
|
22
|
+
action: String(event.action ?? ''),
|
|
23
|
+
phone_number_masked: maskDialerPhoneNumber(event.phone_number) ?? '',
|
|
24
|
+
staff_id: event.staff_id == null || String(event.staff_id).trim() === '' ? null : String(event.staff_id),
|
|
25
|
+
staff_phone_masked: maskDialerPhoneNumber(event.staff_phone),
|
|
26
|
+
pos_id: String(event.pos_id ?? ''),
|
|
27
|
+
created_at: new Date(event.createdAt).toISOString(),
|
|
28
|
+
local_date: localDate,
|
|
29
|
+
local_hour: localHour,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** Compiles one business day into a counter document plus its entry pages. */
|
|
33
|
+
export function compileTenantDialerReports(input) {
|
|
34
|
+
if (input == null)
|
|
35
|
+
return null;
|
|
36
|
+
const { tenantId, brandId, localDate } = input;
|
|
37
|
+
if (typeof tenantId !== 'string' ||
|
|
38
|
+
tenantId === '' ||
|
|
39
|
+
typeof brandId !== 'string' ||
|
|
40
|
+
brandId === '' ||
|
|
41
|
+
typeof localDate !== 'string' ||
|
|
42
|
+
parseMaterializedLocalDate(localDate) == null) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const computedAt = input.computedAt ?? new Date();
|
|
46
|
+
const countersByKey = new Map();
|
|
47
|
+
const rowsByKey = new Map();
|
|
48
|
+
const watermarks = [];
|
|
49
|
+
let attributed = 0;
|
|
50
|
+
let unattributed = 0;
|
|
51
|
+
let outOfDay = 0;
|
|
52
|
+
for (const event of Array.isArray(input.events) ? input.events : []) {
|
|
53
|
+
if (event == null) {
|
|
54
|
+
unattributed += 1;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (String(event.tenant_id ?? tenantId) !== tenantId)
|
|
58
|
+
continue;
|
|
59
|
+
const eventDay = String(event.localDate ?? '');
|
|
60
|
+
const hour = dialerHourKey(event.localHour);
|
|
61
|
+
const createdAt = toDate(event.createdAt);
|
|
62
|
+
if (eventDay !== '' && parseMaterializedLocalDate(eventDay) != null) {
|
|
63
|
+
if (eventDay !== localDate) {
|
|
64
|
+
outOfDay += 1;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (eventDay === '' || hour == null || createdAt == null) {
|
|
69
|
+
// No local day, no whole local hour, or no canonical instant: the event
|
|
70
|
+
// is UNATTRIBUTED. Nothing is invented for it.
|
|
71
|
+
unattributed += 1;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const key = buildMaterializedDialerKey(event.action, event.pos_id, event.staff_id);
|
|
75
|
+
const counters = countersByKey.get(key) ?? freshDialerCounters();
|
|
76
|
+
addDialerEvent(counters, { action: event.action, localHour: event.localHour });
|
|
77
|
+
countersByKey.set(key, counters);
|
|
78
|
+
const row = buildDialerEntryRow(event, localDate, Number(hour));
|
|
79
|
+
const bucket = rowsByKey.get(key);
|
|
80
|
+
if (bucket == null)
|
|
81
|
+
rowsByKey.set(key, [row]);
|
|
82
|
+
else
|
|
83
|
+
bucket.push(row);
|
|
84
|
+
watermarks.push(toDate(event.updatedAt) ?? createdAt);
|
|
85
|
+
attributed += 1;
|
|
86
|
+
}
|
|
87
|
+
if (attributed === 0)
|
|
88
|
+
return null;
|
|
89
|
+
const dayIndex = getDayIndexFromLocalDate(localDate);
|
|
90
|
+
// The SOURCE watermark: the newest folded event. It is NOT clamped to
|
|
91
|
+
// `computedAt` — an unchanged re-close must stay below the stored watermark
|
|
92
|
+
// so the plan can skip it, while a late event (a newer `updatedAt`) advances
|
|
93
|
+
// it and forces the rewrite.
|
|
94
|
+
const watermark = watermarks.reduce((latest, current) => (current.getTime() > latest.getTime() ? current : latest), watermarks[0] ?? computedAt);
|
|
95
|
+
const dialerCounters = {};
|
|
96
|
+
for (const key of [...countersByKey.keys()].sort()) {
|
|
97
|
+
dialerCounters[key] = countersByKey.get(key);
|
|
98
|
+
}
|
|
99
|
+
const sections = {};
|
|
100
|
+
for (const key of [...rowsByKey.keys()].sort()) {
|
|
101
|
+
const rows = rowsByKey.get(key);
|
|
102
|
+
// NEWEST FIRST with a total tie-break: the same order the keyset reader
|
|
103
|
+
// uses, so the stored page and the API agree.
|
|
104
|
+
rows.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime() ||
|
|
105
|
+
(a.event_id < b.event_id ? 1 : a.event_id > b.event_id ? -1 : 0));
|
|
106
|
+
sections[key] = { rows };
|
|
107
|
+
}
|
|
108
|
+
const reports = [
|
|
109
|
+
{
|
|
110
|
+
_id: undefined,
|
|
111
|
+
tenant_id: tenantId,
|
|
112
|
+
brand_id: brandId,
|
|
113
|
+
scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
114
|
+
scope_field: localDate,
|
|
115
|
+
day_index: dayIndex,
|
|
116
|
+
sale_type_key_filters: [...MATERIALIZED_DIALER_REPORT_KEY_FILTERS],
|
|
117
|
+
dialer_counters: dialerCounters,
|
|
118
|
+
schema_version: REPORT_SCHEMA_VERSION,
|
|
119
|
+
source_watermark: watermark,
|
|
120
|
+
computed_at: computedAt,
|
|
121
|
+
createdAt: computedAt,
|
|
122
|
+
updatedAt: computedAt,
|
|
123
|
+
},
|
|
124
|
+
];
|
|
125
|
+
const periodFields = getMaterializedPeriodScopeFields(localDate);
|
|
126
|
+
if (periodFields == null)
|
|
127
|
+
return null;
|
|
128
|
+
const entrySections = Object.keys(sections).length === 0 ? null : sections;
|
|
129
|
+
const entries = [];
|
|
130
|
+
for (const scope of MATERIALIZED_DIALER_ENTRY_SCOPES) {
|
|
131
|
+
const scopeField = periodFields[scope];
|
|
132
|
+
const page = dialerEntryPageOf(scope, localDate);
|
|
133
|
+
if (typeof scopeField !== 'string' || scopeField === '' || page == null)
|
|
134
|
+
return null;
|
|
135
|
+
const pageDoc = {
|
|
136
|
+
_id: undefined,
|
|
137
|
+
tenant_id: tenantId,
|
|
138
|
+
brand_id: brandId,
|
|
139
|
+
scope_context: scope,
|
|
140
|
+
scope_field: scopeField,
|
|
141
|
+
page,
|
|
142
|
+
day_index: scope === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE ? dayIndex : null,
|
|
143
|
+
dialer_entries: entrySections,
|
|
144
|
+
schema_version: MATERIALIZED_TENANT_DIALER_ENTRIES_SCHEMA_VERSION,
|
|
145
|
+
source_watermark: watermark,
|
|
146
|
+
computed_at: computedAt,
|
|
147
|
+
createdAt: computedAt,
|
|
148
|
+
updatedAt: computedAt,
|
|
149
|
+
};
|
|
150
|
+
entries.push(...splitMaterializedDialerEntryPage(pageDoc));
|
|
151
|
+
}
|
|
152
|
+
// BSON budgets: fail at COMPILE time, naming the collection + day + scope,
|
|
153
|
+
// rather than at insert time inside the close transaction.
|
|
154
|
+
assertMaterializedDocumentSize(reports[0], {
|
|
155
|
+
label: `materialized_tenant_dialer_reports:${tenantId}:${localDate}:localDate:${localDate}`,
|
|
156
|
+
maxBytes: MATERIALIZED_DIALER_REPORT_MAX_BSON_BYTES,
|
|
157
|
+
});
|
|
158
|
+
for (const page of entries) {
|
|
159
|
+
assertMaterializedDocumentSize(page, {
|
|
160
|
+
label: 'materialized_tenant_dialer_entries:' +
|
|
161
|
+
tenantId +
|
|
162
|
+
':' +
|
|
163
|
+
localDate +
|
|
164
|
+
':' +
|
|
165
|
+
String(page.scope_context) +
|
|
166
|
+
':' +
|
|
167
|
+
page.scope_field +
|
|
168
|
+
':page=' +
|
|
169
|
+
page.page,
|
|
170
|
+
maxBytes: MATERIALIZED_DIALER_ENTRY_MAX_BSON_BYTES,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
reports,
|
|
175
|
+
entries,
|
|
176
|
+
attributed_events: attributed,
|
|
177
|
+
unattributed_events: unattributed,
|
|
178
|
+
out_of_day_events: outOfDay,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { MATERIALIZED_DIALER_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DIALER_ENTRY_PAGE_ALL, MATERIALIZED_DIALER_ENTRY_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { materializedDocumentBsonBytes } from './materialized-document-size';
|
|
3
|
+
import { getMaterializedPeriodScopeFields } from './scope-helpers';
|
|
4
|
+
/**
|
|
5
|
+
* Dialer ENTRY PAGES — the page identity of `materialized_tenant_dialer_entries`.
|
|
6
|
+
*
|
|
7
|
+
* ## One page per source day
|
|
8
|
+
*
|
|
9
|
+
* | scope | page |
|
|
10
|
+
* | --- | --- |
|
|
11
|
+
* | `localDate` | `'all'` (the scope is already one day) |
|
|
12
|
+
* | `localWeek` | `<source localDate>` |
|
|
13
|
+
* | `localWeekOfYear` | `<source localDate>` |
|
|
14
|
+
* | `localMonth` | `<source localDate>` |
|
|
15
|
+
*
|
|
16
|
+
* There is NO quarter / semester / year entry document. A month is covered by
|
|
17
|
+
* its 28-31 pages.
|
|
18
|
+
*
|
|
19
|
+
* ## Deterministic `#N` sub-pages, only when needed
|
|
20
|
+
*
|
|
21
|
+
* An oversized page splits into `page#0`, `page#1`, … . A page that fits keeps
|
|
22
|
+
* its ORIGINAL identity (no suffix), so existing pages are untouched until they
|
|
23
|
+
* actually grow, and a page is never renumbered. The sub-pages are disjoint:
|
|
24
|
+
* every row appears exactly once, and the reader unions them.
|
|
25
|
+
*
|
|
26
|
+
* ## Broad selections
|
|
27
|
+
*
|
|
28
|
+
* A details reader resolves its exact selected DAYS into either the single day
|
|
29
|
+
* group (one day) or the set of MONTH groups that contain them; rows are then
|
|
30
|
+
* filtered to the exact days via `local_date`. A year is 12 monthly scopes, not
|
|
31
|
+
* a refusal.
|
|
32
|
+
*/
|
|
33
|
+
/** True when a scope context carries entry pages (day + the three period scopes). */
|
|
34
|
+
export function isMaterializedDialerEntryScope(scope) {
|
|
35
|
+
return MATERIALIZED_DIALER_ENTRY_SCOPES.includes(String(scope));
|
|
36
|
+
}
|
|
37
|
+
/** The `page` value a compiled entry document of `scopeContext` must carry. */
|
|
38
|
+
export function dialerEntryPageOf(scopeContext, localDate) {
|
|
39
|
+
const scope = String(scopeContext);
|
|
40
|
+
if (!isMaterializedDialerEntryScope(scope))
|
|
41
|
+
return null;
|
|
42
|
+
if (scope === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE) {
|
|
43
|
+
return MATERIALIZED_DIALER_ENTRY_PAGE_ALL;
|
|
44
|
+
}
|
|
45
|
+
if (typeof localDate !== 'string' || localDate === '')
|
|
46
|
+
return null;
|
|
47
|
+
return localDate;
|
|
48
|
+
}
|
|
49
|
+
const SUB_PAGE_PATTERN = /^(.*)#(\d+)$/;
|
|
50
|
+
/** The unsuffixed source-day page (`all` / `YYYY-MM-DD`). */
|
|
51
|
+
export function dialerEntryPageBase(page) {
|
|
52
|
+
const text = String(page ?? '');
|
|
53
|
+
const match = SUB_PAGE_PATTERN.exec(text);
|
|
54
|
+
return match == null ? text : match[1];
|
|
55
|
+
}
|
|
56
|
+
/** True when a stored page belongs to the source day. */
|
|
57
|
+
export function isDialerEntryPageForSourceDay(scopeContext, page, localDate) {
|
|
58
|
+
const base = dialerEntryPageOf(scopeContext, localDate);
|
|
59
|
+
if (base == null)
|
|
60
|
+
return false;
|
|
61
|
+
return dialerEntryPageBase(page) === base;
|
|
62
|
+
}
|
|
63
|
+
/** The deterministic identity of one chunk. */
|
|
64
|
+
export function materializedDialerEntrySubPage(basePage, index) {
|
|
65
|
+
return `${basePage}#${Math.max(0, Math.floor(index))}`;
|
|
66
|
+
}
|
|
67
|
+
/** Stable identity string of one entry page (tests / diagnostics / map keys). */
|
|
68
|
+
export function materializedDialerEntryPageKey(ref) {
|
|
69
|
+
return `${String(ref?.scope_context)}|${String(ref?.scope_field)}|${String(ref?.page)}`;
|
|
70
|
+
}
|
|
71
|
+
const countRows = (sections) => {
|
|
72
|
+
let rows = 0;
|
|
73
|
+
for (const section of Object.values(sections ?? {}))
|
|
74
|
+
rows += section?.rows?.length ?? 0;
|
|
75
|
+
return rows;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Splits ONE source-day entry page when it exceeds the budget.
|
|
79
|
+
*
|
|
80
|
+
* Rows are walked in their stored (deterministic) key/row order and packed until
|
|
81
|
+
* the next row would cross the budget. The chunks are `page#0`, `page#1`, …; a
|
|
82
|
+
* page that fits keeps its original identity (no suffix). Throws when a single
|
|
83
|
+
* row cannot fit, because silently dropping it would understate the statement.
|
|
84
|
+
*/
|
|
85
|
+
export function splitMaterializedDialerEntryPage(doc, maxBytes = MATERIALIZED_DIALER_ENTRY_MAX_BSON_BYTES) {
|
|
86
|
+
if (doc == null)
|
|
87
|
+
return [];
|
|
88
|
+
if (materializedDocumentBsonBytes(doc) <= maxBytes)
|
|
89
|
+
return [doc];
|
|
90
|
+
const sections = doc.dialer_entries ?? null;
|
|
91
|
+
if (sections == null || countRows(sections) === 0)
|
|
92
|
+
return [doc];
|
|
93
|
+
const basePage = dialerEntryPageBase(doc.page);
|
|
94
|
+
const envelope = { ...doc, page: basePage, dialer_entries: {} };
|
|
95
|
+
const envelopeBytes = materializedDocumentBsonBytes(envelope);
|
|
96
|
+
const chunks = [];
|
|
97
|
+
let current = {};
|
|
98
|
+
let currentBytes = envelopeBytes;
|
|
99
|
+
let currentRows = 0;
|
|
100
|
+
const flush = () => {
|
|
101
|
+
if (currentRows > 0)
|
|
102
|
+
chunks.push(current);
|
|
103
|
+
current = {};
|
|
104
|
+
currentBytes = envelopeBytes;
|
|
105
|
+
currentRows = 0;
|
|
106
|
+
};
|
|
107
|
+
for (const key of Object.keys(sections)) {
|
|
108
|
+
for (const row of sections[key]?.rows ?? []) {
|
|
109
|
+
const rowBytes = materializedDocumentBsonBytes(row) + key.length + 24;
|
|
110
|
+
if (currentRows > 0 && currentBytes + rowBytes > maxBytes - 512)
|
|
111
|
+
flush();
|
|
112
|
+
if (current[key] == null)
|
|
113
|
+
current[key] = { rows: [] };
|
|
114
|
+
current[key].rows.push(row);
|
|
115
|
+
currentBytes += rowBytes;
|
|
116
|
+
currentRows += 1;
|
|
117
|
+
if (currentBytes > maxBytes) {
|
|
118
|
+
throw new Error(`Dialer entry page ${String(doc.scope_context)}:${String(doc.scope_field)}:${basePage} ` +
|
|
119
|
+
'cannot split below the budget (one row is too large)');
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
flush();
|
|
124
|
+
return chunks.map((chunk, index) => {
|
|
125
|
+
const page = {
|
|
126
|
+
...doc,
|
|
127
|
+
page: materializedDialerEntrySubPage(basePage, index),
|
|
128
|
+
dialer_entries: chunk,
|
|
129
|
+
};
|
|
130
|
+
if (materializedDocumentBsonBytes(page) > maxBytes) {
|
|
131
|
+
throw new Error(`Dialer entry sub-page ${String(doc.scope_context)}:${String(doc.scope_field)}:${page.page} ` +
|
|
132
|
+
'exceeds the entry-page budget');
|
|
133
|
+
}
|
|
134
|
+
return page;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Unions all pages of ONE source day (the base page or its `#N` sub-pages) into
|
|
139
|
+
* one logical document. Rows are disjoint by construction; the `event_id`
|
|
140
|
+
* dedupe makes a duplicated page harmless instead of doubling a row.
|
|
141
|
+
*/
|
|
142
|
+
export function mergeMaterializedDialerEntryPages(pages, identity) {
|
|
143
|
+
const list = (pages ?? []).filter((page) => page != null);
|
|
144
|
+
if (list.length === 0)
|
|
145
|
+
return null;
|
|
146
|
+
const sections = {};
|
|
147
|
+
const seen = new Set();
|
|
148
|
+
for (const page of list) {
|
|
149
|
+
for (const [key, section] of Object.entries(page.dialer_entries ?? {})) {
|
|
150
|
+
for (const row of section?.rows ?? []) {
|
|
151
|
+
const rowIdentity = String(row.event_id ?? '');
|
|
152
|
+
if (rowIdentity !== '') {
|
|
153
|
+
if (seen.has(rowIdentity))
|
|
154
|
+
continue;
|
|
155
|
+
seen.add(rowIdentity);
|
|
156
|
+
}
|
|
157
|
+
if (sections[key] == null)
|
|
158
|
+
sections[key] = { rows: [] };
|
|
159
|
+
sections[key].rows.push(row);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const first = list[0];
|
|
164
|
+
return {
|
|
165
|
+
...first,
|
|
166
|
+
...(identity ?? {}),
|
|
167
|
+
dialer_entries: Object.keys(sections).length === 0 ? null : sections,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The BASE page refs belonging to ONE source day. The close reader expands each
|
|
172
|
+
* ref to the base plus any deterministic `#N` sub-pages; no other day's page is
|
|
173
|
+
* ever fetched.
|
|
174
|
+
*
|
|
175
|
+
* Returns `null` for a malformed date.
|
|
176
|
+
*/
|
|
177
|
+
export function buildMaterializedDialerEntryPageRefs(localDate) {
|
|
178
|
+
const fields = getMaterializedPeriodScopeFields(localDate);
|
|
179
|
+
if (fields == null)
|
|
180
|
+
return null;
|
|
181
|
+
const refs = [];
|
|
182
|
+
for (const scope of MATERIALIZED_DIALER_ENTRY_SCOPES) {
|
|
183
|
+
const scopeField = fields[String(scope)];
|
|
184
|
+
const page = dialerEntryPageOf(scope, localDate);
|
|
185
|
+
if (typeof scopeField !== 'string' || scopeField === '' || page == null)
|
|
186
|
+
return null;
|
|
187
|
+
refs.push({ scope_context: scope, scope_field: scopeField, page });
|
|
188
|
+
}
|
|
189
|
+
return refs;
|
|
190
|
+
}
|
|
191
|
+
/** The distinct `YYYY-MM` scope values of a day set, ascending. */
|
|
192
|
+
export function dialerMonthScopeFieldsOfDays(days) {
|
|
193
|
+
const months = new Set();
|
|
194
|
+
for (const day of days ?? []) {
|
|
195
|
+
const match = /^(\d{4})-(\d{2})-\d{2}$/.exec(String(day));
|
|
196
|
+
if (match == null)
|
|
197
|
+
continue;
|
|
198
|
+
months.add(`${match[1]}-${match[2]}`);
|
|
199
|
+
}
|
|
200
|
+
return [...months].sort();
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Resolves the EXACT selected days into the scope groups a DIALER DETAILS read
|
|
204
|
+
* uses:
|
|
205
|
+
*
|
|
206
|
+
* - one day → the `localDate` page (`page='all'`) — the direct path;
|
|
207
|
+
* - many days → the `localMonth` groups that contain them. The reader scans the
|
|
208
|
+
* pages of those months and filters rows to the exact days, so a quarter /
|
|
209
|
+
* semester / year / custom multi-month range is served instead of refused.
|
|
210
|
+
*/
|
|
211
|
+
export function buildMaterializedDialerDetailsScopeGroups(days) {
|
|
212
|
+
const unique = [...new Set((days ?? []).map((day) => String(day)))].sort();
|
|
213
|
+
if (unique.length === 0)
|
|
214
|
+
return null;
|
|
215
|
+
if (unique.length === 1) {
|
|
216
|
+
return [{ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE, scope_field: [unique[0]] }];
|
|
217
|
+
}
|
|
218
|
+
const months = dialerMonthScopeFieldsOfDays(unique);
|
|
219
|
+
if (months.length === 0)
|
|
220
|
+
return null;
|
|
221
|
+
return [{ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH, scope_field: months }];
|
|
222
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { MATERIALIZED_DIALER_KEY_SEPARATOR, MATERIALIZED_DIALER_UNKNOWN_TOKEN, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
/** Normalizes ONE component: missing/blank → `UNKNOWN`, separator → dash. */
|
|
3
|
+
export function normalizeDialerKeyComponent(value) {
|
|
4
|
+
if (value == null)
|
|
5
|
+
return MATERIALIZED_DIALER_UNKNOWN_TOKEN;
|
|
6
|
+
const text = String(value).trim();
|
|
7
|
+
if (text === '')
|
|
8
|
+
return MATERIALIZED_DIALER_UNKNOWN_TOKEN;
|
|
9
|
+
// The separator must never appear inside a component or the key is
|
|
10
|
+
// ambiguous; collapsing it keeps the row instead of dropping it.
|
|
11
|
+
return text.split(MATERIALIZED_DIALER_KEY_SEPARATOR).join('-');
|
|
12
|
+
}
|
|
13
|
+
/** Builds `<ACTION>:<POS_ID>:<STAFF_ID>`. */
|
|
14
|
+
export function buildMaterializedDialerKey(action, posId, staffId) {
|
|
15
|
+
return (normalizeDialerKeyComponent(action) +
|
|
16
|
+
MATERIALIZED_DIALER_KEY_SEPARATOR +
|
|
17
|
+
normalizeDialerKeyComponent(posId) +
|
|
18
|
+
MATERIALIZED_DIALER_KEY_SEPARATOR +
|
|
19
|
+
normalizeDialerKeyComponent(staffId));
|
|
20
|
+
}
|
|
21
|
+
/** Splits a key back into its parts, or `null` when it is malformed. */
|
|
22
|
+
export function parseMaterializedDialerKey(key) {
|
|
23
|
+
if (key == null)
|
|
24
|
+
return null;
|
|
25
|
+
const parts = String(key).split(MATERIALIZED_DIALER_KEY_SEPARATOR);
|
|
26
|
+
if (parts.length !== 3)
|
|
27
|
+
return null;
|
|
28
|
+
const [action, pos_id, staff_id] = parts;
|
|
29
|
+
if (action === '' || pos_id === '' || staff_id === '')
|
|
30
|
+
return null;
|
|
31
|
+
return { action, pos_id, staff_id };
|
|
32
|
+
}
|
|
33
|
+
/** True when NO filter dimension is set (the key passes unchanged). */
|
|
34
|
+
export function isDialerKeyFilterEmpty(filters) {
|
|
35
|
+
if (filters == null)
|
|
36
|
+
return true;
|
|
37
|
+
return ((filters.actions == null || filters.actions.length === 0) &&
|
|
38
|
+
(filters.pos_ids == null || filters.pos_ids.length === 0) &&
|
|
39
|
+
(filters.staff_ids == null || filters.staff_ids.length === 0));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Exact component-level filter match — the SINGLE filter semantics shared by
|
|
43
|
+
* the live compile, the historical counter fold and the details page.
|
|
44
|
+
*
|
|
45
|
+
* `UNKNOWN` is an ordinary component value: filtering on it selects the rows
|
|
46
|
+
* that genuinely have no staff/terminal, never "everything the filter
|
|
47
|
+
* excluded". A malformed key is never hidden by a filter.
|
|
48
|
+
*/
|
|
49
|
+
export function dialerKeyPassesFilters(key, filters) {
|
|
50
|
+
if (isDialerKeyFilterEmpty(filters))
|
|
51
|
+
return true;
|
|
52
|
+
const parsed = parseMaterializedDialerKey(key);
|
|
53
|
+
if (parsed == null)
|
|
54
|
+
return true;
|
|
55
|
+
if (filters?.actions != null && filters.actions.length > 0 && !filters.actions.includes(parsed.action)) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
if (filters?.pos_ids != null && filters.pos_ids.length > 0 && !filters.pos_ids.includes(parsed.pos_id)) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
if (filters?.staff_ids != null && filters.staff_ids.length > 0 && !filters.staff_ids.includes(parsed.staff_id)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
const escapeRegExp = (value) => value.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
67
|
+
/**
|
|
68
|
+
* Anchored MongoDB-key regex for a dialer filter, or `null` when no dimension
|
|
69
|
+
* is set. Each component is either an exact escaped value or a wildcard that
|
|
70
|
+
* cannot contain the separator, so a filter can never match a key PREFIX (the
|
|
71
|
+
* `ALL` action must not match `CALL_ANSWERED`).
|
|
72
|
+
*/
|
|
73
|
+
export function buildDialerKeyFilterRegex(filters) {
|
|
74
|
+
if (isDialerKeyFilterEmpty(filters))
|
|
75
|
+
return null;
|
|
76
|
+
const component = (values) => {
|
|
77
|
+
if (values == null || values.length === 0)
|
|
78
|
+
return '[^:]+';
|
|
79
|
+
return '(?:' + values.map((value) => escapeRegExp(String(value))).join('|') + ')';
|
|
80
|
+
};
|
|
81
|
+
return new RegExp('^' + component(filters?.actions) + ':' + component(filters?.pos_ids) + ':' + component(filters?.staff_ids) + '$');
|
|
82
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
const DIGIT_PATTERN = /\d/g;
|
|
30
|
+
/**
|
|
31
|
+
* Masks one phone number (see the header for the exact rule).
|
|
32
|
+
*
|
|
33
|
+
* Returns `null` for `null` / empty input, so a missing source number stays
|
|
34
|
+
* "unmeasured" rather than becoming the string `"null"`.
|
|
35
|
+
*/
|
|
36
|
+
export function maskDialerPhoneNumber(value) {
|
|
37
|
+
if (value == null)
|
|
38
|
+
return null;
|
|
39
|
+
const raw = String(value).trim();
|
|
40
|
+
if (raw === '')
|
|
41
|
+
return null;
|
|
42
|
+
const digits = raw.match(DIGIT_PATTERN) ?? [];
|
|
43
|
+
if (digits.length === 0) {
|
|
44
|
+
// No number to mask: do not echo arbitrary text (it may itself be PII).
|
|
45
|
+
return '****';
|
|
46
|
+
}
|
|
47
|
+
if (digits.length <= 4) {
|
|
48
|
+
// Too short to reveal ANY digit: the last-four rule would expose half of it.
|
|
49
|
+
return '*'.repeat(digits.length);
|
|
50
|
+
}
|
|
51
|
+
const visible = digits.slice(-4).join('');
|
|
52
|
+
const masked = '*'.repeat(digits.length - 4) + visible;
|
|
53
|
+
return raw.startsWith('+') ? `+${masked}` : masked;
|
|
54
|
+
}
|
|
@@ -5,6 +5,8 @@ import { MATERIALIZED_TENANT_DELIVERY_ENTRIES_COLLECTION_NAME, MATERIALIZED_TENA
|
|
|
5
5
|
import { MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_COLLECTION_NAME, MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_delivery_geo_cells';
|
|
6
6
|
import { MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_INDEX_OPTIONS } from '../../schemas/models/materialized_tenant_delivery_geo_scopes';
|
|
7
7
|
import { MATERIALIZED_TENANT_DELIVERY_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_DELIVERY_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_delivery_reports';
|
|
8
|
+
import { MATERIALIZED_TENANT_DIALER_ENTRIES_COLLECTION_NAME, MATERIALIZED_TENANT_DIALER_ENTRIES_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_dialer_entries';
|
|
9
|
+
import { MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_DIALER_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_dialer_reports';
|
|
8
10
|
import { MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_NAME, MATERIALIZED_TENANT_DISCOUNT_ENTRIES_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_discount_entries';
|
|
9
11
|
import { MATERIALIZED_TENANT_DISCOUNT_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_DISCOUNT_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_discount_reports';
|
|
10
12
|
import { MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME, MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_discount_sources';
|
|
@@ -42,6 +44,8 @@ const INDEXES = {
|
|
|
42
44
|
[MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_COLLECTION_NAME]: MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_INDEX_OPTIONS,
|
|
43
45
|
[MATERIALIZED_TENANT_DELIVERY_ENTRIES_COLLECTION_NAME]: MATERIALIZED_TENANT_DELIVERY_ENTRIES_INDEX_OPTIONS,
|
|
44
46
|
[MATERIALIZED_TENANT_DELIVERY_REPORTS_COLLECTION_NAME]: MATERIALIZED_TENANT_DELIVERY_REPORTS_INDEX_OPTIONS,
|
|
47
|
+
[MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_NAME]: MATERIALIZED_TENANT_DIALER_REPORTS_INDEX_OPTIONS,
|
|
48
|
+
[MATERIALIZED_TENANT_DIALER_ENTRIES_COLLECTION_NAME]: MATERIALIZED_TENANT_DIALER_ENTRIES_INDEX_OPTIONS,
|
|
45
49
|
[MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME]: MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS,
|
|
46
50
|
[MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME]: MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS,
|
|
47
51
|
[TENANT_CALENDAR_COLLECTION_NAME]: TENANT_CALENDAR_INDEX_OPTIONS,
|
|
@@ -60,6 +60,14 @@ const ROW_SPECS = {
|
|
|
60
60
|
orderId: (row) => row.order_id,
|
|
61
61
|
cursorId: (row) => row.payment_id,
|
|
62
62
|
},
|
|
63
|
+
[MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DIALER_ENTRIES]: {
|
|
64
|
+
rowsKey: 'dialer_entries',
|
|
65
|
+
// `created_at` is an ISO string on the dialer row; `toDate` accepts both.
|
|
66
|
+
time: (row) => row.created_at,
|
|
67
|
+
// Dialer rows are EVENT rows, not order rows (like cash ledger rows).
|
|
68
|
+
orderId: () => '',
|
|
69
|
+
cursorId: (row) => row.event_id,
|
|
70
|
+
},
|
|
63
71
|
};
|
|
64
72
|
/** The nested map key an entry collection uses for its row sections. */
|
|
65
73
|
export const getDrilldownRowsKey = (reportType) => ROW_SPECS[reportType].rowsKey;
|
|
@@ -102,7 +110,11 @@ export function buildMaterializedDrilldownRows(reportType, document, sourceLocal
|
|
|
102
110
|
// Discounts are served only when the row carries its kind; the nested
|
|
103
111
|
// collector applies the same requirement before it can be viewed.
|
|
104
112
|
const kindMissing = reportType === MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES && asString(row.kind) === '';
|
|
105
|
-
|
|
113
|
+
// Cash and Dialer rows are ledger/event rows and have no `order_id`; every
|
|
114
|
+
// other report's identity includes the order it belongs to.
|
|
115
|
+
const orderMissing = reportType !== MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES &&
|
|
116
|
+
reportType !== MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DIALER_ENTRIES &&
|
|
117
|
+
orderId === '';
|
|
106
118
|
if (time == null || cursorId === '' || kindMissing || orderMissing) {
|
|
107
119
|
skipped += 1;
|
|
108
120
|
continue;
|