@managesome/eposmatic-toolkit 0.4.54 → 0.4.55

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.
@@ -30,3 +30,21 @@ export function buildDeliveryMaterializationWipeTargets(tenantId) {
30
30
  { name: MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME, filter: iterationFilter },
31
31
  ];
32
32
  }
33
+ /**
34
+ * Phase 5 safety gate: a wipe target may only name a materialized collection
35
+ * and may only filter on tenant/report identity. This is the last line of
36
+ * defence before an operator command talks to MongoDB.
37
+ */
38
+ const ALLOWED_WIPE_FILTER_KEYS = new Set(['tenant_id', 'report_type', 'snapshot.tenant_id']);
39
+ export function assertMaterializedWipeTargets(targets) {
40
+ for (const target of targets) {
41
+ if (!target.name.startsWith('materialized_')) {
42
+ throw new Error(`Refusing wipe: ${target.name} is not a materialized_* collection`);
43
+ }
44
+ for (const key of Object.keys(target.filter)) {
45
+ if (!ALLOWED_WIPE_FILTER_KEYS.has(key)) {
46
+ throw new Error(`Refusing wipe: ${target.name} filters on forbidden key ${key}`);
47
+ }
48
+ }
49
+ }
50
+ }
@@ -72,6 +72,7 @@ export * from './tenant_cash_in_out';
72
72
  export * from './tenant_chart_of_accounts';
73
73
  export * from './tenant_clients';
74
74
  export * from './tenant_day_sessions';
75
+ export * from './tenant_dialer_events';
75
76
  export * from './tenant_hr_attendance_records';
76
77
  export * from './tenant_hr_biometric_enrollments';
77
78
  export * from './tenant_hr_departments';
@@ -0,0 +1,133 @@
1
+ import { Schema } from 'mongoose';
2
+ import { ALL_DIALER_TYPE_EVENTS } from '../../constants';
3
+ import { LocalTimeFieldsSchema } from './common-models';
4
+ /**
5
+ * Shape version of a `tenant_dialer_events` row. Bump only with a migration
6
+ * for already-stored source events.
7
+ */
8
+ export const TENANT_DIALER_EVENTS_SCHEMA_VERSION = 1;
9
+ /**
10
+ * The shared `LocalTimeFieldsSchema` is required for every other model. The
11
+ * dialer source stream is the one place local fields are OPTIONAL: when the
12
+ * running business day is unavailable the event is still stored, un-attributed
13
+ * and WITHOUT any local-time field. Derive an optional copy locally instead of
14
+ * weakening the shared schema.
15
+ */
16
+ const OptionalLocalTimeFieldsSchema = Object.fromEntries(Object.entries(LocalTimeFieldsSchema).map(([field, definition]) => [field, { ...definition, required: false }]));
17
+ /**
18
+ * APPEND-ONLY source event stream for the future Dialer materialized report.
19
+ * Rows are written by items-service with
20
+ * `findOneAndUpdate({ tenant_id, request_id }, { $setOnInsert }, { upsert: true })`,
21
+ * so a retried request returns the ORIGINAL row and never rewrites
22
+ * `createdAt`, the local-time fields or any other stored value. No report
23
+ * fields (for example duration) live here.
24
+ */
25
+ export const TenantDialerEventSchema = new Schema({
26
+ _id: {
27
+ type: Schema.Types.ObjectId,
28
+ required: true,
29
+ immutable: true,
30
+ },
31
+ tenant_id: {
32
+ type: String,
33
+ required: true,
34
+ immutable: true,
35
+ ref: 'tenants',
36
+ },
37
+ request_id: {
38
+ type: String,
39
+ required: true,
40
+ immutable: true,
41
+ },
42
+ action: {
43
+ type: String,
44
+ enum: ALL_DIALER_TYPE_EVENTS,
45
+ required: true,
46
+ immutable: true,
47
+ },
48
+ phone_number: {
49
+ type: String,
50
+ required: true,
51
+ immutable: true,
52
+ },
53
+ pos_id: {
54
+ type: String,
55
+ required: true,
56
+ immutable: true,
57
+ },
58
+ staff_id: {
59
+ type: String,
60
+ required: false,
61
+ default: null,
62
+ immutable: true,
63
+ },
64
+ staff_phone: {
65
+ type: String,
66
+ required: false,
67
+ default: null,
68
+ immutable: true,
69
+ },
70
+ schema_version: {
71
+ type: Number,
72
+ required: true,
73
+ enum: [TENANT_DIALER_EVENTS_SCHEMA_VERSION],
74
+ default: TENANT_DIALER_EVENTS_SCHEMA_VERSION,
75
+ immutable: true,
76
+ },
77
+ ...OptionalLocalTimeFieldsSchema,
78
+ createdAt: {
79
+ type: Schema.Types.Date,
80
+ required: true,
81
+ default: () => new Date(),
82
+ immutable: true,
83
+ },
84
+ updatedAt: {
85
+ type: Schema.Types.Date,
86
+ required: true,
87
+ default: () => new Date(),
88
+ },
89
+ }, {
90
+ timestamps: true,
91
+ versionKey: false,
92
+ minimize: false,
93
+ });
94
+ export const TENANT_DIALER_EVENTS_COLLECTION_NAME = 'tenant_dialer_events';
95
+ /**
96
+ * Describes the indexes for `tenant_dialer_events`. Values are Mongo key
97
+ * patterns that can be passed straight to `.hint()`.
98
+ * - `_id`: unique primary key.
99
+ * - `tenant_id__request_id`: UNIQUE idempotency key — one source event per
100
+ * tenant + request (native: `callId:action`, JS: UUID reused on retry).
101
+ * - `tenant_id__localDate__action`: future report reads per business day and
102
+ * action. Local fields may be absent for un-attributed events, so reads must
103
+ * tolerate a missing `localDate` (sparse-aware querying, not a partial
104
+ * index requirement).
105
+ * - `tenant_id__phone_number__createdAt`: future pairing/attribution reads for
106
+ * one caller phone ordered by the canonical receipt time.
107
+ */
108
+ export const TENANT_DIALER_EVENTS_COLLECTION_INDEXES = {
109
+ _id: { _id: 1 }, // Unique Index
110
+ tenant_id__request_id: { tenant_id: 1, request_id: 1 }, // Unique Index — ingestion idempotency
111
+ tenant_id__localDate__action: { tenant_id: 1, localDate: 1, action: 1 }, // per business day + action
112
+ tenant_id__phone_number__createdAt: { tenant_id: 1, phone_number: 1, createdAt: 1 }, // per caller phone over time
113
+ };
114
+ /**
115
+ * Index OPTIONS to DECLARE (never created at application startup). The
116
+ * materializer/ops path owns creation; `tenant_id__request_id` MUST be created
117
+ * with `unique: true` or concurrent retries could insert a second event.
118
+ */
119
+ export const TENANT_DIALER_EVENTS_INDEX_OPTIONS = [
120
+ {
121
+ key: TENANT_DIALER_EVENTS_COLLECTION_INDEXES.tenant_id__request_id,
122
+ name: 'tenant_id__request_id',
123
+ unique: true,
124
+ },
125
+ {
126
+ key: TENANT_DIALER_EVENTS_COLLECTION_INDEXES.tenant_id__localDate__action,
127
+ name: 'tenant_id__localDate__action',
128
+ },
129
+ {
130
+ key: TENANT_DIALER_EVENTS_COLLECTION_INDEXES.tenant_id__phone_number__createdAt,
131
+ name: 'tenant_id__phone_number__createdAt',
132
+ },
133
+ ];
@@ -77,6 +77,7 @@ export * from './tenant_cash_in_out';
77
77
  export * from './tenant_chart_of_accounts';
