@managesome/eposmatic-toolkit 0.4.35 → 0.4.37

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 (20) hide show
  1. package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +28 -9
  2. package/esm/helpers/materialized-reports/materialized-prep-time-read-helpers.js +5 -14
  3. package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +3 -1
  4. package/esm/schemas/models/materialized_tenant_prep_time_entries.js +1 -0
  5. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.d.ts +10 -2
  6. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +28 -9
  7. package/lib/helpers/materialized-reports/materialized-prep-time-read-helpers.js +5 -14
  8. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +3 -1
  9. package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +3 -0
  10. package/lib/schemas/models/materialized_tenant_prep_time_entries.js +1 -0
  11. package/lib/schemas/types/materialized_tenant_prep_time_entries.d.ts +3 -0
  12. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +9 -2
  13. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +1 -5
  14. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.js +12 -2
  15. package/lib/tests/materialized-reports/materialized-prep-time-duration-consistency.test.js +7 -5
  16. package/lib/tests/materialized-reports/materialized-prep-time-identity.test.d.ts +1 -0
  17. package/lib/tests/materialized-reports/materialized-prep-time-identity.test.js +155 -0
  18. package/lib/tests/materialized-reports/materialized-prep-time-read-helpers.test.js +8 -13
  19. package/lib/tests/materialized-reports/materialized-report-drilldown-rows.test.js +16 -2
  20. package/package.json +1 -1
@@ -96,8 +96,21 @@ export function resolveMaterializedPrepTimeTarget(order, tenantDefaultMinutes) {
96
96
  }
97
97
  return { target_at: null, target_source: MATERIALIZED_PREP_TIME_TARGET_ENUM.NONE };
98
98
  }
99
- /** Stable identity of a row inside its order - part of the page cursor. */
100
- export const buildMaterializedPrepTimeEntryKey = (itemId) => 'item:' + String(itemId ?? 'unknown');
99
+ /**
100
+ * Stable identity of ONE served item row inside its order - part of the page
101
+ * cursor and the flat projection identity.
102
+ *
103
+ * The catalogue item id is the GROUPING key; the order-line id keeps two lines
104
+ * of the same catalogue item distinct. A line is appended only when it differs
105
+ * from the catalogue id, so the common (and legacy) one-line shape is stable.
106
+ */
107
+ export const buildMaterializedPrepTimeEntryKey = (itemId, lineId) => {
108
+ const parts = [String(itemId ?? 'unknown')];
109
+ if (lineId != null && String(lineId) !== '' && String(lineId) !== String(itemId ?? '')) {
110
+ parts.push(String(lineId));
111
+ }
112
+ return 'item:' + parts.join(':');
113
+ };
101
114
  /**
102
115
  * Accumulates ONE order's items into the prep counters + drill-down rows.
103
116
  *
@@ -191,12 +204,17 @@ export function accumulateOrderPrepTime({ order, context, tenantDefaultMinutes =
191
204
  else {
192
205
  counters.promise.no_target_count += 1;
193
206
  }
194
- const itemId = item?._id != null ? String(item._id) : null;
207
+ // CATALOGUE item id: the grouping identity the Items tab reads. The
208
+ // order-line id is a DIFFERENT identity and is preserved as line_id, so
209
+ // two lines of one menu item remain two served events.
210
+ const lineId = item?._id != null ? String(item._id) : null;
211
+ const catalogueItemId = item?.item_id != null && String(item.item_id) !== '' ? String(item.item_id) : lineId;
195
212
  const row = {
196
- entry_key: buildMaterializedPrepTimeEntryKey(itemId),
213
+ entry_key: buildMaterializedPrepTimeEntryKey(catalogueItemId, lineId),
197
214
  order_id: orderId,
198
215
  order_number: order.order_number ?? null,
199
- item_id: itemId,
216
+ item_id: catalogueItemId,
217
+ line_id: lineId,
200
218
  item_name: item?.name != null ? String(item.name) : null,
201
219
  category_name: category?.name != null ? String(category.name) : null,
202
220
  anchor,
@@ -214,10 +232,11 @@ export function accumulateOrderPrepTime({ order, context, tenantDefaultMinutes =
214
232
  created_at: createdAt,
215
233
  local_hour: context.local_hour_label,
216
234
  };
217
- // Only items with a prep signal become rows: an untouched item has nothing
218
- // to inspect, and the drill-down exists to explain the numbers.
219
- if (minutes != null || prepStartedValid)
220
- entries.push(row);
235
+ // EVERY served item becomes a row - including an item with no prep
236
+ // signal. Coverage uses ALL served items as its denominator; a no-signal
237
+ // row is an UNMEASURED row with null duration and is visible in the
238
+ // Unmeasured view.
239
+ entries.push(row);
221
240
  }
222
241
  }
223
242
  if (tracked > 0)
@@ -178,9 +178,7 @@ export function matchesMaterializedPrepTimeReadFilters(row, filters) {
178
178
  if (platforms != null && platforms.length > 0 && !platforms.includes(asString(row.platform))) {
179
179
  return false;
180
180
  }
181
- if (orderStatuses != null &&
182
- orderStatuses.length > 0 &&
183
- !orderStatuses.includes(asString(row.order_status))) {
181
+ if (orderStatuses != null && orderStatuses.length > 0 && !orderStatuses.includes(asString(row.order_status))) {
184
182
  return false;
185
183
  }
186
184
  if (categoryNames != null && categoryNames.length > 0) {
@@ -420,8 +418,7 @@ export function groupMaterializedPrepTimeItemRows(rows, filters) {
420
418
  }
421
419
  /** True when `value` is one of the frozen item sort modes. */
