@managesome/eposmatic-toolkit 0.4.51 → 0.4.53

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 (97) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +106 -2
  2. package/esm/constants/enums/delivery.js +13 -0
  3. package/esm/helpers/materialized-reports/index.js +5 -0
  4. package/esm/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +8 -62
  5. package/esm/helpers/materialized-reports/materialized-delivery-classification-helpers.js +95 -73
  6. package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +118 -146
  7. package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +304 -71
  8. package/esm/helpers/materialized-reports/materialized-delivery-entry-pages.js +307 -0
  9. package/esm/helpers/materialized-reports/materialized-delivery-index-migration.js +103 -0
  10. package/esm/helpers/materialized-reports/materialized-delivery-rows.js +109 -0
  11. package/esm/helpers/materialized-reports/materialized-delivery-wipe-targets.js +32 -0
  12. package/esm/helpers/materialized-reports/materialized-document-size.js +65 -0
  13. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +31 -16
  14. package/esm/helpers/materialized-reports/materialized-integer-contract.js +10 -1
  15. package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +3 -91
  16. package/esm/helpers/materialized-reports/materialized-tenant-delivery-geo-helpers.js +597 -0
  17. package/esm/helpers/materialized-reports/scope-helpers.js +98 -0
  18. package/esm/helpers/materialized-reports/tenant-delivery-formatter.js +70 -20
  19. package/esm/helpers/utils/offer-item-resolution.js +39 -0
  20. package/esm/schemas/models/index.js +12 -10
  21. package/esm/schemas/models/materialized_tenant_delivery_entries.js +33 -12
  22. package/esm/schemas/models/materialized_tenant_delivery_geo_cells.js +83 -0
  23. package/esm/schemas/models/materialized_tenant_delivery_geo_scopes.js +35 -0
  24. package/esm/schemas/models/materialized_tenant_delivery_reports.js +10 -1
  25. package/esm/schemas/types/index.js +14 -12
  26. package/esm/schemas/types/materialized_tenant_delivery_geo_cells.js +1 -0
  27. package/esm/schemas/types/materialized_tenant_delivery_geo_scopes.js +1 -0
  28. package/lib/constants/MATERIALIZED_REPORTS.d.ts +95 -1
  29. package/lib/constants/MATERIALIZED_REPORTS.js +108 -4
  30. package/lib/constants/enums/delivery.d.ts +9 -0
  31. package/lib/constants/enums/delivery.js +14 -1
  32. package/lib/helpers/materialized-reports/index.d.ts +5 -0
  33. package/lib/helpers/materialized-reports/index.js +5 -0
  34. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +8 -62
  35. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +36 -9
  36. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +94 -72
  37. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +23 -26
  38. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +121 -150
  39. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +44 -5
  40. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +306 -69
  41. package/lib/helpers/materialized-reports/materialized-delivery-entry-pages.d.ts +128 -0
  42. package/lib/helpers/materialized-reports/materialized-delivery-entry-pages.js +322 -0
  43. package/lib/helpers/materialized-reports/materialized-delivery-index-migration.d.ts +48 -0
  44. package/lib/helpers/materialized-reports/materialized-delivery-index-migration.js +108 -0
  45. package/lib/helpers/materialized-reports/materialized-delivery-rows.d.ts +15 -0
  46. package/lib/helpers/materialized-reports/materialized-delivery-rows.js +115 -0
  47. package/lib/helpers/materialized-reports/materialized-delivery-wipe-targets.d.ts +17 -0
  48. package/lib/helpers/materialized-reports/materialized-delivery-wipe-targets.js +35 -0
  49. package/lib/helpers/materialized-reports/materialized-document-size.d.ts +47 -0
  50. package/lib/helpers/materialized-reports/materialized-document-size.js +73 -0
  51. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +28 -13
  52. package/lib/helpers/materialized-reports/materialized-integer-contract.js +10 -1
  53. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +2 -90
  54. package/lib/helpers/materialized-reports/materialized-tenant-delivery-geo-helpers.d.ts +186 -0
  55. package/lib/helpers/materialized-reports/materialized-tenant-delivery-geo-helpers.js +620 -0
  56. package/lib/helpers/materialized-reports/scope-helpers.d.ts +8 -0
  57. package/lib/helpers/materialized-reports/scope-helpers.js +99 -0
  58. package/lib/helpers/materialized-reports/tenant-delivery-formatter.d.ts +41 -0
  59. package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +70 -20
  60. package/lib/helpers/utils/offer-item-resolution.d.ts +28 -0
  61. package/lib/helpers/utils/offer-item-resolution.js +41 -0
  62. package/lib/schemas/models/index.d.ts +12 -10
  63. package/lib/schemas/models/index.js +12 -10
  64. package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +31 -9
  65. package/lib/schemas/models/materialized_tenant_delivery_entries.js +32 -11
  66. package/lib/schemas/models/materialized_tenant_delivery_geo_cells.d.ts +129 -0
  67. package/lib/schemas/models/materialized_tenant_delivery_geo_cells.js +86 -0
  68. package/lib/schemas/models/materialized_tenant_delivery_geo_scopes.d.ts +71 -0
  69. package/lib/schemas/models/materialized_tenant_delivery_geo_scopes.js +38 -0
  70. package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +10 -1
  71. package/lib/schemas/models/materialized_tenant_delivery_reports.js +10 -1
  72. package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +15 -15
  73. package/lib/schemas/types/index.d.ts +14 -12
  74. package/lib/schemas/types/index.js +14 -12
  75. package/lib/schemas/types/materialized_tenant_delivery_entries.d.ts +36 -4
  76. package/lib/schemas/types/materialized_tenant_delivery_geo_cells.d.ts +178 -0
  77. package/lib/schemas/types/materialized_tenant_delivery_geo_cells.js +2 -0
  78. package/lib/schemas/types/materialized_tenant_delivery_geo_scopes.d.ts +21 -0
  79. package/lib/schemas/types/materialized_tenant_delivery_geo_scopes.js +2 -0
  80. package/lib/schemas/types/materialized_tenant_delivery_reports.d.ts +49 -7
  81. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.js +56 -19
  82. package/lib/tests/materialized-reports/materialized-delivery-budgets.test.d.ts +1 -0
  83. package/lib/tests/materialized-reports/materialized-delivery-budgets.test.js +91 -0
  84. package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +271 -15
  85. package/lib/tests/materialized-reports/materialized-delivery-final-audit.test.d.ts +1 -0
  86. package/lib/tests/materialized-reports/materialized-delivery-final-audit.test.js +128 -0
  87. package/lib/tests/materialized-reports/materialized-delivery-fold.test.js +42 -5
  88. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +13 -1
  89. package/lib/tests/materialized-reports/materialized-delivery-geo.test.d.ts +1 -0
  90. package/lib/tests/materialized-reports/materialized-delivery-geo.test.js +446 -0
  91. package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +117 -25
  92. package/lib/tests/materialized-reports/materialized-delivery-practical.test.d.ts +1 -0
  93. package/lib/tests/materialized-reports/materialized-delivery-practical.test.js +344 -0
  94. package/lib/tests/materialized-reports/materialized-integer-contract.test.js +14 -0
  95. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +1 -1
  96. package/lib/tests/offer-item-resolution.test.js +57 -0
  97. package/package.json +4 -2