78
78
  export * from './tenant_clients';
79
79
  export * from './tenant_day_sessions';
80
+ export * from './tenant_dialer_events';
80
81
  export * from './tenant_hr_attendance_records';
81
82
  export * from './tenant_hr_biometric_enrollments';
82
83
  export * from './tenant_hr_departments';
@@ -0,0 +1 @@
1
+ export {};
@@ -15,3 +15,4 @@ export interface IDeliveryMaterializationWipeTarget {
15
15
  filter: Record<string, unknown>;
16
16
  }
17
17
  export declare function buildDeliveryMaterializationWipeTargets(tenantId?: string | null): IDeliveryMaterializationWipeTarget[];
18
+ export declare function assertMaterializedWipeTargets(targets: readonly IDeliveryMaterializationWipeTarget[]): void;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildDeliveryMaterializationWipeTargets = buildDeliveryMaterializationWipeTargets;
4
+ exports.assertMaterializedWipeTargets = assertMaterializedWipeTargets;
4
5
  const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
5
6
  const materialized_report_drilldown_rows_1 = require("../../schemas/models/materialized_report_drilldown_rows");
6
7
  const materialized_tenant_delivery_entries_1 = require("../../schemas/models/materialized_tenant_delivery_entries");
@@ -33,3 +34,21 @@ function buildDeliveryMaterializationWipeTargets(tenantId) {
33
34
  { name: materialized_tenant_reports_iterations_1.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME, filter: iterationFilter },
34
35
  ];
35
36
  }
37
+ /**
38
+ * Phase 5 safety gate: a wipe target may only name a materialized collection
39
+ * and may only filter on tenant/report identity. This is the last line of
40
+ * defence before an operator command talks to MongoDB.
41
+ */
42
+ const ALLOWED_WIPE_FILTER_KEYS = new Set(['tenant_id', 'report_type', 'snapshot.tenant_id']);
43
+ function assertMaterializedWipeTargets(targets) {
44
+ for (const target of targets) {
45
+ if (!target.name.startsWith('materialized_')) {
46
+ throw new Error(`Refusing wipe: ${target.name} is not a materialized_* collection`);
47
+ }
48
+ for (const key of Object.keys(target.filter)) {
49
+ if (!ALLOWED_WIPE_FILTER_KEYS.has(key)) {
50
+ throw new Error(`Refusing wipe: ${target.name} filters on forbidden key ${key}`);
51
+ }
52
+ }
53
+ }
54
+ }
@@ -72,6 +72,7 @@ export * from './tenant_cash_in_out';
72
72
  export * from './tenant_chart_of_accounts';
73
73
  export * from './tenant_clients';
74
74
  export * from './tenant_day_sessions';
75
+ export * from './tenant_dialer_events';
75
76
  export * from './tenant_hr_attendance_records';
76
77
  export * from './tenant_hr_biometric_enrollments';
77
78
  export * from './tenant_hr_departments';