422
420
  export function isMaterializedPrepTimeItemSort(value) {
423
- return (typeof value === 'string' &&
424
- ALL_MATERIALIZED_PREP_TIME_ITEM_SORTS.includes(value));
421
+ return typeof value === 'string' && ALL_MATERIALIZED_PREP_TIME_ITEM_SORTS.includes(value);
425
422
  }
426
423
  /** The sort value of one item row (the cursor stores it verbatim). */
427
424
  export function getMaterializedPrepTimeItemSortValue(row, sort) {
@@ -478,9 +475,7 @@ export function compareMaterializedPrepTimeItemRows(a, b, sort) {
478
475
  }
479
476
  /** Stable sort (never mutates the input array). */
480
477
  export function sortMaterializedPrepTimeItemRows(rows, sort) {
481
- const mode = isMaterializedPrepTimeItemSort(sort)
482
- ? sort
483
- : DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
478
+ const mode = isMaterializedPrepTimeItemSort(sort) ? sort : DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
484
479
  return (Array.isArray(rows) ? rows : []).slice().sort((a, b) => {
485
480
  const compared = compareSortValues(getMaterializedPrepTimeItemSortValue(a, mode), getMaterializedPrepTimeItemSortValue(b, mode), mode);
486
481
  if (compared !== 0)
@@ -499,9 +494,7 @@ const compareItemToCursor = (row, sort, cursor) => {
499
494
  * row can be duplicated or skipped between pages.
500
495
  */
501
496
  export function getMaterializedPrepTimeItemPage(rows, sort, cursor, limit = 25) {
502
- const mode = isMaterializedPrepTimeItemSort(sort)
503
- ? sort
504
- : DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
497
+ const mode = isMaterializedPrepTimeItemSort(sort) ? sort : DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
505
498
  const size = Number.isFinite(limit) && limit > 0 ? Math.floor(limit) : 0;
506
499
  const all = Array.isArray(rows) ? rows : [];
507
500
  const remaining = cursor == null ? all.slice() : all.filter((row) => compareItemToCursor(row, mode, cursor) > 0);
@@ -525,9 +518,7 @@ export function getMaterializedPrepTimeItemPage(rows, sort, cursor, limit = 25)
525
518
  export function buildMaterializedPrepTimeItemsPage(args) {
526
519
  const filtered = applyMaterializedPrepTimeReadFilters(args.rows, args.filters);
527
520
  const coverage = buildMaterializedPrepTimeCoverage(applyMaterializedPrepTimeReadFilters(args.rows, withoutMaterializedPrepTimeAnchorFilter(args.filters)));
528
- const sort = isMaterializedPrepTimeItemSort(args.sort)
529
- ? args.sort
530
- : DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
521
+ const sort = isMaterializedPrepTimeItemSort(args.sort) ? args.sort : DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
531
522
  const grouped = groupMaterializedPrepTimeItemRows(filtered);
532
523
  const sorted = sortMaterializedPrepTimeItemRows(grouped, sort);
533
524
  const page = getMaterializedPrepTimeItemPage(sorted, sort, args.cursor ?? null, args.limit ?? 25);
@@ -25,7 +25,9 @@ const ROW_SPECS = {
25
25
  rowsKey: 'prep_time_entries',
26
26
  time: (row) => row.created_at,
27
27
  orderId: (row) => row.order_id,
28
- cursorId: (row) => row.item_id,
28
+ // ROW identity, not the catalogue item id: two lines of the same menu item
29
+ // in one order must both survive the unique flat-row identity.
30
+ cursorId: (row) => row.entry_key,
29
31
  },
30
32
  [MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES]: {
31
33
  rowsKey: 'cash_entries',
@@ -27,6 +27,7 @@ const PrepTimeEntrySchema = new Schema({
27
27
  order_id: { type: String, required: true },
28
28
  order_number: { type: Number, default: null },
29
29
  item_id: { type: String, default: null },
30
+ line_id: { type: String, default: null },
30
31
  item_name: { type: String, default: null },
31
32
  category_name: { type: String, default: null },
32
33
  // ---- measurement ----
@@ -36,6 +36,7 @@ export interface IPrepTimeOrderSource {
36
36
  name?: string | null;
37
37
  items?: {
38
38
  _id?: string;
39
+ item_id?: string | null;
39
40
  name?: string | null;
40
41
  is_ready?: boolean;
41
42
  ready_minutes?: number | string | null;
@@ -76,8 +77,15 @@ export declare function resolveMaterializedPrepTimeTarget(order: Pick<IPrepTimeO
76
77
  target_at: Date | null;
77
78
  target_source: MATERIALIZED_PREP_TIME_TARGET_ENUM;
78
79
  };
79
- /** Stable identity of a row inside its order - part of the page cursor. */
80
- export declare const buildMaterializedPrepTimeEntryKey: (itemId: string | null) => string;
80
+ /**
81
+ * Stable identity of ONE served item row inside its order - part of the page
82
+ * cursor and the flat projection identity.
83
+ *
84
+ * The catalogue item id is the GROUPING key; the order-line id keeps two lines
85
+ * of the same catalogue item distinct. A line is appended only when it differs
86
+ * from the catalogue id, so the common (and legacy) one-line shape is stable.
87
+ */
88
+ export declare const buildMaterializedPrepTimeEntryKey: (itemId: string | null, lineId?: string | null) => string;
81
89
  export interface IAccumulateOrderPrepTimeResult {
82
90
  counters: IMaterializedPrepTimeCounters;
83
91
  entries: IMaterializedPrepTimeEntry[];
@@ -104,8 +104,21 @@ function resolveMaterializedPrepTimeTarget(order, tenantDefaultMinutes) {
104
104
  }
105
105
  return { target_at: null, target_source: MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_TARGET_ENUM.NONE };
106
106
  }
107
- /** Stable identity of a row inside its order - part of the page cursor. */
108
- const buildMaterializedPrepTimeEntryKey = (itemId) => 'item:' + String(itemId ?? 'unknown');
107
+ /**
108
+ * Stable identity of ONE served item row inside its order - part of the page
109
+ * cursor and the flat projection identity.
110
+ *
111
+ * The catalogue item id is the GROUPING key; the order-line id keeps two lines
112
+ * of the same catalogue item distinct. A line is appended only when it differs
113
+ * from the catalogue id, so the common (and legacy) one-line shape is stable.
114
+ */
115
+ const buildMaterializedPrepTimeEntryKey = (itemId, lineId) => {
116
+ const parts = [String(itemId ?? 'unknown')];
117
+ if (lineId != null && String(lineId) !== '' && String(lineId) !== String(itemId ?? '')) {
118
+ parts.push(String(lineId));
119
+ }
120
+ return 'item:' + parts.join(':');
121
+ };
109
122
  exports.buildMaterializedPrepTimeEntryKey = buildMaterializedPrepTimeEntryKey;
110
123
  /**
111
124
  * Accumulates ONE order's items into the prep counters + drill-down rows.
@@ -200,12 +213,17 @@ function accumulateOrderPrepTime({ order, context, tenantDefaultMinutes = null,
200
213
  else {
201
214
  counters.promise.no_target_count += 1;
202
215
  }
203
- const itemId = item?._id != null ? String(item._id) : null;
216
+ // CATALOGUE item id: the grouping identity the Items tab reads. The
217
+ // order-line id is a DIFFERENT identity and is preserved as line_id, so
218
+ // two lines of one menu item remain two served events.
219
+ const lineId = item?._id != null ? String(item._id) : null;
220
+ const catalogueItemId = item?.item_id != null && String(item.item_id) !== '' ? String(item.item_id) : lineId;
204
221
  const row = {
205
- entry_key: (0, exports.buildMaterializedPrepTimeEntryKey)(itemId),
222
+ entry_key: (0, exports.buildMaterializedPrepTimeEntryKey)(catalogueItemId, lineId),
206
223
  order_id: orderId,
207
224
  order_number: order.order_number ?? null,
208
- item_id: itemId,
225
+ item_id: catalogueItemId,
226
+ line_id: lineId,
209
227
  item_name: item?.name != null ? String(item.name) : null,
210
228
  category_name: category?.name != null ? String(category.name) : null,
211
229
  anchor,
@@ -223,10 +241,11 @@ function accumulateOrderPrepTime({ order, context, tenantDefaultMinutes = null,
223
241
  created_at: createdAt,
224
242
  local_hour: context.local_hour_label,
225
243
  };
226
- // Only items with a prep signal become rows: an untouched item has nothing
227
- // to inspect, and the drill-down exists to explain the numbers.
228
- if (minutes != null || prepStartedValid)
229
- entries.push(row);
244
+ // EVERY served item becomes a row - including an item with no prep
245
+ // signal. Coverage uses ALL served items as its denominator; a no-signal
246
+ // row is an UNMEASURED row with null duration and is visible in the
247
+ // Unmeasured view.
248
+ entries.push(row);
230
249
  }
231
250
  }
232
251
  if (tracked > 0)
@@ -205,9 +205,7 @@ function matchesMaterializedPrepTimeReadFilters(row, filters) {
205
205
  if (platforms != null && platforms.length > 0 && !platforms.includes(asString(row.platform))) {
206
206
  return false;
207
207
  }
208
- if (orderStatuses != null &&
209
- orderStatuses.length > 0 &&
210
- !orderStatuses.includes(asString(row.order_status))) {
208
+ if (orderStatuses != null && orderStatuses.length > 0 && !orderStatuses.includes(asString(row.order_status))) {
211
209
  return false;
212
210
  }
213
211
  if (categoryNames != null && categoryNames.length > 0) {
@@ -448,8 +446,7 @@ function groupMaterializedPrepTimeItemRows(rows, filters) {
448
446
  }
449
447
  /** True when `value` is one of the frozen item sort modes. */
450
448
  function isMaterializedPrepTimeItemSort(value) {
451
- return (typeof value === 'string' &&
452
- materialized_reports_apis_1.ALL_MATERIALIZED_PREP_TIME_ITEM_SORTS.includes(value));
449
+ return typeof value === 'string' && materialized_reports_apis_1.ALL_MATERIALIZED_PREP_TIME_ITEM_SORTS.includes(value);
453
450
  }
454
451
  /** The sort value of one item row (the cursor stores it verbatim). */
455
452
  function getMaterializedPrepTimeItemSortValue(row, sort) {
@@ -506,9 +503,7 @@ function compareMaterializedPrepTimeItemRows(a, b, sort) {
506
503
  }
507
504
  /** Stable sort (never mutates the input array). */
508
505
  function sortMaterializedPrepTimeItemRows(rows, sort) {
509
- const mode = isMaterializedPrepTimeItemSort(sort)
510
- ? sort
511
- : materialized_reports_apis_1.DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
506
+ const mode = isMaterializedPrepTimeItemSort(sort) ? sort : materialized_reports_apis_1.DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
512
507
  return (Array.isArray(rows) ? rows : []).slice().sort((a, b) => {
513
508
  const compared = compareSortValues(getMaterializedPrepTimeItemSortValue(a, mode), getMaterializedPrepTimeItemSortValue(b, mode), mode);
514
509
  if (compared !== 0)
@@ -527,9 +522,7 @@ const compareItemToCursor = (row, sort, cursor) => {
527
522
  * row can be duplicated or skipped between pages.
528
523
  */
529
524
  function getMaterializedPrepTimeItemPage(rows, sort, cursor, limit = 25) {
530
- const mode = isMaterializedPrepTimeItemSort(sort)
531
- ? sort
532
- : materialized_reports_apis_1.DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
525
+ const mode = isMaterializedPrepTimeItemSort(sort) ? sort : materialized_reports_apis_1.DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
533
526
  const size = Number.isFinite(limit) && limit > 0 ? Math.floor(limit) : 0;
534
527
  const all = Array.isArray(rows) ? rows : [];
535
528
  const remaining = cursor == null ? all.slice() : all.filter((row) => compareItemToCursor(row, mode, cursor) > 0);
@@ -553,9 +546,7 @@ function getMaterializedPrepTimeItemPage(rows, sort, cursor, limit = 25) {
553
546
  function buildMaterializedPrepTimeItemsPage(args) {
554
547
  const filtered = applyMaterializedPrepTimeReadFilters(args.rows, args.filters);
555
548
  const coverage = buildMaterializedPrepTimeCoverage(applyMaterializedPrepTimeReadFilters(args.rows, (0, exports.withoutMaterializedPrepTimeAnchorFilter)(args.filters)));
556
- const sort = isMaterializedPrepTimeItemSort(args.sort)
557
- ? args.sort
558
- : materialized_reports_apis_1.DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
549
+ const sort = isMaterializedPrepTimeItemSort(args.sort) ? args.sort : materialized_reports_apis_1.DEFAULT_MATERIALIZED_PREP_TIME_ITEM_SORT;
559
550
  const grouped = groupMaterializedPrepTimeItemRows(filtered);
560
551
  const sorted = sortMaterializedPrepTimeItemRows(grouped, sort);
561
552
  const page = getMaterializedPrepTimeItemPage(sorted, sort, args.cursor ?? null, args.limit ?? 25);
@@ -31,7 +31,9 @@ const ROW_SPECS = {
31
31
  rowsKey: 'prep_time_entries',
32
32
  time: (row) => row.created_at,
33
33
  orderId: (row) => row.order_id,
34
- cursorId: (row) => row.item_id,
34
+ // ROW identity, not the catalogue item id: two lines of the same menu item
35
+ // in one order must both survive the unique flat-row identity.
36
+ cursorId: (row) => row.entry_key,
35
37
  },
36
38
  [MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES]: {
37
39
  rowsKey: 'cash_entries',
@@ -37,6 +37,7 @@ declare const PrepTimeEntrySchema: Schema<any, import("mongoose").Model<any, any
37
37
  item_name: string;
38
38
  created_at: NativeDate;
39
39
  local_hour: string;
40
+ line_id: string;
40
41
  category_name: string;
41
42
  duration_minutes: number;
42
43
  ready_at: NativeDate;
@@ -58,6 +59,7 @@ declare const PrepTimeEntrySchema: Schema<any, import("mongoose").Model<any, any
58
59
  item_name: string;
59
60
  created_at: NativeDate;
60
61
  local_hour: string;
62
+ line_id: string;
61
63
  category_name: string;
62
64
  duration_minutes: number;
63
65
  ready_at: NativeDate;
@@ -79,6 +81,7 @@ declare const PrepTimeEntrySchema: Schema<any, import("mongoose").Model<any, any
79
81
  item_name: string;
80
82
  created_at: NativeDate;
81
83
  local_hour: string;
84
+ line_id: string;
82
85
  category_name: string;
83
86
  duration_minutes: number;
84
87
  ready_at: NativeDate;
@@ -30,6 +30,7 @@ const PrepTimeEntrySchema = new mongoose_1.Schema({
30
30
  order_id: { type: String, required: true },
31
31
  order_number: { type: Number, default: null },
32
32
  item_id: { type: String, default: null },
33
+ line_id: { type: String, default: null },
33
34
  item_name: { type: String, default: null },
34
35
  category_name: { type: String, default: null },
35
36
  // ---- measurement ----
@@ -23,7 +23,10 @@ export interface IMaterializedPrepTimeEntry {
23
23
  entry_key: string;
24
24
  order_id: string;
25
25
  order_number?: number | null;
26
+ /** CATALOGUE/menu item id - the grouping identity of the Items read. */
26
27
  item_id: string | null;
28
+ /** ORDER-LINE id - the row/event identity inside the order. */
29
+ line_id: string | null;
27
30
  item_name: string | null;
28
31
  category_name: string | null;
29
32
  /** Which population this row belongs to - the split that makes the report correct. */
@@ -263,7 +263,7 @@ describe('accumulateOrderPrepTime - promise grading', () => {
263
263
  });
264
264
  });
265
265
  describe('accumulateOrderPrepTime - drill-down rows', () => {
266
- it('emits a row per item WITH a signal, each stating its anchor', () => {
266
+ it('emits a row for EVERY served item, each stating its anchor', () => {
267
267
  const result = (0, materialized_prep_time_classification_helpers_1.accumulateOrderPrepTime)({
268
268
  order: order({
269
269
  categories: [
@@ -279,11 +279,18 @@ describe('accumulateOrderPrepTime - drill-down rows', () => {
279
279
  }),
280
280
  context,
281
281
  });
282
- expect(result.entries).toHaveLength(2);
282
+ expect(result.entries).toHaveLength(3);
283
283
  const measured = result.entries.find((row) => row.item_id === 'm1');
284
284
  const fallback = result.entries.find((row) => row.item_id === 'f1');
285
+ const untracked = result.entries.find((row) => row.item_id === 'nothing');
285
286
  expect(measured.anchor).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ANCHOR_ENUM.MEASURED);
286
287
  expect(fallback.anchor).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ANCHOR_ENUM.UNMEASURED);
288
+ // No prep signal still becomes a ROW: the Items/Series coverage must use the
289
+ // same served population as the Summary.
290
+ expect(untracked.anchor).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ANCHOR_ENUM.UNMEASURED);
291
+ expect(untracked.duration_minutes).toBeNull();
292
+ expect(untracked.ready_at).toBeNull();
293
+ expect(untracked.delta_minutes).toBeNull();
287
294
  // The ready instant is RECONSTRUCTED for the measured row only.
288
295
  expect(measured.ready_at?.toISOString()).toBe('2026-09-13T10:09:00.000Z');
289
296
  expect(fallback.ready_at).toBeNull();
@@ -74,11 +74,7 @@ describe('day vs period scope classification', () => {
74
74
  'localWeekOfYear',
75
75
  'localYear',
76
76
  ]);
77
- expect(materialized_prep_time_close_day_helpers_1.MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES.sort()).toEqual([
78
- 'localMonth',
79
- 'localWeek',
80
- 'localWeekOfYear',
81
- ]);
77
+ expect(materialized_prep_time_close_day_helpers_1.MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES.sort()).toEqual(['localMonth', 'localWeek', 'localWeekOfYear']);
82
78
  });
83
79
  it('enumerates the FULL report sibling days (through year) separately from the item days', () => {
84
80
  // Item period days stay month-bounded (Sep 1..30 for this Monday).
@@ -185,8 +185,18 @@ describe('compileTenantPrepTimeReports', () => {
185
185
  expect(counters.coverage.tracked_count).toBe(0);
186
186
  expect(counters.coverage.own_anchor_count).toBe(0);
187
187
  expect(counters.duration.count).toBe(0);
188
- // No rows: an untouched item has nothing to inspect.
189
- expect(result.entries).toEqual([]);
188
+ // One row per served item, even with no prep signal: the entry population
189
+ // must match the Summary's coverage denominator.
190
+ const dateEntries = result.entries.find((doc) => doc.scope_context === 'localDate');
191
+ const rows = dateEntries.prep_time_entries['COMPLETED:POS_WALK-IN'].rows;
192
+ expect(rows).toHaveLength(1);
193
+ expect(rows[0]).toMatchObject({
194
+ anchor: 'UNMEASURED',
195
+ duration_minutes: null,
196
+ prep_started_at: null,
197
+ ready_at: null,
198
+ delta_minutes: null,
199
+ });
190
200
  });
191
201
  it('counts a served-but-unobserved item in coverage but NOT in duration', () => {
192
202
  const unobserved = order({
@@ -44,7 +44,11 @@ const order = () => ({
44
44
  },
45
45
  {
46
46
  name: 'Other',
47
- items: [item('b', 20), item('invalid-high', MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_MAX_MINUTES + 1), item('invalid-negative', -3)],
47
+ items: [
48
+ item('b', 20),
49
+ item('invalid-high', MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_MAX_MINUTES + 1),
50
+ item('invalid-negative', -3),
51
+ ],
48
52
  },
49
53
  ],
50
54
  });
@@ -55,10 +59,8 @@ const compiled = (0, materialized_prep_time_compile_helpers_1.compileTenantPrepT
55
59
  orders: [order()],
56
60
  computedAt: new Date(`${DAY}T23:00:00.000Z`),
57
61
  });
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 dayReport = compiled.reports.find((report) => report.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE && report.scope_field === DAY);
63
+ const dayEntry = compiled.entries.find((entry) => entry.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE && entry.scope_field === DAY);
62
64
  const readRows = Object.values(dayEntry.prep_time_entries ?? {}).flatMap((section) => (section.rows ?? []).map((row) => ({
63
65
  tenant_id: TENANT,
64
66
  order_id: row.order_id,
@@ -0,0 +1,155 @@
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 materialized_prep_time_read_helpers_1 = require("../../helpers/materialized-reports/materialized-prep-time-read-helpers");
6
+ const DAY = '2026-09-20';
7
+ const AT = new Date(`${DAY}T10:00:00.000Z`);
8
+ const order = (id, items) => ({
9
+ _id: id,
10
+ tenant_id: 'tenant-1',
11
+ order_number: 1,
12
+ order_status: 'COMPLETED',
13
+ platform: 'POS_WALK-IN',
14
+ localDate: DAY,
15
+ localHour: 10,
16
+ createdAt: AT,
17
+ updatedAt: AT,
18
+ categories: [{ name: 'Mains', items }],
19
+ });
20
+ const item = (lineId, catalogueId, minutes, prepStarted) => ({
21
+ _id: lineId,
22
+ item_id: catalogueId,
23
+ name: 'Burger',
24
+ is_ready: true,
25
+ ready_minutes: minutes,
26
+ prep_started_at: prepStarted ? AT : null,
27
+ });
28
+ const readRows = (orders) => {
29
+ const compiled = (0, materialized_prep_time_compile_helpers_1.compileTenantPrepTimeReports)({
30
+ tenantId: 'tenant-1',
31
+ brandId: 'brand-1',
32
+ localDate: DAY,
33
+ orders,
34
+ computedAt: new Date(`${DAY}T23:00:00.000Z`),
35
+ });
36
+ const dayEntry = compiled.entries.find((entry) => entry.scope_context === 'localDate');
37
+ const rows = [];
38
+ for (const section of Object.values(dayEntry.prep_time_entries ?? {})) {
39
+ for (const row of section.rows) {
40
+ rows.push({
41
+ tenant_id: 'tenant-1',
42
+ order_id: row.order_id,
43
+ item_id: row.item_id,
44
+ item_name: row.item_name,
45
+ category_name: row.category_name,
46
+ anchor: row.anchor,
47
+ duration_minutes: row.duration_minutes,
48
+ delta_minutes: row.delta_minutes,
49
+ order_status: row.order_status,
50
+ platform: row.platform,
51
+ created_at: row.created_at,
52
+ source_local_date: DAY,
53
+ });
54
+ }
55
+ }
56
+ return { compiled, rows };
57
+ };
58
+ describe('prep time catalogue identity and coverage population', () => {
59
+ it('groups two order lines of one catalogue item into one Item, with distinct orders', () => {
60
+ const first = order('order-a', [
61
+ item('line-a', 'menu-burger', 5, true),
62
+ item('line-b', 'menu-burger', null, false),
63
+ ]);
64
+ const second = order('order-b', [item('line-c', 'menu-burger', 7, true)]);
65
+ const { rows } = readRows([first, second]);
66
+ const page = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeItemsPage)({
67
+ rows,
68
+ filters: { anchor: MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ANCHOR_ENUM.MEASURED },
69
+ sort: 'late_desc',
70
+ });
71
+ expect(page.rows).toHaveLength(1);
72
+ // Rows follow the LISTED anchor view (MEASURED), so this row's served
73
+ // population is the measured subset; coverage spans both anchors.
74
+ expect(page.rows[0]).toMatchObject({
75
+ item_id: 'menu-burger',
76
+ orders_count: 2,
77
+ served_count: 2,
78
+ measured_count: 2,
79
+ unmeasured_count: 0,
80
+ avg_minutes: 6,
81
+ });
82
+ expect(page.coverage).toEqual({
83
+ served_count: 3,
84
+ measured_count: 2,
85
+ unmeasured_count: 1,
86
+ coverage_percent: 66.7,
87
+ });
88
+ const unmeasured = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeItemsPage)({
89
+ rows,
90
+ filters: { anchor: MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ANCHOR_ENUM.UNMEASURED },
91
+ sort: 'late_desc',
92
+ });
93
+ expect(unmeasured.rows).toHaveLength(1);
94
+ expect(unmeasured.rows[0]).toMatchObject({
95
+ item_id: 'menu-burger',
96
+ served_count: 1,
97
+ measured_count: 0,
98
+ unmeasured_count: 1,
99
+ avg_minutes: null,
100
+ on_time_percent: null,
101
+ });
102
+ // Measured + Unmeasured row populations add back to the coverage denominator.
103
+ expect(page.rows[0].served_count + unmeasured.rows[0].served_count).toBe(page.coverage.served_count);
104
+ });
105
+ it('keeps all three events distinct for the item drill-down and uses the catalogue item_id', () => {
106
+ const first = order('order-a', [
107
+ item('line-a', 'menu-burger', 5, true),
108
+ item('line-b', 'menu-burger', null, false),
109
+ ]);
110
+ const second = order('order-b', [item('line-c', 'menu-burger', 7, true)]);
111
+ const { compiled } = readRows([first, second]);
112
+ const dayEntry = compiled.entries.find((entry) => entry.scope_context === 'localDate');
113
+ const events = Object.values(dayEntry.prep_time_entries ?? {}).flatMap((section) => section.rows);
114
+ expect(events.map((event) => event.item_id)).toEqual(['menu-burger', 'menu-burger', 'menu-burger']);
115
+ expect(events.map((event) => event.order_id)).toEqual(['order-a', 'order-a', 'order-b']);
116
+ expect(new Set(events.map((event) => event.entry_key)).size).toBe(3);
117
+ expect(events.map((event) => event.line_id)).toEqual(['line-a', 'line-b', 'line-c']);
118
+ });
119
+ it('makes Items, Categories and Series coverage agree on a no-signal population', () => {
120
+ const mixed = order('order-a', [
121
+ item('line-a', 'menu-burger', 5, true),
122
+ item('line-b', 'menu-fries', null, false),
123
+ item('line-c', 'menu-cola', null, false),
124
+ ]);
125
+ const { compiled, rows } = readRows([mixed]);
126
+ const dayReport = compiled.reports.find((report) => report.scope_context === 'localDate');
127
+ const counters = dayReport.prep_time_counters['COMPLETED:POS_WALK-IN'];
128
+ expect(counters.coverage).toMatchObject({ item_count: 3, own_anchor_count: 1 });
129
+ const items = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeItemsPage)({
130
+ rows,
131
+ filters: { anchor: MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ANCHOR_ENUM.MEASURED },
132
+ sort: 'late_desc',
133
+ });
134
+ const categories = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeCategoriesResult)({ rows });
135
+ const series = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeSeriesResult)({ rows });
136
+ const expected = {
137
+ served_count: 3,
138
+ measured_count: 1,
139
+ unmeasured_count: 2,
140
+ coverage_percent: 33.3,
141
+ };
142
+ expect(items.coverage).toEqual(expected);
143
+ expect(categories.coverage).toEqual(expected);
144
+ expect(series.coverage).toEqual(expected);
145
+ expect(items.rows).toHaveLength(1);
146
+ expect(items.rows[0]).toMatchObject({ served_count: 1, measured_count: 1 });
147
+ const unmeasured = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeItemsPage)({
148
+ rows,
149
+ filters: { anchor: MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ANCHOR_ENUM.UNMEASURED },
150
+ sort: 'late_desc',
151
+ });
152
+ expect(unmeasured.rows).toHaveLength(2);
153
+ expect(unmeasured.rows.reduce((total, row) => total + row.served_count, 0)).toBe(2);
154
+ });
155
+ });
@@ -432,12 +432,12 @@ describe('prep time item sorting + cursor paging', () => {
432
432
  }
433
433
  });
434
434
  it('breaks exact sort-value ties by item_id ascending', () => {
435
- const tied = [
436
- items()[0],
437
- { ...items()[0], item_id: 'i-0' },
438
- { ...items()[0], item_id: 'i-z' },
439
- ];
440
- expect((0, materialized_prep_time_read_helpers_1.sortMaterializedPrepTimeItemRows)(tied, 'late_desc').map((item) => item.item_id)).toEqual(['i-0', 'i-a', 'i-z']);
435
+ const tied = [items()[0], { ...items()[0], item_id: 'i-0' }, { ...items()[0], item_id: 'i-z' }];
436
+ expect((0, materialized_prep_time_read_helpers_1.sortMaterializedPrepTimeItemRows)(tied, 'late_desc').map((item) => item.item_id)).toEqual([
437
+ 'i-0',
438
+ 'i-a',
439
+ 'i-z',
440
+ ]);
441
441
  });
442
442
  it('never mutates the input rows when sorting', () => {
443
443
  const input = items();
@@ -527,9 +527,7 @@ describe('prep time categories', () => {
527
527
  });
528
528
  it('returns null stats for a measured-empty category', () => {
529
529
  const result = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeCategoriesResult)({
530
- rows: [
531
- row({ anchor: 'UNMEASURED', duration_minutes: null, category_name: 'Cold' }),
532
- ],
530
+ rows: [row({ anchor: 'UNMEASURED', duration_minutes: null, category_name: 'Cold' })],
533
531
  });
534
532
  expect(result.rows[0].avg_minutes).toBeNull();
535
533
  expect(result.rows[0].median_minutes).toBeNull();
@@ -568,10 +566,7 @@ describe('prep time series', () => {
568
566
  ];
569
567
  it('groups by source local date ascending and never emits empty dates', () => {
570
568
  const result = (0, materialized_prep_time_read_helpers_1.buildMaterializedPrepTimeSeriesResult)({ rows });
571
- expect(result.points.map((point) => point.local_date)).toEqual([
572
- '2026-09-10',
573
- '2026-09-11',
574
- ]);
569
+ expect(result.points.map((point) => point.local_date)).toEqual(['2026-09-10', '2026-09-11']);
575
570
  expect(result.points[0]).toMatchObject({
576
571
  served_count: 2,
577
572
  measured_count: 1,
@@ -34,14 +34,28 @@ describe('flat drill-down row projection', () => {
34
34
  expect(result.rows[0].time).toEqual(new Date('2026-09-14T10:00:00.000Z'));
35
35
  expect(result.rows[0].data).toMatchObject({ kind: 'DISCOUNT' });
36
36
  });
37
- test('projects prep rows with item_id as the cursor tie-break', () => {
37
+ test('projects prep rows with the row entry_key as the cursor tie-break', () => {
38
38
  const result = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PREP_TIME_ENTRIES, {
39
39
  ...base,
40
40
  prep_time_entries: {
41
41
  K: { rows: [row({ item_id: 'item-1', entry_key: 'prep:1' })] },
42
42
  },
43
43
  });
44
- expect(result.rows[0].cursor_id).toBe('item-1');
44
+ expect(result.rows[0].cursor_id).toBe('prep:1');
45
+ });
46
+ test('keeps two order lines of one catalogue item distinct in the flat projection', () => {
47
+ const result = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PREP_TIME_ENTRIES, {
48
+ ...base,
49
+ prep_time_entries: {
50
+ K: {
51
+ rows: [
52
+ row({ item_id: 'menu-1', entry_key: 'item:menu-1:line-a' }),
53
+ row({ item_id: 'menu-1', entry_key: 'item:menu-1:line-b' }),
54
+ ],
55
+ },
56
+ },
57
+ });
58
+ expect(result.rows.map((entry) => entry.cursor_id)).toEqual(['item:menu-1:line-a', 'item:menu-1:line-b']);
45
59
  });
46
60
  test('projects cash rows as drawer-ledger rows (no order id)', () => {
47
61
  const result = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_CASH_ENTRIES, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.35",
3
+ "version": "0.4.37",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",