@@ -0,0 +1,344 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const delivery_1 = require("../../constants/enums/delivery");
4
+ const index_1 = require("../../helpers/materialized-reports/index");
5
+ const materialized_delivery_entry_pages_1 = require("../../helpers/materialized-reports/materialized-delivery-entry-pages");
6
+ const materialized_document_size_1 = require("../../helpers/materialized-reports/materialized-document-size");
7
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
8
+ const TENANT = 'tenant-practical';
9
+ const BRAND = 'brand-practical';
10
+ const DAY = '2026-09-10';
11
+ const order = (id, over = {}) => ({
12
+ _id: id,
13
+ tenant_id: TENANT,
14
+ platform: 'POS_DELIVERY',
15
+ order_status: 'COMPLETED',
16
+ createdAt: new Date(DAY + 'T10:00:00.000Z'),
17
+ localDate: DAY,
18
+ localHour: 10,
19
+ net_sale_amount: 5000,
20
+ amount_collected: 5000,
21
+ delivery_details: { city: 'Istanbul', state: 'Istanbul', delivery_cost: 300 },
22
+ ...over,
23
+ });
24
+ const delivery = (id, orderId, over = {}) => ({
25
+ _id: id,
26
+ tenant_id: TENANT,
27
+ order_id: orderId,
28
+ rider_id: 'rider-1',
29
+ rider_name: 'Sam',
30
+ status: 'COMPLETED',
31
+ createdAt: new Date(DAY + 'T10:05:00.000Z'),
32
+ updatedAt: new Date(DAY + 'T10:35:00.000Z'),
33
+ delivered_at: new Date(DAY + 'T10:32:00.000Z'),
34
+ expected_delivery_at: null,
35
+ distance_in_meters_from_store: 3000,
36
+ rider_earnings: {
37
+ status: delivery_1.RIDER_EARNINGS_STATUS_ENUM.FINAL,
38
+ amount: 700,
39
+ },
40
+ tracking_enabled: true,
41
+ localDate: DAY,
42
+ ...over,
43
+ });
44
+ describe('delivery practical-scale correctness', () => {
45
+ it('uses a per-metric denominator for completion and total time', () => {
46
+ const compiled = (0, index_1.compileTenantDeliveryReports)({
47
+ tenantId: TENANT,
48
+ brandId: BRAND,
49
+ localDate: DAY,
50
+ orders: [order('o1'), order('o2', { localHour: 11 })],
51
+ deliveries: [
52
+ delivery('d1', 'o1'),
53
+ delivery('d2', 'o2', {
54
+ status: 'IN_PROGRESS',
55
+ delivered_at: null,
56
+ updatedAt: new Date(DAY + 'T11:06:00.000Z'),
57
+ }),
58
+ ],
59
+ computedAt: new Date(DAY + 'T23:00:00.000Z'),
60
+ });
61
+ const page = (0, index_1.getFormattedTenantDeliveryReporting)(compiled.reports)[0][0];
62
+ expect(page.summary.average_complete_minutes).toBe(27);
63
+ expect(page.summary.average_total_minutes).toBe(32);
64
+ // The old denominator was graded_count (2), which produced 13.5 / 16.
65
+ expect(page.summary.average_complete_minutes).not.toBe(13.5);
66
+ expect(page.summary.average_total_minutes).not.toBe(16);
67
+ });
68
+ it('attributes a delivery to the ORDER local day, even after midnight', () => {
69
+ const orderDate = '2026-09-10';
70
+ const o = order('o-midnight', {
71
+ localDate: orderDate,
72
+ createdAt: new Date('2026-09-10T20:50:00.000Z'),
73
+ localHour: 23,
74
+ });
75
+ const d = delivery('d-midnight', 'o-midnight', {
76
+ localDate: '2026-09-11',
77
+ createdAt: new Date('2026-09-10T21:10:00.000Z'),
78
+ updatedAt: new Date('2026-09-10T21:40:00.000Z'),
79
+ delivered_at: new Date('2026-09-10T21:35:00.000Z'),
80
+ });
81
+ const compiled = (0, index_1.compileTenantDeliveryReports)({
82
+ tenantId: TENANT,
83
+ brandId: BRAND,
84
+ localDate: orderDate,
85
+ orders: [o],
86
+ deliveries: [d],
87
+ computedAt: new Date(),
88
+ });
89
+ const row = Object.values(compiled.entries[0].delivery_entries ?? {}).flatMap((section) => section.rows)[0];
90
+ expect(row.source_local_date).toBe(orderDate);
91
+ expect(row.source_day_unattributed).toBe(false);
92
+ expect(row.order_status).toBe('COMPLETED');
93
+ expect(row.street).toBeUndefined();
94
+ });
95
+ it('excludes pickup orders from H3 cells and unknown aggregates', () => {
96
+ const cells = (0, index_1.compileDeliveryGeoCounters)({
97
+ tenantId: TENANT,
98
+ brandId: BRAND,
99
+ localDate: DAY,
100
+ orders: [
101
+ order('o-delivery', {
102
+ delivery_details: { city: 'Istanbul', delivery_cost: 300, lat: 41, lng: 29 },
103
+ }),
104
+ order('o-pickup', { delivery_details: null }),
105
+ ],
106
+ deliveries: [],
107
+ computedAt: new Date(),
108
+ });
109
+ expect(cells.reduce((sum, cell) => sum + cell.orders, 0)).toBe(1);
110
+ expect(cells.some((cell) => cell.h3_cell === '__UNKNOWN__')).toBe(false);
111
+ });
112
+ it('returns cap counts in the geo response', () => {
113
+ const cells = Array.from({ length: 2002 }, (_, index) => ({
114
+ tenant_id: TENANT,
115
+ brand_id: BRAND,
116
+ scope_context: 'localDate',
117
+ scope_field: DAY,
118
+ city: 'Istanbul',
119
+ h3_cell: String(800000000000000 + index),
120
+ resolution: 8,
121
+ location: {
122
+ type: 'Point',
123
+ coordinates: [29, 41],
124
+ },
125
+ orders: 3,
126
+ app_orders: 3,
127
+ delivery_records: 3,
128
+ completed: 3,
129
+ cancelled: 0,
130
+ net_sale_amount: 0,
131
+ delivery_charge_amount: 0,
132
+ rider_revenue_amount: 0,
133
+ schema_version: 1,
134
+ computed_at: new Date(),
135
+ }));
136
+ const response = (0, index_1.buildDeliveryGeoResponse)({
137
+ cells: cells.slice(0, 2000),
138
+ totalOrders: 6006,
139
+ totalAppOrders: 6006,
140
+ cappedCellCount: 2,
141
+ cappedOrderCount: 6,
142
+ });
143
+ expect(response.capped_cell_count).toBe(2);
144
+ expect(response.capped_order_count).toBe(6);
145
+ });
146
+ it('keeps a 5,000-order cell far below the 2 MiB geo budget', () => {
147
+ const orders = Array.from({ length: 5000 }, (_, index) => order('big-' + index, {
148
+ platform: 'P' + (index % 7),
149
+ order_status: ['COMPLETED', 'CANCELLED', 'PENDING'][index % 3],
150
+ delivery_details: { city: 'Istanbul', delivery_cost: 300, lat: 41, lng: 29 },
151
+ }));
152
+ const result = (0, index_1.compileTenantDeliveryGeoCellsWithStats)({
153
+ tenantId: TENANT,
154
+ brandId: BRAND,
155
+ localDate: DAY,
156
+ orders,
157
+ deliveries: [],
158
+ computedAt: new Date(),
159
+ });
160
+ expect(result.cells).toHaveLength(1);
161
+ const keys = Object.keys(result.cells[0].by_key ?? {});
162
+ expect(keys.length).toBeLessThanOrEqual(64);
163
+ expect((0, materialized_document_size_1.materializedDocumentBsonBytes)(result.cells[0])).toBeLessThan(2 * 1024 * 1024);
164
+ });
165
+ it('reports write-time cell cap counts', () => {
166
+ const orders = [41.0, 41.1, 41.2].map((lat, index) => order('cap-' + index, {
167
+ delivery_details: { city: 'Istanbul', delivery_cost: 300, lat, lng: 29 + index * 0.1 },
168
+ }));
169
+ const result = (0, index_1.compileTenantDeliveryGeoCellsWithStats)({
170
+ tenantId: TENANT,
171
+ brandId: BRAND,
172
+ localDate: DAY,
173
+ orders,
174
+ deliveries: [],
175
+ computedAt: new Date(),
176
+ maxCells: 1,
177
+ });
178
+ expect(result.cells).toHaveLength(1);
179
+ expect(result.cappedCellCount).toBe(2);
180
+ expect(result.cappedOrderCount).toBe(2);
181
+ });
182
+ it('filters H3 cells by platform and order status through by_key', () => {
183
+ const cells = (0, index_1.compileDeliveryGeoCounters)({
184
+ tenantId: TENANT,
185
+ brandId: BRAND,
186
+ localDate: DAY,
187
+ orders: [
188
+ order('o-completed-pos', {
189
+ order_status: 'COMPLETED',
190
+ platform: 'POS_DELIVERY',
191
+ delivery_details: { city: 'Istanbul', delivery_cost: 300, lat: 41, lng: 29 },
192
+ }),
193
+ order('o-cancelled-web', {
194
+ order_status: 'CANCELLED',
195
+ platform: 'WEB_DELIVERY',
196
+ delivery_details: { city: 'Istanbul', delivery_cost: 300, lat: 41.0001, lng: 29.0001 },
197
+ }),
198
+ ],
199
+ deliveries: [],
200
+ computedAt: new Date(),
201
+ });
202
+ const all = (0, index_1.buildDeliveryGeoResponse)({ cells, totalOrders: 2, totalAppOrders: 0, minOrders: 1 });
203
+ const cancelled = (0, index_1.buildDeliveryGeoResponse)({
204
+ cells,
205
+ totalOrders: 1,
206
+ totalAppOrders: 0,
207
+ minOrders: 1,
208
+ filters: { order_statuses: ['CANCELLED'] },
209
+ });
210
+ const web = (0, index_1.buildDeliveryGeoResponse)({
211
+ cells,
212
+ totalOrders: 1,
213
+ totalAppOrders: 0,
214
+ minOrders: 1,
215
+ filters: { platforms: ['WEB_DELIVERY'] },
216
+ });
217
+ const both = (0, index_1.buildDeliveryGeoResponse)({
218
+ cells,
219
+ totalOrders: 1,
220
+ totalAppOrders: 0,
221
+ minOrders: 1,
222
+ filters: { platforms: ['WEB_DELIVERY'], order_statuses: ['CANCELLED'] },
223
+ });
224
+ expect(all.cells.reduce((sum, cell) => sum + cell.orders, 0)).toBe(2);
225
+ expect(cancelled.cells.reduce((sum, cell) => sum + cell.orders, 0)).toBe(1);
226
+ expect(web.cells.reduce((sum, cell) => sum + cell.orders, 0)).toBe(1);
227
+ expect(both.cells.reduce((sum, cell) => sum + cell.orders, 0)).toBe(1);
228
+ expect(cancelled.cells[0].orders).toBe(1);
229
+ expect(both.cells[0].orders).toBe(1);
230
+ for (const cell of cells) {
231
+ expect(Object.values(cell.by_key ?? {}).reduce((sum, value) => sum + value.orders, 0)).toBe(cell.orders);
232
+ }
233
+ });
234
+ it('city filter narrows cells without touching the by_key sums', () => {
235
+ const cells = (0, index_1.compileDeliveryGeoCounters)({
236
+ tenantId: TENANT,
237
+ brandId: BRAND,
238
+ localDate: DAY,
239
+ orders: [
240
+ order('o-istanbul', {
241
+ delivery_details: { city: 'Istanbul', delivery_cost: 300, lat: 41, lng: 29 },
242
+ }),
243
+ order('o-ankara', {
244
+ delivery_details: { city: 'Ankara', delivery_cost: 300, lat: 39.9, lng: 32.9 },
245
+ }),
246
+ ],
247
+ deliveries: [],
248
+ computedAt: new Date(),
249
+ });
250
+ const istanbul = (0, index_1.buildDeliveryGeoResponse)({
251
+ cells,
252
+ totalOrders: 1,
253
+ totalAppOrders: 0,
254
+ minOrders: 1,
255
+ filters: { cities: ['Istanbul'] },
256
+ });
257
+ expect(istanbul.cells).toHaveLength(1);
258
+ expect(istanbul.cells[0].city).toBe('Istanbul');
259
+ expect(istanbul.cells[0].orders).toBe(1);
260
+ for (const cell of cells) {
261
+ expect(Object.values(cell.by_key ?? {}).reduce((sum, value) => sum + value.orders, 0)).toBe(cell.orders);
262
+ }
263
+ });
264
+ it('splits a synthetic over-budget entry page deterministically', () => {
265
+ const rows = Array.from({ length: 35000 }, (_, index) => ({
266
+ delivery_id: 'd-' + index,
267
+ entry_key: 'e-' + index,
268
+ order_id: 'o-' + index,
269
+ source_local_date: DAY,
270
+ delivered_at: new Date(DAY + 'T10:00:00.000Z'),
271
+ created_at: new Date(DAY + 'T09:00:00.000Z'),
272
+ rider_name: 'r'.repeat(420),
273
+ }));
274
+ const document = {
275
+ tenant_id: TENANT,
276
+ brand_id: BRAND,
277
+ scope_context: 'localMonth',
278
+ scope_field: '2026-09',
279
+ page: DAY,
280
+ day_index: null,
281
+ sale_type_key_filters: ['ORDER_STATUS', 'PLATFORM', 'CITY'],
282
+ delivery_entries: { 'COMPLETED:POS_DELIVERY': { rows } },
283
+ off_app_order_count: 3,
284
+ schema_version: 3,
285
+ source_watermark: new Date(DAY + 'T23:00:00.000Z'),
286
+ computed_at: new Date(DAY + 'T23:00:00.000Z'),
287
+ createdAt: new Date(DAY + 'T23:00:00.000Z'),
288
+ updatedAt: new Date(DAY + 'T23:00:00.000Z'),
289
+ };
290
+ expect((0, materialized_document_size_1.materializedDocumentBsonBytes)(document)).toBeGreaterThan(MATERIALIZED_REPORTS_1.MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES);
291
+ const pages = (0, index_1.splitMaterializedDeliveryEntryPage)(document);
292
+ expect(pages.length).toBeGreaterThan(1);
293
+ const ids = new Set();
294
+ let count = 0;
295
+ for (const page of pages) {
296
+ expect((0, materialized_document_size_1.materializedDocumentBsonBytes)(page)).toBeLessThanOrEqual(MATERIALIZED_REPORTS_1.MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES);
297
+ expect((0, materialized_delivery_entry_pages_1.isDeliveryEntryPageForSourceDay)(page.scope_context, page.page, DAY)).toBe(true);
298
+ for (const section of Object.values(page.delivery_entries ?? {})) {
299
+ for (const row of section.rows) {
300
+ ids.add(String(row.delivery_id));
301
+ count += 1;
302
+ }
303
+ }
304
+ }
305
+ expect(count).toBe(rows.length);
306
+ expect(ids.size).toBe(rows.length);
307
+ const merged = (0, index_1.mergeMaterializedDeliveryEntryPages)(pages);
308
+ const mergedRows = Object.values(merged.delivery_entries ?? {}).flatMap((section) => section.rows);
309
+ expect(mergedRows.length).toBe(rows.length);
310
+ expect(new Set(mergedRows.map((row) => row.delivery_id)).size).toBe(rows.length);
311
+ expect(merged.off_app_order_count).toBe(3);
312
+ });
313
+ it('filters totals by order status, platform and city', () => {
314
+ const compiled = (0, index_1.compileTenantDeliveryReports)({
315
+ tenantId: TENANT,
316
+ brandId: BRAND,
317
+ localDate: DAY,
318
+ orders: [
319
+ order('o1', { order_status: 'COMPLETED', platform: 'POS_DELIVERY' }),
320
+ order('o2', {
321
+ order_status: 'CANCELLED',
322
+ platform: 'WEB_DELIVERY',
323
+ delivery_details: { city: 'Ankara', delivery_cost: 300 },
324
+ }),
325
+ ],
326
+ deliveries: [],
327
+ computedAt: new Date(),
328
+ });
329
+ const all = (0, index_1.getFormattedTenantDeliveryReporting)(compiled.reports)[0][0];
330
+ const cancelled = (0, index_1.getFormattedTenantDeliveryReporting)(compiled.reports, {
331
+ order_statuses: ['CANCELLED'],
332
+ })[0][0];
333
+ const pos = (0, index_1.getFormattedTenantDeliveryReporting)(compiled.reports, {
334
+ platforms: ['POS_DELIVERY'],
335
+ })[0][0];
336
+ const ankara = (0, index_1.getFormattedTenantDeliveryReporting)(compiled.reports, {
337
+ cities: ['Ankara'],
338
+ })[0][0];
339
+ expect(all.summary.orders).toBe(2);
340
+ expect(cancelled.summary.orders).toBe(1);
341
+ expect(pos.summary.orders).toBe(1);
342
+ expect(ankara.summary.orders).toBe(1);
343
+ });
344
+ });
@@ -232,3 +232,17 @@ describe('a FULL discount document set is integer-only', () => {
232
232
  }
233
233
  });