@@ -88,6 +88,7 @@ __exportStar(require("./tenant_cash_in_out"), exports);
88
88
  __exportStar(require("./tenant_chart_of_accounts"), exports);
89
89
  __exportStar(require("./tenant_clients"), exports);
90
90
  __exportStar(require("./tenant_day_sessions"), exports);
91
+ __exportStar(require("./tenant_dialer_events"), exports);
91
92
  __exportStar(require("./tenant_hr_attendance_records"), exports);
92
93
  __exportStar(require("./tenant_hr_biometric_enrollments"), exports);
93
94
  __exportStar(require("./tenant_hr_departments"), exports);
@@ -13,9 +13,9 @@ export declare const TenantActivityLogSchema: Schema<any, import("mongoose").Mod
13
13
  updatedAt: NativeDate;
14
14
  tenant_id: string;
15
15
  title: string;
16
+ action: import("../../constants").TENANT_ACTIVITY_LOG_ACTION_ENUM;
16
17
  entity_type: import("../../constants").TENANT_ACTIVITY_LOG_ENTITY_TYPE_ENUM;
17
18
  entity_id: string;
18
- action: import("../../constants").TENANT_ACTIVITY_LOG_ACTION_ENUM;
19
19
  severity: TENANT_ACTIVITY_LOG_SEVERITY_ENUM;
