@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,100 @@
1
+ import { Schema } from 'mongoose';
2
+ /**
3
+ * Mongoose schema for `materialized_tenant_dialer_reports`.
4
+ *
5
+ * `dialer_counters` is `Mixed` on purpose: it is a key-addressed map of bounded
6
+ * counter blocks (`<ACTION>:<POS_ID>:<STAFF_ID>` → counters + `by_hour`), and
7
+ * the keys ARE the data — Mongoose cannot type them without a fixed key set.
8
+ *
9
+ * The collection holds COUNTERS only. Itemized event rows live in the sibling
10
+ * `materialized_tenant_dialer_entries` collection (one page per source day).
11
+ */
12
+ /** Document shape version. Bump on a key-grammar/metric change + backfill. */
13
+ export declare const MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION = 1;
14
+ export declare const MaterializedTenantDialerReportSchema: Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
15
+ timestamps: true;
16
+ versionKey: false;
17
+ minimize: false;
18
+ }, {
19
+ createdAt: NativeDate;
20
+ updatedAt: NativeDate;
21
+ } & {
22
+ createdAt: NativeDate;
23
+ updatedAt: NativeDate;
24
+ brand_id: string;
25
+ tenant_id: string;
26
+ day_index: import("../../constants/ENUMS").DAY_INDEX_ENUM | null;
27
+ iteration: number;
28
+ scope_context: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_SCOPE_ENUM;
29
+ scope_field: string;
30
+ sale_type_key_filters: string[];
31
+ schema_version: number;
32
+ source_watermark: NativeDate;
33
+ computed_at: NativeDate;
34
+ dialer_counters: any;
35
+ }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
36
+ createdAt: NativeDate;
37
+ updatedAt: NativeDate;
38
+ } & {
39
+ createdAt: NativeDate;
40
+ updatedAt: NativeDate;
41
+ brand_id: string;
42
+ tenant_id: string;
43
+ day_index: import("../../constants/ENUMS").DAY_INDEX_ENUM | null;
44
+ iteration: number;
45
+ scope_context: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_SCOPE_ENUM;
46
+ scope_field: string;
47
+ sale_type_key_filters: string[];
48
+ schema_version: number;
49
+ source_watermark: NativeDate;
50
+ computed_at: NativeDate;
51
+ dialer_counters: any;
52
+ }>> & import("mongoose").FlatRecord<{
53
+ createdAt: NativeDate;
54
+ updatedAt: NativeDate;
55
+ } & {
56
+ createdAt: NativeDate;
57
+ updatedAt: NativeDate;
58
+ brand_id: string;
59
+ tenant_id: string;
60
+ day_index: import("../../constants/ENUMS").DAY_INDEX_ENUM | null;
61
+ iteration: number;
62
+ scope_context: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_SCOPE_ENUM;
63
+ scope_field: string;
64
+ sale_type_key_filters: string[];
65
+ schema_version: number;
66
+ source_watermark: NativeDate;
67
+ computed_at: NativeDate;
68
+ dialer_counters: any;
69
+ }> & {
70
+ _id: import("mongoose").Types.ObjectId;
71
+ } & {
72
+ __v: number;
73
+ }>;
74
+ export declare const MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_NAME = "materialized_tenant_dialer_reports";
75
+ /**
76
+ * The single UNIQUE compound identity index, like every materialized report
77
+ * collection. Declared ONLY — no service calls `index()` or `ensureIndexes`, and
78
+ * the services run with `autoIndex: false`. Applying it is an owner deploy step.
79
+ */
80
+ export declare const MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_INDEXES: {
81
+ _id: {
82
+ _id: number;
83
+ };
84
+ tenant_id__scope_context__scope_field__day_index: {
85
+ tenant_id: number;
86
+ scope_context: number;
87
+ scope_field: number;
88
+ day_index: number;
89
+ };
90
+ };
91
+ export declare const MATERIALIZED_TENANT_DIALER_REPORTS_INDEX_OPTIONS: {
92
+ key: {
93
+ tenant_id: number;
94
+ scope_context: number;
95
+ scope_field: number;
96
+ day_index: number;
97
+ };
98
+ name: string;
99
+ unique: boolean;
100
+ }[];
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MATERIALIZED_TENANT_DIALER_REPORTS_INDEX_OPTIONS = exports.MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_NAME = exports.MaterializedTenantDialerReportSchema = exports.MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ const ENUMS_1 = require("../../constants/ENUMS");
6
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
7
+ /**
8
+ * Mongoose schema for `materialized_tenant_dialer_reports`.
9
+ *
10
+ * `dialer_counters` is `Mixed` on purpose: it is a key-addressed map of bounded
11
+ * counter blocks (`<ACTION>:<POS_ID>:<STAFF_ID>` → counters + `by_hour`), and
12
+ * the keys ARE the data — Mongoose cannot type them without a fixed key set.
13
+ *
14
+ * The collection holds COUNTERS only. Itemized event rows live in the sibling
15
+ * `materialized_tenant_dialer_entries` collection (one page per source day).
16
+ */
17
+ /** Document shape version. Bump on a key-grammar/metric change + backfill. */
18
+ exports.MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION = 1;
19
+ exports.MaterializedTenantDialerReportSchema = new mongoose_1.Schema({
20
+ tenant_id: { type: String, required: true },
21
+ brand_id: { type: String, required: true },
22
+ scope_context: { type: String, required: true, enum: MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_REPORT_SCOPES },
23
+ scope_field: { type: String, required: true },
24
+ day_index: { type: String, default: null, enum: [...ENUMS_1.ALL_DAY_INDEX, null] },
25
+ sale_type_key_filters: {
26
+ type: [String],
27
+ required: true,
28
+ default: () => [...MATERIALIZED_REPORTS_1.DIALER_KEY_FILTERS],
29
+ validate: {
30
+ validator: (value) => value.length > 0 &&
31
+ value.every((entry) => MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_REPORT_KEY_FILTERS.includes(entry)),
32
+ message: 'sale_type_key_filters must be a non-empty subset of the known key filters',
33
+ },
34
+ },
35
+ dialer_counters: { type: mongoose_1.Schema.Types.Mixed, required: true, default: null },
36
+ iteration: { type: Number, required: true, min: 1, default: 1 },
37
+ schema_version: {
38
+ type: Number,
39
+ required: true,
40
+ immutable: true,
41
+ default: exports.MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION,
42
+ },
43
+ source_watermark: { type: mongoose_1.Schema.Types.Date, required: true },
44
+ computed_at: { type: mongoose_1.Schema.Types.Date, required: true },
45
+ createdAt: { type: mongoose_1.Schema.Types.Date, required: true, default: () => new Date(), immutable: true },
46
+ updatedAt: { type: mongoose_1.Schema.Types.Date, required: true, default: () => new Date() },
47
+ }, { timestamps: true, versionKey: false, minimize: false });
48
+ exports.MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_NAME = 'materialized_tenant_dialer_reports';
49
+ /**
50
+ * The single UNIQUE compound identity index, like every materialized report
51
+ * collection. Declared ONLY — no service calls `index()` or `ensureIndexes`, and
52
+ * the services run with `autoIndex: false`. Applying it is an owner deploy step.
53
+ */
54
+ exports.MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_INDEXES = {
55
+ _id: { _id: 1 },
56
+ tenant_id__scope_context__scope_field__day_index: {
57
+ tenant_id: 1,
58
+ scope_context: 1,
59
+ scope_field: 1,
60
+ day_index: 1,
61
+ },
62
+ };
63
+ exports.MATERIALIZED_TENANT_DIALER_REPORTS_INDEX_OPTIONS = [
64
+ {
65
+ key: { tenant_id: 1, scope_context: 1, scope_field: 1, day_index: 1 },
66
+ name: 'tenant_id__scope_context__scope_field__day_index',
67
+ unique: true,
68
+ },
69
+ ];
@@ -27,8 +27,8 @@ export declare const TenantDialerEventSchema: Schema<any, import("mongoose").Mod
27
27
  phone_number: string;