234
234
  });
235
+ describe('geometry is the one exception', () => {
236
+ it('accepts a GeoJSON centroid while still rejecting money decimals', () => {
237
+ expect((0, materialized_integer_contract_1.findNonIntegerNumericLeaves)({
238
+ h3_cell: '88194ad33dfffff',
239
+ orders: 3,
240
+ location: { type: 'Point', coordinates: [28.9784123, 41.0082456] },
241
+ })).toEqual([]);
242
+ expect((0, materialized_integer_contract_1.findNonIntegerNumericLeaves)({
243
+ orders: 3,
244
+ net_sale_amount: 12.5,
245
+ location: { type: 'Point', coordinates: [28.9784123, 41.0082456] },
246
+ }).map((violation) => violation.path)).toEqual(['net_sale_amount']);
247
+ });
248
+ });
@@ -209,7 +209,7 @@ describe('materialized_tenant_sales_overview_reports schema', () => {
209
209
  });
210
210
  it('rejects unsupported key-filter dimensions', () => {
211
211
  // NOTE: this test previously used 'CITY' as its unsupported example. The delivery
212
- // report (M4) legitimately ADDED CITY as a real dimension - a key is <PLATFORM>:<CITY>
212
+ // report (M4) legitimately ADDED CITY as a real dimension - a key is <ORDER_STATUS>:<PLATFORM>:<CITY>
213
213
  // - so the fixture silently stopped testing rejection. The sentinel is now a value
214
214
  // that is deliberately not a dimension, and the guard below makes the test fail
215
215
  // LOUDLY if that ever changes, instead of quietly passing on a valid value.
@@ -97,3 +97,60 @@ describe('getRewardSelectionQuantity', () => {
97
97
  expect((0, offer_item_resolution_1.getRewardSelectionQuantity)({ offer: noItems, stats: undefined })).toBe(1);
98
98
  });
99
99
  });
