@managesome/eposmatic-toolkit 0.4.31 → 0.4.34

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 (55) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +3 -5
  2. package/esm/constants/enums/offers.js +0 -6
  3. package/esm/helpers/materialized-reports/index.js +1 -0
  4. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
  5. package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +12 -8
  6. package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +86 -9
  7. package/esm/helpers/materialized-reports/materialized-prep-time-read-helpers.js +613 -0
  8. package/esm/helpers/store/cart-actions.js +3 -12
  9. package/esm/helpers/types/materialized-reports-apis.js +10 -0
  10. package/esm/helpers/utils/offer-apply-helpers.js +2 -10
  11. package/esm/helpers/utils/offers-helpers.js +117 -2
  12. package/esm/schemas/models/index.js +0 -1
  13. package/esm/schemas/types/index.js +0 -1
  14. package/lib/constants/MATERIALIZED_REPORTS.d.ts +3 -2
  15. package/lib/constants/MATERIALIZED_REPORTS.js +3 -5
  16. package/lib/constants/enums/offers.d.ts +0 -5
  17. package/lib/constants/enums/offers.js +1 -7
  18. package/lib/helpers/materialized-reports/index.d.ts +1 -0
  19. package/lib/helpers/materialized-reports/index.js +1 -0
  20. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
  21. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +12 -8
  22. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +15 -0
  23. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +86 -8
  24. package/lib/helpers/materialized-reports/materialized-prep-time-read-helpers.d.ts +179 -0
  25. package/lib/helpers/materialized-reports/materialized-prep-time-read-helpers.js +639 -0
  26. package/lib/helpers/store/cart-actions.js +1 -10
  27. package/lib/helpers/types/materialized-reports-apis.d.ts +145 -1
  28. package/lib/helpers/types/materialized-reports-apis.js +11 -0
  29. package/lib/helpers/utils/offer-apply-helpers.js +2 -10
  30. package/lib/helpers/utils/offers-helpers.d.ts +50 -0
  31. package/lib/helpers/utils/offers-helpers.js +120 -2
  32. package/lib/schemas/models/index.d.ts +0 -1
  33. package/lib/schemas/models/index.js +0 -1
  34. package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +6 -6
  35. package/lib/schemas/types/index.d.ts +0 -1
  36. package/lib/schemas/types/index.js +0 -1
  37. package/lib/schemas/types/materialized_tenant_prep_time_reports.d.ts +7 -5
  38. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +28 -5
  39. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +17 -4
  40. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.js +6 -4
  41. package/lib/tests/materialized-reports/materialized-prep-time-duration-consistency.test.js +135 -0
  42. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.js +71 -3
  43. package/lib/tests/materialized-reports/materialized-prep-time-read-helpers.test.d.ts +1 -0
  44. package/lib/tests/materialized-reports/materialized-prep-time-read-helpers.test.js +620 -0
  45. package/lib/tests/offers-helpers-points-evidence.test.d.ts +1 -0
  46. package/lib/tests/offers-helpers-points-evidence.test.js +338 -0
  47. package/lib/tests/offers-matrix.test.d.ts +1 -0
  48. package/lib/tests/offers-matrix.test.js +401 -0
  49. package/package.json +1 -1
  50. package/esm/schemas/models/rewards_usage.js +0 -61
  51. package/lib/schemas/models/rewards_usage.d.ts +0 -53
  52. package/lib/schemas/models/rewards_usage.js +0 -64
  53. package/lib/schemas/types/rewards_usage.d.ts +0 -13
  54. package/lib/schemas/types/rewards_usage.js +0 -2
  55. /package/{esm/schemas/types/rewards_usage.js → lib/tests/materialized-reports/materialized-prep-time-duration-consistency.test.d.ts} +0 -0
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
+ const materialized_prep_time_compile_helpers_1 = require("../../helpers/materialized-reports/materialized-prep-time-compile-helpers");
5
+ const tenant_prep_time_formatter_1 = require("../../helpers/materialized-reports/tenant-prep-time-formatter");
6
+ const materialized_prep_time_read_helpers_1 = require("../../helpers/materialized-reports/materialized-prep-time-read-helpers");
7
+ /**
8
+ * ONE duration population across the WHOLE report.
9
+ *
10
+ * The bug this pins: the Summary used to include an out-of-range (or negative)
11
+ * duration in `duration.count` while excluding it from `duration.sum`, so
12
+ * `avg = sum / count` was silently understated and disagreed with the
13
+ * item/category reads. VALID durations alone now feed count/sum/min/max/buckets/
14
+ * instant count, and the Summary average equals the item/category weighted
15
+ * average over the same population.
16
+ */
17
+ const TENANT = 'tenant-consistency';
18
+ const BRAND = 'brand-consistency';
19
+ const DAY = '2026-09-10';
20
+ const BASE = new Date(`${DAY}T10:00:00.000Z`);
21
+ const item = (id, minutes) => ({
22
+ _id: id,
23
+ name: id,
24
+ is_ready: true,
25
+ ready_minutes: minutes,
26
+ prep_started_at: BASE,
27
+ quantity: 1,
28
+ });
29
+ const order = () => ({
30
+ _id: 'order-consistency',
31
+ tenant_id: TENANT,
32
+ order_number: 1,
33
+ order_status: 'COMPLETED',
34
+ platform: 'POS_WALK-IN',
35
+ localHour: 10,
36
+ localDate: DAY,
37
+ is_deleted: false,
38
+ createdAt: BASE,
39
+ updatedAt: BASE,
40
+ categories: [
41
+ {
42
+ name: 'Mains',
43
+ items: [item('a', 8), item('c', 30)],
44
+ },
45
+ {
46
+ name: 'Other',
47
+ items: [item('b', 20), item('invalid-high', MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_MAX_MINUTES + 1), item('invalid-negative', -3)],
48
+ },
49
+ ],
50
+ });
51
+ const compiled = (0, materialized_prep_time_compile_helpers_1.compileTenantPrepTimeReports)({
52
+ tenantId: TENANT,
53
+ brandId: BRAND,
54
+ localDate: DAY,
55
+ orders: [order()],
56
+ computedAt: new Date(`${DAY}T23:00:00.000Z`),
57
+ });
58
+ const dayReport = compiled.reports.find((report) => report.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE &&
59
+ report.scope_field === DAY);
60
+ const dayEntry = compiled.entries.find((entry) => entry.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE &&
61
+ entry.scope_field === DAY);
62
+ const readRows = Object.values(dayEntry.prep_time_entries ?? {}).flatMap((section) => (section.rows ?? []).map((row) => ({
63
+ tenant_id: TENANT,
64
+ order_id: row.order_id,
65
+ item_id: String(row.item_id),
66
+ item_name: row.item_name,
67
+ category_name: row.category_name,
68
+ anchor: row.anchor,
69
+ duration_minutes: row.duration_minutes,
70
+ delta_minutes: row.delta_minutes,
71
+ order_status: row.order_status,
72
+ platform: row.platform,
73
+ created_at: row.created_at,
74
+ source_local_date: DAY,
75
+ })));
76
+ describe('prep time duration population - summary vs item/category', () => {
77
+ it('excludes invalid durations from count, sum, buckets and instant count', () => {
78
+ const formatted = (0, tenant_prep_time_formatter_1.getFormattedTenantPrepTimeReporting)([dayReport]);
79
+ const summary = formatted[0][0].summary;
80
+ // Valid: 8, 30, 20. Invalid: MAX+1 and -3 — measured coverage still counts
81
+ // all five, but the duration population has three observations.
82
+ expect(dayReport.prep_time_counters['COMPLETED:POS_WALK-IN'].coverage.own_anchor_count).toBe(5);
83
+ expect(dayReport.prep_time_counters['COMPLETED:POS_WALK-IN'].duration.count).toBe(3);
84
+ expect(summary.own_anchor_items).toBe(5);
85
+ expect(summary.avg_minutes).toBe(19.3);
86
+ const buckets = dayReport.prep_time_counters['COMPLETED:POS_WALK-IN'].duration.buckets;
87
+ expect(Object.values(buckets).reduce((sum, value) => sum + value, 0)).toBe(3);
88
+ });
89
+ it('has NO split denominator: summary avg equals the item weighted avg', () => {
90
+ const formatted = (0, tenant_prep_time_formatter_1.getFormattedTenantPrepTimeReporting)([dayReport]);
91
+ const summary = formatted[0][0].summary;
92
+ const items = (0, materialized_prep_time_read_helpers_1.groupMaterializedPrepTimeItemRows)(readRows);
93
+ // Weight by each item's VALID duration count (the same population the
94
+ // summary uses). Items whose only durations are invalid have avg null and
95
+ // contribute 0 observations.
96
+ let weightedSum = 0;
97
+ let validCount = 0;
98
+ for (const itemRow of items) {
99
+ const count = readRows.filter((row) => row.item_id === itemRow.item_id &&
100
+ row.anchor === 'MEASURED' &&
101
+ (0, materialized_prep_time_read_helpers_1.isMaterializedPrepTimeDurationCountable)(row.duration_minutes)).length;
102
+ expect(itemRow.measured_count).toBe(readRows.filter((row) => row.item_id === itemRow.item_id).length);
103
+ if (count === 0) {
104
+ expect(itemRow.avg_minutes).toBeNull();
105
+ continue;
106
+ }
107
+ weightedSum += (itemRow.avg_minutes ?? 0) * count;
108
+ validCount += count;
109
+ }
110
+ expect(validCount).toBe(3);
111
+ expect(Math.abs(weightedSum / validCount - summary.avg_minutes)).toBeLessThan(0.1);
112
+ // The invalid-only item is measured but has no duration statistics.
113
+ const invalidOnly = items.find((row) => row.item_id === 'invalid-negative');
114
+ expect(invalidOnly.measured_count).toBe(1);
115
+ expect(invalidOnly.avg_minutes).toBeNull();
116
+ expect(invalidOnly.median_minutes).toBeNull();
117
+ expect(invalidOnly.p90_minutes).toBeNull();
118
+ });
119
+ it('has summary avg equal to the CATEGORY weighted avg for the same population', () => {
120
+ const formatted = (0, tenant_prep_time_formatter_1.getFormattedTenantPrepTimeReporting)([dayReport]);
121
+ const summary = formatted[0][0].summary;
122
+ const categories = (0, materialized_prep_time_read_helpers_1.groupMaterializedPrepTimeCategoryRows)(readRows);
123
+ let weightedSum = 0;
124
+ let validCount = 0;
125
+ for (const category of categories) {
126
+ const count = readRows.filter((row) => row.category_name === category.category_name &&
127
+ row.anchor === 'MEASURED' &&
128
+ (0, materialized_prep_time_read_helpers_1.isMaterializedPrepTimeDurationCountable)(row.duration_minutes)).length;
129
+ weightedSum += (category.avg_minutes ?? 0) * count;
130
+ validCount += count;
131
+ }
132
+ expect(validCount).toBe(3);
133
+ expect(Math.abs(weightedSum / validCount - summary.avg_minutes)).toBeLessThan(0.1);
134
+ });
135
+ });
@@ -76,11 +76,8 @@ describe('buildTenantPrepTimeCloseDayPlan', () => {
76
76
  const periodWrites = plan.writes.filter((write) => write.report.scope_context !== 'localDate');
77
77
  expect(periodWrites.map((write) => write.report.scope_context).sort()).toEqual([
78
78
  'localMonth',
79
- 'localQuarter',
80
- 'localSemester',
81
79
  'localWeek',
82
80
  'localWeekOfYear',
83
- 'localYear',
84
81
  ]);
85
82
  for (const write of periodWrites) {
86
83
  expect(write.report.prep_time_counters[KEY].coverage.item_count).toBe(3);
@@ -174,3 +171,74 @@ describe('buildTenantPrepTimeCloseDayPlan', () => {
174
171
  expect((0, materialized_prep_time_close_day_helpers_1.needsMaterializedPrepTimeReportWrite)(stored, newer)).toBe(true);
175
172
  });
176
173
  });
174
+ /** A day/period entry document (rows are irrelevant to the iteration contract). */
175
+ const entry = (scope, field, over = {}) => ({
176
+ _id: scope + '|' + field,
177
+ tenant_id: 'tenant-a',
178
+ brand_id: 'brand-a',
179
+ scope_context: scope,
180
+ scope_field: field,
181
+ day_index: scope === 'localDate' ? 'MON' : null,
182
+ sale_type_key_filters: ['ORDER_STATUS', 'PLATFORM'],
183
+ prep_time_entries: { [KEY]: { rows: [] } },
184
+ schema_version: 1,
185
+ source_watermark: new Date('2026-09-14T23:00:00.000Z'),
186
+ computed_at: new Date('2026-09-14T23:00:00.000Z'),
187
+ createdAt: new Date('2026-09-14T23:00:00.000Z'),
188
+ updatedAt: new Date('2026-09-14T23:00:00.000Z'),
189
+ ...over,
190
+ });
191
+ describe('buildTenantPrepTimeCloseDayPlan - reclose iterations + force', () => {
192
+ it('ADVANCES the report and entry iterations on a forced reclose', () => {
193
+ const existingDay = report('localDate', '2026-09-14', {
194
+ coverage: { item_count: 2 },
195
+ iteration: 4,
196
+ });
197
+ const existingDayEntry = entry('localDate', '2026-09-14', { iteration: 6 });
198
+ const plan = build({
199
+ reports: [report('localDate', '2026-09-14', { coverage: { item_count: 3 } })],
200
+ entries: [entry('localDate', '2026-09-14')],
201
+ existing: [existingDay],
202
+ existingEntries: [existingDayEntry],
203
+ reason: 'RECLOSE',
204
+ force: true,
205
+ });
206
+ const dayWrite = plan.writes.find((write) => write.report.scope_context === 'localDate');
207
+ expect(dayWrite.report.iteration).toBe(5);
208
+ expect(dayWrite.iteration.iteration).toBe(5);
209
+ const dayEntryWrite = plan.entryWrites.find((write) => write.report.scope_context === 'localDate');
210
+ expect(dayEntryWrite.report.iteration).toBe(7);
211
+ expect(dayEntryWrite.iteration.iteration).toBe(7);
212
+ });
213
+ it('SKIPS an unchanged reclose without force and writes nothing', () => {
214
+ const existingDay = report('localDate', '2026-09-14', {
215
+ coverage: { item_count: 3 },
216
+ iteration: 4,
217
+ source_watermark: new Date('2026-09-14T23:05:00.000Z'),
218
+ });
219
+ const existingDayEntry = entry('localDate', '2026-09-14', {
220
+ iteration: 6,
221
+ source_watermark: new Date('2026-09-14T23:05:00.000Z'),
222
+ });
223
+ const plan = build({
224
+ reports: [
225
+ report('localDate', '2026-09-14', {
226
+ coverage: { item_count: 3 },
227
+ source_watermark: new Date('2026-09-14T23:00:00.000Z'),
228
+ }),
229
+ ],
230
+ entries: [
231
+ entry('localDate', '2026-09-14', {
232
+ source_watermark: new Date('2026-09-14T23:00:00.000Z'),
233
+ }),
234
+ ],
235
+ existing: [existingDay],
236
+ existingEntries: [existingDayEntry],
237
+ reason: 'RECLOSE',
238
+ });
239
+ expect(plan.writes.some((write) => write.report.scope_context === 'localDate')).toBe(false);
240
+ expect(plan.entryWrites.some((write) => write.report.scope_context === 'localDate')).toBe(false);
241
+ expect(plan.skippedDocuments.length).toBeGreaterThan(0);
242
+ expect(plan.skippedEntryDocuments.length).toBeGreaterThan(0);
243
+ });
244
+ });