@managesome/eposmatic-toolkit 0.3.10 → 0.4.0

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 (145) hide show
  1. package/esm/constants/ENUMS.js +17 -1988
  2. package/esm/constants/MATERIALIZED_REPORTS.js +105 -0
  3. package/esm/constants/enums/cash.js +45 -0
  4. package/esm/constants/enums/communications.js +329 -0
  5. package/esm/constants/enums/delivery.js +35 -0
  6. package/esm/constants/enums/geo.js +150 -0
  7. package/esm/constants/enums/hr.js +130 -0
  8. package/esm/constants/enums/internal.js +132 -0
  9. package/esm/constants/enums/items.js +126 -0
  10. package/esm/constants/enums/misc.js +35 -0
  11. package/esm/constants/enums/offers.js +102 -0
  12. package/esm/constants/enums/order.js +135 -0
  13. package/esm/constants/enums/payments.js +186 -0
  14. package/esm/constants/enums/platform.js +215 -0
  15. package/esm/constants/enums/printing.js +107 -0
  16. package/esm/constants/enums/users.js +245 -0
  17. package/esm/constants/enums/wallet.js +32 -0
  18. package/esm/helpers/materialized-reports/index.js +5 -0
  19. package/esm/helpers/materialized-reports/key-helpers.js +59 -0
  20. package/esm/helpers/materialized-reports/materialized-tenant-reports-iterations-helpers.js +60 -0
  21. package/esm/helpers/materialized-reports/scope-helpers.js +107 -0
  22. package/esm/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +251 -0
  23. package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +860 -0
  24. package/esm/helpers/store/cart-actions.js +1 -1
  25. package/esm/helpers/store/common-tenant-purchase-order-actions.js +7 -7
  26. package/esm/helpers/store/common-warehouse-purchase-order-actions.js +1 -1
  27. package/esm/helpers/store/common-warehouse-purchase-order-rfq-actions.js +1 -1
  28. package/esm/helpers/store/common-warehouse-sale-order-actions.js +1 -1
  29. package/esm/helpers/store/common-warehouse-sale-order-rfq-actions.js +1 -1
  30. package/esm/helpers/store/offline-ordering.js +1 -1
  31. package/esm/helpers/store/tenant-purchase-orders-cart-actions.js +1 -1
  32. package/esm/helpers/store/warehouse-purchase-order-rfq-cart-actions.js +1 -1
  33. package/esm/helpers/store/warehouse-purchase-orders-cart-actions.js +1 -1
  34. package/esm/helpers/store/warehouse-sale-order-rfq-cart-actions.js +1 -1
  35. package/esm/helpers/store/warehouse-sale-orders-cart-actions.js +1 -1
  36. package/esm/helpers/utils/cancellation-helpers.js +7 -0
  37. package/esm/helpers/utils/common.js +0 -46
  38. package/esm/helpers/utils/getFormFieldsDiff.js +1 -1
  39. package/esm/helpers/utils/index.js +2 -0
  40. package/esm/helpers/utils/phone-helpers.js +40 -0
  41. package/esm/helpers/utils/receipt-engine.js +3 -1
  42. package/esm/helpers/utils/specials-helpers.js +1 -1
  43. package/esm/helpers/utils/void-items-report-helpers.js +2 -1
  44. package/esm/schemas/models/materialized_tenant_reports_iterations.js +82 -0
  45. package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +399 -0
  46. package/esm/schemas/types/materialized_tenant_reports_iterations.js +1 -0
  47. package/esm/schemas/types/materialized_tenant_sales_overview_reports.js +1 -0
  48. package/lib/constants/ENUMS.d.ts +15 -1275
  49. package/lib/constants/ENUMS.js +31 -1994
  50. package/lib/constants/MATERIALIZED_REPORTS.d.ts +96 -0
  51. package/lib/constants/MATERIALIZED_REPORTS.js +108 -0
  52. package/lib/constants/enums/cash.d.ts +35 -0
  53. package/lib/constants/enums/cash.js +48 -0
  54. package/lib/constants/enums/communications.d.ts +79 -0
  55. package/lib/constants/enums/communications.js +332 -0
  56. package/lib/constants/enums/delivery.d.ts +26 -0
  57. package/lib/constants/enums/delivery.js +38 -0
  58. package/lib/constants/enums/geo.d.ts +133 -0
  59. package/lib/constants/enums/geo.js +153 -0
  60. package/lib/constants/enums/hr.d.ts +105 -0
  61. package/lib/constants/enums/hr.js +133 -0
  62. package/lib/constants/enums/internal.d.ts +104 -0
  63. package/lib/constants/enums/internal.js +135 -0
  64. package/lib/constants/enums/items.d.ts +94 -0
  65. package/lib/constants/enums/items.js +129 -0
  66. package/lib/constants/enums/misc.d.ts +25 -0
  67. package/lib/constants/enums/misc.js +38 -0
  68. package/lib/constants/enums/offers.d.ts +80 -0
  69. package/lib/constants/enums/offers.js +105 -0
  70. package/lib/constants/enums/order.d.ts +108 -0
  71. package/lib/constants/enums/order.js +138 -0
  72. package/lib/constants/enums/payments.d.ts +129 -0
  73. package/lib/constants/enums/payments.js +189 -0
  74. package/lib/constants/enums/platform.d.ts +100 -0
  75. package/lib/constants/enums/platform.js +218 -0
  76. package/lib/constants/enums/printing.d.ts +89 -0
  77. package/lib/constants/enums/printing.js +110 -0
  78. package/lib/constants/enums/users.d.ts +142 -0
  79. package/lib/constants/enums/users.js +248 -0
  80. package/lib/constants/enums/wallet.d.ts +26 -0
  81. package/lib/constants/enums/wallet.js +35 -0
  82. package/lib/helpers/materialized-reports/index.d.ts +5 -0
  83. package/lib/helpers/materialized-reports/index.js +21 -0
  84. package/lib/helpers/materialized-reports/key-helpers.d.ts +35 -0
  85. package/lib/helpers/materialized-reports/key-helpers.js +64 -0
  86. package/lib/helpers/materialized-reports/materialized-tenant-reports-iterations-helpers.d.ts +66 -0
  87. package/lib/helpers/materialized-reports/materialized-tenant-reports-iterations-helpers.js +63 -0
  88. package/lib/helpers/materialized-reports/scope-helpers.d.ts +57 -0
  89. package/lib/helpers/materialized-reports/scope-helpers.js +114 -0
  90. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.d.ts +125 -0
  91. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +256 -0
  92. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.d.ts +223 -0
  93. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +875 -0
  94. package/lib/helpers/store/cart-actions.js +6 -6
  95. package/lib/helpers/store/common-tenant-purchase-order-actions.d.ts +2 -2
  96. package/lib/helpers/store/common-tenant-purchase-order-actions.js +10 -10
  97. package/lib/helpers/store/common-warehouse-purchase-order-actions.js +4 -4
  98. package/lib/helpers/store/common-warehouse-purchase-order-rfq-actions.js +4 -4
  99. package/lib/helpers/store/common-warehouse-sale-order-actions.js +4 -4
  100. package/lib/helpers/store/common-warehouse-sale-order-rfq-actions.js +4 -4
  101. package/lib/helpers/store/offline-ordering.js +2 -2
  102. package/lib/helpers/store/tenant-purchase-orders-cart-actions.js +2 -2
  103. package/lib/helpers/store/warehouse-purchase-order-rfq-cart-actions.js +2 -2
  104. package/lib/helpers/store/warehouse-purchase-orders-cart-actions.js +2 -2
  105. package/lib/helpers/store/warehouse-sale-order-rfq-cart-actions.js +2 -2
  106. package/lib/helpers/store/warehouse-sale-orders-cart-actions.js +2 -2
  107. package/lib/helpers/utils/cancellation-helpers.d.ts +1 -0
  108. package/lib/helpers/utils/cancellation-helpers.js +13 -0
  109. package/lib/helpers/utils/common.d.ts +0 -6
  110. package/lib/helpers/utils/common.js +1 -54
  111. package/lib/helpers/utils/getFormFieldsDiff.js +5 -2
  112. package/lib/helpers/utils/index.d.ts +2 -0
  113. package/lib/helpers/utils/index.js +2 -0
  114. package/lib/helpers/utils/phone-helpers.d.ts +5 -0
  115. package/lib/helpers/utils/phone-helpers.js +46 -0
  116. package/lib/helpers/utils/receipt-engine.js +4 -2
  117. package/lib/helpers/utils/specials-helpers.js +2 -2
  118. package/lib/helpers/utils/void-items-report-helpers.js +3 -2
  119. package/lib/schemas/models/materialized_tenant_reports_iterations.d.ts +80 -0
  120. package/lib/schemas/models/materialized_tenant_reports_iterations.js +85 -0
  121. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +569 -0
  122. package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +402 -0
  123. package/lib/schemas/types/materialized_tenant_reports_iterations.d.ts +62 -0
  124. package/lib/schemas/types/materialized_tenant_reports_iterations.js +2 -0
  125. package/lib/schemas/types/materialized_tenant_sales_overview_reports.d.ts +242 -0
  126. package/lib/schemas/types/materialized_tenant_sales_overview_reports.js +3 -0
  127. package/lib/tests/materialized-reports/fixtures.d.ts +81 -0
  128. package/lib/tests/materialized-reports/fixtures.js +87 -0
  129. package/lib/tests/materialized-reports/fixtures.test.d.ts +1 -0
  130. package/lib/tests/materialized-reports/fixtures.test.js +123 -0
  131. package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-helpers.test.d.ts +1 -0
  132. package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-helpers.test.js +75 -0
  133. package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-schema.test.d.ts +1 -0
  134. package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-schema.test.js +73 -0
  135. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.d.ts +1 -0
  136. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +207 -0
  137. package/lib/tests/materialized-reports/sales-overview-baseline.test.d.ts +1 -0
  138. package/lib/tests/materialized-reports/sales-overview-baseline.test.js +326 -0
  139. package/lib/tests/materialized-reports/sales-overview-reference.d.ts +63 -0
  140. package/lib/tests/materialized-reports/sales-overview-reference.js +131 -0
  141. package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.d.ts +1 -0
  142. package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.js +335 -0
  143. package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.d.ts +1 -0
  144. package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +885 -0
  145. package/package.json +5 -2
