@managesome/eposmatic-toolkit 0.4.15 → 0.4.16

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 (25) hide show
  1. package/esm/helpers/materialized-reports/index.js +1 -0
  2. package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +162 -0
  3. package/esm/helpers/reporting-contract/report-contract.js +1 -0
  4. package/esm/schemas/models/index.js +1 -0
  5. package/esm/schemas/models/materialized_report_drilldown_rows.js +155 -0
  6. package/esm/schemas/types/index.js +1 -0
  7. package/esm/schemas/types/materialized_report_drilldown_rows.js +10 -0
  8. package/lib/helpers/materialized-reports/index.d.ts +1 -0
  9. package/lib/helpers/materialized-reports/index.js +1 -0
  10. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.d.ts +40 -0
  11. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +169 -0
  12. package/lib/helpers/reporting-contract/report-contract.d.ts +3 -0
  13. package/lib/helpers/reporting-contract/report-contract.js +1 -0
  14. package/lib/schemas/models/index.d.ts +1 -0
  15. package/lib/schemas/models/index.js +1 -0
  16. package/lib/schemas/models/materialized_report_drilldown_rows.d.ts +130 -0
  17. package/lib/schemas/models/materialized_report_drilldown_rows.js +158 -0
  18. package/lib/schemas/types/index.d.ts +1 -0
  19. package/lib/schemas/types/index.js +1 -0
  20. package/lib/schemas/types/materialized_report_drilldown_rows.d.ts +64 -0
  21. package/lib/schemas/types/materialized_report_drilldown_rows.js +13 -0
  22. package/lib/tests/materialized-reports/materialized-report-drilldown-rows.test.d.ts +1 -0
  23. package/lib/tests/materialized-reports/materialized-report-drilldown-rows.test.js +143 -0
  24. package/lib/tests/reporting-contract.test.js +7 -0
  25. package/package.json +1 -1
@@ -21,6 +21,7 @@ export * from './materialized-cash-compile-helpers';
21
21
  export * from './materialized-prep-time-classification-helpers';
22
22
  export * from './materialized-prep-time-close-day-helpers';
23
23
  export * from './materialized-prep-time-compile-helpers';
24
+ export * from './materialized-report-drilldown-rows';
24
25
  export * from './materialized-reports-read-helpers';
25
26
  export * from './materialized-reports-scope-group-helpers';
26
27
  export * from './materialized-reports-selection-helpers';
@@ -0,0 +1,162 @@
1
+ import { MATERIALIZED_REPORT_TYPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
2
+ import { MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION } from '../../schemas/models/materialized_report_drilldown_rows';
3
+ const asString = (value) => (typeof value === 'string' ? value : value == null ? '' : String(value));
4
+ const toDate = (value) => {
5
+ if (value instanceof Date)
6
+ return Number.isNaN(value.getTime()) ? null : value;
7
+ if (typeof value === 'string' || typeof value === 'number') {
8
+ const date = new Date(value);
9
+ return Number.isNaN(date.getTime()) ? null : date;
10
+ }
11
+ // Demo files are Extended JSON before upload; production docs are Dates.
12
+ if (value != null && typeof value === 'object' && '$date' in value) {
13
+ return toDate(value.$date);
14
+ }
15
+ return null;
16
+ };
17
+ const ROW_SPECS = {
18
+ [MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES]: {
19
+ rowsKey: 'discount_entries',
20
+ time: (row) => row.created_at,
21
+ orderId: (row) => row.order_id,
22
+ cursorId: (row) => row.entry_key,
23
+ },
24
+ [MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PREP_TIME_ENTRIES]: {
25
+ rowsKey: 'prep_time_entries',
26
+ time: (row) => row.created_at,
27
+ orderId: (row) => row.order_id,
28
+ cursorId: (row) => row.item_id,
29
+ },
30
+ [MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES]: {
31
+ rowsKey: 'cash_entries',
32
+ time: (row) => row.created_at,
33
+ // Cash rows are drawer-ledger rows, not orders.
34
+ orderId: () => '',
35
+ cursorId: (row) => row.cash_in_out_id ?? row.entry_key,
36
+ },
37
+ [MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES]: {
38
+ rowsKey: 'delivery_entries',
39
+ time: (row) => row.delivered_at ?? row.created_at,
40
+ orderId: (row) => row.order_id,
41
+ cursorId: (row) => row.delivery_id,
42
+ },
43
+ [MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_ENTRIES]: {
44
+ rowsKey: 'sales_entries',
45
+ time: (row) => row.time,
46
+ orderId: (row) => row.order_id,
47
+ cursorId: (row) => row.order_id,
48
+ },
49
+ [MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES]: {
50
+ rowsKey: 'sales_entries',
51
+ time: (row) => row.time,
52
+ orderId: (row) => row.order_id,
53
+ cursorId: (row) => row.order_id,
54
+ },
55
+ [MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PAYMENTS_COMPARISON_ENTRIES]: {
56
+ rowsKey: 'payment_entries',
57
+ time: (row) => row.time,
58
+ orderId: (row) => row.order_id,
59
+ cursorId: (row) => row.payment_id,
60
+ },
61
+ };
62
+ /** The nested map key an entry collection uses for its row sections. */
63
+ export const getDrilldownRowsKey = (reportType) => ROW_SPECS[reportType].rowsKey;
64
+ /**
65
+ * Projects one entry document. Rows missing the report's stable identity or a
66
+ * usable time anchor are skipped and counted; the writer can decide whether to
67
+ * fail loudly.
68
+ */
69
+ export function buildMaterializedDrilldownRows(reportType, document) {
70
+ if (document == null)
71
+ return { rows: [], skipped: 0 };
72
+ const spec = ROW_SPECS[reportType];
73
+ if (spec == null)
74
+ return { rows: [], skipped: 0 };
75
+ const tenantId = asString(document.tenant_id);
76
+ const brandId = asString(document.brand_id);
77
+ const scopeContextRaw = asString(document.scope_context);
78
+ const scopeField = asString(document.scope_field);
79
+ if (tenantId === '' || scopeContextRaw === '' || scopeField === '') {
80
+ return { rows: [], skipped: 0 };
81
+ }
82
+ const scopeContext = scopeContextRaw;
83
+ const sections = document[spec.rowsKey];
84
+ const computedAt = toDate(document.computed_at) ?? new Date();
85
+ const dayIndex = (document.day_index ?? null);
86
+ const rows = [];
87
+ let skipped = 0;
88
+ for (const [key, section] of Object.entries(sections ?? {})) {
89
+ if (section == null || !Array.isArray(section.rows))
90
+ continue;
91
+ for (const raw of section.rows) {
92
+ if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) {
93
+ skipped += 1;
94
+ continue;
95
+ }
96
+ const row = raw;
97
+ const time = toDate(spec.time(row));
98
+ const orderId = asString(spec.orderId(row));
99
+ const cursorId = asString(spec.cursorId(row));
100
+ // Discounts are served only when the row carries its kind; the nested
101
+ // collector applies the same requirement before it can be viewed.
102
+ const kindMissing = reportType === MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES && asString(row.kind) === '';
103
+ const orderMissing = reportType !== MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES && orderId === '';
104
+ if (time == null || cursorId === '' || kindMissing || orderMissing) {
105
+ skipped += 1;
106
+ continue;
107
+ }
108
+ rows.push({
109
+ tenant_id: tenantId,
110
+ brand_id: brandId,
111
+ report_type: reportType,
112
+ scope_context: scopeContext,
113
+ scope_field: scopeField,
114
+ day_index: dayIndex,
115
+ key: asString(key),
116
+ order_id: orderId,
117
+ cursor_id: cursorId,
118
+ time,
119
+ data: row,
120
+ schema_version: MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION,
121
+ computed_at: computedAt,
122
+ });
123
+ }
124
+ }
125
+ return { rows, skipped };
126
+ }
127
+ /** Flattens many entry documents of one report (used by backfill/demo). */
128
+ export function buildMaterializedDrilldownRowsFromDocuments(reportType, documents) {
129
+ const rows = [];
130
+ let skipped = 0;
131
+ for (const document of documents ?? []) {
132
+ const result = buildMaterializedDrilldownRows(reportType, document);
133
+ rows.push(...result.rows);
134
+ skipped += result.skipped;
135
+ }
136
+ return { rows, skipped };
137
+ }
138
+ /** Scope metadata (row count + delivery off-app count) for the flat reader. */
139
+ export function buildMaterializedDrilldownScope(reportType, document, rows) {
140
+ if (document == null)
141
+ return null;
142
+ const tenantId = asString(document.tenant_id);
143
+ const scopeContextRaw = asString(document.scope_context);
144
+ const scopeField = asString(document.scope_field);
145
+ if (tenantId === '' || scopeContextRaw === '' || scopeField === '')
146
+ return null;
147
+ const scopeContext = scopeContextRaw;
148
+ const offApp = reportType === MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES
149
+ ? Math.max(0, Number(document.off_app_order_count) || 0)
150
+ : 0;
151
+ return {
152
+ tenant_id: tenantId,
153
+ brand_id: asString(document.brand_id),
154
+ report_type: reportType,
155
+ scope_context: scopeContext,
156
+ scope_field: scopeField,
157
+ row_count: rows.length,
158
+ off_app_order_count: offApp,
159
+ schema_version: MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION,
160
+ computed_at: toDate(document.computed_at) ?? new Date(),
161
+ };
162
+ }
@@ -175,6 +175,7 @@ export function buildReportDetailsResponse(input) {
175
175
  meta: input.meta,
176
176
  rows: input.rows,
177
177
  next_cursor: input.nextCursor ?? null,
178
+ ...(input.offAppOrderCount != null ? { off_app_order_count: input.offAppOrderCount } : {}),
178
179
  };