100
+ describe('getClaimedRewardQuantity / getRemainingRewardQuantity', () => {
101
+ const categories = (items) => [
102
+ { category_id: 'c1', items: items.slice(0, 2) },
103
+ { category_id: 'c2', items: items.slice(2) },
104
+ ];
105
+ const stamped = (quantity, ids = {}) => ({
106
+ quantity,
107
+ item_discount_details: ids.loyalty == null && ids.voucher == null
108
+ ? null
109
+ : {
110
+ loyalty_discount_id: ids.loyalty ?? null,
111
+ voucher_discount_id: ids.voucher ?? null,
112
+ },
113
+ });
114
+ it('returns 0 for empty or missing categories', () => {
115
+ expect((0, offer_item_resolution_1.getClaimedRewardQuantity)({ categories: [], offerId: 'a' })).toBe(0);
116
+ expect((0, offer_item_resolution_1.getClaimedRewardQuantity)({ categories: undefined, offerId: 'a' })).toBe(0);
117
+ expect((0, offer_item_resolution_1.getClaimedRewardQuantity)({ categories: null, offerId: 'a' })).toBe(0);
118
+ });
119
+ it('returns 0 when no line is stamped with the offer id', () => {
120
+ const result = (0, offer_item_resolution_1.getClaimedRewardQuantity)({
121
+ categories: categories([
122
+ stamped(2, { loyalty: 'other' }),
123
+ stamped(3),
124
+ stamped(4, { voucher: 'another' }),
125
+ stamped(5, { loyalty: 'a' }),
126
+ ]),
127
+ offerId: 'a',
128
+ });
129
+ expect(result).toBe(5);
130
+ });
131
+ it('sums loyalty and voucher stamps across categories', () => {
132
+ const result = (0, offer_item_resolution_1.getClaimedRewardQuantity)({
133
+ categories: categories([
134
+ stamped(2, { loyalty: 'a' }),
135
+ stamped(1, { voucher: 'a' }),
136
+ stamped(3, { loyalty: 'a' }),
137
+ stamped(9, { loyalty: 'b' }),
138
+ ]),
139
+ offerId: 'a',
140
+ });
141
+ expect(result).toBe(6);
142
+ });
143
+ it('subtracts claimed units from the order allowance and never goes negative', () => {
144
+ const cart = categories([stamped(2, { loyalty: 'a' }), stamped(1, { voucher: 'a' })]);
145
+ expect((0, offer_item_resolution_1.getRemainingRewardQuantity)({ categories: cart, offerId: 'a', allowance: 6 })).toBe(3);
146
+ expect((0, offer_item_resolution_1.getRemainingRewardQuantity)({ categories: cart, offerId: 'a', allowance: 3 })).toBe(0);
147
+ expect((0, offer_item_resolution_1.getRemainingRewardQuantity)({ categories: cart, offerId: 'a', allowance: 1 })).toBe(0);
148
+ });
149
+ it('returns the full allowance for a cart with no claim of this offer', () => {
150
+ expect((0, offer_item_resolution_1.getRemainingRewardQuantity)({
151
+ categories: categories([stamped(2, { loyalty: 'other' })]),
152
+ offerId: 'a',
153
+ allowance: 6,
154
+ })).toBe(6);
155
+ });
156
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.51",
3
+ "version": "0.4.53",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -26,13 +26,15 @@
26
26
  "fixtures:prep-time": "tsx scripts/analyze-prep-time.ts",
27
27
  "fixtures:prep-time-parity": "tsx scripts/analyze-prep-time-parity.ts",
28
28
  "fixtures:delivery": "tsx scripts/analyze-delivery.ts",
29
- "sim:close-day": "tsx simulations/simulate-day-close.ts"
29
+ "sim:close-day": "tsx simulations/simulate-day-close.ts",
30
+ "wipe:delivery": "tsx scripts/wipe-delivery-materialization.ts"
30
31
  },
31
32
  "dependencies": {
32
33
  "bson-objectid": "^2.0.4",
33
34
  "crypto-js": "^4.2.0",
34
35
  "emoji-strip": "^1.0.1",
35
36
  "google-libphonenumber": "^3.2.40",
37
+ "h3-js": "^4.5.0",
36
38
  "lodash": "^4.17.21",
37
39
  "moment": "^2.30.1",
38
40
  "mongoose": "^8.13.2"