@@ -0,0 +1,73 @@
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 MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
8
+ const materialized_tenant_reports_iterations_1 = require("../../schemas/models/materialized_tenant_reports_iterations");
9
+ const Model = mongoose_1.default.models[materialized_tenant_reports_iterations_1.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME] ??
10
+ mongoose_1.default.model(materialized_tenant_reports_iterations_1.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME, materialized_tenant_reports_iterations_1.MaterializedTenantReportIterationSchema, materialized_tenant_reports_iterations_1.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME);
11
+ const validRow = () => ({
12
+ _id: new mongoose_1.default.Types.ObjectId().toString(),
13
+ report_type: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_OVERVIEW,
14
+ report_id: new mongoose_1.default.Types.ObjectId().toString(),
15
+ iteration: 2,
16
+ schema_version: 1,
17
+ reason: 'RECLOSE',
18
+ snapshot: {
19
+ _id: new mongoose_1.default.Types.ObjectId().toString(),
20
+ tenant_id: 'z2-isb',
21
+ scope_context: 'localDate',
22
+ scope_field: '2026-09-30',
23
+ sale_type_metrics: { 'COMPLETED:POS_WALK-IN': { marker: true } },
24
+ },
25
+ createdAt: new Date('2026-09-30T11:00:00.000Z'),
26
+ updatedAt: new Date('2026-09-30T11:00:00.000Z'),
27
+ });
28
+ describe('materialized_tenant_reports_iterations schema', () => {
29
+ it('exposes the agreed collection name', () => {
30
+ expect(materialized_tenant_reports_iterations_1.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME).toBe('materialized_tenant_reports_iterations');
31
+ });
32
+ it('validates a well-formed iteration row', () => {
33
+ const doc = new Model(validRow());
34
+ expect(doc.validateSync()).toBeUndefined();
35
+ expect(doc.get('report_type')).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_OVERVIEW);
36
+ expect(doc.get('iteration')).toBe(2);
37
+ expect(doc.get('schema_version')).toBe(1);
38
+ expect(doc.get('reason')).toBe('RECLOSE');
39
+ });
40
+ it('stores the full report snapshot untouched (Mixed — no nested casting)', () => {
41
+ const row = validRow();
42
+ const doc = new Model(row);
43
+ const snapshot = doc.get('snapshot');
44
+ expect(snapshot).toMatchObject(row.snapshot);
45
+ expect(snapshot['sale_type_metrics']).toEqual({ 'COMPLETED:POS_WALK-IN': { marker: true } });
46
+ });
47
+ it('rejects an unknown report_type', () => {
48
+ const error = new Model({ ...validRow(), report_type: 'NOPE' }).validateSync();
49
+ expect(error?.errors).toHaveProperty('report_type');
50
+ });
51
+ it('rejects missing required fields (report_id, snapshot, reason)', () => {
52
+ const missingReportId = { ...validRow() };
53
+ delete missingReportId.report_id;
54
+ expect(new Model(missingReportId).validateSync()?.errors).toHaveProperty('report_id');
55
+ const missingSnapshot = { ...validRow() };
56
+ delete missingSnapshot.snapshot;
57
+ expect(new Model(missingSnapshot).validateSync()?.errors).toHaveProperty('snapshot');
58
+ const missingReason = { ...validRow() };
59
+ delete missingReason.reason;
60
+ expect(new Model(missingReason).validateSync()?.errors).toHaveProperty('reason');
61
+ });
62
+ it('enforces iteration >= 1', () => {
63
+ const error = new Model({ ...validRow(), iteration: 0 }).validateSync();
64
+ expect(error?.errors).toHaveProperty('iteration');
65
+ });
66
+ it('declares the unique (report_id, iteration) index and covers every report type', () => {
67
+ expect(materialized_tenant_reports_iterations_1.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_INDEXES.report_id__iteration).toEqual({
68
+ report_id: 1,
69
+ iteration: -1,
70
+ });
71
+ expect(MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_REPORT_TYPES).toContain(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_OVERVIEW);
72
+ });
73
+ });
@@ -0,0 +1,207 @@
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 ENUMS_1 = require("../../constants/ENUMS");
9
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
10
+ const materialized_tenant_sales_overview_reports_1 = require("../../schemas/models/materialized_tenant_sales_overview_reports");
11
+ const Model = mongoose_1.default.models[materialized_tenant_sales_overview_reports_1.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME] ??
12
+ mongoose_1.default.model(materialized_tenant_sales_overview_reports_1.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME, materialized_tenant_sales_overview_reports_1.MaterializedTenantSalesOverviewReportSchema, materialized_tenant_sales_overview_reports_1.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME);
13
+ const paymentMetric = (payment_type) => ({
14
+ payment_type,
15
+ count: 1,
16
+ gross_amount: 30000,
17
+ amount_due: 30000,
18
+ amount_paid: 30000,
19
+ amount_refunded: 0,
20
+ card_charges: 0,
21
+ });
22
+ const orderMetric = () => ({
23
+ count: 3,
24
+ gross_amount: 120000,
25
+ discount_amount: 12000,
26
+ discount_by_type: { special_discount: 8000, loyalty_discount: 2500, voucher_discount: 1500 },
27
+ gst_amount: 6000,
28
+ tax_amount: 0,
29
+ tip_amount: 1000,
30
+ tax_invoice_provider_charges: 100,
31
+ delivery_charges: 2500,
32
+ });
33
+ const saleTypeMetric = () => ({
34
+ orders: orderMetric(),
35
+ payments: [paymentMetric('CASH')],
36
+ });
37
+ const creditBill = () => ({
38
+ order_id: new mongoose_1.default.Types.ObjectId().toString(),
39
+ payment_id: new mongoose_1.default.Types.ObjectId().toString(),
40
+ amount: 1200,
41
+ received_amount: 600, // partially collected
42
+ issued_at: new Date('2026-09-29T22:10:00.000Z'),
43
+ settled_at: null,
44
+ });
45
+ const receivedCreditBill = () => ({
46
+ order_id: new mongoose_1.default.Types.ObjectId().toString(),
47
+ payment_id: new mongoose_1.default.Types.ObjectId().toString(),
48
+ amount: 1200,
49
+ received_amount: 1200,
50
+ credit_note: 'CN-2026-09-30',
51
+ issued_local_date: '2026-09-29',
52
+ });
53
+ const validDocument = () => ({
54
+ _id: new mongoose_1.default.Types.ObjectId().toString(),
55
+ tenant_id: 'z2-isb',
56
+ brand_id: 'brand-1',
57
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
58
+ scope_field: '2026-09-30',
59
+ day_index: ENUMS_1.DAY_INDEX_ENUM.WED,
60
+ sale_type_key_filters: ['ORDER_STATUS', 'PLATFORM'],
61
+ sale_type_metrics: {
62
+ 'COMPLETED:POS_TAKE-AWAY': {
63
+ [constants_1.REPORT_SALE_TYPE_ENUM.TOTAL_SALES]: saleTypeMetric(),
64
+ [constants_1.REPORT_SALE_TYPE_ENUM.AMOUNT_COLLECTED]: saleTypeMetric(),
65
+ [constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS]: {
66
+ issued: { count: 1, amount: 1200, settled_count: 0, settled_amount: 600, bills: [creditBill()] },
67
+ received: { count: 1, amount: 1200, bills: [receivedCreditBill()] },
68
+ },
69
+ },
70
+ },
71
+ schema_version: 1,
72
+ source_watermark: new Date('2026-09-30T10:45:30.592Z'),
73
+ computed_at: new Date('2026-09-30T11:00:00.000Z'),
74
+ });
75
+ describe('materialized_tenant_sales_overview_reports schema', () => {
76
+ it('exposes the agreed collection name', () => {
77
+ expect(materialized_tenant_sales_overview_reports_1.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME).toBe('materialized_tenant_sales_overview_reports');
78
+ });
79
+ it('validates a well-formed scope document and casts metrics to a Map', () => {
80
+ const doc = new Model(validDocument());
81
+ expect(doc.validateSync()).toBeUndefined();
82
+ expect(doc.get('scope_context')).toBe('localDate');
83
+ expect(doc.get('scope_field')).toBe('2026-09-30');
84
+ expect(doc.get('iteration')).toBe(1);
85
+ const metrics = doc.get('sale_type_metrics');
86
+ expect(metrics).toBeInstanceOf(Map);
87
+ const entry = metrics.get('COMPLETED:POS_TAKE-AWAY');
88
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.TOTAL_SALES].orders).toMatchObject({
89
+ count: 3,
90
+ gross_amount: 120000,
91
+ });
92
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.TOTAL_SALES].payments).toHaveLength(1);
93
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS]).toBeDefined();
94
+ });
95
+ it('carries the order money breakdown (discounts by source, tax, tip, delivery, provider charges)', () => {
96
+ const doc = new Model(validDocument());
97
+ const metrics = doc.get('sale_type_metrics');
98
+ const orders = metrics.get('COMPLETED:POS_TAKE-AWAY')?.[constants_1.REPORT_SALE_TYPE_ENUM.TOTAL_SALES].orders;
99
+ expect(orders).toMatchObject({
100
+ discount_amount: 12000,
101
+ discount_by_type: { special_discount: 8000, loyalty_discount: 2500, voucher_discount: 1500 },
102
+ gst_amount: 6000,
103
+ tax_amount: 0,
104
+ tip_amount: 1000,
105
+ tax_invoice_provider_charges: 100,
106
+ delivery_charges: 2500,
107
+ });
108
+ });
109
+ it('rejects an order metric missing the money breakdown fields', () => {
110
+ const document = validDocument();
111
+ const orders = document.sale_type_metrics['COMPLETED:POS_TAKE-AWAY'][constants_1.REPORT_SALE_TYPE_ENUM.TOTAL_SALES]
112
+ .orders;
113
+ delete orders.tip_amount;
114
+ const error = new Model(document).validateSync();
115
+ expect(Object.keys(error?.errors ?? {}).some((path) => path.includes('tip_amount'))).toBe(true);
116
+ });
117
+ it('accepts open payment_type strings (legacy + aggregator values)', () => {
118
+ const document = validDocument();
119
+ document.sale_type_metrics['COMPLETED:POS_TAKE-AWAY'][constants_1.REPORT_SALE_TYPE_ENUM.TOTAL_SALES].payments = [
120
+ paymentMetric('CARD_PAYMENT'),
121
+ paymentMetric('FOODPANDA'),
122
+ paymentMetric('GOLOOTLO'),
123
+ ];
124
+ expect(new Model(document).validateSync()).toBeUndefined();
125
+ });
126
+ it('validates the per-key credit-bill ledger and applies zero defaults when omitted', () => {
127
+ const doc = new Model(validDocument());
128
+ expect(doc.validateSync()).toBeUndefined();
129
+ const metrics = doc.get('sale_type_metrics');
130
+ const entry = metrics.get('COMPLETED:POS_TAKE-AWAY');
131
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.count).toBe(1);
132
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.bills[0].received_amount).toBe(600);
133
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.bills[0].settled_at).toBeNull();
134
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].received.bills[0].received_amount).toBe(1200);
135
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].received.bills[0].credit_note).toBe('CN-2026-09-30');
136
+ const minimal = { ...validDocument() };
137
+ const minimalMetrics = minimal.sale_type_metrics;
138
+ delete minimalMetrics['COMPLETED:POS_TAKE-AWAY'][constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS];
139
+ const docWithDefaults = new Model(minimal);
140
+ expect(docWithDefaults.validateSync()).toBeUndefined();
141
+ const defaultMetrics = docWithDefaults.get('sale_type_metrics');
142
+ const defaultEntry = defaultMetrics.get('COMPLETED:POS_TAKE-AWAY');
143
+ expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.count).toBe(0);
144
+ expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.amount).toBe(0);
145
+ expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.settled_count).toBe(0);
146
+ expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.settled_amount).toBe(0);
147
+ expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.bills).toEqual([]);
148
+ expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].received.count).toBe(0);
149
+ });
150
+ it('accepts an empty metrics map (day with no orders) and applies key-filter defaults', () => {
151
+ const document = { ...validDocument(), sale_type_metrics: {} };
152
+ const doc = new Model(document);
153
+ expect(doc.validateSync()).toBeUndefined();
154
+ expect(doc.get('sale_type_metrics').size).toBe(0);
155
+ const minimal = { ...validDocument() };
156
+ delete minimal.sale_type_key_filters;
157
+ const docWithDefaults = new Model(minimal);
158
+ expect(docWithDefaults.get('sale_type_key_filters')).toEqual(['ORDER_STATUS', 'PLATFORM']);
159
+ });
160
+ it('rejects missing required envelope + scope fields', () => {
161
+ const error = new Model({}).validateSync();
162
+ for (const path of ['tenant_id', 'brand_id', 'scope_context', 'scope_field', 'source_watermark', 'computed_at']) {
163
+ expect(error?.errors).toHaveProperty(path);
164
+ }
165
+ });
166
+ it('rejects an unsupported scope_context', () => {
167
+ const badScope = new Model({ ...validDocument(), scope_context: 'MIDNIGHT' }).validateSync();
168
+ expect(badScope?.errors).toHaveProperty('scope_context');
169
+ });
170
+ it('treats day_index as a document field, not a scope_context', () => {
171
+ expect(MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_REPORT_SCOPES).not.toContain('day_index');
172
+ expect(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPES_WITH_DAY_INDEX).toEqual([
173
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
174
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
175
+ ]);
176
+ const badScope = new Model({ ...validDocument(), scope_context: 'day_index' }).validateSync();
177
+ expect(badScope?.errors).toHaveProperty('scope_context');
178
+ });
179
+ it('carries the denormalized day_index on localHour / localDate docs, null elsewhere', () => {
180
+ const dateDoc = new Model(validDocument());
181
+ expect(dateDoc.validateSync()).toBeUndefined();
182
+ expect(dateDoc.get('day_index')).toBe(ENUMS_1.DAY_INDEX_ENUM.WED);
183
+ const hourDoc = new Model({
184
+ ...validDocument(),
185
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
186
+ scope_field: '2026-09-30 23',
187
+ });
188
+ expect(hourDoc.validateSync()).toBeUndefined();
189
+ expect(hourDoc.get('day_index')).toBe(ENUMS_1.DAY_INDEX_ENUM.WED);
190
+ const minimal = { ...validDocument() };
191
+ delete minimal.day_index;
192
+ const docWithDefaults = new Model(minimal);
193
+ expect(docWithDefaults.validateSync()).toBeUndefined();
194
+ expect(docWithDefaults.get('day_index')).toBeNull();
195
+ });
196
+ it('rejects an invalid day_index value', () => {
197
+ const badDayIndex = new Model({ ...validDocument(), day_index: 'FUNDAY' }).validateSync();
198
+ expect(badDayIndex?.errors).toHaveProperty('day_index');
199
+ });
200
+ it('rejects unsupported key-filter dimensions', () => {
201
+ const badFilters = new Model({
202
+ ...validDocument(),
203
+ sale_type_key_filters: ['ORDER_STATUS', 'CITY'],
204
+ }).validateSync();
205
+ expect(Object.keys(badFilters?.errors ?? {})).toContain('sale_type_key_filters.1');
206
+ });
207
+ });
@@ -0,0 +1,326 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("../../constants");
4
+ const fixtures_1 = require("./fixtures");
5
+ const sales_overview_reference_1 = require("./sales-overview-reference");
6
+ /**
7
+ * Pinned baselines for the Reports > Overview report, computed from one month
8
+ * of live orders per store. These values are the acceptance target for the
9
+ * future `materialized_tenant_sales_overview_reports` collection: a materialized
10
+ * cube built from the same orders MUST reproduce them for the same filters.
11
+ *
12
+ * Pinning notes:
13
+ * - amounts are cents; live data sums as doubles, so gross values are rounded
14
+ * to 2 decimals before comparison (`round2`);
15
+ * - headline totals/additional facets were cross-checked against an independent
16
+ * naive reduction over the raw JSON during test development;
17
+ * - credit bills are live data now: both stores carry 8 issued bills
18
+ * (`PAY_LATER`/PENDING + `deferred_settlement`) and 6 settled-later bills
19
+ * (fully paid, `order_settled_at` moved to a later day), so the
20
+ * `CREDIT_BILLS` facets are pinned from the enriched fixtures;
21
+ * - fixtures carry enriched `ILocalTimeFields` (GMT+5, 6am–6am business day) —
22
+ * see `scripts/enrich-order-fixtures.ts`.
23
+ */
24
+ const round2 = (value) => Math.round(value * 100) / 100;
25
+ const toSummaryMap = (summaries) => summaries.reduce((acc, summary) => {
26
+ acc[summary._id] = { count: summary.count, grossAmount: round2(summary.grossAmount) };
27
+ return acc;
28
+ }, {});
29
+ const MONTH_WINDOW = {
30
+ from: new Date('2026-09-01T00:00:00.000Z'),
31
+ to: new Date('2026-09-30T23:59:59.999Z'),
32
+ };
33
+ const dayWindow = (day) => ({
34
+ from: new Date(Date.UTC(2026, 8, day, 0, 0, 0, 0)),
35
+ to: new Date(Date.UTC(2026, 8, day, 23, 59, 59, 999)),
36
+ });
37
+ const BASHA = 'basha-istanbul-f7';
38
+ const BRUZO = 'z2-isb';
39
+ describe('sales-overview baseline — basha (2026-09)', () => {
40
+ const orders = (0, fixtures_1.loadOrdersDataset)('basha');
41
+ const monthFilters = { ...MONTH_WINDOW, tenants: [BASHA], groupId: BASHA };
42
+ it('matches the pinned month totals (set total, cancelled, dashboard derivations)', () => {
43
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, monthFilters);
44
+ const totalSales = overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0];
45
+ expect(totalSales).toMatchObject({ _id: BASHA, count: 2916 });
46
+ expect(round2(totalSales.grossAmount)).toBe(30556084.25);
47
+ const cancelled = overview[constants_1.REPORT_TYPE_ENUM.CANCELLED_ORDERS][0];
48
+ expect(cancelled).toMatchObject({ _id: BASHA, count: 15 });
49
+ expect(round2(cancelled.grossAmount)).toBe(33959);
50
+ // Every order in the fixture is either a sale or a cancelled order.
51
+ expect(totalSales.count + cancelled.count).toBe(fixtures_1.ORDERS_DATASETS.basha.orderCount);
52
+ });
53
+ it('matches the pinned payments breakdown, including legacy CARD_PAYMENT', () => {
54
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, monthFilters);
55
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES])).toEqual({
56
+ CARD_PAYMENT: { count: 2433, grossAmount: 25798196.75 },
57
+ CASH: { count: 459, grossAmount: 4534480.25 },
58
+ UNPAID: { count: 20, grossAmount: 220427.25 },
59
+ COMPLIMENTARY: { count: 4, grossAmount: 2980 },
60
+ });
61
+ });
62
+ it('matches the pinned platform breakdown', () => {
63
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, monthFilters);
64
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.SALES_BY_PLATFORM])).toEqual({
65
+ 'APP_DINE-IN': { count: 2800, grossAmount: 29566435.75 },
66
+ 'POS_TAKE-AWAY': { count: 50, grossAmount: 409101.5 },
67
+ WEB_DELIVERY: { count: 28, grossAmount: 140931.5 },
68
+ POS_DELIVERY: { count: 22, grossAmount: 332336 },
69
+ 'WEB_TAKE-AWAY': { count: 5, grossAmount: 24071 },
70
+ 'POS_DINE-IN': { count: 4, grossAmount: 14504.75 },
71
+ 'POS_DRIVE-THRU': { count: 2, grossAmount: 10391.75 },
72
+ POS_KIOSK: { count: 2, grossAmount: 23742.5 },
73
+ 'WEB_DINE-IN': { count: 2, grossAmount: 25569.5 },
74
+ 'POS_WALK-IN': { count: 1, grossAmount: 9000 },
75
+ });
76
+ });
77
+ it('produces identical results under SETTLED_AT and CREATED_AT (all settlements inside the month)', () => {
78
+ const settled = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, monthFilters);
79
+ const created = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
80
+ ...monthFilters,
81
+ timeBias: constants_1.REPORT_TIME_BIAS_ENUM.CREATED_AT,
82
+ });
83
+ expect(created).toEqual(settled);
84
+ });
85
+ it('moves settled-later credit bills between days when the time bias changes', () => {
86
+ // One settled-later credit bill is punched on Sep 25 and collected Sep 27.
87
+ const window = (day, timeBias) => (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
88
+ ...dayWindow(day),
89
+ tenants: [BASHA],
90
+ groupId: BASHA,
91
+ timeBias,
92
+ });
93
+ expect(window(25)[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].count).toBe(59);
94
+ expect(window(25, constants_1.REPORT_TIME_BIAS_ENUM.CREATED_AT)[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].count).toBe(60);
95
+ expect(window(27)[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].count).toBe(91);
96
+ expect(window(27, constants_1.REPORT_TIME_BIAS_ENUM.CREATED_AT)[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].count).toBe(90);
97
+ });
98
+ it('excludes cancelled orders payments from the payments facet', () => {
99
+ // 21 pending UNPAID payments exist in the month, 15 of them on cancelled
100
+ // orders; the UNPAID facet adds the 8 issued credit bills
101
+ // (`PAY_LATER`/PENDING) and the 6 settled-later bills
102
+ // (`PAY_LATER`/SETTLED, remapped to UNPAID).
103
+ const pendingUnpaid = (status) => orders
104
+ .filter((order) => status == null || order.order_status === status)
105
+ .flatMap((order) => order.payments)
106
+ .filter((payment) => payment.payment_type === 'UNPAID' && payment.payment_status === 'PENDING');
107
+ expect(pendingUnpaid()).toHaveLength(21);
108
+ expect(pendingUnpaid(constants_1.ORDER_STATUS_ENUM.CANCELLED)).toHaveLength(15);
109
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, monthFilters);
110
+ const unpaid = overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES].find((s) => s._id === 'UNPAID');
111
+ expect(unpaid?.count).toBe(20);
112
+ });
113
+ it('applies AMOUNT_COLLECTED sale type (drops zero-collected orders and their pending payments)', () => {
114
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
115
+ ...monthFilters,
116
+ saleType: constants_1.REPORT_SALE_TYPE_ENUM.AMOUNT_COLLECTED,
117
+ });
118
+ const totalSales = overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0];
119
+ expect(totalSales.count).toBe(2906);
120
+ expect(round2(totalSales.grossAmount)).toBe(30496919.25);
121
+ // No cancelled orders qualify; the UNPAID bucket carries the credit bills
122
+ // + settled-later bills whose orders still collected > 0.
123
+ expect(overview[constants_1.REPORT_TYPE_ENUM.CANCELLED_ORDERS]).toEqual([]);
124
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES])).toEqual({
125
+ CARD_PAYMENT: { count: 2433, grossAmount: 25798196.75 },
126
+ CASH: { count: 459, grossAmount: 4534480.25 },
127
+ UNPAID: { count: 14, grossAmount: 164242.25 },
128
+ });
129
+ });
130
+ it('returns credit bills (deferred settlement) for the sale type', () => {
131
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
132
+ ...monthFilters,
133
+ saleType: constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS,
134
+ });
135
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES])).toEqual({
136
+ [BASHA]: { count: 8, grossAmount: 97298.75 },
137
+ });
138
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES])).toEqual({
139
+ UNPAID: { count: 8, grossAmount: 97298.75 },
140
+ });
141
+ });
142
+ it('applies platform + order status filters together', () => {
143
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
144
+ ...monthFilters,
145
+ platforms: ['APP_DINE-IN'],
146
+ orderStatuses: [constants_1.ORDER_STATUS_ENUM.COMPLETED, constants_1.ORDER_STATUS_ENUM.READY],
147
+ });
148
+ expect(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0]).toMatchObject({ count: 2789 });
149
+ expect(round2(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].grossAmount)).toBe(29422484.25);
150
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES])).toEqual({
151
+ CARD_PAYMENT: { count: 2355, grossAmount: 25152102.5 },
152
+ CASH: { count: 416, grossAmount: 4109963.5 },
153
+ UNPAID: { count: 14, grossAmount: 157438.25 },
154
+ COMPLIMENTARY: { count: 4, grossAmount: 2980 },
155
+ });
156
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.SALES_BY_PLATFORM])).toEqual({
157
+ 'APP_DINE-IN': { count: 2789, grossAmount: 29422484.25 },
158
+ });
159
+ expect(overview[constants_1.REPORT_TYPE_ENUM.CANCELLED_ORDERS]).toEqual([]);
160
+ });
161
+ it('returns empty facets for a window with no orders', () => {
162
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
163
+ ...monthFilters,
164
+ from: new Date('2025-01-01T00:00:00.000Z'),
165
+ to: new Date('2025-01-31T23:59:59.999Z'),
166
+ });
167
+ expect(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES]).toEqual([]);
168
+ expect(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES]).toEqual([]);
169
+ expect(overview[constants_1.REPORT_TYPE_ENUM.SALES_BY_PLATFORM]).toEqual([]);
170
+ expect(overview[constants_1.REPORT_TYPE_ENUM.CANCELLED_ORDERS]).toEqual([]);
171
+ });
172
+ });
173
+ describe('sales-overview baseline — bruzo (2026-09)', () => {
174
+ const orders = (0, fixtures_1.loadOrdersDataset)('bruzo');
175
+ const monthFilters = { ...MONTH_WINDOW, tenants: [BRUZO], groupId: BRUZO };
176
+ it('matches the pinned month totals', () => {
177
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, monthFilters);
178
+ const totalSales = overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0];
179
+ expect(totalSales).toMatchObject({ _id: BRUZO, count: 2070 });
180
+ expect(round2(totalSales.grossAmount)).toBe(2662179.24);
181
+ const cancelled = overview[constants_1.REPORT_TYPE_ENUM.CANCELLED_ORDERS][0];
182
+ expect(cancelled).toMatchObject({ _id: BRUZO, count: 96 });
183
+ expect(round2(cancelled.grossAmount)).toBe(112570.95);
184
+ expect(totalSales.count + cancelled.count).toBe(fixtures_1.ORDERS_DATASETS.bruzo.orderCount);
185
+ });
186
+ it('matches the pinned payments breakdown (aggregator + legacy payment types pass through)', () => {
187
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, monthFilters);
188
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES])).toEqual({
189
+ CASH: { count: 770, grossAmount: 936933.67 },
190
+ ONLINE: { count: 511, grossAmount: 577492.97 },
191
+ CARD_PAYMENT: { count: 353, grossAmount: 600670.94 },
192
+ FOODPANDA: { count: 309, grossAmount: 372893.56 },
193
+ PLAZA: { count: 61, grossAmount: 83809.65 },
194
+ COMPLIMENTARY: { count: 41, grossAmount: 43165 },
195
+ UNPAID: { count: 19, grossAmount: 29067.45 },
196
+ MARKETING: { count: 4, grossAmount: 15078 },
197
+ GOLOOTLO: { count: 2, grossAmount: 3068 },
198
+ });
199
+ });
200
+ it('matches the pinned platform breakdown', () => {
201
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, monthFilters);
202
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.SALES_BY_PLATFORM])).toEqual({
203
+ 'POS_TAKE-AWAY': { count: 414, grossAmount: 465827.39 },
204
+ APP_DELIVERY: { count: 395, grossAmount: 496541 },
205
+ 'POS_DINE-IN': { count: 388, grossAmount: 640803.04 },
206
+ WEB_DELIVERY: { count: 342, grossAmount: 391438.95 },
207
+ 'FOODPANDA_TAKE-AWAY': { count: 311, grossAmount: 375639.56 },
208
+ 'APP_DINE-IN': { count: 80, grossAmount: 132715 },
209
+ 'APP_TAKE-AWAY': { count: 63, grossAmount: 73220 },
210
+ POS_DELIVERY: { count: 41, grossAmount: 50899.7 },
211
+ 'WEB_TAKE-AWAY': { count: 28, grossAmount: 26567.6 },
212
+ FOODPANDA_DELIVERY: { count: 2, grossAmount: 2286 },
213
+ 'POS_WALK-IN': { count: 2, grossAmount: 1186 },
214
+ TALABAT_DELIVERY: { count: 2, grossAmount: 2146 },
215
+ 'UBER-EATS_DELIVERY': { count: 2, grossAmount: 2909 },
216
+ });
217
+ });
218
+ it('excludes cancelled orders payments from the payments facet', () => {
219
+ // 101 pending UNPAID payments exist in the month, 96 of them on cancelled
220
+ // orders; the UNPAID facet adds the 8 issued credit bills and the 6
221
+ // settled-later bills (remapped from PAY_LATER to UNPAID).
222
+ const pendingUnpaid = (status) => orders
223
+ .filter((order) => status == null || order.order_status === status)
224
+ .flatMap((order) => order.payments)
225
+ .filter((payment) => payment.payment_type === 'UNPAID' && payment.payment_status === 'PENDING');
226
+ expect(pendingUnpaid()).toHaveLength(101);
227
+ expect(pendingUnpaid(constants_1.ORDER_STATUS_ENUM.CANCELLED)).toHaveLength(96);
228
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, monthFilters);
229
+ const unpaid = overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES].find((s) => s._id === 'UNPAID');
230
+ expect(unpaid?.count).toBe(19);
231
+ });
232
+ it('moves cross-midnight orders between days when the time bias changes', () => {
233
+ // 5 original cross-midnight settlements + 1 settled-later credit bill
234
+ // punched Sep 10 / collected Sep 11 change the day bias.
235
+ const settledBiasSep10 = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
236
+ ...dayWindow(10),
237
+ tenants: [BRUZO],
238
+ groupId: BRUZO,
239
+ });
240
+ const createdBiasSep10 = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
241
+ ...dayWindow(10),
242
+ tenants: [BRUZO],
243
+ groupId: BRUZO,
244
+ timeBias: constants_1.REPORT_TIME_BIAS_ENUM.CREATED_AT,
245
+ });
246
+ const settledBiasSep11 = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
247
+ ...dayWindow(11),
248
+ tenants: [BRUZO],
249
+ groupId: BRUZO,
250
+ });
251
+ const createdBiasSep11 = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
252
+ ...dayWindow(11),
253
+ tenants: [BRUZO],
254
+ groupId: BRUZO,
255
+ timeBias: constants_1.REPORT_TIME_BIAS_ENUM.CREATED_AT,
256
+ });
257
+ expect(settledBiasSep10[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].count).toBe(99);
258
+ expect(createdBiasSep10[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].count).toBe(103);
259
+ expect(settledBiasSep11[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].count).toBe(73);
260
+ expect(createdBiasSep11[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].count).toBe(69);
261
+ });
262
+ it('applies AMOUNT_COLLECTED sale type', () => {
263
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
264
+ ...monthFilters,
265
+ saleType: constants_1.REPORT_SALE_TYPE_ENUM.AMOUNT_COLLECTED,
266
+ });
267
+ expect(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].count).toBe(2023);
268
+ expect(round2(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].grossAmount)).toBe(2613397.24);
269
+ expect(overview[constants_1.REPORT_TYPE_ENUM.CANCELLED_ORDERS]).toEqual([]);
270
+ // COMPLIMENTARY orders drop out of the collected view; the UNPAID bucket
271
+ // carries the credit bills + settled-later bills whose orders collected > 0.
272
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES])).toEqual({
273
+ CASH: { count: 770, grossAmount: 936933.67 },
274
+ ONLINE: { count: 511, grossAmount: 577492.97 },
275
+ CARD_PAYMENT: { count: 353, grossAmount: 600670.94 },
276
+ FOODPANDA: { count: 309, grossAmount: 372893.56 },
277
+ PLAZA: { count: 61, grossAmount: 83809.65 },
278
+ UNPAID: { count: 13, grossAmount: 23450.45 },
279
+ MARKETING: { count: 4, grossAmount: 15078 },
280
+ GOLOOTLO: { count: 2, grossAmount: 3068 },
281
+ });
282
+ });
283
+ it('returns credit bills (deferred settlement) for the sale type', () => {
284
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
285
+ ...monthFilters,
286
+ saleType: constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS,
287
+ });
288
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES])).toEqual({
289
+ [BRUZO]: { count: 8, grossAmount: 13948.45 },
290
+ });
291
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES])).toEqual({
292
+ UNPAID: { count: 8, grossAmount: 13948.45 },
293
+ });
294
+ });
295
+ it('applies platform + order status filters together (aggregator store)', () => {
296
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
297
+ ...monthFilters,
298
+ platforms: ['FOODPANDA_TAKE-AWAY'],
299
+ orderStatuses: [constants_1.ORDER_STATUS_ENUM.COMPLETED, constants_1.ORDER_STATUS_ENUM.CANCELLED],
300
+ });
301
+ expect(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0]).toMatchObject({ count: 310 });
302
+ expect(round2(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES][0].grossAmount)).toBe(374990.56);
303
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES])).toEqual({
304
+ FOODPANDA: { count: 308, grossAmount: 372244.56 },
305
+ PLAZA: { count: 1, grossAmount: 1648 },
306
+ UNPAID: { count: 1, grossAmount: 1098 },
307
+ });
308
+ expect(toSummaryMap(overview[constants_1.REPORT_TYPE_ENUM.SALES_BY_PLATFORM])).toEqual({
309
+ 'FOODPANDA_TAKE-AWAY': { count: 310, grossAmount: 374990.56 },
310
+ });
311
+ const cancelled = overview[constants_1.REPORT_TYPE_ENUM.CANCELLED_ORDERS][0];
312
+ expect(cancelled).toMatchObject({ count: 2 });
313
+ expect(round2(cancelled.grossAmount)).toBe(4195);
314
+ });
315
+ it('supports a CANCELLED-only status selection', () => {
316
+ const overview = (0, sales_overview_reference_1.computeSalesOverviewReference)(orders, {
317
+ ...monthFilters,
318
+ orderStatuses: [constants_1.ORDER_STATUS_ENUM.CANCELLED],
319
+ });
320
+ // The admin panel renders "No Data" when TOTAL_SALES[0] is absent.
321
+ expect(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_SALES]).toEqual([]);
322
+ expect(overview[constants_1.REPORT_TYPE_ENUM.TOTAL_PAYMENT_SALES]).toEqual([]);
323
+ expect(overview[constants_1.REPORT_TYPE_ENUM.SALES_BY_PLATFORM]).toEqual([]);
324
+ expect(overview[constants_1.REPORT_TYPE_ENUM.CANCELLED_ORDERS][0]).toMatchObject({ count: 96 });
325
+ });
326
+ });