179
180
  }
180
181
  export function buildReportAnalyticsResponse(input) {
@@ -37,6 +37,7 @@ export * from './materialized_tenant_cash_reports';
37
37
  export * from './materialized_tenant_prep_time_entries';
38
38
  export * from './materialized_tenant_prep_time_reports';
39
39
  export * from './materialized_tenant_reports_iterations';
40
+ export * from './materialized_report_drilldown_rows';
40
41
  export * from './materialized_tenant_sales_comparison_entries';
41
42
  export * from './materialized_tenant_sales_comparison_reports';
42
43
  export * from './materialized_tenant_hourly_comparison_entries';
@@ -0,0 +1,155 @@
1
+ import { Schema } from 'mongoose';
2
+ import { ALL_DAY_INDEX } from '../../constants/ENUMS';
3
+ import { ALL_MATERIALIZED_REPORT_SCOPES, MATERIALIZED_REPORT_TYPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
4
+ import { ALL_DRILLDOWN_REPORT_TYPES, } from '../types/materialized_report_drilldown_rows';
5
+ /**
6
+ * FLAT drill-down rows and their scope metadata (see the type module).
7
+ *
8
+ * These are READ projections of the nested `*_entries` documents. The writer
9
+ * replaces all rows for one `(tenant, report_type, scope_context, scope_field)`
10
+ * scope after that scope's entry document is written; re-closing a day is
11
+ * therefore idempotent, and a stale scope can be deleted by identity.
12
+ */
13
+ export const MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION = 1;
14
+ export const MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME = 'materialized_report_drilldown_rows';
15
+ export const MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME = 'materialized_report_drilldown_scopes';
16
+ const ReportTypeEnum = ALL_DRILLDOWN_REPORT_TYPES;
17
+ export const MaterializedReportDrilldownRowSchema = new Schema({
18
+ tenant_id: { type: String, required: true, immutable: true },
19
+ brand_id: { type: String, required: true },
20
+ report_type: {
21
+ type: String,
22
+ required: true,
23
+ immutable: true,
24
+ enum: ReportTypeEnum,
25
+ },
26
+ scope_context: {
27
+ type: String,
28
+ required: true,
29
+ immutable: true,
30
+ enum: ALL_MATERIALIZED_REPORT_SCOPES,
31
+ },
32
+ scope_field: { type: String, required: true, immutable: true },
33
+ day_index: { type: String, default: null, enum: [...ALL_DAY_INDEX, null] },
34
+ key: { type: String, required: true },
35
+ // Cash rows are drawer-ledger rows and have no order id; the field is
36
+ // still present (empty) because it is part of the identity/cursor tuple.
37
+ order_id: { type: String, default: '' },
38
+ cursor_id: { type: String, required: true },
39
+ time: { type: Schema.Types.Date, required: true },
40
+ // The original row is polymorphic across reports; the report_type
41
+ // discriminator says which shape it is.
42
+ data: { type: Schema.Types.Mixed, required: true },
43
+ schema_version: {
44
+ type: Number,
45
+ required: true,
46
+ immutable: true,
47
+ default: MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION,
48
+ },
49
+ computed_at: { type: Schema.Types.Date, required: true },
50
+ }, { timestamps: true, versionKey: false, minimize: false });
51
+ export const MaterializedReportDrilldownScopeSchema = new Schema({
52
+ tenant_id: { type: String, required: true, immutable: true },
53
+ brand_id: { type: String, required: true },
54
+ report_type: {
55
+ type: String,
56
+ required: true,
57
+ immutable: true,
58
+ enum: ReportTypeEnum,
59
+ },
60
+ scope_context: {
61
+ type: String,
62
+ required: true,
63
+ immutable: true,
64
+ enum: ALL_MATERIALIZED_REPORT_SCOPES,
65
+ },
66
+ scope_field: { type: String, required: true, immutable: true },
67
+ row_count: { type: Number, required: true, min: 0, default: 0 },
68
+ off_app_order_count: { type: Number, required: true, min: 0, default: 0 },
69
+ schema_version: {
70
+ type: Number,
71
+ required: true,
72
+ immutable: true,
73
+ default: MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION,
74
+ },
75
+ computed_at: { type: Schema.Types.Date, required: true },
76
+ }, { timestamps: true, versionKey: false, minimize: false });
77
+ /**
78
+ * Index keys consumed by the flat reader.
79
+ *
80
+ * `identity` is the write-side upsert/delete key. The two read indexes cover
81
+ * the two cursor orders the reports already expose:
82
+ * - ascending tie-break: discounts/prep/sales/hourly/payments;
83
+ * - descending tie-break: cash/delivery.
84
+ */
85
+ export const MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES = {
86
+ _id: { _id: 1 },
87
+ identity: {
88
+ tenant_id: 1,
89
+ report_type: 1,
90
+ scope_context: 1,
91
+ scope_field: 1,
92
+ order_id: 1,
93
+ cursor_id: 1,
94
+ },
95
+ read_asc: {
96
+ tenant_id: 1,
97
+ report_type: 1,
98
+ scope_context: 1,
99
+ scope_field: 1,
100
+ time: -1,
101
+ order_id: 1,
102
+ cursor_id: 1,
103
+ },
104
+ read_desc: {
105
+ tenant_id: 1,
106
+ report_type: 1,
107
+ scope_context: 1,
108
+ scope_field: 1,
109
+ time: -1,
110
+ cursor_id: -1,
111
+ },
112
+ };
113
+ export const MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS = [
114
+ {
115
+ key: MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.identity,
116
+ name: 'tenant_id__report_type__scope__order_id__cursor_id',
117
+ unique: true,
118
+ },
119
+ {
120
+ key: MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.read_asc,
121
+ name: 'tenant_id__report_type__scope__time_desc__order_id__cursor_id',
122
+ },
123
+ {
124
+ key: MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.read_desc,
125
+ name: 'tenant_id__report_type__scope__time_desc__cursor_id_desc',
126
+ },
127
+ ];
128
+ export const MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_INDEXES = {
129
+ _id: { _id: 1 },
130
+ identity: {
131
+ tenant_id: 1,
132
+ report_type: 1,
133
+ scope_context: 1,
134
+ scope_field: 1,
135
+ },
136
+ };
137
+ export const MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS = [
138
+ {
139
+ key: MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_INDEXES.identity,
140
+ name: 'tenant_id__report_type__scope',
141
+ unique: true,
142
+ },
143
+ ];
144
+ /** The report-type values the flat reader accepts. */
145
+ export const ALL_DRILLDOWN_REPORT_TYPE_VALUES = ALL_DRILLDOWN_REPORT_TYPES;
146
+ /** Kept explicit so the JSON/demo upload can validate a file's report type. */
147
+ export const DRILLDOWN_REPORT_TYPE_BY_ENTRY_COLLECTION = {
148
+ materialized_tenant_discount_entries: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES,
149
+ materialized_tenant_prep_time_entries: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PREP_TIME_ENTRIES,
150
+ materialized_tenant_cash_in_out_entries: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES,
151
+ materialized_tenant_delivery_entries: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES,
152
+ materialized_tenant_sales_comparison_entries: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_ENTRIES,
153
+ materialized_tenant_hourly_comparison_entries: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES,
154
+ materialized_tenant_payments_comparison_entries: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PAYMENTS_COMPARISON_ENTRIES,
155
+ };
@@ -128,3 +128,4 @@ export * from './materialized_tenant_hourly_comparison_reports';
128
128
  export * from './materialized_tenant_hourly_comparison_entries';
129
129
  export * from './materialized_tenant_payments_comparison_reports';
130
130
  export * from './materialized_tenant_payments_comparison_entries';
131
+ export * from './materialized_report_drilldown_rows';
@@ -0,0 +1,10 @@
1
+ import { MATERIALIZED_REPORT_TYPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
2
+ export const ALL_DRILLDOWN_REPORT_TYPES = [
3
+ MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES,
4
+ MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PREP_TIME_ENTRIES,
5
+ MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES,
6
+ MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES,
7
+ MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_ENTRIES,
8
+ MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES,
9
+ MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PAYMENTS_COMPARISON_ENTRIES,
10
+ ];
@@ -21,6 +21,7 @@ export * from './materialized-cash-compile-helpers';
21
21
  export * from './materialized-prep-time-classification-helpers';
22
22
  export * from './materialized-prep-time-close-day-helpers';
23
23
  export * from './materialized-prep-time-compile-helpers';
24
+ export * from './materialized-report-drilldown-rows';
24
25
  export * from './materialized-reports-read-helpers';
25
26
  export * from './materialized-reports-scope-group-helpers';
26
27
  export * from './materialized-reports-selection-helpers';
@@ -37,6 +37,7 @@ __exportStar(require("./materialized-cash-compile-helpers"), exports);
37
37
  __exportStar(require("./materialized-prep-time-classification-helpers"), exports);
38
38
  __exportStar(require("./materialized-prep-time-close-day-helpers"), exports);
39
39
  __exportStar(require("./materialized-prep-time-compile-helpers"), exports);
40
+ __exportStar(require("./materialized-report-drilldown-rows"), exports);
40
41
  __exportStar(require("./materialized-reports-read-helpers"), exports);
41
42
  __exportStar(require("./materialized-reports-scope-group-helpers"), exports);
42
43
  __exportStar(require("./materialized-reports-selection-helpers"), exports);
@@ -0,0 +1,40 @@
1
+ import { IMaterializedReportDrilldownRow, IMaterializedReportDrilldownScope, TDrilldownReportType } from '../../schemas/types/materialized_report_drilldown_rows';
2
+ /**
3
+ * Pure projection from one nested entry document to FLAT drill-down rows.
4
+ *
5
+ * This module is write-side only: the close-day writer (workers-service, next
6
+ * session) calls it after persisting an entry document, then replaces the rows
7
+ * for that `(tenant, report_type, scope_context, scope_field)` scope. The read
8
+ * path never flattens nested documents.
9
+ *
10
+ * The row identity is intentionally the SAME tie-break each report's cursor
11
+ * already uses, so the API cursor shape does not change when the read path
12
+ * switches from nested to flat.
13
+ */
14
+ interface IEntryDocumentLike {
15
+ tenant_id?: unknown;
16
+ brand_id?: unknown;
17
+ scope_context?: unknown;
18
+ scope_field?: unknown;
19
+ day_index?: unknown;
20
+ computed_at?: unknown;
21
+ off_app_order_count?: unknown;
22
+ [key: string]: unknown;
23
+ }
24
+ /** The nested map key an entry collection uses for its row sections. */
25
+ export declare const getDrilldownRowsKey: (reportType: TDrilldownReportType) => string;
26
+ export interface IBuildDrilldownRowsResult {
27
+ rows: IMaterializedReportDrilldownRow[];
28
+ skipped: number;
29
+ }
30
+ /**
31
+ * Projects one entry document. Rows missing the report's stable identity or a
32
+ * usable time anchor are skipped and counted; the writer can decide whether to
33
+ * fail loudly.
34
+ */
35
+ export declare function buildMaterializedDrilldownRows(reportType: TDrilldownReportType, document: IEntryDocumentLike | null | undefined): IBuildDrilldownRowsResult;
36
+ /** Flattens many entry documents of one report (used by backfill/demo). */
37
+ export declare function buildMaterializedDrilldownRowsFromDocuments(reportType: TDrilldownReportType, documents: IEntryDocumentLike[] | null | undefined): IBuildDrilldownRowsResult;
38
+ /** Scope metadata (row count + delivery off-app count) for the flat reader. */
39
+ export declare function buildMaterializedDrilldownScope(reportType: TDrilldownReportType, document: IEntryDocumentLike | null | undefined, rows: IMaterializedReportDrilldownRow[]): IMaterializedReportDrilldownScope | null;
40
+ export {};
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDrilldownRowsKey = void 0;
4
+ exports.buildMaterializedDrilldownRows = buildMaterializedDrilldownRows;
5
+ exports.buildMaterializedDrilldownRowsFromDocuments = buildMaterializedDrilldownRowsFromDocuments;
6
+ exports.buildMaterializedDrilldownScope = buildMaterializedDrilldownScope;
7
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
8
+ const materialized_report_drilldown_rows_1 = require("../../schemas/models/materialized_report_drilldown_rows");
9
+ const asString = (value) => (typeof value === 'string' ? value : value == null ? '' : String(value));
10
+ const toDate = (value) => {
11
+ if (value instanceof Date)
12
+ return Number.isNaN(value.getTime()) ? null : value;
13
+ if (typeof value === 'string' || typeof value === 'number') {
14
+ const date = new Date(value);
15
+ return Number.isNaN(date.getTime()) ? null : date;
16
+ }
17
+ // Demo files are Extended JSON before upload; production docs are Dates.
18
+ if (value != null && typeof value === 'object' && '$date' in value) {
19
+ return toDate(value.$date);
20
+ }
21
+ return null;
22
+ };
23
+ const ROW_SPECS = {
24
+ [MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES]: {
25
+ rowsKey: 'discount_entries',
26
+ time: (row) => row.created_at,
27
+ orderId: (row) => row.order_id,
28
+ cursorId: (row) => row.entry_key,
29
+ },
30
+ [MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PREP_TIME_ENTRIES]: {
31
+ rowsKey: 'prep_time_entries',
32
+ time: (row) => row.created_at,
33
+ orderId: (row) => row.order_id,
34
+ cursorId: (row) => row.item_id,
35
+ },
36
+ [MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES]: {
37
+ rowsKey: 'cash_entries',
38
+ time: (row) => row.created_at,
39
+ // Cash rows are drawer-ledger rows, not orders.
40
+ orderId: () => '',
41
+ cursorId: (row) => row.cash_in_out_id ?? row.entry_key,
42
+ },
43
+ [MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES]: {
44
+ rowsKey: 'delivery_entries',
45
+ time: (row) => row.delivered_at ?? row.created_at,
46
+ orderId: (row) => row.order_id,
47
+ cursorId: (row) => row.delivery_id,
48
+ },
49
+ [MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_ENTRIES]: {
50
+ rowsKey: 'sales_entries',
51
+ time: (row) => row.time,
52
+ orderId: (row) => row.order_id,
53
+ cursorId: (row) => row.order_id,
54
+ },
55
+ [MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES]: {
56
+ rowsKey: 'sales_entries',
57
+ time: (row) => row.time,
58
+ orderId: (row) => row.order_id,
59
+ cursorId: (row) => row.order_id,
60
+ },
61
+ [MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PAYMENTS_COMPARISON_ENTRIES]: {
62
+ rowsKey: 'payment_entries',
63
+ time: (row) => row.time,
64
+ orderId: (row) => row.order_id,
65
+ cursorId: (row) => row.payment_id,
66
+ },
67
+ };
68
+ /** The nested map key an entry collection uses for its row sections. */
69
+ const getDrilldownRowsKey = (reportType) => ROW_SPECS[reportType].rowsKey;
70
+ exports.getDrilldownRowsKey = getDrilldownRowsKey;
71
+ /**
72
+ * Projects one entry document. Rows missing the report's stable identity or a
73
+ * usable time anchor are skipped and counted; the writer can decide whether to
74
+ * fail loudly.
75
+ */
76
+ function buildMaterializedDrilldownRows(reportType, document) {
77
+ if (document == null)
78
+ return { rows: [], skipped: 0 };
79
+ const spec = ROW_SPECS[reportType];
80
+ if (spec == null)
81
+ return { rows: [], skipped: 0 };
82
+ const tenantId = asString(document.tenant_id);
83
+ const brandId = asString(document.brand_id);
84
+ const scopeContextRaw = asString(document.scope_context);
85
+ const scopeField = asString(document.scope_field);
86
+ if (tenantId === '' || scopeContextRaw === '' || scopeField === '') {
87
+ return { rows: [], skipped: 0 };
88
+ }
89
+ const scopeContext = scopeContextRaw;
90
+ const sections = document[spec.rowsKey];
91
+ const computedAt = toDate(document.computed_at) ?? new Date();
92
+ const dayIndex = (document.day_index ?? null);
93
+ const rows = [];
94
+ let skipped = 0;
95
+ for (const [key, section] of Object.entries(sections ?? {})) {
96
+ if (section == null || !Array.isArray(section.rows))
97
+ continue;
98
+ for (const raw of section.rows) {
99
+ if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) {
100
+ skipped += 1;
101
+ continue;
102
+ }
103
+ const row = raw;
104
+ const time = toDate(spec.time(row));
105
+ const orderId = asString(spec.orderId(row));
106
+ const cursorId = asString(spec.cursorId(row));
107
+ // Discounts are served only when the row carries its kind; the nested
108
+ // collector applies the same requirement before it can be viewed.
109
+ const kindMissing = reportType === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES && asString(row.kind) === '';
110
+ const orderMissing = reportType !== MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES && orderId === '';
111
+ if (time == null || cursorId === '' || kindMissing || orderMissing) {
112
+ skipped += 1;
113
+ continue;
114
+ }
115
+ rows.push({
116
+ tenant_id: tenantId,
117
+ brand_id: brandId,
118
+ report_type: reportType,
119
+ scope_context: scopeContext,
120
+ scope_field: scopeField,
121
+ day_index: dayIndex,
122
+ key: asString(key),
123
+ order_id: orderId,
124
+ cursor_id: cursorId,
125
+ time,
126
+ data: row,
127
+ schema_version: materialized_report_drilldown_rows_1.MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION,
128
+ computed_at: computedAt,
129
+ });
130
+ }
131
+ }
132
+ return { rows, skipped };
133
+ }
134
+ /** Flattens many entry documents of one report (used by backfill/demo). */
135
+ function buildMaterializedDrilldownRowsFromDocuments(reportType, documents) {
136
+ const rows = [];
137
+ let skipped = 0;
138
+ for (const document of documents ?? []) {
139
+ const result = buildMaterializedDrilldownRows(reportType, document);
140
+ rows.push(...result.rows);
141
+ skipped += result.skipped;
142
+ }
143
+ return { rows, skipped };
144
+ }
145
+ /** Scope metadata (row count + delivery off-app count) for the flat reader. */
146
+ function buildMaterializedDrilldownScope(reportType, document, rows) {
147
+ if (document == null)
148
+ return null;
149
+ const tenantId = asString(document.tenant_id);
150
+ const scopeContextRaw = asString(document.scope_context);
151
+ const scopeField = asString(document.scope_field);
152
+ if (tenantId === '' || scopeContextRaw === '' || scopeField === '')
153
+ return null;
154
+ const scopeContext = scopeContextRaw;
155
+ const offApp = reportType === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES
156
+ ? Math.max(0, Number(document.off_app_order_count) || 0)
157
+ : 0;
158
+ return {
159
+ tenant_id: tenantId,
160
+ brand_id: asString(document.brand_id),
161
+ report_type: reportType,
162
+ scope_context: scopeContext,
163
+ scope_field: scopeField,
164
+ row_count: rows.length,
165
+ off_app_order_count: offApp,
166
+ schema_version: materialized_report_drilldown_rows_1.MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION,
167
+ computed_at: toDate(document.computed_at) ?? new Date(),
168
+ };
169
+ }
@@ -104,6 +104,8 @@ export interface IReportDetailsResponse<T, TCursor = unknown> {
104
104
  meta: IReportResponseMeta;
105
105
  rows: T[];
106
106
  next_cursor: TCursor | null;
107
+ /** Delivery only: orders with no delivery document (OFF_APP view). */
108
+ off_app_order_count?: number;
107
109
  }
108
110
  /**
109
111
  * Analytics response family. Live and historical of the SAME analytics
@@ -141,6 +143,7 @@ export interface IBuildReportDetailsResponseInput<T, TCursor = unknown> {
141
143
  meta: IReportResponseMeta | null | undefined;
142
144
  rows: T[];
143
145
  nextCursor?: TCursor | null;
146
+ offAppOrderCount?: number | null;
144
147
  }
145
148
  export declare function buildReportDetailsResponse<T, TCursor = unknown>(input: IBuildReportDetailsResponseInput<T, TCursor> | null | undefined): IReportDetailsResponse<T, TCursor> | null;
146
149
  export interface IBuildReportAnalyticsResponseInput<T> {
@@ -186,6 +186,7 @@ function buildReportDetailsResponse(input) {
186
186
  meta: input.meta,
187
187
  rows: input.rows,
188
188
  next_cursor: input.nextCursor ?? null,
189
+ ...(input.offAppOrderCount != null ? { off_app_order_count: input.offAppOrderCount } : {}),
189
190
  };
190
191
  }
191
192
  function buildReportAnalyticsResponse(input) {
@@ -37,6 +37,7 @@ export * from './materialized_tenant_cash_reports';
37
37
  export * from './materialized_tenant_prep_time_entries';
38
38
  export * from './materialized_tenant_prep_time_reports';
39
39
  export * from './materialized_tenant_reports_iterations';
40
+ export * from './materialized_report_drilldown_rows';
40
41
  export * from './materialized_tenant_sales_comparison_entries';
41
42
  export * from './materialized_tenant_sales_comparison_reports';
42
43
  export * from './materialized_tenant_hourly_comparison_entries';
@@ -53,6 +53,7 @@ __exportStar(require("./materialized_tenant_cash_reports"), exports);
53
53
  __exportStar(require("./materialized_tenant_prep_time_entries"), exports);
54
54
  __exportStar(require("./materialized_tenant_prep_time_reports"), exports);
55
55
  __exportStar(require("./materialized_tenant_reports_iterations"), exports);
56
+ __exportStar(require("./materialized_report_drilldown_rows"), exports);
56
57
  __exportStar(require("./materialized_tenant_sales_comparison_entries"), exports);
57
58
  __exportStar(require("./materialized_tenant_sales_comparison_reports"), exports);
58
59
  __exportStar(require("./materialized_tenant_hourly_comparison_entries"), exports);
@@ -0,0 +1,130 @@
1
+ import { Schema } from 'mongoose';
2
+ import { MATERIALIZED_REPORT_TYPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
3
+ import { IMaterializedReportDrilldownRow, IMaterializedReportDrilldownScope } from '../types/materialized_report_drilldown_rows';
4
+ /**
5
+ * FLAT drill-down rows and their scope metadata (see the type module).
6
+ *
7
+ * These are READ projections of the nested `*_entries` documents. The writer
8
+ * replaces all rows for one `(tenant, report_type, scope_context, scope_field)`
9
+ * scope after that scope's entry document is written; re-closing a day is
10
+ * therefore idempotent, and a stale scope can be deleted by identity.
11
+ */
12
+ export declare const MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION = 1;
13
+ export declare const MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME = "materialized_report_drilldown_rows";
14
+ export declare const MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME = "materialized_report_drilldown_scopes";
15
+ export declare const MaterializedReportDrilldownRowSchema: Schema<IMaterializedReportDrilldownRow, import("mongoose").Model<IMaterializedReportDrilldownRow, any, any, any, import("mongoose").Document<unknown, any, IMaterializedReportDrilldownRow> & IMaterializedReportDrilldownRow & Required<{
16
+ _id: string;
17
+ }> & {
18
+ __v: number;
19
+ }, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, IMaterializedReportDrilldownRow, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<IMaterializedReportDrilldownRow>> & import("mongoose").FlatRecord<IMaterializedReportDrilldownRow> & Required<{
20
+ _id: string;
21
+ }> & {
22
+ __v: number;
23
+ }>;
24
+ export declare const MaterializedReportDrilldownScopeSchema: Schema<IMaterializedReportDrilldownScope, import("mongoose").Model<IMaterializedReportDrilldownScope, any, any, any, import("mongoose").Document<unknown, any, IMaterializedReportDrilldownScope> & IMaterializedReportDrilldownScope & Required<{
25
+ _id: string;
26
+ }> & {
27
+ __v: number;
28
+ }, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, IMaterializedReportDrilldownScope, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<IMaterializedReportDrilldownScope>> & import("mongoose").FlatRecord<IMaterializedReportDrilldownScope> & Required<{
29
+ _id: string;
30
+ }> & {
31
+ __v: number;
32
+ }>;
33
+ /**
34
+ * Index keys consumed by the flat reader.
35
+ *
36
+ * `identity` is the write-side upsert/delete key. The two read indexes cover
37
+ * the two cursor orders the reports already expose:
38
+ * - ascending tie-break: discounts/prep/sales/hourly/payments;
39
+ * - descending tie-break: cash/delivery.
40
+ */
41
+ export declare const MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES: {
42
+ readonly _id: {
43
+ readonly _id: 1;
44
+ };
45
+ readonly identity: {
46
+ readonly tenant_id: 1;
47
+ readonly report_type: 1;
48
+ readonly scope_context: 1;
49
+ readonly scope_field: 1;
50
+ readonly order_id: 1;
51
+ readonly cursor_id: 1;
52
+ };
53
+ readonly read_asc: {
54
+ readonly tenant_id: 1;
55
+ readonly report_type: 1;
56
+ readonly scope_context: 1;
57
+ readonly scope_field: 1;
58
+ readonly time: -1;
59
+ readonly order_id: 1;
60
+ readonly cursor_id: 1;
61
+ };
62
+ readonly read_desc: {
63
+ readonly tenant_id: 1;
64
+ readonly report_type: 1;
65
+ readonly scope_context: 1;
66
+ readonly scope_field: 1;
67
+ readonly time: -1;
68
+ readonly cursor_id: -1;
69
+ };
70
+ };
71
+ export declare const MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS: ({
72
+ key: {
73
+ readonly tenant_id: 1;
74
+ readonly report_type: 1;
75
+ readonly scope_context: 1;
76
+ readonly scope_field: 1;
77
+ readonly order_id: 1;
78
+ readonly cursor_id: 1;
79
+ };
80
+ name: string;
81
+ unique: boolean;
82
+ } | {
83
+ key: {
84
+ readonly tenant_id: 1;
85
+ readonly report_type: 1;
86
+ readonly scope_context: 1;
87
+ readonly scope_field: 1;
88
+ readonly time: -1;
89
+ readonly order_id: 1;
90
+ readonly cursor_id: 1;
91
+ };
92
+ name: string;
93
+ unique?: undefined;
94
+ } | {
95
+ key: {
96
+ readonly tenant_id: 1;
97
+ readonly report_type: 1;
98
+ readonly scope_context: 1;
99
+ readonly scope_field: 1;
100
+ readonly time: -1;
101
+ readonly cursor_id: -1;
102
+ };
103
+ name: string;
104
+ unique?: undefined;
105
+ })[];
106
+ export declare const MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_INDEXES: {
107
+ readonly _id: {
108
+ readonly _id: 1;
109
+ };
110
+ readonly identity: {
111
+ readonly tenant_id: 1;
112
+ readonly report_type: 1;
113
+ readonly scope_context: 1;
114
+ readonly scope_field: 1;
115
+ };
116
+ };
117
+ export declare const MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS: {
118
+ key: {
119
+ readonly tenant_id: 1;
120
+ readonly report_type: 1;
121
+ readonly scope_context: 1;
122
+ readonly scope_field: 1;
123
+ };
124
+ name: string;
125
+ unique: boolean;
126
+ }[];
127
+ /** The report-type values the flat reader accepts. */
128
+ export declare const ALL_DRILLDOWN_REPORT_TYPE_VALUES: import("../types/materialized_report_drilldown_rows").TDrilldownReportType[];
129
+ /** Kept explicit so the JSON/demo upload can validate a file's report type. */
130
+ export declare const DRILLDOWN_REPORT_TYPE_BY_ENTRY_COLLECTION: Record<string, MATERIALIZED_REPORT_TYPE_ENUM>;
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DRILLDOWN_REPORT_TYPE_BY_ENTRY_COLLECTION = exports.ALL_DRILLDOWN_REPORT_TYPE_VALUES = exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS = exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_INDEXES = exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS = exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES = exports.MaterializedReportDrilldownScopeSchema = exports.MaterializedReportDrilldownRowSchema = exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME = exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME = exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_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
+ const materialized_report_drilldown_rows_1 = require("../types/materialized_report_drilldown_rows");
8
+ /**
9
+ * FLAT drill-down rows and their scope metadata (see the type module).
10
+ *
11
+ * These are READ projections of the nested `*_entries` documents. The writer
12
+ * replaces all rows for one `(tenant, report_type, scope_context, scope_field)`
13
+ * scope after that scope's entry document is written; re-closing a day is
14
+ * therefore idempotent, and a stale scope can be deleted by identity.
15
+ */
16
+ exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION = 1;
17
+ exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME = 'materialized_report_drilldown_rows';
18
+ exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME = 'materialized_report_drilldown_scopes';
19
+ const ReportTypeEnum = materialized_report_drilldown_rows_1.ALL_DRILLDOWN_REPORT_TYPES;
20
+ exports.MaterializedReportDrilldownRowSchema = new mongoose_1.Schema({
21
+ tenant_id: { type: String, required: true, immutable: true },
22
+ brand_id: { type: String, required: true },
23
+ report_type: {
24
+ type: String,
25
+ required: true,
26
+ immutable: true,
27
+ enum: ReportTypeEnum,
28
+ },
29
+ scope_context: {
30
+ type: String,
31
+ required: true,
32
+ immutable: true,
33
+ enum: MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_REPORT_SCOPES,
34
+ },
35
+ scope_field: { type: String, required: true, immutable: true },
36
+ day_index: { type: String, default: null, enum: [...ENUMS_1.ALL_DAY_INDEX, null] },
37
+ key: { type: String, required: true },
38
+ // Cash rows are drawer-ledger rows and have no order id; the field is
39
+ // still present (empty) because it is part of the identity/cursor tuple.
40
+ order_id: { type: String, default: '' },
41
+ cursor_id: { type: String, required: true },
42
+ time: { type: mongoose_1.Schema.Types.Date, required: true },
43
+ // The original row is polymorphic across reports; the report_type
44
+ // discriminator says which shape it is.
45
+ data: { type: mongoose_1.Schema.Types.Mixed, required: true },
46
+ schema_version: {
47
+ type: Number,
48
+ required: true,
49
+ immutable: true,
50
+ default: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION,
51
+ },
52
+ computed_at: { type: mongoose_1.Schema.Types.Date, required: true },
53
+ }, { timestamps: true, versionKey: false, minimize: false });
54
+ exports.MaterializedReportDrilldownScopeSchema = new mongoose_1.Schema({
55
+ tenant_id: { type: String, required: true, immutable: true },
56
+ brand_id: { type: String, required: true },
57
+ report_type: {
58
+ type: String,
59
+ required: true,
60
+ immutable: true,
61
+ enum: ReportTypeEnum,
62
+ },
63
+ scope_context: {
64
+ type: String,
65
+ required: true,
66
+ immutable: true,
67
+ enum: MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_REPORT_SCOPES,
68
+ },
69
+ scope_field: { type: String, required: true, immutable: true },
70
+ row_count: { type: Number, required: true, min: 0, default: 0 },
71
+ off_app_order_count: { type: Number, required: true, min: 0, default: 0 },
72
+ schema_version: {
73
+ type: Number,
74
+ required: true,
75
+ immutable: true,
76
+ default: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION,
77
+ },
78
+ computed_at: { type: mongoose_1.Schema.Types.Date, required: true },
79
+ }, { timestamps: true, versionKey: false, minimize: false });
80
+ /**
81
+ * Index keys consumed by the flat reader.
82
+ *
83
+ * `identity` is the write-side upsert/delete key. The two read indexes cover
84
+ * the two cursor orders the reports already expose:
85
+ * - ascending tie-break: discounts/prep/sales/hourly/payments;
86
+ * - descending tie-break: cash/delivery.
87
+ */
88
+ exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES = {
89
+ _id: { _id: 1 },
90
+ identity: {
91
+ tenant_id: 1,
92
+ report_type: 1,
93
+ scope_context: 1,
94
+ scope_field: 1,
95
+ order_id: 1,
96
+ cursor_id: 1,
97
+ },
98
+ read_asc: {
99
+ tenant_id: 1,
100
+ report_type: 1,
101
+ scope_context: 1,
102
+ scope_field: 1,
103
+ time: -1,
104
+ order_id: 1,
105
+ cursor_id: 1,
106
+ },
107
+ read_desc: {
108
+ tenant_id: 1,
109
+ report_type: 1,
110
+ scope_context: 1,
111
+ scope_field: 1,
112
+ time: -1,
113
+ cursor_id: -1,
114
+ },
115
+ };
116
+ exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS = [
117
+ {
118
+ key: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.identity,
119
+ name: 'tenant_id__report_type__scope__order_id__cursor_id',
120
+ unique: true,
121
+ },
122
+ {
123
+ key: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.read_asc,
124
+ name: 'tenant_id__report_type__scope__time_desc__order_id__cursor_id',
125
+ },
126
+ {
127
+ key: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.read_desc,
128
+ name: 'tenant_id__report_type__scope__time_desc__cursor_id_desc',
129
+ },
130
+ ];
131
+ exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_INDEXES = {
132
+ _id: { _id: 1 },
133
+ identity: {
134
+ tenant_id: 1,
135
+ report_type: 1,
136
+ scope_context: 1,
137
+ scope_field: 1,
138
+ },
139
+ };
140
+ exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS = [
141
+ {
142
+ key: exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_INDEXES.identity,
143
+ name: 'tenant_id__report_type__scope',
144
+ unique: true,
145
+ },
146
+ ];
147
+ /** The report-type values the flat reader accepts. */
148
+ exports.ALL_DRILLDOWN_REPORT_TYPE_VALUES = materialized_report_drilldown_rows_1.ALL_DRILLDOWN_REPORT_TYPES;
149
+ /** Kept explicit so the JSON/demo upload can validate a file's report type. */
150
+ exports.DRILLDOWN_REPORT_TYPE_BY_ENTRY_COLLECTION = {
151
+ materialized_tenant_discount_entries: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES,
152
+ materialized_tenant_prep_time_entries: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PREP_TIME_ENTRIES,
153
+ materialized_tenant_cash_in_out_entries: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES,
154
+ materialized_tenant_delivery_entries: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES,
155
+ materialized_tenant_sales_comparison_entries: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_ENTRIES,
156
+ materialized_tenant_hourly_comparison_entries: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES,
157
+ materialized_tenant_payments_comparison_entries: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PAYMENTS_COMPARISON_ENTRIES,
158
+ };
@@ -128,3 +128,4 @@ export * from './materialized_tenant_hourly_comparison_reports';
128
128
  export * from './materialized_tenant_hourly_comparison_entries';
129
129
  export * from './materialized_tenant_payments_comparison_reports';
130
130
  export * from './materialized_tenant_payments_comparison_entries';
131
+ export * from './materialized_report_drilldown_rows';
@@ -144,3 +144,4 @@ __exportStar(require("./materialized_tenant_hourly_comparison_reports"), exports
144
144
  __exportStar(require("./materialized_tenant_hourly_comparison_entries"), exports);
145
145
  __exportStar(require("./materialized_tenant_payments_comparison_reports"), exports);
146
146
  __exportStar(require("./materialized_tenant_payments_comparison_entries"), exports);
147
+ __exportStar(require("./materialized_report_drilldown_rows"), exports);
@@ -0,0 +1,64 @@
1
+ import { DAY_INDEX_ENUM } from '../../constants/ENUMS';
2
+ import { MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
3
+ /**
4
+ * Types for the FLAT drill-down row collections.
5
+ *
6
+ * The nested `*_entries` collections remain the write-side materialization
7
+ * output. These collections are the read-side projection: ONE document per
8
+ * itemized row, so a drill-down can be sorted and paged inside MongoDB instead
9
+ * of loading every entry document and every row into Node.
10
+ *
11
+ * `data` is the original row payload (Mixed) so the API response stays
12
+ * byte-for-byte compatible with the nested path. The top-level fields are the
13
+ * fields a page query needs: selection identity, sort anchor, and cursor
14
+ * tie-break.
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;
17
+ export declare const ALL_DRILLDOWN_REPORT_TYPES: TDrilldownReportType[];
18
+ /** One flattened itemized row. */
19
+ export interface IMaterializedReportDrilldownRow {
20
+ _id?: string;
21
+ tenant_id: string;
22
+ brand_id: string;
23
+ report_type: TDrilldownReportType;
24
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM;
25
+ scope_field: string;
26
+ day_index?: DAY_INDEX_ENUM | null;
27
+ /** The sale-type key the row was stored under (`ORDER_STATUS:PLATFORM`). */
28
+ key: string;
29
+ /** The order this row belongs to (empty for a ledger-only row, e.g. cash). */
30
+ order_id: string;
31
+ /**
32
+ * The cursor tie-break inside the report's existing `(time, order_id, id)`
33
+ * contract: entry_key / item_id / cash_in_out_id / delivery_id / order_id /
34
+ * payment_id.
35
+ */
36
+ cursor_id: string;
37
+ /** Sort anchor; always a Date so Mongo can order and range-scan it. */
38
+ time: Date;
39
+ /** The original row payload, unchanged. */
40
+ data: Record<string, unknown>;
41
+ schema_version: number;
42
+ computed_at: Date;
43
+ createdAt?: Date;
44
+ updatedAt?: Date;
45
+ }
46
+ /**
47
+ * Scope-level metadata for a flat drill-down. Delivery is the only report that
48
+ * needs a value that is not a row (`off_app_order_count`), but `row_count` is
49
+ * useful for backfill/verification for every report.
50
+ */
51
+ export interface IMaterializedReportDrilldownScope {
52
+ _id?: string;
53
+ tenant_id: string;
54
+ brand_id: string;
55
+ report_type: TDrilldownReportType;
56
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM;
57
+ scope_field: string;
58
+ row_count: number;
59
+ off_app_order_count: number;
60
+ schema_version: number;
61
+ computed_at: Date;
62
+ createdAt?: Date;
63
+ updatedAt?: Date;
64
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ALL_DRILLDOWN_REPORT_TYPES = void 0;
4
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
5
+ exports.ALL_DRILLDOWN_REPORT_TYPES = [
6
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES,
7
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PREP_TIME_ENTRIES,
8
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES,
9
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES,
10
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_ENTRIES,
11
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES,
12
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PAYMENTS_COMPARISON_ENTRIES,
13
+ ];
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
+ const materialized_report_drilldown_rows_1 = require("../../helpers/materialized-reports/materialized-report-drilldown-rows");
5
+ const base = {
6
+ tenant_id: 'tenant-a',
7
+ brand_id: 'brand-a',
8
+ scope_context: 'localDate',
9
+ scope_field: '2026-09-14',
10
+ day_index: 'MON',
11
+ computed_at: new Date('2026-09-14T23:00:00.000Z'),
12
+ };
13
+ const row = (over) => ({
14
+ order_id: 'order-1',
15
+ created_at: new Date('2026-09-14T10:00:00.000Z'),
16
+ ...over,
17
+ });
18
+ describe('flat drill-down row projection', () => {
19
+ test('projects discounts with entry_key as the cursor tie-break', () => {
20
+ const result = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES, {
21
+ ...base,
22
+ discount_entries: {
23
+ 'COMPLETED:POS': { rows: [row({ entry_key: 'discount:1', kind: 'DISCOUNT' })] },
24
+ },
25
+ });
26
+ expect(result.rows).toHaveLength(1);
27
+ expect(result.rows[0]).toMatchObject({
28
+ tenant_id: 'tenant-a',
29
+ report_type: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES,
30
+ key: 'COMPLETED:POS',
31
+ order_id: 'order-1',
32
+ cursor_id: 'discount:1',
33
+ });
34
+ expect(result.rows[0].time).toEqual(new Date('2026-09-14T10:00:00.000Z'));
35
+ expect(result.rows[0].data).toMatchObject({ kind: 'DISCOUNT' });
36
+ });
37
+ test('projects prep rows with item_id as the cursor tie-break', () => {
38
+ const result = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PREP_TIME_ENTRIES, {
39
+ ...base,
40
+ prep_time_entries: {
41
+ K: { rows: [row({ item_id: 'item-1', entry_key: 'prep:1' })] },
42
+ },
43
+ });
44
+ expect(result.rows[0].cursor_id).toBe('item-1');
45
+ });
46
+ test('projects cash rows as drawer-ledger rows (no order id)', () => {
47
+ const result = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES, {
48
+ ...base,
49
+ cash_entries: {
50
+ K: { rows: [row({ cash_in_out_id: 'cash-1', entry_key: 'cash:1' })] },
51
+ },
52
+ });
53
+ expect(result.rows[0]).toMatchObject({ order_id: '', cursor_id: 'cash-1' });
54
+ });
55
+ test('projects delivery rows and carries the off-app scope count', () => {
56
+ const document = {
57
+ ...base,
58
+ off_app_order_count: 4,
59
+ delivery_entries: {
60
+ K: {
61
+ rows: [
62
+ row({
63
+ delivery_id: 'delivery-1',
64
+ entry_key: 'delivery:1',
65
+ delivered_at: new Date('2026-09-14T10:30:00.000Z'),
66
+ }),
67
+ ],
68
+ },
69
+ },
70
+ };
71
+ const result = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES, document);
72
+ expect(result.rows[0]).toMatchObject({
73
+ cursor_id: 'delivery-1',
74
+ order_id: 'order-1',
75
+ });
76
+ expect(result.rows[0].time).toEqual(new Date('2026-09-14T10:30:00.000Z'));
77
+ expect((0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownScope)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES, document, result.rows)).toMatchObject({ row_count: 1, off_app_order_count: 4 });
78
+ });
79
+ test('projects sales/hourly/payments comparison rows from ISO time', () => {
80
+ const sales = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_COMPARISON_ENTRIES, {
81
+ ...base,
82
+ sales_entries: {
83
+ K: {
84
+ rows: [
85
+ {
86
+ order_id: 'order-1',
87
+ time: '2026-09-14T10:00:00.000Z',
88
+ platform: 'POS',
89
+ order_status: 'COMPLETED',
90
+ },
91
+ ],
92
+ },
93
+ },
94
+ });
95
+ expect(sales.rows[0]).toMatchObject({
96
+ order_id: 'order-1',
97
+ cursor_id: 'order-1',
98
+ });
99
+ const hourly = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES, {
100
+ ...base,
101
+ scope_context: 'localHour',
102
+ scope_field: '2026-09-14 10',
103
+ sales_entries: {
104
+ K: { rows: [{ order_id: 'order-2', time: '2026-09-14T10:00:00.000Z' }] },
105
+ },
106
+ });
107
+ expect(hourly.rows[0].scope_context).toBe('localHour');
108
+ const payments = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PAYMENTS_COMPARISON_ENTRIES, {
109
+ ...base,
110
+ payment_entries: {
111
+ K: {
112
+ rows: [
113
+ {
114
+ order_id: 'order-3',
115
+ payment_id: 'payment-3',
116
+ time: '2026-09-14T10:00:00.000Z',
117
+ },
118
+ ],
119
+ },
120
+ },
121
+ });
122
+ expect(payments.rows[0]).toMatchObject({
123
+ order_id: 'order-3',
124
+ cursor_id: 'payment-3',
125
+ });
126
+ });
127
+ test('skips a row without a usable time or identity', () => {
128
+ const result = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES, {
129
+ ...base,
130
+ discount_entries: {
131
+ K: {
132
+ rows: [
133
+ row({ entry_key: 'ok', kind: 'DISCOUNT' }),
134
+ row({ entry_key: 'no-time', created_at: null }),
135
+ row({ entry_key: '' }),
136
+ ],
137
+ },
138
+ },
139
+ });
140
+ expect(result.rows).toHaveLength(1);
141
+ expect(result.skipped).toBe(2);
142
+ });
143
+ });
@@ -129,6 +129,13 @@ describe('report response builders', () => {
129
129
  });
130
130
  expect(details.rows).toHaveLength(1);
131
131
  expect(details.next_cursor).not.toBeNull();
132
+ const deliveryDetails = (0, reporting_contract_1.buildReportDetailsResponse)({
133
+ report: reporting_contract_1.REPORT_KEY_ENUM.DELIVERY,
134
+ meta: historicalMeta(),
135
+ rows: [],
136
+ offAppOrderCount: 7,
137
+ });
138
+ expect(deliveryDetails.off_app_order_count).toBe(7);
132
139
  const analytics = (0, reporting_contract_1.buildReportAnalyticsResponse)({
133
140
  analytics: 'OVERVIEW',
134
141
  meta: liveMeta(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.15",
3
+ "version": "0.4.16",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",