28
28
  pos_id: string;
29
29
  schema_version: number;
30
- request_id: string;
31
30
  action: import("../../constants").DIALER_TYPES_ENUM;
31
+ request_id: string;
32
32
  staff_id?: string | null | undefined;
33
33
  staff_phone?: string | null | undefined;
34
34
  }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
@@ -42,8 +42,8 @@ export declare const TenantDialerEventSchema: Schema<any, import("mongoose").Mod
42
42
  phone_number: string;
43
43
  pos_id: string;
44
44
  schema_version: number;
45
- request_id: string;
46
45
  action: import("../../constants").DIALER_TYPES_ENUM;
46
+ request_id: string;
47
47
  staff_id?: string | null | undefined;
48
48
  staff_phone?: string | null | undefined;
49
49
  }>> & import("mongoose").FlatRecord<{
@@ -57,8 +57,8 @@ export declare const TenantDialerEventSchema: Schema<any, import("mongoose").Mod
57
57
  phone_number: string;
58
58
  pos_id: string;
59
59
  schema_version: number;
60
- request_id: string;
61
60
  action: import("../../constants").DIALER_TYPES_ENUM;
61
+ request_id: string;
62
62
  staff_id?: string | null | undefined;
63
63
  staff_phone?: string | null | undefined;
64
64
  }> & Required<{
@@ -41,6 +41,8 @@ export * from './materialized_tenant_delivery_geo_scopes';
41
41
  export * from './materialized_tenant_delivery_reports';
42
42
  export * from './materialized_tenant_discount_entries';
43
43
  export * from './materialized_tenant_discount_reports';
44
+ export * from './materialized_tenant_dialer_entries';
45
+ export * from './materialized_tenant_dialer_reports';
44
46
  export * from './materialized_tenant_discount_sources';
45
47
  export * from './materialized_tenant_hourly_comparison_entries';
46
48
  export * from './materialized_tenant_hourly_comparison_reports';
@@ -57,6 +57,8 @@ __exportStar(require("./materialized_tenant_delivery_geo_scopes"), exports);
57
57
  __exportStar(require("./materialized_tenant_delivery_reports"), exports);
58
58
  __exportStar(require("./materialized_tenant_discount_entries"), exports);
59
59
  __exportStar(require("./materialized_tenant_discount_reports"), exports);
60
+ __exportStar(require("./materialized_tenant_dialer_entries"), exports);
61
+ __exportStar(require("./materialized_tenant_dialer_reports"), exports);
60
62
  __exportStar(require("./materialized_tenant_discount_sources"), exports);
61
63
  __exportStar(require("./materialized_tenant_hourly_comparison_entries"), exports);
62
64
  __exportStar(require("./materialized_tenant_hourly_comparison_reports"), exports);
@@ -13,7 +13,7 @@ import { MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM } from '.
13
13
  * fields a page query needs: selection identity, sort anchor, and cursor
14
14
  * tie-break.
15
15
  */
16
- export type TDrilldownReportType = MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PREP_TIME_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PAYMENTS_COMPARISON_ENTRIES;
16
+ export type TDrilldownReportType = MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PREP_TIME_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PAYMENTS_COMPARISON_ENTRIES | MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DIALER_ENTRIES;
17
17
  export declare const ALL_DRILLDOWN_REPORT_TYPES: TDrilldownReportType[];
18
18
  /** One flattened itemized row. */
19
19
  export interface IMaterializedReportDrilldownRow {
@@ -10,4 +10,5 @@ exports.ALL_DRILLDOWN_REPORT_TYPES = [
10
10
  MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_ENTRIES,
11
11
  MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES,
12
12
  MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PAYMENTS_COMPARISON_ENTRIES,
13
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DIALER_ENTRIES,
13
14
  ];
@@ -0,0 +1,83 @@
1
+ import { DAY_INDEX_ENUM } from '../../constants/ENUMS';
2
+ import { MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
3
+ /**
4
+ * Types for `materialized_tenant_dialer_entries`.
5
+ *
6
+ * ## One page per source day
7
+ *
8
+ * The entry collection follows the discounts/delivery page model exactly: the
9
+ * report document is counters only, and an entry document is ONE PAGE bounded
10
+ * by ONE source day.
11
+ *
12
+ * | scope | page |
13
+ * | --- | --- |
14
+ * | `localDate` | `'all'` (the scope is already one day) |
15
+ * | `localWeek` | `<source localDate>` |
16
+ * | `localWeekOfYear` | `<source localDate>` |
17
+ * | `localMonth` | `<source localDate>` |
18
+ *
19
+ * An oversized page splits into deterministic `page#0`, `page#1`, … sub-pages;
20
+ * the unsuffixed identity is used while the page fits.
21
+ *
22
+ * ## Phones are MASKED at compile time
23
+ *
24
+ * A materialized row (and therefore every API response built from it) carries
25
+ * only `phone_number_masked` / `staff_phone_masked`. The RAW source numbers are
26
+ * never copied into a materialized document.
27
+ */
28
+ export interface IDialerEntryRow {
29
+ /** Source event `_id` — the row identity and the keyset cursor tie-break. */
30
+ event_id: string;
31
+ request_id: string;
32
+ action: string;
33
+ /** Masked caller number — never the raw source value. */
34
+ phone_number_masked: string;
35
+ staff_id: string | null;
36
+ /** Masked staff number — never the raw source value. */
37
+ staff_phone_masked: string | null;
38
+ pos_id: string;
39
+ /** ISO-8601 instant of the source event (`createdAt`). */
40
+ created_at: string;
41
+ /** The event's own tenant-local business day (attributed events only). */
42
+ local_date: string;
43
+ /** The event's own tenant-local hour (`0`..`23`). */
44
+ local_hour: number;
45
+ }
46
+ /** Per-key row section of one entry page. */
47
+ export interface IDialerEntrySection {
48
+ rows: IDialerEntryRow[];
49
+ }
50
+ /** One `materialized_tenant_dialer_entries` PAGE. */
51
+ export interface IMaterializedTenantDialerEntry {
52
+ _id: string;
53
+ tenant_id: string;
54
+ brand_id: string;
55
+ /** One of `MATERIALIZED_DIALER_ENTRY_SCOPES`. */
56
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM;
57
+ scope_field: string;
58
+ /** `'all'` for the day scope; the source `YYYY-MM-DD` for a period scope. */
59
+ page: string;
60
+ /** Denormalized weekday — set ONLY on the `localDate` page. */
61
+ day_index: DAY_INDEX_ENUM | null;
62
+ /** One row section per observed `<ACTION>:<POS_ID>:<STAFF_ID>` key. */
63
+ dialer_entries: Record<string, IDialerEntrySection> | null;
64
+ /** Shared materialized-envelope field (structural version of THIS document). */
65
+ iteration?: number;
66
+ schema_version: number;
67
+ source_watermark: Date;
68
+ computed_at: Date;
69
+ createdAt: Date;
70
+ updatedAt: Date;
71
+ }
72
+ /** Attributed vs unattributed counts of one closing day (worker observability). */
73
+ export interface IDialerSourceDayCounts {
74
+ /** Events whose `localDate` is the closing day — materialized. */
75
+ attributed_events: number;
76
+ /**
77
+ * Events inside the closing day's opening→closing window that carry NO
78
+ * `localDate`. They are NEVER attributed to a day; the count is reported so
79
+ * the gap is visible. Late events (written after the close) are not counted
80
+ * here until a re-close.
81
+ */
82
+ unattributed_events: number;
83
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,77 @@
1
+ import { DAY_INDEX_ENUM } from '../../constants/ENUMS';
2
+ import { MATERIALIZED_REPORT_KEY_FILTER_ENUM, MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
3
+ /**
4
+ * Types for `materialized_tenant_dialer_reports`.
5
+ *
6
+ * ## An EVENT report, not a call report
7
+ *
8
+ * The source (`tenant_dialer_events`) is append-only and stores ONE row per
9
+ * dialer action: `INCOMING_CALL`, `OUTGOING_CALL`, `CALL_ANSWERED`,
10
+ * `MISSED_CALL`, `CALL_DECLINED`, `CALL_ENDED`, `POS_PING`. There is no
11
+ * duration and no order attribution, so the counters count EVENTS and the
12
+ * page derives every rate itself. The approved contract is
13
+ * `outcome_denominator = answered + missed + declined`, with
14
+ * `answer_rate = answered / outcome_denominator` and
15
+ * `missed_rate = missed / outcome_denominator` (percentage, 2 decimals, null
16
+ * when the denominator is 0). Nothing derived is stored.
17
+ *
18
+ * ## Attributed events only
19
+ *
20
+ * A day is `event.localDate`. An event written without local fields is
21
+ * UNATTRIBUTED: it is never assigned to a guessed day, so it is excluded from
22
+ * every day/period document (the materializer reports its count separately).
23
+ *
24
+ * ## Key grammar
25
+ *
26
+ * `sale_type_key_filters` declares `<ACTION>:<POS_ID>:<STAFF_ID>`. A component
27
+ * the source does not carry is encoded as `UNKNOWN`, never omitted.
28
+ */
29
+ export interface IDialerCounters {
30
+ /** Every attributed event (`INCOMING_CALL` + `OUTGOING_CALL` + outcomes + pings). */
31
+ events: number;
32
+ incoming_calls: number;
33
+ outgoing_calls: number;
34
+ answered: number;
35
+ missed: number;
36
+ declined: number;
37
+ ended: number;
38
+ pos_pings: number;
39
+ /**
40
+ * Event count by the event's tenant-local hour: keys `"0"`..`"23"`.
41
+ *
42
+ * Every key is initialized to 0 so absence is unambiguously zero. An event
43
+ * with a `localDate` but no usable `localHour` still counts in `events`; it
44
+ * is not bucketed into an invented hour, so `Σ by_hour` can only be lower
45
+ * than `events` for malformed partial local fields.
46
+ */
47
+ by_hour: Record<string, number>;
48
+ }
49
+ /** The declared key dimensions, in grammar order. */
50
+ export type TDialerKeyFilters = [
51
+ MATERIALIZED_REPORT_KEY_FILTER_ENUM.ACTION,
52
+ MATERIALIZED_REPORT_KEY_FILTER_ENUM.POS_ID,
53
+ MATERIALIZED_REPORT_KEY_FILTER_ENUM.STAFF_ID
54
+ ];
55
+ /** One `materialized_tenant_dialer_reports` document (counters only). */
56
+ export interface IMaterializedTenantDialerReport {
57
+ _id: string;
58
+ tenant_id: string;
59
+ brand_id: string;
60
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM;
61
+ scope_field: string;
62
+ /** Denormalized weekday — set on the DAY document, null on every period. */
63
+ day_index: DAY_INDEX_ENUM | null;
64
+ /** The dimensions the key encodes, in grammar order. */
65
+ sale_type_key_filters: TDialerKeyFilters;
66
+ /** Keyed by `<ACTION>:<POS_ID>:<STAFF_ID>`. */
67
+ dialer_counters: Record<string, IDialerCounters>;
68
+ /** Shared materialized-envelope field (structural version of THIS document). */
69
+ iteration?: number;
70
+ schema_version: number;
71
+ source_watermark: Date;
72
+ computed_at: Date;
73
+ createdAt: Date;
74
+ updatedAt: Date;
75
+ }
76
+ /** The default key-filter declaration of a dialer document. */
77
+ export declare const MATERIALIZED_DIALER_REPORT_KEY_FILTERS: TDialerKeyFilters;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MATERIALIZED_DIALER_REPORT_KEY_FILTERS = void 0;
4
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
5
+ /** The default key-filter declaration of a dialer document. */
6
+ exports.MATERIALIZED_DIALER_REPORT_KEY_FILTERS = [
7
+ MATERIALIZED_REPORTS_1.DIALER_KEY_FILTERS[0],
8
+ MATERIALIZED_REPORTS_1.DIALER_KEY_FILTERS[1],
9
+ MATERIALIZED_REPORTS_1.DIALER_KEY_FILTERS[2],
10
+ ];
@@ -120,7 +120,7 @@ describe('M4 close-day plan', () => {
120
120
  expect(monthAfter.report.source_watermark.getTime()).toBeGreaterThan(new Date(monthBefore.source_watermark).getTime());
121
121
  const key = 'COMPLETED:POS_WALK-IN:Istanbul';
122
122
  expect(monthAfter.report.delivery_counters[key].deliveries.count).toBe(2);
123
- expect(monthAfter.report.schema_version).toBe(2);
123
+ expect(monthAfter.report.schema_version).toBe(1);
124
124
  });
125
125
  it('EMPTYING a day removes it and its contribution from every period', () => {
126
126
  const dayOne = linkedDay('2026-09-14T23:00:00.000Z');
@@ -281,7 +281,7 @@ describe('delivery practical-scale correctness', () => {
281
281
  sale_type_key_filters: ['ORDER_STATUS', 'PLATFORM', 'CITY'],
282
282
  delivery_entries: { 'COMPLETED:POS_DELIVERY': { rows } },
283
283
  off_app_order_count: 3,
284
- schema_version: 3,
284
+ schema_version: 1,
285
285
  source_watermark: new Date(DAY + 'T23:00:00.000Z'),
286
286
  computed_at: new Date(DAY + 'T23:00:00.000Z'),
287
287
  createdAt: new Date(DAY + 'T23:00:00.000Z'),
@@ -0,0 +1,213 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const DIALER_ENUMS_1 = require("../../constants/DIALER_ENUMS");
4
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
5
+ const materialized_dialer_compile_helpers_1 = require("../../helpers/materialized-reports/materialized-dialer-compile-helpers");
6
+ const materialized_dialer_key_helpers_1 = require("../../helpers/materialized-reports/materialized-dialer-key-helpers");
7
+ const materialized_dialer_masking_1 = require("../../helpers/materialized-reports/materialized-dialer-masking");
8
+ const materialized_dialer_entry_pages_1 = require("../../helpers/materialized-reports/materialized-dialer-entry-pages");
9
+ const materialized_dialer_accumulator_helpers_1 = require("../../helpers/materialized-reports/materialized-dialer-accumulator-helpers");
10
+ /**
11
+ * Dialer day compile.
12
+ *
13
+ * The defining property of this report: the source is an APPEND-ONLY EVENT
14
+ * stream. A day is exactly the events whose own tenant-local `localDate` is the
15
+ * day, an event without local fields is never attributed to a guessed day, and
16
+ * the counters are plain event counts (no duration, no order attribution).
17
+ */
18
+ const TENANT = 'tenant-dialer';
19
+ const BRAND = 'brand-dialer';
20
+ const DAY = '2026-09-14';
21
+ const COMPUTED_AT = new Date('2026-09-15T06:00:00.000Z');
22
+ let sequence = 0;
23
+ const event = (over = {}) => {
24
+ sequence += 1;
25
+ return {
26
+ _id: `event-${String(sequence).padStart(4, '0')}`,
27
+ tenant_id: TENANT,
28
+ request_id: `call-${sequence}:${over.action ?? DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL}`,
29
+ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL,
30
+ phone_number: '+923001234567',
31
+ pos_id: 'POS-01',
32
+ staff_id: 'staff-1',
33
+ staff_phone: '+923009999999',
34
+ localDate: DAY,
35
+ localHour: 10,
36
+ localMinute: 5,
37
+ localSecond: 0,
38
+ createdAt: new Date(`${DAY}T05:05:00.000Z`),
39
+ updatedAt: new Date(`${DAY}T05:05:00.000Z`),
40
+ ...over,
41
+ };
42
+ };
43
+ const compile = (events, over = {}) => (0, materialized_dialer_compile_helpers_1.compileTenantDialerReports)({
44
+ tenantId: TENANT,
45
+ brandId: BRAND,
46
+ localDate: DAY,
47
+ events,
48
+ computedAt: COMPUTED_AT,
49
+ ...over,
50
+ });
51
+ const dayEntry = (compiled) => compiled.entries.find((entry) => String(entry.scope_context) === 'localDate');
52
+ const allRows = (compiled) => Object.values(dayEntry(compiled).dialer_entries ?? {}).flatMap((section) => section.rows);
53
+ describe('dialer day compile', () => {
54
+ it('counts every action into its counter and into `events`', () => {
55
+ const compiled = compile(Object.values(DIALER_ENUMS_1.DIALER_TYPES_ENUM).map((action) => event({ action, localHour: 9, request_id: `call:${action}` })));
56
+ const counters = (0, materialized_dialer_accumulator_helpers_1.sumDialerCounterMap)(compiled.reports[0].dialer_counters);
57
+ expect(Object.keys(compiled.reports[0].dialer_counters)).toHaveLength(7);
58
+ // The raw counter shape is fixed: counts + `by_hour`, no duration/rates.
59
+ expect(Object.keys(counters).sort()).toEqual([
60
+ 'answered',
61
+ 'by_hour',
62
+ 'declined',
63
+ 'ended',
64
+ 'events',
65
+ 'incoming_calls',
66
+ 'missed',
67
+ 'outgoing_calls',
68
+ 'pos_pings',
69
+ ]);
70
+ expect(counters.events).toBe(7);
71
+ expect(counters.incoming_calls).toBe(1);
72
+ expect(counters.outgoing_calls).toBe(1);
73
+ expect(counters.answered).toBe(1);
74
+ expect(counters.missed).toBe(1);
75
+ expect(counters.declined).toBe(1);
76
+ expect(counters.ended).toBe(1);
77
+ expect(counters.pos_pings).toBe(1);
78
+ expect(counters.by_hour['9']).toBe(7);
79
+ expect(compiled.attributed_events).toBe(7);
80
+ });
81
+ it('builds the key grammar and encodes a missing component as UNKNOWN', () => {
82
+ const compiled = compile([
83
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL, pos_id: 'POS-01', staff_id: 'staff-1' }),
84
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.INCOMING_CALL, pos_id: 'POS-02', staff_id: null }),
85
+ event({ action: DIALER_ENUMS_1.DIALER_TYPES_ENUM.POS_PING, pos_id: 'POS-01', staff_id: '' }),
86
+ ]);
87
+ expect(Object.keys(compiled.reports[0].dialer_counters).sort()).toEqual([
88
+ 'INCOMING_CALL:POS-01:staff-1',
89
+ 'INCOMING_CALL:POS-02:UNKNOWN',
90
+ 'POS_PING:POS-01:UNKNOWN',
91
+ ]);
92
+ expect(compiled.reports[0].sale_type_key_filters).toEqual(['ACTION', 'POS_ID', 'STAFF_ID']);
93
+ });
94
+ it('initializes all 24 by_hour keys and never invents an hour', () => {
95
+ const compiled = compile([event({ localHour: 0 }), event({ localHour: 23 })]);
96
+ const counters = Object.values(compiled.reports[0].dialer_counters)[0];
97
+ expect(Object.keys(counters.by_hour).sort()).toEqual([...MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_HOUR_KEYS].sort());
98
+ expect(counters.by_hour['0']).toBe(1);
99
+ expect(counters.by_hour['23']).toBe(1);
100
+ expect(counters.by_hour['12']).toBe(0);
101
+ expect(counters.events).toBe(2);
102
+ });
103
+ it('ignores events without a local date/hour and events of another day', () => {
104
+ const compiled = compile([
105
+ event({ localDate: DAY, localHour: 10 }),
106
+ event({ localDate: undefined, localHour: undefined }),
107
+ event({ localDate: DAY, localHour: undefined }),
108
+ event({ localDate: '2026-09-13', localHour: 23 }),
109
+ ]);
110
+ expect(compiled.attributed_events).toBe(1);
111
+ expect(compiled.unattributed_events).toBe(2);
112
+ expect(compiled.out_of_day_events).toBe(1);
113
+ expect(Object.values(compiled.reports[0].dialer_counters)[0].events).toBe(1);
114
+ expect(allRows(compiled)).toHaveLength(1);
115
+ });
116
+ it('returns null for an empty (or fully unattributed) day', () => {
117
+ expect(compile([])).toBeNull();
118
+ expect(compile([event({ localDate: undefined, localHour: undefined })])).toBeNull();
119
+ });
120
+ it('masks caller and staff phones in every materialized row', () => {
121
+ const compiled = compile([event({ phone_number: '+923001234567', staff_phone: '0300-7654321' })]);
122
+ const [row] = allRows(compiled);
123
+ expect(row.phone_number_masked).toBe('+********4567');
124
+ expect(row.staff_phone_masked).toBe('*******4321');
125
+ const serialized = JSON.stringify(compiled);
126
+ expect(serialized).not.toContain('+923001234567');
127
+ expect(serialized).not.toContain('0300-7654321');
128
+ expect(serialized).not.toContain('923001234567');
129
+ expect(serialized).not.toContain('7654321');
130
+ });
131
+ it('creates one page per source day for every entry scope', () => {
132
+ const compiled = compile([event()]);
133
+ const pages = compiled.entries.map((entry) => `${entry.scope_context}:${entry.scope_field}:${entry.page}`);
134
+ expect(pages.sort()).toEqual([
135
+ `localDate:${DAY}:all`,
136
+ 'localMonth:2026-09:2026-09-14',
137
+ 'localWeek:2026-09-13:2026-09-14',
138
+ 'localWeekOfYear:2026-W38:2026-09-14',
139
+ ].sort());
140
+ const day = dayEntry(compiled);
141
+ expect(day.day_index).not.toBeNull();
142
+ for (const target of compiled.entries.filter((entry) => entry !== day)) {
143
+ expect(target.day_index).toBeNull();
144
+ }
145
+ });
146
+ it('orders rows newest-first with a total event-id tie-break', () => {
147
+ const compiled = compile([
148
+ event({ _id: 'e-a', createdAt: new Date(`${DAY}T01:00:00.000Z`) }),
149
+ event({ _id: 'e-c', createdAt: new Date(`${DAY}T03:00:00.000Z`) }),
150
+ event({ _id: 'e-b', createdAt: new Date(`${DAY}T02:00:00.000Z`) }),
151
+ ]);
152
+ expect(allRows(compiled).map((row) => row.event_id)).toEqual(['e-c', 'e-b', 'e-a']);
153
+ expect(allRows(compiled)[0].created_at).toBe('2026-09-14T03:00:00.000Z');
154
+ });
155
+ it('uses the source watermark (max updatedAt) and the source day', () => {
156
+ const compiled = compile([
157
+ event({ updatedAt: new Date('2026-09-14T10:00:00.000Z') }),
158
+ event({ updatedAt: new Date('2026-09-14T11:00:00.000Z') }),
159
+ ]);
160
+ expect(compiled.reports[0].source_watermark.toISOString()).toBe('2026-09-14T11:00:00.000Z');
161
+ expect(compiled.reports[0].scope_context).toBe('localDate');
162
+ expect(compiled.reports[0].scope_field).toBe(DAY);
163
+ });
164
+ it('declares 15 MiB report/page budgets', () => {
165
+ expect(MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_REPORT_MAX_BSON_BYTES).toBe(15 * 1024 * 1024);
166
+ expect(MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_ENTRY_MAX_BSON_BYTES).toBe(15 * 1024 * 1024);
167
+ expect(MATERIALIZED_REPORTS_1.MATERIALIZED_DIALER_REPORT_MAX_BSON_BYTES).toBeLessThan(16 * 1024 * 1024);
168
+ });
169
+ it('splits an oversized page into deterministic #N sub-pages and keeps a fitting page unsuffixed', () => {
170
+ const compiled = compile(Array.from({ length: 40 }, (_, index) => event({ _id: `split-${index}`, phone_number: `+9230000${String(index).padStart(5, '0')}` })));
171
+ const day = dayEntry(compiled);
172
+ const pages = (0, materialized_dialer_entry_pages_1.splitMaterializedDialerEntryPage)(day, 2600);
173
+ expect(pages.length).toBeGreaterThan(1);
174
+ expect(pages.map((page) => page.page)).toEqual(pages.map((_, index) => `all#${index}`));
175
+ const ids = pages.flatMap((page) => Object.values(page.dialer_entries ?? {}).flatMap((section) => section.rows.map((row) => row.event_id)));
176
+ expect(new Set(ids).size).toBe(40);
177
+ // A page that fits keeps its original identity.
178
+ expect((0, materialized_dialer_entry_pages_1.splitMaterializedDialerEntryPage)(day, 15 * 1024 * 1024)).toHaveLength(1);
179
+ expect((0, materialized_dialer_entry_pages_1.splitMaterializedDialerEntryPage)(day, 15 * 1024 * 1024)[0].page).toBe('all');
180
+ });
181
+ });
182
+ describe('dialer key grammar', () => {
183
+ it('builds, parses and sanitizes the separator', () => {
184
+ expect((0, materialized_dialer_key_helpers_1.buildMaterializedDialerKey)('INCOMING_CALL', 'POS:1', null)).toBe('INCOMING_CALL:POS-1:UNKNOWN');
185
+ expect((0, materialized_dialer_key_helpers_1.parseMaterializedDialerKey)('INCOMING_CALL:POS-01:staff-1')).toEqual({
186
+ action: 'INCOMING_CALL',
187
+ pos_id: 'POS-01',
188
+ staff_id: 'staff-1',
189
+ });
190
+ expect((0, materialized_dialer_key_helpers_1.parseMaterializedDialerKey)('broken')).toBeNull();
191
+ expect((0, materialized_dialer_key_helpers_1.parseMaterializedDialerKey)('A::B')).toBeNull();
192
+ });
193
+ it('filters whole components only and never matches a prefix', () => {
194
+ const key = 'CALL_ANSWERED:POS-01:staff-1';
195
+ expect((0, materialized_dialer_key_helpers_1.dialerKeyPassesFilters)(key, null)).toBe(true);
196
+ expect((0, materialized_dialer_key_helpers_1.dialerKeyPassesFilters)(key, { actions: ['CALL_ANSWERED'] })).toBe(true);
197
+ expect((0, materialized_dialer_key_helpers_1.dialerKeyPassesFilters)(key, { actions: ['CALL'] })).toBe(false);
198
+ expect((0, materialized_dialer_key_helpers_1.dialerKeyPassesFilters)(key, { pos_ids: ['POS-01'], staff_ids: ['staff-1'] })).toBe(true);
199
+ expect((0, materialized_dialer_key_helpers_1.dialerKeyPassesFilters)(key, { staff_ids: ['UNKNOWN'] })).toBe(false);
200
+ const regex = (0, materialized_dialer_key_helpers_1.buildDialerKeyFilterRegex)({ actions: ['INCOMING_CALL'], staff_ids: ['staff-1'] });
201
+ expect(regex.test('INCOMING_CALL:POS-01:staff-1')).toBe(true);
202
+ expect(regex.test('INCOMING_CALL:POS-01:staff-10')).toBe(false);
203
+ expect((0, materialized_dialer_key_helpers_1.buildDialerKeyFilterRegex)(null)).toBeNull();
204
+ });
205
+ it('masks with the documented rule', () => {
206
+ expect((0, materialized_dialer_masking_1.maskDialerPhoneNumber)('+923001234567')).toBe('+********4567');
207
+ expect((0, materialized_dialer_masking_1.maskDialerPhoneNumber)('0300-1234567')).toBe('*******4567');
208
+ expect((0, materialized_dialer_masking_1.maskDialerPhoneNumber)('12345')).toBe('*2345');
209
+ expect((0, materialized_dialer_masking_1.maskDialerPhoneNumber)('1234')).toBe('****');
210
+ expect((0, materialized_dialer_masking_1.maskDialerPhoneNumber)('')).toBeNull();
211
+ expect((0, materialized_dialer_masking_1.maskDialerPhoneNumber)(null)).toBeNull();
212
+ });
213
+ });