@managesome/eposmatic-toolkit 0.4.55 → 0.4.57

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 (78) 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-dialer-accumulator-helpers.js +100 -0
  4. package/esm/helpers/materialized-reports/materialized-dialer-close-day-helpers.js +328 -0
  5. package/esm/helpers/materialized-reports/materialized-dialer-compile-helpers.js +180 -0
  6. package/esm/helpers/materialized-reports/materialized-dialer-entry-pages.js +222 -0
  7. package/esm/helpers/materialized-reports/materialized-dialer-key-helpers.js +82 -0
  8. package/esm/helpers/materialized-reports/materialized-dialer-masking.js +54 -0
  9. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
  10. package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +13 -1
  11. package/esm/helpers/materialized-reports/report-platform/definition.js +87 -3
  12. package/esm/helpers/materialized-reports/tenant-dialer-formatter.js +95 -0
  13. package/esm/helpers/reporting-contract/report-contract.js +3 -0
  14. package/esm/schemas/models/index.js +2 -0
  15. package/esm/schemas/models/materialized_report_drilldown_rows.js +1 -0
  16. package/esm/schemas/models/materialized_tenant_delivery_entries.js +5 -5
  17. package/esm/schemas/models/materialized_tenant_delivery_reports.js +7 -7
  18. package/esm/schemas/models/materialized_tenant_dialer_entries.js +65 -0
  19. package/esm/schemas/models/materialized_tenant_dialer_reports.js +66 -0
  20. package/esm/schemas/types/index.js +2 -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/lib/constants/MATERIALIZED_REPORTS.d.ts +72 -2
  25. package/lib/constants/MATERIALIZED_REPORTS.js +93 -1
  26. package/lib/helpers/materialized-reports/index.d.ts +7 -0
  27. package/lib/helpers/materialized-reports/index.js +7 -0
  28. package/lib/helpers/materialized-reports/materialized-dialer-accumulator-helpers.d.ts +39 -0
  29. package/lib/helpers/materialized-reports/materialized-dialer-accumulator-helpers.js +110 -0
  30. package/lib/helpers/materialized-reports/materialized-dialer-close-day-helpers.d.ts +90 -0
  31. package/lib/helpers/materialized-reports/materialized-dialer-close-day-helpers.js +340 -0
  32. package/lib/helpers/materialized-reports/materialized-dialer-compile-helpers.d.ts +67 -0
  33. package/lib/helpers/materialized-reports/materialized-dialer-compile-helpers.js +183 -0
  34. package/lib/helpers/materialized-reports/materialized-dialer-entry-pages.d.ts +97 -0
  35. package/lib/helpers/materialized-reports/materialized-dialer-entry-pages.js +235 -0
  36. package/lib/helpers/materialized-reports/materialized-dialer-key-helpers.d.ts +51 -0
  37. package/lib/helpers/materialized-reports/materialized-dialer-key-helpers.js +90 -0
  38. package/lib/helpers/materialized-reports/materialized-dialer-masking.d.ts +35 -0
  39. package/lib/helpers/materialized-reports/materialized-dialer-masking.js +57 -0
  40. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
  41. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +13 -1
  42. package/lib/helpers/materialized-reports/report-platform/definition.js +86 -2
  43. package/lib/helpers/materialized-reports/tenant-dialer-formatter.d.ts +91 -0
  44. package/lib/helpers/materialized-reports/tenant-dialer-formatter.js +98 -0
  45. package/lib/helpers/reporting-contract/report-contract.d.ts +1 -0
  46. package/lib/helpers/reporting-contract/report-contract.js +3 -0
  47. package/lib/schemas/models/index.d.ts +2 -0
  48. package/lib/schemas/models/index.js +2 -0
  49. package/lib/schemas/models/materialized_report_drilldown_rows.js +1 -0
  50. package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +5 -5
  51. package/lib/schemas/models/materialized_tenant_delivery_entries.js +5 -5
  52. package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +7 -7
  53. package/lib/schemas/models/materialized_tenant_delivery_reports.js +7 -7
  54. package/lib/schemas/models/materialized_tenant_dialer_entries.d.ts +107 -0
  55. package/lib/schemas/models/materialized_tenant_dialer_entries.js +68 -0
  56. package/lib/schemas/models/materialized_tenant_dialer_reports.d.ts +100 -0
  57. package/lib/schemas/models/materialized_tenant_dialer_reports.js +69 -0
  58. package/lib/schemas/models/tenant_dialer_events.d.ts +3 -3
  59. package/lib/schemas/types/index.d.ts +2 -0
  60. package/lib/schemas/types/index.js +2 -0
  61. package/lib/schemas/types/materialized_report_drilldown_rows.d.ts +1 -1
  62. package/lib/schemas/types/materialized_report_drilldown_rows.js +1 -0
  63. package/lib/schemas/types/materialized_tenant_dialer_entries.d.ts +83 -0
  64. package/lib/schemas/types/materialized_tenant_dialer_entries.js +2 -0
  65. package/lib/schemas/types/materialized_tenant_dialer_reports.d.ts +77 -0
  66. package/lib/schemas/types/materialized_tenant_dialer_reports.js +10 -0
  67. package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +1 -1
  68. package/lib/tests/materialized-reports/materialized-delivery-practical.test.js +1 -1
  69. package/lib/tests/materialized-reports/materialized-dialer-compile.test.d.ts +1 -0
  70. package/lib/tests/materialized-reports/materialized-dialer-compile.test.js +213 -0
  71. package/lib/tests/materialized-reports/materialized-dialer-formatter.test.d.ts +1 -0
  72. package/lib/tests/materialized-reports/materialized-dialer-formatter.test.js +159 -0
  73. package/lib/tests/materialized-reports/materialized-dialer-plan.test.d.ts +1 -0
  74. package/lib/tests/materialized-reports/materialized-dialer-plan.test.js +223 -0
  75. package/lib/tests/materialized-reports/materialized-tenant-dialer-schema.test.d.ts +1 -0
  76. package/lib/tests/materialized-reports/materialized-tenant-dialer-schema.test.js +183 -0
  77. package/lib/tests/materialized-reports/report-platform/registry-conformance.test.js +16 -3
  78. package/package.json +1 -1
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const materialized_dialer_compile_helpers_1 = require("../../helpers/materialized-reports/materialized-dialer-compile-helpers");
4
+ const tenant_dialer_formatter_1 = require("../../helpers/materialized-reports/tenant-dialer-formatter");
5
+ const DIALER_ENUMS_1 = require("../../constants/DIALER_ENUMS");
6
+ /**
7
+ * Dialer formatter.
8
+ *
9
+ * The stored documents are counts only: the answer/missed rates and every
10
+ * breakdown are derived HERE, so the page never sees a stored average and the
11
+ * formatter cannot drift from the counters.
12
+ *
13
+ * APPROVED RATE CONTRACT (load-bearing):
14
+ * `outcome_denominator = answered + missed + declined`;
15
+ * `answer_rate = answered / outcome_denominator`;
16
+ * `missed_rate = missed / outcome_denominator`.
17
+ * Percentage, 2 decimals, null when the denominator is 0. `events` is NOT the
18
+ * denominator — incoming/outgoing/ended/ping events are activity, not outcomes.
19
+ */
20
+ const TENANT = 'tenant-format';
21
+ const BRAND = 'brand-format';
22
+ const DAY = '2026-09-14';
23
+ const event = (over = {}) => ({
24
+ _id: `f-${Math.random().toString(36).slice(2)}`,
25
+ tenant_id: TENANT,
26
+ request_id: `r-${Math.random().toString(36).slice(2)}`,
27
+ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL,
28
+ phone_number: '+923001234567',
29
+ pos_id: 'POS-01',
30
+ staff_id: 'staff-1',
31
+ localDate: DAY,
32
+ localHour: 10,
33
+ createdAt: new Date(`${DAY}T05:00:00.000Z`),
34
+ updatedAt: new Date(`${DAY}T05:00:00.000Z`),
35
+ ...over,
36
+ });
37
+ const compiled = (events) => (0, materialized_dialer_compile_helpers_1.compileTenantDialerReports)({
38
+ tenantId: TENANT,
39
+ brandId: BRAND,
40
+ localDate: DAY,
41
+ events,
42
+ computedAt: new Date(`${DAY}T23:00:00.000Z`),
43
+ });
44
+ const pageOf = (events, options = {}) => (0, tenant_dialer_formatter_1.formatMaterializedDialerReport)(compiled(events).reports, options);
45
+ describe('dialer formatter', () => {
46
+ it('sums counters, derives rates from the outcome denominator and exposes all 24 hours', () => {
47
+ const page = pageOf([
48
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL }),
49
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_ANSWERED }),
50
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.MISSED_CALL, localHour: 11 }),
51
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_ANSWERED, localHour: 11 }),
52
+ ]);
53
+ expect(page.summary.events).toBe(4);
54
+ expect(page.summary.answered).toBe(2);
55
+ expect(page.summary.missed).toBe(1);
56
+ expect(page.summary.declined).toBe(0);
57
+ expect(page.summary.incoming_calls).toBe(1);
58
+ // denominator = 2 answered + 1 missed + 0 declined = 3 (NOT 4 events).
59
+ expect(page.summary.answer_rate).toBe(66.67);
60
+ expect(page.summary.missed_rate).toBe(33.33);
61
+ expect(page.by_hour).toHaveLength(24);
62
+ expect(page.by_hour[10].events).toBe(2);
63
+ expect(page.by_hour[11].events).toBe(2);
64
+ expect(page.by_hour[0].events).toBe(0);
65
+ expect(page.by_hour.map((row) => row.label)).toEqual(Array.from({ length: 24 }, (_, hour) => String(hour)));
66
+ expect(page.tenant_ids).toEqual([TENANT]);
67
+ expect(page.scopes).toEqual([{ scope_context: 'localDate', scope_field: DAY }]);
68
+ });
69
+ it('ignores non-outcome events in the denominator and includes declined', () => {
70
+ const page = pageOf([
71
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL }),
72
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.OUTGOING_CALL }),
73
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_ENDED }),
74
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.POS_PING }),
75
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_ANSWERED }),
76
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_ANSWERED }),
77
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.MISSED_CALL }),
78
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_DECLINED }),
79
+ ]);
80
+ expect(page.summary.events).toBe(8);
81
+ // denominator = 2 answered + 1 missed + 1 declined = 4 (not 8 events).
82
+ expect(page.summary.answer_rate).toBe(50);
83
+ expect(page.summary.missed_rate).toBe(25);
84
+ });
85
+ it('returns null rates when there is no outcome, and 0 is a real rate', () => {
86
+ const activityOnly = pageOf([
87
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL }),
88
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.POS_PING }),
89
+ ]);
90
+ expect(activityOnly.summary.events).toBe(2);
91
+ expect(activityOnly.summary.answer_rate).toBeNull();
92
+ expect(activityOnly.summary.missed_rate).toBeNull();
93
+ const declinedOnly = pageOf([event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_DECLINED })]);
94
+ expect(declinedOnly.summary.answer_rate).toBe(0);
95
+ expect(declinedOnly.summary.missed_rate).toBe(0);
96
+ });
97
+ it('rounds every rate to 2 decimals', () => {
98
+ const page = pageOf([
99
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_ANSWERED }),
100
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_DECLINED }),
101
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_DECLINED }),
102
+ ]);
103
+ // 1 / 3 = 33.333… %, 2 / 3 = 66.666… % (missed is 0 / 3).
104
+ expect(page.summary.answer_rate).toBe(33.33);
105
+ expect(page.summary.missed_rate).toBe(0);
106
+ });
107
+ it('groups by action, terminal and staff deterministically with the same denominator', () => {
108
+ const page = pageOf([
109
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL, pos_id: 'POS-01', staff_id: 'staff-1' }),
110
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_ANSWERED, pos_id: 'POS-01', staff_id: 'staff-1' }),
111
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_ANSWERED, pos_id: 'POS-01', staff_id: 'staff-1' }),
112
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.MISSED_CALL, pos_id: 'POS-01', staff_id: 'staff-1' }),
113
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.MISSED_CALL, pos_id: 'POS-02', staff_id: 'staff-2' }),
114
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.CALL_DECLINED, pos_id: 'POS-02', staff_id: 'staff-2' }),
115
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL, pos_id: 'POS-03', staff_id: null }),
116
+ ]);
117
+ expect(page.by_pos.map((row) => [row.key, row.events])).toEqual([
118
+ ['POS-01', 4],
119
+ ['POS-02', 2],
120
+ ['POS-03', 1],
121
+ ]);
122
+ expect(page.by_staff.map((row) => row.key).sort()).toEqual(['UNKNOWN', 'staff-1', 'staff-2']);
123
+ expect(page.by_action.find((row) => row.key === 'MISSED_CALL').events).toBe(2);
124
+ const pos01 = page.by_pos.find((row) => row.key === 'POS-01');
125
+ // 2 answered / (2 answered + 1 missed + 0 declined) = 66.67 %.
126
+ expect(pos01.answer_rate).toBe(66.67);
127
+ const pos02 = page.by_pos.find((row) => row.key === 'POS-02');
128
+ // 0 answered / (0 + 1 missed + 1 declined) = 0 % — a real rate, not null.
129
+ expect(pos02.answer_rate).toBe(0);
130
+ const pos03 = page.by_pos.find((row) => row.key === 'POS-03');
131
+ // No outcome at all -> null, even though the row has an event.
132
+ expect(pos03.answer_rate).toBeNull();
133
+ expect(page.by_staff.find((row) => row.key === 'staff-2').answer_rate).toBe(0);
134
+ expect(page.by_action.find((row) => row.key === 'CALL_ANSWERED').answer_rate).toBe(100);
135
+ expect(page.by_action.find((row) => row.key === 'INCOMING_CALL').answer_rate).toBeNull();
136
+ // A filter narrows the breakdown AND the totals with the same semantics.
137
+ const filtered = (0, tenant_dialer_formatter_1.formatMaterializedDialerReport)(compiled([
138
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL, pos_id: 'POS-02' }),
139
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.MISSED_CALL, pos_id: 'POS-02' }),
140
+ ]).reports, { filters: { pos_ids: ['POS-02'] } });
141
+ expect(filtered.summary.events).toBe(2);
142
+ expect(filtered.summary.answer_rate).toBe(0);
143
+ expect(filtered.by_pos).toHaveLength(1);
144
+ });
145
+ it('returns a zeroed page for no documents (absence means zero)', () => {
146
+ const page = (0, tenant_dialer_formatter_1.formatMaterializedDialerReport)([]);
147
+ expect(page.summary.events).toBe(0);
148
+ expect(page.summary.answer_rate).toBeNull();
149
+ expect(page.summary.missed_rate).toBeNull();
150
+ expect(page.by_hour.every((row) => row.events === 0)).toBe(true);
151
+ expect(page.by_action).toEqual([]);
152
+ expect(page.by_pos).toEqual([]);
153
+ expect(page.by_staff).toEqual([]);
154
+ });
155
+ it('never exposes a phone number', () => {
156
+ const docs = compiled([event()]).reports;
157
+ expect(JSON.stringify((0, tenant_dialer_formatter_1.formatMaterializedDialerReport)(docs))).not.toContain('923001234567');
158
+ });
159
+ });
@@ -0,0 +1,223 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
+ const materialized_dialer_compile_helpers_1 = require("../../helpers/materialized-reports/materialized-dialer-compile-helpers");
5
+ const materialized_dialer_close_day_helpers_1 = require("../../helpers/materialized-reports/materialized-dialer-close-day-helpers");
6
+ const materialized_dialer_accumulator_helpers_1 = require("../../helpers/materialized-reports/materialized-dialer-accumulator-helpers");
7
+ const DIALER_ENUMS_1 = require("../../constants/DIALER_ENUMS");
8
+ /**
9
+ * Dialer close-day plan.
10
+ *
11
+ * The properties that make a re-close safe:
12
+ * - the DAY document is replaced from the fresh compile;
13
+ * - every touched PERIOD is re-folded from the whole day set (never incremented);
14
+ * - entry pages are replaced by page identity, and a page the day no longer owns
15
+ * is deleted;
16
+ * - a re-close with an unchanged watermark writes nothing;
17
+ * - an emptied day deletes its day document and its pages.
18
+ */
19
+ const TENANT = 'tenant-plan';
20
+ const BRAND = 'brand-plan';
21
+ const DAY = '2026-09-14';
22
+ const COMPUTED_AT = new Date('2026-09-15T06:00:00.000Z');
23
+ const event = (id, action = DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL) => ({
24
+ _id: id,
25
+ tenant_id: TENANT,
26
+ request_id: id,
27
+ action,
28
+ phone_number: '+923001234567',
29
+ pos_id: 'POS-01',
30
+ staff_id: 'staff-1',
31
+ localDate: DAY,
32
+ localHour: 10,
33
+ createdAt: new Date(`${DAY}T05:00:00.000Z`),
34
+ updatedAt: new Date(`${DAY}T05:00:00.000Z`),
35
+ });
36
+ const compile = (ids = ['e1', 'e2']) => (0, materialized_dialer_compile_helpers_1.compileTenantDialerReports)({
37
+ tenantId: TENANT,
38
+ brandId: BRAND,
39
+ localDate: DAY,
40
+ events: ids.map((id) => event(id)),
41
+ computedAt: COMPUTED_AT,
42
+ });
43
+ const scope = (doc) => `${doc.scope_context}|${doc.scope_field}`;
44
+ describe('dialer close-day plan', () => {
45
+ it('writes the day document, re-folded periods and one page per entry scope', () => {
46
+ const compiled = compile();
47
+ const plan = (0, materialized_dialer_close_day_helpers_1.buildTenantDialerCloseDayPlan)({
48
+ tenantId: TENANT,
49
+ reports: compiled.reports,
50
+ entries: compiled.entries,
51
+ localDate: DAY,
52
+ reason: 'NEW_DAY',
53
+ computedAt: COMPUTED_AT,
54
+ });
55
+ expect(plan.writes.map((write) => scope(write.report)).sort()).toEqual([
56
+ `localDate|${DAY}`,
57
+ 'localMonth|2026-09',
58
+ 'localQuarter|2026-Q3',
59
+ 'localSemester|2026-S2',
60
+ 'localWeek|2026-09-13',
61
+ 'localWeekOfYear|2026-W38',
62
+ 'localYear|2026',
63
+ ].sort());
64
+ expect(plan.entryWrites).toHaveLength(4);
65
+ expect(plan.staleDocuments).toHaveLength(0);
66
+ // Every write carries an iteration row of the right report type.
67
+ expect(plan.writes.every((write) => write.iteration.report_type === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DIALER_REPORTS)).toBe(true);
68
+ expect(plan.entryWrites.every((write) => write.iteration.report_type === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DIALER_ENTRIES)).toBe(true);
69
+ const month = plan.writes.find((write) => write.report.scope_context === 'localMonth');
70
+ expect(Object.values(month.report.dialer_counters)[0].events).toBe(2);
71
+ expect(month.report.day_index).toBeNull();
72
+ });
73
+ it('folds sibling DAY documents into the period counters', () => {
74
+ const compiled = compile(['e1']);
75
+ const sibling = compile(['e2']);
76
+ const siblingDay = {
77
+ ...sibling.reports[0],
78
+ _id: 'sibling-id',
79
+ scope_field: '2026-09-13',
80
+ dialer_counters: {
81
+ 'INCOMING_CALL:POS-01:staff-1': {
82
+ ...(0, materialized_dialer_accumulator_helpers_1.freshDialerCounters)(),
83
+ events: 5,
84
+ incoming_calls: 5,
85
+ by_hour: { ...(0, materialized_dialer_accumulator_helpers_1.freshDialerCounters)().by_hour, '9': 5 },
86
+ },
87
+ },
88
+ };
89
+ const plan = (0, materialized_dialer_close_day_helpers_1.buildTenantDialerCloseDayPlan)({
90
+ tenantId: TENANT,
91
+ reports: compiled.reports,
92
+ entries: compiled.entries,
93
+ existing: [siblingDay],
94
+ localDate: DAY,
95
+ reason: 'RECLOSE',
96
+ computedAt: COMPUTED_AT,
97
+ });
98
+ const month = plan.writes.find((write) => write.report.scope_context === 'localMonth');
99
+ const counters = month.report.dialer_counters['INCOMING_CALL:POS-01:staff-1'];
100
+ expect(counters.events).toBe(1 + 5);
101
+ expect(counters.by_hour['9']).toBe(5);
102
+ expect(counters.by_hour['10']).toBe(1);
103
+ });
104
+ it('is idempotent on an unchanged re-close (watermark unchanged)', () => {
105
+ const compiled = compile();
106
+ const first = (0, materialized_dialer_close_day_helpers_1.buildTenantDialerCloseDayPlan)({
107
+ tenantId: TENANT,
108
+ reports: compiled.reports,
109
+ entries: compiled.entries,
110
+ localDate: DAY,
111
+ reason: 'NEW_DAY',
112
+ computedAt: COMPUTED_AT,
113
+ });
114
+ const existingReports = first.writes.map((write) => ({ ...write.report, _id: `id-${write.report.scope_context}` }));
115
+ const existingEntries = first.entryWrites.map((write) => ({
116
+ ...write.report,
117
+ _id: `entry-${write.report.scope_context}`,
118
+ iteration: 1,
119
+ }));
120
+ const second = (0, materialized_dialer_close_day_helpers_1.buildTenantDialerCloseDayPlan)({
121
+ tenantId: TENANT,
122
+ reports: compiled.reports.map((report) => ({
123
+ ...report,
124
+ source_watermark: new Date(existingReports[0].source_watermark),
125
+ })),
126
+ entries: compiled.entries.map((entry) => ({
127
+ ...entry,
128
+ source_watermark: new Date(existingReports[0].source_watermark),
129
+ })),
130
+ existing: existingReports,
131
+ existingEntries,
132
+ localDate: DAY,
133
+ reason: 'RECLOSE',
134
+ computedAt: COMPUTED_AT,
135
+ });
136
+ expect(second.writes).toHaveLength(0);
137
+ expect(second.entryWrites).toHaveLength(0);
138
+ expect(second.skippedDocuments.length).toBeGreaterThan(0);
139
+ expect(second.skippedEntryDocuments).toHaveLength(4);
140
+ });
141
+ it('deletes the day document and its pages when the day no longer has events', () => {
142
+ const compiled = compile();
143
+ const first = (0, materialized_dialer_close_day_helpers_1.buildTenantDialerCloseDayPlan)({
144
+ tenantId: TENANT,
145
+ reports: compiled.reports,
146
+ entries: compiled.entries,
147
+ localDate: DAY,
148
+ reason: 'NEW_DAY',
149
+ computedAt: COMPUTED_AT,
150
+ });
151
+ const existingReports = first.writes.map((write) => ({ ...write.report, _id: `id-${write.report.scope_context}` }));
152
+ const existingEntries = first.entryWrites.map((write) => ({
153
+ ...write.report,
154
+ _id: `entry-${write.report.scope_context}`,
155
+ }));
156
+ const emptied = (0, materialized_dialer_close_day_helpers_1.buildTenantDialerCloseDayPlan)({
157
+ tenantId: TENANT,
158
+ reports: [],
159
+ entries: [],
160
+ existing: existingReports,
161
+ existingEntries,
162
+ localDate: DAY,
163
+ reason: 'RECLOSE',
164
+ computedAt: COMPUTED_AT,
165
+ });
166
+ expect(emptied.writes).toHaveLength(0);
167
+ expect(emptied.entryWrites).toHaveLength(0);
168
+ expect(emptied.staleDocuments.map((doc) => scope(doc)).sort()).toEqual(existingReports
169
+ .filter((doc) => doc.scope_context !== 'localDate')
170
+ .map(scope)
171
+ .sort()
172
+ .concat([`localDate|${DAY}`])
173
+ .sort());
174
+ expect(emptied.staleEntryDocuments).toHaveLength(4);
175
+ });
176
+ it('does not use `_id` from the compile as a stored identity', () => {
177
+ const compiled = compile();
178
+ const plan = (0, materialized_dialer_close_day_helpers_1.buildTenantDialerCloseDayPlan)({
179
+ tenantId: TENANT,
180
+ reports: compiled.reports,
181
+ entries: compiled.entries,
182
+ localDate: DAY,
183
+ reason: 'NEW_DAY',
184
+ computedAt: COMPUTED_AT,
185
+ });
186
+ for (const write of [...plan.writes, ...plan.entryWrites]) {
187
+ expect(typeof write.report._id).toBe('string');
188
+ expect(String(write.report._id)).not.toBe('undefined');
189
+ expect(write.iteration.report_id).toBe(String(write.report._id));
190
+ }
191
+ });
192
+ it('folds counters with the shared merge (sums every hour)', () => {
193
+ const left = (0, materialized_dialer_accumulator_helpers_1.freshDialerCounters)();
194
+ const right = (0, materialized_dialer_accumulator_helpers_1.freshDialerCounters)();
195
+ left.events = 1;
196
+ left.by_hour['0'] = 1;
197
+ right.events = 2;
198
+ right.by_hour['23'] = 2;
199
+ (0, materialized_dialer_accumulator_helpers_1.mergeDialerCounters)(left, right);
200
+ expect(left.events).toBe(3);
201
+ expect(left.by_hour['0']).toBe(1);
202
+ expect(left.by_hour['23']).toBe(2);
203
+ // Merge returns the same key set (24 hours) and never a partial map.
204
+ expect(Object.keys(left.by_hour)).toHaveLength(24);
205
+ });
206
+ it('produces documents whose day scope is the only one carrying day_index', () => {
207
+ const compiled = compile();
208
+ const plan = (0, materialized_dialer_close_day_helpers_1.buildTenantDialerCloseDayPlan)({
209
+ tenantId: TENANT,
210
+ reports: compiled.reports,
211
+ entries: compiled.entries,
212
+ localDate: DAY,
213
+ reason: 'NEW_DAY',
214
+ computedAt: COMPUTED_AT,
215
+ });
216
+ for (const write of plan.writes) {
217
+ if (write.report.scope_context === 'localDate')
218
+ expect(write.report.day_index).not.toBeNull();
219
+ else
220
+ expect(write.report.day_index).toBeNull();
221
+ }
222
+ });
223
+ });
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
+ const materialized_tenant_dialer_entries_1 = require("../../schemas/models/materialized_tenant_dialer_entries");
5
+ const materialized_tenant_dialer_reports_1 = require("../../schemas/models/materialized_tenant_dialer_reports");
6
+ const definition_1 = require("../../helpers/materialized-reports/report-platform/definition");
7
+ const validate_report_registry_1 = require("../../helpers/materialized-reports/report-platform/validate-report-registry");
8
+ const report_contract_1 = require("../../helpers/reporting-contract/report-contract");
9
+ /**
10
+ * Dialer schema + registry contract.
11
+ *
12
+ * Indexes are DECLARED here, never created: the model must not call `.index()`
13
+ * and no service runs `ensureIndexes`. Applying them is an owner deploy step.
14
+ */
15
+ describe('dialer schema contract', () => {
16
+ it('declares the two primary collections and their unique identities', () => {
17
+ expect(materialized_tenant_dialer_reports_1.MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_NAME).toBe('materialized_tenant_dialer_reports');
18
+ expect(materialized_tenant_dialer_entries_1.MATERIALIZED_TENANT_DIALER_ENTRIES_COLLECTION_NAME).toBe('materialized_tenant_dialer_entries');
19
+ expect(materialized_tenant_dialer_reports_1.MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION).toBe(1);
20
+ expect(materialized_tenant_dialer_entries_1.MATERIALIZED_TENANT_DIALER_ENTRIES_SCHEMA_VERSION).toBe(1);
21
+ expect(materialized_tenant_dialer_reports_1.MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_INDEXES.tenant_id__scope_context__scope_field__day_index).toEqual({
22
+ tenant_id: 1,
23
+ scope_context: 1,
24
+ scope_field: 1,
25
+ day_index: 1,
26
+ });
27
+ expect(materialized_tenant_dialer_reports_1.MATERIALIZED_TENANT_DIALER_REPORTS_INDEX_OPTIONS).toEqual([
28
+ {
29
+ key: { tenant_id: 1, scope_context: 1, scope_field: 1, day_index: 1 },
30
+ name: 'tenant_id__scope_context__scope_field__day_index',
31
+ unique: true,
32
+ },
33
+ ]);
34
+ expect(materialized_tenant_dialer_entries_1.MATERIALIZED_TENANT_DIALER_ENTRIES_COLLECTION_INDEXES.tenant_id__scope_context__scope_field__page).toEqual({
35
+ tenant_id: 1,
36
+ scope_context: 1,
37
+ scope_field: 1,
38
+ page: 1,
39
+ });
40
+ expect(materialized_tenant_dialer_entries_1.MATERIALIZED_TENANT_DIALER_ENTRIES_INDEX_OPTIONS).toEqual([
41
+ {
42
+ key: { tenant_id: 1, scope_context: 1, scope_field: 1, page: 1 },
43
+ name: 'tenant_id__scope_context__scope_field__page',
44
+ unique: true,
45
+ },
46
+ ]);
47
+ });
48
+ it('declares its scopes, key filters and report types', () => {
49
+ expect(MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_REPORT_SCOPES).toEqual([
50
+ 'localDate',
51
+ 'localWeek',
52
+ 'localWeekOfYear',
53
+ 'localMonth',
54
+ 'localQuarter',
55
+ 'localSemester',
56
+ 'localYear',
57
+ ]);
58
+ expect(MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_ENTRY_SCOPES).toEqual(['localDate', 'localWeek', 'localWeekOfYear', 'localMonth']);
59
+ expect(MATERIALIZED_REPORTS_1.DIALER_KEY_FILTERS).toEqual(['ACTION', 'POS_ID', 'STAFF_ID']);
60
+ expect(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DIALER_REPORTS).toBe('TENANT_DIALER_REPORTS');
61
+ expect(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DIALER_ENTRIES).toBe('TENANT_DIALER_ENTRIES');
62
+ });
63
+ it('declares the identity fields and defaults on the models', () => {
64
+ const reportPaths = Object.keys(materialized_tenant_dialer_reports_1.MaterializedTenantDialerReportSchema.paths);
65
+ expect(reportPaths).toEqual(expect.arrayContaining([
66
+ 'tenant_id',
67
+ 'brand_id',
68
+ 'scope_context',
69
+ 'scope_field',
70
+ 'day_index',
71
+ 'sale_type_key_filters',
72
+ 'dialer_counters',
73
+ 'schema_version',
74
+ 'source_watermark',
75
+ 'computed_at',
76
+ ]));
77
+ expect(materialized_tenant_dialer_reports_1.MaterializedTenantDialerReportSchema.path('sale_type_key_filters').defaultValue()).toEqual(MATERIALIZED_REPORTS_1.DIALER_KEY_FILTERS);
78
+ const entryPaths = Object.keys(materialized_tenant_dialer_entries_1.MaterializedTenantDialerEntriesSchema.paths);
79
+ expect(entryPaths).toEqual(expect.arrayContaining(['page', 'dialer_entries', 'schema_version', 'source_watermark', 'computed_at']));
80
+ expect(entryPaths).not.toContain('sale_type_key_filters');
81
+ });
82
+ it('keeps the approved raw contract: no duration, order attribution, geo or raw phone', () => {
83
+ const reportPaths = Object.keys(materialized_tenant_dialer_reports_1.MaterializedTenantDialerReportSchema.paths);
84
+ const entryPaths = Object.keys(materialized_tenant_dialer_entries_1.MaterializedTenantDialerEntriesSchema.paths);
85
+ for (const forbidden of [
86
+ 'duration',
87
+ 'duration_seconds',
88
+ 'call_duration',
89
+ 'answer_rate',
90
+ 'missed_rate',
91
+ 'order_id',
92
+ 'phone_number',
93
+ 'staff_phone',
94
+ 'location',
95
+ 'h3_cell',
96
+ ]) {
97
+ expect(reportPaths).not.toContain(forbidden);
98
+ expect(entryPaths).not.toContain(forbidden);
99
+ }
100
+ // The entry row carries MASKED phones only.
101
+ const rowShape = {
102
+ event_id: 'e',
103
+ request_id: 'r',
104
+ action: 'INCOMING_CALL',
105
+ phone_number_masked: '+********4567',
106
+ staff_id: null,
107
+ staff_phone_masked: null,
108
+ pos_id: 'POS-01',
109
+ created_at: '2026-09-14T05:00:00.000Z',
110
+ local_date: '2026-09-14',
111
+ local_hour: 10,
112
+ };
113
+ expect(Object.keys(rowShape).sort()).toEqual([
114
+ 'action',
115
+ 'created_at',
116
+ 'event_id',
117
+ 'local_date',
118
+ 'local_hour',
119
+ 'phone_number_masked',
120
+ 'pos_id',
121
+ 'request_id',
122
+ 'staff_id',
123
+ 'staff_phone_masked',
124
+ ]);
125
+ // No geo projection is registered for this report.
126
+ expect(definition_1.REPORT_DEFINITIONS[report_contract_1.REPORT_KEY_ENUM.DIALER].projections).toEqual([definition_1.REPORT_PROJECTION.FLAT_DRILLDOWN]);
127
+ expect(definition_1.REPORT_DEFINITIONS[report_contract_1.REPORT_KEY_ENUM.DIALER].sideCollections.map((collection) => collection.name)).toEqual([
128
+ 'materialized_report_drilldown_rows',
129
+ 'materialized_report_drilldown_scopes',
130
+ ]);
131
+ });
132
+ });
133
+ describe('dialer report definition', () => {
134
+ const definition = definition_1.REPORT_DEFINITIONS[report_contract_1.REPORT_KEY_ENUM.DIALER];
135
+ it('is a registered production report with the shared contract', () => {
136
+ expect(definition).toBeDefined();
137
+ expect(definition.status).toBe(definition_1.REPORT_DEFINITION_STATUS.PRODUCTION);
138
+ expect(definition.segment).toBe('dialer');
139
+ expect(report_contract_1.REPORT_ROUTE_SEGMENTS[report_contract_1.REPORT_KEY_ENUM.DIALER]).toBe('dialer');
140
+ expect(report_contract_1.REPORT_CAPABILITIES[report_contract_1.REPORT_KEY_ENUM.DIALER]).toEqual({
141
+ live: true,
142
+ historical: true,
143
+ details: true,
144
+ });
145
+ expect(definition.capabilities).toEqual(report_contract_1.REPORT_CAPABILITIES[report_contract_1.REPORT_KEY_ENUM.DIALER]);
146
+ });
147
+ it('declares page-per-source-day entries with deterministic sub-pages and a flat projection', () => {
148
+ expect(definition.entryStorage).toBe(definition_1.REPORT_ENTRY_STORAGE.PAGE_PER_SOURCE_DAY);
149
+ expect(definition.entrySplit).toBe(definition_1.REPORT_ENTRY_SPLIT.DETERMINISTIC_SUBPAGES);
150
+ expect(definition.maxReportScope).toBe('localYear');
151
+ expect(definition.maxEntryScope).toBe('localMonth');
152
+ expect(definition.projections).toEqual([definition_1.REPORT_PROJECTION.FLAT_DRILLDOWN]);
153
+ expect(definition.entryScopes).toEqual(MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_ENTRY_SCOPES);
154
+ expect(definition.reportScopes).toEqual(MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_REPORT_SCOPES);
155
+ });
156
+ it('declares server-side key filter dimensions and the report adapter ids', () => {
157
+ expect(definition.filters.map((filter) => filter.id)).toEqual(expect.arrayContaining(['ACTION', 'POS_ID', 'STAFF_ID', 'DETAILS_VIEW']));
158
+ for (const id of ['ACTION', 'POS_ID', 'STAFF_ID']) {
159
+ const filter = definition.filters.find((entry) => entry.id === id);
160
+ expect(filter.mode).toBe('SERVER');
161
+ expect(filter.kind).toBe('KEY_COMPONENT');
162
+ expect(filter.appliesTo).toBe('BOTH');
163
+ }
164
+ expect(definition.adapters).toEqual({
165
+ writer: 'materializeDialerReport',
166
+ live: 'getLiveTenantDialerReports',
167
+ historical: 'getMaterializedTenantDialerReports',
168
+ details: 'getMaterializedDialerDetailsPage',
169
+ });
170
+ expect(definition.cursor).toBe('KEYSET_STRUCTURED');
171
+ });
172
+ it('keeps the declarative registry valid', () => {
173
+ const validation = (0, validate_report_registry_1.validateReportRegistry)();
174
+ expect(validation.errors).toEqual([]);
175
+ expect(validate_report_registry_1.REPORT_REGISTRY_VALIDATION.ok).toBe(true);
176
+ // Validator debt is declared explicitly (no new schema validator was written):
177
+ // the report, the entries, and the two shared flat side collections.
178
+ const dialerWarnings = validation.warnings.filter((warning) => warning.startsWith('DIALER:'));
179
+ expect(dialerWarnings).toHaveLength(4);
180
+ expect(dialerWarnings[0]).toContain('report validator debt');
181
+ expect(dialerWarnings[1]).toContain('entries validator debt');
182
+ });
183
+ });
@@ -49,6 +49,8 @@ const prepEntryModel = __importStar(require("../../../schemas/models/materialize
49
49
  const deliveryReportModel = __importStar(require("../../../schemas/models/materialized_tenant_delivery_reports"));
50
50
  const deliveryEntryModel = __importStar(require("../../../schemas/models/materialized_tenant_delivery_entries"));
51
51
  const deliveryGeoModel = __importStar(require("../../../schemas/models/materialized_tenant_delivery_geo_cells"));
52
+ const dialerReportModel = __importStar(require("../../../schemas/models/materialized_tenant_dialer_reports"));
53
+ const dialerEntryModel = __importStar(require("../../../schemas/models/materialized_tenant_dialer_entries"));
52
54
  const flatModel = __importStar(require("../../../schemas/models/materialized_report_drilldown_rows"));
53
55
  const definition_1 = require("../../../helpers/materialized-reports/report-platform/definition");
54
56
  const registry_1 = require("../../../helpers/materialized-reports/report-platform/registry");
@@ -110,6 +112,16 @@ const MODEL_BY_COLLECTION = {
110
112
  schemaVersion: MATERIALIZED_REPORTS_1.MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_SCHEMA_VERSION,
111
113
  indexOptions: [],
112
114
  },
115
+ materialized_tenant_dialer_reports: {
116
+ collectionName: dialerReportModel.MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_NAME,
117
+ schemaVersion: dialerReportModel.MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION,
118
+ indexOptions: dialerReportModel.MATERIALIZED_TENANT_DIALER_REPORTS_INDEX_OPTIONS,
119
+ },
120
+ materialized_tenant_dialer_entries: {
121
+ collectionName: dialerEntryModel.MATERIALIZED_TENANT_DIALER_ENTRIES_COLLECTION_NAME,
122
+ schemaVersion: dialerEntryModel.MATERIALIZED_TENANT_DIALER_ENTRIES_SCHEMA_VERSION,
123
+ indexOptions: dialerEntryModel.MATERIALIZED_TENANT_DIALER_ENTRIES_INDEX_OPTIONS,
124
+ },
113
125
  materialized_report_drilldown_rows: {
114
126
  collectionName: flatModel.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME,
115
127
  schemaVersion: flatModel.MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION,
@@ -127,15 +139,16 @@ const definitionCollections = () => definition_1.PRODUCTION_REPORT_DEFINITIONS.f
127
139
  ...definition.sideCollections,
128
140
  ]);
129
141
  describe('report registry conformance', () => {
130
- it('defines exactly the four production reports', () => {
142
+ it('defines exactly the production reports', () => {
131
143
  expect(definition_1.PRODUCTION_REPORT_DEFINITIONS.map((definition) => definition.key)).toEqual([
132
144
  report_contract_1.REPORT_KEY_ENUM.OVERVIEW,
133
145
  report_contract_1.REPORT_KEY_ENUM.DISCOUNTS,
134
146
  report_contract_1.REPORT_KEY_ENUM.PREP_TIME,
135
147
  report_contract_1.REPORT_KEY_ENUM.DELIVERY,
148
+ report_contract_1.REPORT_KEY_ENUM.DIALER,
136
149
  ]);
137
- expect((0, registry_1.getProductionReportDefinitions)()).toHaveLength(4);
138
- expect(new Set(definition_1.PRODUCTION_REPORT_DEFINITIONS.map((definition) => definition.key)).size).toBe(4);
150
+ expect((0, registry_1.getProductionReportDefinitions)()).toHaveLength(5);
151
+ expect(new Set(definition_1.PRODUCTION_REPORT_DEFINITIONS.map((definition) => definition.key)).size).toBe(5);
139
152
  expect(definition_1.PRODUCTION_REPORT_DEFINITIONS.every((definition) => definition.status === 'PRODUCTION')).toBe(true);
140
153
  });
141
154
  it('covers every REPORT_KEY with a definition or an explicit exemption', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.55",
3
+ "version": "0.4.57",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",