20
20
  changes: import("mongoose").Types.DocumentArray<{
21
21
  field: string;
@@ -49,9 +49,9 @@ export declare const TenantActivityLogSchema: Schema<any, import("mongoose").Mod
49
49
  updatedAt: NativeDate;
50
50
  tenant_id: string;
51
51
  title: string;
52
+ action: import("../../constants").TENANT_ACTIVITY_LOG_ACTION_ENUM;
52
53
  entity_type: import("../../constants").TENANT_ACTIVITY_LOG_ENTITY_TYPE_ENUM;
53
54
  entity_id: string;
54
- action: import("../../constants").TENANT_ACTIVITY_LOG_ACTION_ENUM;
55
55
  severity: TENANT_ACTIVITY_LOG_SEVERITY_ENUM;
56
56
  changes: import("mongoose").Types.DocumentArray<{
57
57
  field: string;
@@ -85,9 +85,9 @@ export declare const TenantActivityLogSchema: Schema<any, import("mongoose").Mod
85
85
  updatedAt: NativeDate;
86
86
  tenant_id: string;
87
87
  title: string;
88
+ action: import("../../constants").TENANT_ACTIVITY_LOG_ACTION_ENUM;
88
89
  entity_type: import("../../constants").TENANT_ACTIVITY_LOG_ENTITY_TYPE_ENUM;
89
90
  entity_id: string;
90
- action: import("../../constants").TENANT_ACTIVITY_LOG_ACTION_ENUM;
91
91
  severity: TENANT_ACTIVITY_LOG_SEVERITY_ENUM;
92
92
  changes: import("mongoose").Types.DocumentArray<{
93
93
  field: string;
@@ -0,0 +1,130 @@
1
+ import { Schema } from 'mongoose';
2
+ /**
3
+ * Shape version of a `tenant_dialer_events` row. Bump only with a migration
4
+ * for already-stored source events.
5
+ */
6
+ export declare const TENANT_DIALER_EVENTS_SCHEMA_VERSION = 1;
7
+ /**
8
+ * APPEND-ONLY source event stream for the future Dialer materialized report.
9
+ * Rows are written by items-service with
10
+ * `findOneAndUpdate({ tenant_id, request_id }, { $setOnInsert }, { upsert: true })`,
11
+ * so a retried request returns the ORIGINAL row and never rewrites
12
+ * `createdAt`, the local-time fields or any other stored value. No report
13
+ * fields (for example duration) live here.
14
+ */
15
+ export declare const TenantDialerEventSchema: Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
16
+ timestamps: true;
17
+ versionKey: false;
18
+ minimize: false;
19
+ }, {
20
+ createdAt: NativeDate;
21
+ updatedAt: NativeDate;
22
+ } & {
23
+ _id: import("mongoose").Types.ObjectId;
24
+ createdAt: NativeDate;
25
+ updatedAt: NativeDate;
26
+ tenant_id: string;
27
+ phone_number: string;
28
+ pos_id: string;
29
+ schema_version: number;
30
+ request_id: string;
31
+ action: import("../../constants").DIALER_TYPES_ENUM;
32
+ staff_id?: string | null | undefined;
33
+ staff_phone?: string | null | undefined;
34
+ }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
35
+ createdAt: NativeDate;
36
+ updatedAt: NativeDate;
37
+ } & {
38
+ _id: import("mongoose").Types.ObjectId;
39
+ createdAt: NativeDate;
40
+ updatedAt: NativeDate;
41
+ tenant_id: string;
42
+ phone_number: string;
43
+ pos_id: string;
44
+ schema_version: number;
45
+ request_id: string;
46
+ action: import("../../constants").DIALER_TYPES_ENUM;
47
+ staff_id?: string | null | undefined;
48
+ staff_phone?: string | null | undefined;
49
+ }>> & import("mongoose").FlatRecord<{
50
+ createdAt: NativeDate;
51
+ updatedAt: NativeDate;
52
+ } & {
53
+ _id: import("mongoose").Types.ObjectId;
54
+ createdAt: NativeDate;
55
+ updatedAt: NativeDate;
56
+ tenant_id: string;
57
+ phone_number: string;
58
+ pos_id: string;
59
+ schema_version: number;
60
+ request_id: string;
61
+ action: import("../../constants").DIALER_TYPES_ENUM;
62
+ staff_id?: string | null | undefined;
63
+ staff_phone?: string | null | undefined;
64
+ }> & Required<{
65
+ _id: import("mongoose").Types.ObjectId;
66
+ }> & {
67
+ __v: number;
68
+ }>;
69
+ export declare const TENANT_DIALER_EVENTS_COLLECTION_NAME = "tenant_dialer_events";
70
+ /**
71
+ * Describes the indexes for `tenant_dialer_events`. Values are Mongo key
72
+ * patterns that can be passed straight to `.hint()`.
73
+ * - `_id`: unique primary key.
74
+ * - `tenant_id__request_id`: UNIQUE idempotency key — one source event per
75
+ * tenant + request (native: `callId:action`, JS: UUID reused on retry).
76
+ * - `tenant_id__localDate__action`: future report reads per business day and
77
+ * action. Local fields may be absent for un-attributed events, so reads must
78
+ * tolerate a missing `localDate` (sparse-aware querying, not a partial
79
+ * index requirement).
80
+ * - `tenant_id__phone_number__createdAt`: future pairing/attribution reads for
81
+ * one caller phone ordered by the canonical receipt time.
82
+ */
83
+ export declare const TENANT_DIALER_EVENTS_COLLECTION_INDEXES: {
84
+ _id: {
85
+ _id: number;
86
+ };
87
+ tenant_id__request_id: {
88
+ tenant_id: number;
89
+ request_id: number;
90
+ };
91
+ tenant_id__localDate__action: {
92
+ tenant_id: number;
93
+ localDate: number;
94
+ action: number;
95
+ };
96
+ tenant_id__phone_number__createdAt: {
97
+ tenant_id: number;
98
+ phone_number: number;
99
+ createdAt: number;
100
+ };
101
+ };
102
+ /**
103
+ * Index OPTIONS to DECLARE (never created at application startup). The
104
+ * materializer/ops path owns creation; `tenant_id__request_id` MUST be created
105
+ * with `unique: true` or concurrent retries could insert a second event.
106
+ */
107
+ export declare const TENANT_DIALER_EVENTS_INDEX_OPTIONS: ({
108
+ key: {
109
+ tenant_id: number;
110
+ request_id: number;
111
+ };
112
+ name: string;
113
+ unique: boolean;
114
+ } | {
115
+ key: {
116
+ tenant_id: number;
117
+ localDate: number;
118
+ action: number;
119
+ };
120
+ name: string;
121
+ unique?: undefined;
122
+ } | {
123
+ key: {
124
+ tenant_id: number;
125
+ phone_number: number;
126
+ createdAt: number;
127
+ };
128
+ name: string;
129
+ unique?: undefined;
130
+ })[];
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TENANT_DIALER_EVENTS_INDEX_OPTIONS = exports.TENANT_DIALER_EVENTS_COLLECTION_INDEXES = exports.TENANT_DIALER_EVENTS_COLLECTION_NAME = exports.TenantDialerEventSchema = exports.TENANT_DIALER_EVENTS_SCHEMA_VERSION = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ const constants_1 = require("../../constants");
6
+ const common_models_1 = require("./common-models");
7
+ /**
8
+ * Shape version of a `tenant_dialer_events` row. Bump only with a migration
9
+ * for already-stored source events.
10
+ */
11
+ exports.TENANT_DIALER_EVENTS_SCHEMA_VERSION = 1;
12
+ /**
13
+ * The shared `LocalTimeFieldsSchema` is required for every other model. The
14
+ * dialer source stream is the one place local fields are OPTIONAL: when the
15
+ * running business day is unavailable the event is still stored, un-attributed
16
+ * and WITHOUT any local-time field. Derive an optional copy locally instead of
17
+ * weakening the shared schema.
18
+ */
19
+ const OptionalLocalTimeFieldsSchema = Object.fromEntries(Object.entries(common_models_1.LocalTimeFieldsSchema).map(([field, definition]) => [field, { ...definition, required: false }]));
20
+ /**
21
+ * APPEND-ONLY source event stream for the future Dialer materialized report.
22
+ * Rows are written by items-service with
23
+ * `findOneAndUpdate({ tenant_id, request_id }, { $setOnInsert }, { upsert: true })`,
24
+ * so a retried request returns the ORIGINAL row and never rewrites
25
+ * `createdAt`, the local-time fields or any other stored value. No report
26
+ * fields (for example duration) live here.
27
+ */
28
+ exports.TenantDialerEventSchema = new mongoose_1.Schema({
29
+ _id: {
30
+ type: mongoose_1.Schema.Types.ObjectId,
31
+ required: true,
32
+ immutable: true,
33
+ },
34
+ tenant_id: {
35
+ type: String,
36
+ required: true,
37
+ immutable: true,
38
+ ref: 'tenants',
39
+ },
40
+ request_id: {
41
+ type: String,
42
+ required: true,
43
+ immutable: true,
44
+ },
45
+ action: {
46
+ type: String,
47
+ enum: constants_1.ALL_DIALER_TYPE_EVENTS,
48
+ required: true,
49
+ immutable: true,
50
+ },
51
+ phone_number: {
52
+ type: String,
53
+ required: true,
54
+ immutable: true,
55
+ },
56
+ pos_id: {
57
+ type: String,
58
+ required: true,
59
+ immutable: true,
60
+ },
61
+ staff_id: {
62
+ type: String,
63
+ required: false,
64
+ default: null,
65
+ immutable: true,
66
+ },
67
+ staff_phone: {
68
+ type: String,
69
+ required: false,
70
+ default: null,
71
+ immutable: true,
72
+ },
73
+ schema_version: {
74
+ type: Number,
75
+ required: true,
76
+ enum: [exports.TENANT_DIALER_EVENTS_SCHEMA_VERSION],
77
+ default: exports.TENANT_DIALER_EVENTS_SCHEMA_VERSION,
78
+ immutable: true,
79
+ },
80
+ ...OptionalLocalTimeFieldsSchema,
81
+ createdAt: {
82
+ type: mongoose_1.Schema.Types.Date,
83
+ required: true,
84
+ default: () => new Date(),
85
+ immutable: true,
86
+ },
87
+ updatedAt: {
88
+ type: mongoose_1.Schema.Types.Date,
89
+ required: true,
90
+ default: () => new Date(),
91
+ },
92
+ }, {
93
+ timestamps: true,
94
+ versionKey: false,
95
+ minimize: false,
96
+ });
97
+ exports.TENANT_DIALER_EVENTS_COLLECTION_NAME = 'tenant_dialer_events';
98
+ /**
99
+ * Describes the indexes for `tenant_dialer_events`. Values are Mongo key
100
+ * patterns that can be passed straight to `.hint()`.
101
+ * - `_id`: unique primary key.
102
+ * - `tenant_id__request_id`: UNIQUE idempotency key — one source event per
103
+ * tenant + request (native: `callId:action`, JS: UUID reused on retry).
104
+ * - `tenant_id__localDate__action`: future report reads per business day and
105
+ * action. Local fields may be absent for un-attributed events, so reads must
106
+ * tolerate a missing `localDate` (sparse-aware querying, not a partial
107
+ * index requirement).
108
+ * - `tenant_id__phone_number__createdAt`: future pairing/attribution reads for
109
+ * one caller phone ordered by the canonical receipt time.
110
+ */
111
+ exports.TENANT_DIALER_EVENTS_COLLECTION_INDEXES = {
112
+ _id: { _id: 1 }, // Unique Index
113
+ tenant_id__request_id: { tenant_id: 1, request_id: 1 }, // Unique Index — ingestion idempotency
114
+ tenant_id__localDate__action: { tenant_id: 1, localDate: 1, action: 1 }, // per business day + action
115
+ tenant_id__phone_number__createdAt: { tenant_id: 1, phone_number: 1, createdAt: 1 }, // per caller phone over time
116
+ };
117
+ /**
118
+ * Index OPTIONS to DECLARE (never created at application startup). The
119
+ * materializer/ops path owns creation; `tenant_id__request_id` MUST be created
120
+ * with `unique: true` or concurrent retries could insert a second event.
121
+ */
122
+ exports.TENANT_DIALER_EVENTS_INDEX_OPTIONS = [
123
+ {
124
+ key: exports.TENANT_DIALER_EVENTS_COLLECTION_INDEXES.tenant_id__request_id,
125
+ name: 'tenant_id__request_id',
126
+ unique: true,
127
+ },
128
+ {
129
+ key: exports.TENANT_DIALER_EVENTS_COLLECTION_INDEXES.tenant_id__localDate__action,
130
+ name: 'tenant_id__localDate__action',
131
+ },
132
+ {
133
+ key: exports.TENANT_DIALER_EVENTS_COLLECTION_INDEXES.tenant_id__phone_number__createdAt,
134
+ name: 'tenant_id__phone_number__createdAt',
135
+ },
136
+ ];
@@ -13,9 +13,9 @@ export declare const WarehouseActivityLogSchema: Schema<any, import("mongoose").
13
13
  updatedAt: NativeDate;
14
14
  title: string;
15
15
  warehouse_id: string;
16
+ action: import("../../constants").WAREHOUSE_ACTIVITY_LOG_ACTION_ENUM;
16
17
  entity_type: import("../../constants").WAREHOUSE_ACTIVITY_LOG_ENTITY_TYPE_ENUM;
17
18
  entity_id: string;
18
- action: import("../../constants").WAREHOUSE_ACTIVITY_LOG_ACTION_ENUM;
19
19
  severity: WAREHOUSE_ACTIVITY_LOG_SEVERITY_ENUM;
20
20
  changes: import("mongoose").Types.DocumentArray<{
21
21
  field: string;
@@ -49,9 +49,9 @@ export declare const WarehouseActivityLogSchema: Schema<any, import("mongoose").
49
49
  updatedAt: NativeDate;
50
50
  title: string;
51
51
  warehouse_id: string;
52
+ action: import("../../constants").WAREHOUSE_ACTIVITY_LOG_ACTION_ENUM;
52
53
  entity_type: import("../../constants").WAREHOUSE_ACTIVITY_LOG_ENTITY_TYPE_ENUM;
53
54
  entity_id: string;
54
- action: import("../../constants").WAREHOUSE_ACTIVITY_LOG_ACTION_ENUM;
55
55
  severity: WAREHOUSE_ACTIVITY_LOG_SEVERITY_ENUM;
56
56
  changes: import("mongoose").Types.DocumentArray<{
57
57
  field: string;
@@ -85,9 +85,9 @@ export declare const WarehouseActivityLogSchema: Schema<any, import("mongoose").
85
85
  updatedAt: NativeDate;
86
86
  title: string;
87
87
  warehouse_id: string;
88
+ action: import("../../constants").WAREHOUSE_ACTIVITY_LOG_ACTION_ENUM;
88
89
  entity_type: import("../../constants").WAREHOUSE_ACTIVITY_LOG_ENTITY_TYPE_ENUM;
89
90
  entity_id: string;
90
- action: import("../../constants").WAREHOUSE_ACTIVITY_LOG_ACTION_ENUM;
91
91
  severity: WAREHOUSE_ACTIVITY_LOG_SEVERITY_ENUM;
92
92
  changes: import("mongoose").Types.DocumentArray<{
93
93
  field: string;
@@ -77,6 +77,7 @@ export * from './tenant_cash_in_out';
77
77
  export * from './tenant_chart_of_accounts';
78
78
  export * from './tenant_clients';
79
79
  export * from './tenant_day_sessions';
80
+ export * from './tenant_dialer_events';
80
81
  export * from './tenant_hr_attendance_records';
81
82
  export * from './tenant_hr_biometric_enrollments';
82
83
  export * from './tenant_hr_departments';
@@ -93,6 +93,7 @@ __exportStar(require("./tenant_cash_in_out"), exports);
93
93
  __exportStar(require("./tenant_chart_of_accounts"), exports);
94
94
  __exportStar(require("./tenant_clients"), exports);
95
95
  __exportStar(require("./tenant_day_sessions"), exports);
96
+ __exportStar(require("./tenant_dialer_events"), exports);
96
97
  __exportStar(require("./tenant_hr_attendance_records"), exports);
97
98
  __exportStar(require("./tenant_hr_biometric_enrollments"), exports);
98
99
  __exportStar(require("./tenant_hr_departments"), exports);
@@ -0,0 +1,35 @@
1
+ import { DIALER_TYPES_ENUM } from '../../constants';
2
+ import { ILocalTimeFields } from '../../helpers';
3
+ /**
4
+ * One raw dialer event (`tenant_dialer_events`) — an APPEND-ONLY source event
5
+ * from the POS dialer, kept so a future Dialer materialized report can derive
6
+ * call duration / success rate from event pairs for the same phone number.
7
+ *
8
+ * This is a SOURCE collection, not a materialized report:
9
+ * - `request_id` is the dialer's idempotency key (native: `callId:action`,
10
+ * JS: a UUID reused for the retry); `(tenant_id, request_id)` is UNIQUE.
11
+ * - `createdAt` is the canonical event receipt time. There is no separate
12
+ * `received_at` field and no fallback business-day flag.
13
+ * - Local-time fields are OPTIONAL: they are populated only when
14
+ * `track_order_numbers.runningLocalDate` is a valid running business day and
15
+ * the tenant operational offset is usable. A missing/invalid running day is
16
+ * a valid un-attributed event (no local fields, never a fabricated
17
+ * `localDate`); a future materializer treats it as un-attributed.
18
+ *
19
+ * Duration is NOT stored here — it will be derived later from
20
+ * INCOMING_CALL -> CALL_ENDED / CALL_DECLINED / MISSED_CALL pairs for the same
21
+ * tenant + phone number.
22
+ */
23
+ export interface ITenantDialerEvent extends Partial<ILocalTimeFields> {
24
+ _id: string;
25
+ tenant_id: string;
26
+ request_id: string;
27
+ action: DIALER_TYPES_ENUM;
28
+ phone_number: string;
29
+ pos_id: string;
30
+ staff_id: string | null;
31
+ staff_phone: string | null;
32
+ schema_version: 1;
33
+ createdAt: Date;
34
+ updatedAt: Date;
35
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const materialized_delivery_wipe_targets_1 = require("../../helpers/materialized-reports/materialized-delivery-wipe-targets");
4
+ /**
5
+ * PHASE 5 — operator wipe safety is pure and database-free. These tests only
6
+ * build/validate the target list; they never connect to MongoDB.
7
+ */
8
+ describe('Delivery wipe target safety', () => {
9
+ it('accepts every real Delivery wipe target and only materialized collections', () => {
10
+ const targets = (0, materialized_delivery_wipe_targets_1.buildDeliveryMaterializationWipeTargets)('tenant-1');
11
+ expect(() => (0, materialized_delivery_wipe_targets_1.assertMaterializedWipeTargets)(targets)).not.toThrow();
12
+ for (const target of targets) {
13
+ expect(target.name.startsWith('materialized_')).toBe(true);
14
+ }
15
+ });
16
+ it('refuses a source collection', () => {
17
+ expect(() => (0, materialized_delivery_wipe_targets_1.assertMaterializedWipeTargets)([{ name: 'orders', filter: { tenant_id: 'tenant-1' } }])).toThrow(/not a materialized/);
18
+ });
19
+ it('refuses a filter outside tenant/report identity', () => {
20
+ expect(() => (0, materialized_delivery_wipe_targets_1.assertMaterializedWipeTargets)([
21
+ {
22
+ name: 'materialized_tenant_delivery_reports',
23
+ filter: { tenant_id: 'tenant-1', order_id: 'order-1' },
24
+ },
25
+ ])).toThrow(/forbidden key/);
26
+ });
27
+ it('builds tenant-scoped and all-tenant targets without source collections', () => {
28
+ for (const targets of [
29
+ (0, materialized_delivery_wipe_targets_1.buildDeliveryMaterializationWipeTargets)(),
30
+ (0, materialized_delivery_wipe_targets_1.buildDeliveryMaterializationWipeTargets)('tenant-1'),
31
+ ]) {
32
+ expect(() => (0, materialized_delivery_wipe_targets_1.assertMaterializedWipeTargets)(targets)).not.toThrow();
33
+ expect(targets.map((target) => target.name)).not.toContain('orders');
34
+ expect(targets.map((target) => target.name)).not.toContain('deliveries');
35
+ }
36
+ });
37
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const mongoose_1 = __importDefault(require("mongoose"));
7
+ const constants_1 = require("../constants");
8
+ const tenant_dialer_events_1 = require("../schemas/models/tenant_dialer_events");
9
+ const Model = mongoose_1.default.models[tenant_dialer_events_1.TENANT_DIALER_EVENTS_COLLECTION_NAME] ??
10
+ mongoose_1.default.model(tenant_dialer_events_1.TENANT_DIALER_EVENTS_COLLECTION_NAME, tenant_dialer_events_1.TenantDialerEventSchema, tenant_dialer_events_1.TENANT_DIALER_EVENTS_COLLECTION_NAME);
11
+ const LOCAL_TIME_FIELDS = [
12
+ 'day_index',
13
+ 'localSecond',
14
+ 'localMinute',
15
+ 'localHour',
16
+ 'localDay',
17
+ 'localWeek',
18
+ 'localWeekOfYear',
19
+ 'localMonth',
20
+ 'localQuarter',
21
+ 'localSemester',
22
+ 'localYear',
23
+ 'localDate',
24
+ 'localTime',
25
+ 'localDayOfYear',
26
+ ];
27
+ /** Source fields only — a valid un-attributed event. Proves the `Partial` type. */
28
+ const baseRow = () => ({
29
+ _id: new mongoose_1.default.Types.ObjectId().toString(),
30
+ tenant_id: 'tenant-dialer-events',
31
+ request_id: 'call-123:INCOMING_CALL',
32
+ action: constants_1.DIALER_TYPES_ENUM.INCOMING_CALL,
33
+ phone_number: '+923001234567',
34
+ pos_id: 'pos-1',
35
+ staff_id: null,
36
+ staff_phone: null,
37
+ schema_version: 1,
38
+ createdAt: new Date('2026-03-10T20:30:45.000Z'),
39
+ updatedAt: new Date('2026-03-10T20:30:45.000Z'),
40
+ });
41
+ const localFields = () => ({
42
+ day_index: constants_1.DAY_INDEX_ENUM.TUE,
43
+ localSecond: 45,
44
+ localMinute: 30,
45
+ localHour: 1,
46
+ localDay: 2,
47
+ localWeek: 2,
48
+ localWeekOfYear: 11,
49
+ localMonth: 3,
50
+ localQuarter: 1,
51
+ localSemester: 1,
52
+ localYear: 2026,
53
+ localDate: '2026-03-10',
54
+ localTime: '01:30:45',
55
+ localDayOfYear: 69,
56
+ });
57
+ const attributedRow = () => ({ ...baseRow(), ...localFields() });
58
+ describe('tenant_dialer_events schema', () => {
59
+ it('exposes the agreed collection name', () => {
60
+ expect(tenant_dialer_events_1.TENANT_DIALER_EVENTS_COLLECTION_NAME).toBe('tenant_dialer_events');
61
+ });
62
+ it('is an un-attributed-valid Partial<ILocalTimeFields> event', () => {
63
+ // `baseRow()` has NO local-time fields and still satisfies the event type.
64
+ const unAttributed = baseRow();
65
+ expect(new Model(unAttributed).validateSync()).toBeUndefined();
66
+ for (const field of LOCAL_TIME_FIELDS) {
67
+ const path = tenant_dialer_events_1.TenantDialerEventSchema.path(field);
68
+ expect(path).toBeDefined();
69
+ expect(path.options.required).toBeFalsy();
70
+ expect(unAttributed).not.toHaveProperty(field);
71
+ }
72
+ });
73
+ it('validates a fully attributed source event with every local-time field', () => {
74
+ const doc = new Model(attributedRow());
75
+ expect(doc.validateSync()).toBeUndefined();
76
+ const stored = doc.toObject();
77
+ expect(stored).toMatchObject({
78
+ tenant_id: 'tenant-dialer-events',
79
+ request_id: 'call-123:INCOMING_CALL',
80
+ action: constants_1.DIALER_TYPES_ENUM.INCOMING_CALL,
81
+ phone_number: '+923001234567',
82
+ pos_id: 'pos-1',
83
+ staff_id: null,
84
+ staff_phone: null,
85
+ schema_version: 1,
86
+ localDate: '2026-03-10',
87
+ localTime: '01:30:45',
88
+ day_index: constants_1.DAY_INDEX_ENUM.TUE,
89
+ localSecond: 45,
90
+ localMinute: 30,
91
+ localHour: 1,
92
+ localDay: 2,
93
+ localWeek: 2,
94
+ localWeekOfYear: 11,
95
+ localMonth: 3,
96
+ localQuarter: 1,
97
+ localSemester: 1,
98
+ localYear: 2026,
99
+ localDayOfYear: 69,
100
+ });
101
+ });
102
+ it('has NO received_at and NO running_day_missing', () => {
103
+ expect(tenant_dialer_events_1.TenantDialerEventSchema.path('received_at')).toBeUndefined();
104
+ expect(tenant_dialer_events_1.TenantDialerEventSchema.path('running_day_missing')).toBeUndefined();
105
+ expect(attributedRow()).not.toHaveProperty('received_at');
106
+ expect(attributedRow()).not.toHaveProperty('running_day_missing');
107
+ });
108
+ it('keeps createdAt as the canonical receipt time (required, immutable)', () => {
109
+ const path = tenant_dialer_events_1.TenantDialerEventSchema.path('createdAt');
110
+ expect(path).toBeDefined();
111
+ expect(path.options.required).toBeTruthy();
112
+ expect(path.options.immutable).toBe(true);
113
+ const row = { ...baseRow() };
114
+ delete row.createdAt;
115
+ // Defaults fill the canonical time when omitted on a direct create.
116
+ expect(new Model(row).createdAt).toBeInstanceOf(Date);
117
+ });
118
+ it('accepts every DIALER_TYPES_ENUM action', () => {
119
+ for (const action of constants_1.ALL_DIALER_TYPE_EVENTS) {
120
+ const doc = new Model({ ...baseRow(), action });
121
+ expect(doc.validateSync()).toBeUndefined();
122
+ }
123
+ });
124
+ it('rejects an unknown action', () => {
125
+ const error = new Model({ ...baseRow(), action: 'NOT_A_DIALER_ACTION' }).validateSync();
126
+ expect(error?.errors).toHaveProperty('action');
127
+ });
128
+ it('rejects invalid local-time values but allows the fields to be absent', () => {
129
+ const badHour = new Model({ ...attributedRow(), localHour: 25 }).validateSync();
130
+ expect(badHour?.errors).toHaveProperty('localHour');
131
+ const badDate = new Model({ ...attributedRow(), localDate: undefined }).validateSync();
132
+ expect(badDate).toBeUndefined();
133
+ });
134
+ it('does not weaken the required source fields', () => {
135
+ for (const field of ['tenant_id', 'request_id', 'action', 'phone_number', 'pos_id']) {
136
+ const row = { ...baseRow() };
137
+ delete row[field];
138
+ expect(new Model(row).validateSync()?.errors).toHaveProperty(field);
139
+ }
140
+ for (const field of [
141
+ 'tenant_id',
142
+ 'request_id',
143
+ 'action',
144
+ 'phone_number',
145
+ 'pos_id',
146
+ 'schema_version',
147
+ 'createdAt',
148
+ 'updatedAt',
149
+ ]) {
150
+ const path = tenant_dialer_events_1.TenantDialerEventSchema.path(field);
151
+ expect(path).toBeDefined();
152
+ expect(path.options.required).toBeTruthy();
153
+ }
154
+ });
155
+ it('keeps staff fields nullable', () => {
156
+ expect(new Model(baseRow()).get('staff_id')).toBeNull();
157
+ expect(new Model(baseRow()).get('staff_phone')).toBeNull();
158
+ expect(new Model({ ...baseRow(), staff_id: 'staff-1', staff_phone: '+923007654321' }).validateSync()).toBeUndefined();
159
+ });
160
+ it('enforces schema_version = 1', () => {
161
+ expect(tenant_dialer_events_1.TENANT_DIALER_EVENTS_SCHEMA_VERSION).toBe(1);
162
+ expect(new Model({ ...baseRow(), schema_version: 2 }).validateSync()?.errors).toHaveProperty('schema_version');
163
+ });
164
+ it('does NOT declare duration_seconds (derived later, never stored)', () => {
165
+ expect(tenant_dialer_events_1.TenantDialerEventSchema.path('duration_seconds')).toBeUndefined();
166
+ expect(attributedRow()).not.toHaveProperty('duration_seconds');
167
+ });
168
+ it('declares the unique ingestion index plus the two source-read indexes', () => {
169
+ expect(tenant_dialer_events_1.TENANT_DIALER_EVENTS_COLLECTION_INDEXES).toEqual({
170
+ _id: { _id: 1 },
171
+ tenant_id__request_id: { tenant_id: 1, request_id: 1 },
172
+ tenant_id__localDate__action: { tenant_id: 1, localDate: 1, action: 1 },
173
+ tenant_id__phone_number__createdAt: { tenant_id: 1, phone_number: 1, createdAt: 1 },
174
+ });
175
+ expect(tenant_dialer_events_1.TENANT_DIALER_EVENTS_INDEX_OPTIONS).toEqual([
176
+ {
177
+ key: { tenant_id: 1, request_id: 1 },
178
+ name: 'tenant_id__request_id',
179
+ unique: true,
180
+ },
181
+ {
182
+ key: { tenant_id: 1, localDate: 1, action: 1 },
183
+ name: 'tenant_id__localDate__action',
184
+ },
185
+ {
186
+ key: { tenant_id: 1, phone_number: 1, createdAt: 1 },
187
+ name: 'tenant_id__phone_number__createdAt',
188
+ },
189
+ ]);
190
+ });
191
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.54",
3
+ "version": "0.4.55",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",