@managesome/eposmatic-toolkit 0.4.34 → 0.4.36
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.
- package/esm/constants/MATERIALIZED_REPORTS.js +25 -5
- package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +30 -9
- package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +98 -44
- package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +7 -3
- package/esm/helpers/materialized-reports/materialized-prep-time-read-helpers.js +35 -10
- package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +3 -1
- package/esm/schemas/models/materialized_tenant_prep_time_entries.js +3 -2
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +17 -5
- package/lib/constants/MATERIALIZED_REPORTS.js +27 -7
- package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.d.ts +10 -2
- package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +30 -9
- package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +13 -6
- package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +99 -44
- package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +6 -2
- package/lib/helpers/materialized-reports/materialized-prep-time-read-helpers.d.ts +19 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-read-helpers.js +37 -10
- package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +3 -1
- package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +3 -0
- package/lib/schemas/models/materialized_tenant_prep_time_entries.js +2 -1
- package/lib/schemas/types/materialized_tenant_prep_time_entries.d.ts +3 -0
- package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +9 -2
- package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +20 -1
- package/lib/tests/materialized-reports/materialized-prep-time-compile.test.js +22 -3
- package/lib/tests/materialized-reports/materialized-prep-time-constants.test.js +17 -6
- package/lib/tests/materialized-reports/materialized-prep-time-identity.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-prep-time-identity.test.js +163 -0
- package/lib/tests/materialized-reports/materialized-prep-time-plan.test.js +38 -0
- package/lib/tests/materialized-reports/materialized-report-drilldown-rows.test.js +19 -2
- package/package.json +1 -1
|
@@ -70,15 +70,22 @@ export declare function foldMaterializedPrepTimePeriodRows({ periodScopeField, p
|
|
|
70
70
|
}>;
|
|
71
71
|
/** The period scope contexts (everything except the day scope). */
|
|
72
72
|
export declare const MATERIALIZED_PREP_TIME_PERIOD_SCOPES: MATERIALIZED_REPORT_SCOPE_ENUM[];
|
|
73
|
+
/** Period scopes that carry ENTRY rows / flat rows (month-capped). */
|
|
74
|
+
export declare const MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES: MATERIALIZED_REPORT_SCOPE_ENUM[];
|
|
73
75
|
/**
|
|
74
|
-
* Every `localDate` label whose DAY entry document can contribute to a
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* fold needs — never a full-period entry document and never a year-wide scan.
|
|
79
|
-
* Returns `null` (never throws) for a malformed date.
|
|
76
|
+
* Every `localDate` label whose DAY entry document can contribute to a
|
|
77
|
+
* month-capped period (week / ISO week / month). The worker uses this to fetch
|
|
78
|
+
* exactly the sibling day entries the entry fold needs — never a full-period
|
|
79
|
+
* entry document and never a year-wide scan.
|
|
80
80
|
*/
|
|
81
81
|
export declare function getMaterializedPrepTimePeriodDayScopeFields(localDate: string): string[] | null;
|
|
82
|
+
/**
|
|
83
|
+
* Every `localDate` label whose DAY counter document can contribute to ANY
|
|
84
|
+
* report period the closing day rolls into, including quarter / semester /
|
|
85
|
+
* year. Used by the worker's SUMMARY counter fold only; entry rows stay
|
|
86
|
+
* month-capped.
|
|
87
|
+
*/
|
|
88
|
+
export declare function getMaterializedPrepTimeReportPeriodDayScopeFields(localDate: string): string[] | null;
|
|
82
89
|
/** True when a stored prep counter document equals the incoming one (skip write). */
|
|
83
90
|
export declare function needsMaterializedPrepTimeReportWrite(existing: IMaterializedTenantPrepTimeReport | null | undefined, incoming: IMaterializedTenantPrepTimeReport | null | undefined): boolean;
|
|
84
91
|
/** One planned write: the document plus its shared-history iteration row. */
|
|
@@ -3,12 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.MATERIALIZED_PREP_TIME_PERIOD_SCOPES = exports.isMaterializedPrepTimeDayScope = exports.mergeMaterializedPrepTimeEntrySections = void 0;
|
|
6
|
+
exports.MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES = exports.MATERIALIZED_PREP_TIME_PERIOD_SCOPES = exports.isMaterializedPrepTimeDayScope = exports.mergeMaterializedPrepTimeEntrySections = void 0;
|
|
7
7
|
exports.updateMaterializedPrepTimeEntries = updateMaterializedPrepTimeEntries;
|
|
8
8
|
exports.needsMaterializedPrepTimeEntriesWrite = needsMaterializedPrepTimeEntriesWrite;
|
|
9
9
|
exports.foldMaterializedPrepTimePeriodCounters = foldMaterializedPrepTimePeriodCounters;
|
|
10
10
|
exports.foldMaterializedPrepTimePeriodRows = foldMaterializedPrepTimePeriodRows;
|
|
11
11
|
exports.getMaterializedPrepTimePeriodDayScopeFields = getMaterializedPrepTimePeriodDayScopeFields;
|
|
12
|
+
exports.getMaterializedPrepTimeReportPeriodDayScopeFields = getMaterializedPrepTimeReportPeriodDayScopeFields;
|
|
12
13
|
exports.needsMaterializedPrepTimeReportWrite = needsMaterializedPrepTimeReportWrite;
|
|
13
14
|
exports.buildTenantPrepTimeCloseDayPlan = buildTenantPrepTimeCloseDayPlan;
|
|
14
15
|
const mongoose_1 = __importDefault(require("mongoose"));
|
|
@@ -169,66 +170,116 @@ function foldMaterializedPrepTimePeriodRows({ periodScopeField, periodScope, day
|
|
|
169
170
|
}
|
|
170
171
|
/** The period scope contexts (everything except the day scope). */
|
|
171
172
|
exports.MATERIALIZED_PREP_TIME_PERIOD_SCOPES = MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_REPORT_SCOPES.filter((scope) => !(0, exports.isMaterializedPrepTimeDayScope)(String(scope)));
|
|
173
|
+
/** Period scopes that carry ENTRY rows / flat rows (month-capped). */
|
|
174
|
+
exports.MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES = MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ITEM_SCOPES.filter((scope) => !(0, exports.isMaterializedPrepTimeDayScope)(String(scope)));
|
|
172
175
|
/* ------------------------------------------------------------------------ */
|
|
173
176
|
/* Period sibling-day enumeration */
|
|
174
177
|
/* ------------------------------------------------------------------------ */
|
|
175
178
|
const pad2 = (value) => String(value).padStart(2, '0');
|
|
176
179
|
const toLocalDateKey = (date) => `${date.getUTCFullYear()}-${pad2(date.getUTCMonth() + 1)}-${pad2(date.getUTCDate())}`;
|
|
177
180
|
const addUtcDays = (date, amount) => new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate() + amount));
|
|
178
|
-
const addMonthDays = (year, month, days) => {
|
|
181
|
+
const addMonthDays = (year, month, count, days) => {
|
|
179
182
|
if (!Number.isInteger(year) || !Number.isInteger(month) || month < 1 || month > 12)
|
|
180
183
|
return false;
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
+
for (let offset = 0; offset < count; offset += 1) {
|
|
185
|
+
const date = new Date(Date.UTC(year, month - 1 + offset, 1));
|
|
186
|
+
const monthYear = date.getUTCFullYear();
|
|
187
|
+
const monthNumber = date.getUTCMonth() + 1;
|
|
188
|
+
const last = new Date(Date.UTC(monthYear, monthNumber, 0)).getUTCDate();
|
|
189
|
+
for (let day = 1; day <= last; day += 1) {
|
|
190
|
+
days.add(`${monthYear}-${pad2(monthNumber)}-${pad2(day)}`);
|
|
191
|
+
}
|
|
184
192
|
}
|
|
185
193
|
return true;
|
|
186
194
|
};
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
195
|
+
const addPeriodDays = (scope, scopeField, days) => {
|
|
196
|
+
switch (scope) {
|
|
197
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK: {
|
|
198
|
+
const parts = (0, scope_helpers_1.parseMaterializedLocalDate)(scopeField);
|
|
199
|
+
if (parts == null)
|
|
200
|
+
return false;
|
|
201
|
+
const start = new Date(Date.UTC(parts.year, parts.month - 1, parts.day));
|
|
202
|
+
if (start.getUTCDay() !== 0)
|
|
203
|
+
return false;
|
|
204
|
+
for (let offset = 0; offset < 7; offset += 1)
|
|
205
|
+
days.add(toLocalDateKey(addUtcDays(start, offset)));
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK_OF_YEAR: {
|
|
209
|
+
const match = /^(\d{4})-W(\d{2})$/.exec(scopeField);
|
|
210
|
+
if (match == null)
|
|
211
|
+
return false;
|
|
212
|
+
const weekYear = Number(match[1]);
|
|
213
|
+
const week = Number(match[2]);
|
|
214
|
+
if (!Number.isInteger(week) || week < 1 || week > 53)
|
|
215
|
+
return false;
|
|
216
|
+
const jan4 = new Date(Date.UTC(weekYear, 0, 4));
|
|
217
|
+
const isoWeekday = jan4.getUTCDay() || 7;
|
|
218
|
+
const weekOneMonday = addUtcDays(jan4, 1 - isoWeekday);
|
|
219
|
+
const start = addUtcDays(weekOneMonday, (week - 1) * 7);
|
|
220
|
+
for (let offset = 0; offset < 7; offset += 1)
|
|
221
|
+
days.add(toLocalDateKey(addUtcDays(start, offset)));
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH: {
|
|
225
|
+
const match = /^(\d{4})-(\d{2})$/.exec(scopeField);
|
|
226
|
+
if (match == null)
|
|
227
|
+
return false;
|
|
228
|
+
return addMonthDays(Number(match[1]), Number(match[2]), 1, days);
|
|
229
|
+
}
|
|
230
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER: {
|
|
231
|
+
const match = /^(\d{4})-Q([1-4])$/.exec(scopeField);
|
|
232
|
+
if (match == null)
|
|
233
|
+
return false;
|
|
234
|
+
return addMonthDays(Number(match[1]), (Number(match[2]) - 1) * 3 + 1, 3, days);
|
|
235
|
+
}
|
|
236
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER: {
|
|
237
|
+
const match = /^(\d{4})-S([12])$/.exec(scopeField);
|
|
238
|
+
if (match == null)
|
|
239
|
+
return false;
|
|
240
|
+
return addMonthDays(Number(match[1]), (Number(match[2]) - 1) * 6 + 1, 6, days);
|
|
241
|
+
}
|
|
242
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR: {
|
|
243
|
+
const match = /^(\d{4})$/.exec(scopeField);
|
|
244
|
+
if (match == null)
|
|
245
|
+
return false;
|
|
246
|
+
return addMonthDays(Number(match[1]), 1, 12, days);
|
|
247
|
+
}
|
|
248
|
+
default:
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
const enumeratePeriodDays = (localDate, scopes) => {
|
|
196
253
|
const periodFields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(localDate);
|
|
197
254
|
if (periodFields == null)
|
|
198
255
|
return null;
|
|
199
256
|
const days = new Set([localDate]);
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
return null;
|
|
207
|
-
for (let offset = 0; offset < 7; offset += 1) {
|
|
208
|
-
days.add(toLocalDateKey(addUtcDays(weekStart, offset)));
|
|
209
|
-
}
|
|
210
|
-
const isoWeekField = periodFields[MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK_OF_YEAR];
|
|
211
|
-
const isoMatch = typeof isoWeekField === 'string' ? /^(\d{4})-W(\d{2})$/.exec(isoWeekField) : null;
|
|
212
|
-
if (isoMatch == null)
|
|
213
|
-
return null;
|
|
214
|
-
const weekYear = Number(isoMatch[1]);
|
|
215
|
-
const week = Number(isoMatch[2]);
|
|
216
|
-
if (!Number.isInteger(week) || week < 1 || week > 53)
|
|
217
|
-
return null;
|
|
218
|
-
const jan4 = new Date(Date.UTC(weekYear, 0, 4));
|
|
219
|
-
const isoWeekday = jan4.getUTCDay() || 7;
|
|
220
|
-
const weekOneMonday = addUtcDays(jan4, 1 - isoWeekday);
|
|
221
|
-
const isoStart = addUtcDays(weekOneMonday, (week - 1) * 7);
|
|
222
|
-
for (let offset = 0; offset < 7; offset += 1) {
|
|
223
|
-
days.add(toLocalDateKey(addUtcDays(isoStart, offset)));
|
|
257
|
+
for (const scope of scopes) {
|
|
258
|
+
const scopeField = periodFields[scope];
|
|
259
|
+
if (typeof scopeField !== 'string' || scopeField.length === 0)
|
|
260
|
+
return null;
|
|
261
|
+
if (!addPeriodDays(scope, scopeField, days))
|
|
262
|
+
return null;
|
|
224
263
|
}
|
|
225
|
-
const monthField = periodFields[MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH];
|
|
226
|
-
const monthMatch = typeof monthField === 'string' ? /^(\d{4})-(\d{2})$/.exec(monthField) : null;
|
|
227
|
-
if (monthMatch == null)
|
|
228
|
-
return null;
|
|
229
|
-
if (!addMonthDays(Number(monthMatch[1]), Number(monthMatch[2]), days))
|
|
230
|
-
return null;
|
|
231
264
|
return [...days].sort();
|
|
265
|
+
};
|
|
266
|
+
/**
|
|
267
|
+
* Every `localDate` label whose DAY entry document can contribute to a
|
|
268
|
+
* month-capped period (week / ISO week / month). The worker uses this to fetch
|
|
269
|
+
* exactly the sibling day entries the entry fold needs — never a full-period
|
|
270
|
+
* entry document and never a year-wide scan.
|
|
271
|
+
*/
|
|
272
|
+
function getMaterializedPrepTimePeriodDayScopeFields(localDate) {
|
|
273
|
+
return enumeratePeriodDays(localDate, exports.MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Every `localDate` label whose DAY counter document can contribute to ANY
|
|
277
|
+
* report period the closing day rolls into, including quarter / semester /
|
|
278
|
+
* year. Used by the worker's SUMMARY counter fold only; entry rows stay
|
|
279
|
+
* month-capped.
|
|
280
|
+
*/
|
|
281
|
+
function getMaterializedPrepTimeReportPeriodDayScopeFields(localDate) {
|
|
282
|
+
return enumeratePeriodDays(localDate, exports.MATERIALIZED_PREP_TIME_PERIOD_SCOPES);
|
|
232
283
|
}
|
|
233
284
|
/** True when a stored prep counter document equals the incoming one (skip write). */
|
|
234
285
|
function needsMaterializedPrepTimeReportWrite(existing, incoming) {
|
|
@@ -485,7 +536,11 @@ function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
485
536
|
continue;
|
|
486
537
|
staleEntryDocuments.push(doc);
|
|
487
538
|
}
|
|
539
|
+
const itemPeriodScopes = new Set(exports.MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES);
|
|
488
540
|
for (const { scope, field } of touchedPeriods.values()) {
|
|
541
|
+
// ENTRY documents are month-capped; quarter/semester/year are COUNTER only.
|
|
542
|
+
if (!itemPeriodScopes.has(String(scope)))
|
|
543
|
+
continue;
|
|
489
544
|
const sections = foldMaterializedPrepTimePeriodRows({
|
|
490
545
|
periodScope: scope,
|
|
491
546
|
periodScopeField: field,
|
|
@@ -93,6 +93,7 @@ function compileTenantPrepTimeReports(input) {
|
|
|
93
93
|
return null;
|
|
94
94
|
const dayIndex = (0, scope_helpers_1.getDayIndexFromLocalDate)(localDate);
|
|
95
95
|
const at = computedAt ?? new Date();
|
|
96
|
+
const itemScopes = new Set(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ITEM_SCOPES);
|
|
96
97
|
const byScope = new Map();
|
|
97
98
|
const scopeContextOf = new Map();
|
|
98
99
|
const bucketFor = (scope, scopeField, key) => {
|
|
@@ -145,7 +146,10 @@ function compileTenantPrepTimeReports(input) {
|
|
|
145
146
|
continue;
|
|
146
147
|
const bucket = bucketFor(scope, scopeField, key);
|
|
147
148
|
mergeMaterializedPrepTimeCounters(bucket.counters, result.counters);
|
|
148
|
-
|
|
149
|
+
// ENTRY rows / flat rows are month-capped; the Summary counters go to the
|
|
150
|
+
// full report range. Never accumulate quarter/semester/year item rows.
|
|
151
|
+
if (itemScopes.has(scope))
|
|
152
|
+
bucket.rows.push(...result.entries);
|
|
149
153
|
foldWatermark(bucket, watermark);
|
|
150
154
|
}
|
|
151
155
|
}
|
|
@@ -186,7 +190,7 @@ function compileTenantPrepTimeReports(input) {
|
|
|
186
190
|
createdAt: at,
|
|
187
191
|
updatedAt: at,
|
|
188
192
|
});
|
|
189
|
-
if (Object.keys(entrySections).length > 0) {
|
|
193
|
+
if (itemScopes.has(scopeContext) && Object.keys(entrySections).length > 0) {
|
|
190
194
|
entries.push({
|
|
191
195
|
_id: new mongoose_1.default.Types.ObjectId().toHexString(),
|
|
192
196
|
tenant_id: tenantId,
|
|
@@ -97,6 +97,14 @@ export declare function applyMaterializedPrepTimeReadFilters(rows: IPrepTimeRead
|
|
|
97
97
|
* can still state "1,192 of 1,552 served items have their own prep clock".
|
|
98
98
|
*/
|
|
99
99
|
export declare const withoutMaterializedPrepTimeAnchorFilter: (filters?: IPrepTimeReadFilters | null) => IPrepTimeReadFilters;
|
|
100
|
+
/**
|
|
101
|
+
* Coverage from COUNTS (the server-side Series aggregation groups in Mongo and
|
|
102
|
+
* therefore has no raw rows). Same math as `buildMaterializedPrepTimeCoverage`.
|
|
103
|
+
*/
|
|
104
|
+
export declare function buildMaterializedPrepTimeCoverageFromCounts(input: {
|
|
105
|
+
served_count: number;
|
|
106
|
+
measured_count: number;
|
|
107
|
+
}): IPrepTimeCoverage;
|
|
100
108
|
/** Coverage of the supplied population (measured / served, one decimal). */
|
|
101
109
|
export declare function buildMaterializedPrepTimeCoverage(rows: IPrepTimeReadRow[] | null | undefined): IPrepTimeCoverage;
|
|
102
110
|
/**
|
|
@@ -111,6 +119,17 @@ export declare function getMaterializedPrepTimePercentile(values: number[] | nul
|
|
|
111
119
|
export declare function getMaterializedPrepTimeMedian(values: number[] | null | undefined): number | null;
|
|
112
120
|
/** EXACT p90 (interpolated). */
|
|
113
121
|
export declare function getMaterializedPrepTimeP90(values: number[] | null | undefined): number | null;
|
|
122
|
+
/** The three duration statistics for ONE population (avg / exact median / exact p90). */
|
|
123
|
+
export interface IPrepTimeDurationSummary {
|
|
124
|
+
avg_minutes: number | null;
|
|
125
|
+
median_minutes: number | null;
|
|
126
|
+
p90_minutes: number | null;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Duration statistics for a list of VALID durations (invalid values are
|
|
130
|
+
* filtered defensively). `null` for an empty population — never 0.
|
|
131
|
+
*/
|
|
132
|
+
export declare function summarizeMaterializedPrepTimeDurations(durations: number[] | null | undefined): IPrepTimeDurationSummary;
|
|
114
133
|
/**
|
|
115
134
|
* Groups filtered rows by `item_id` (never by name). Counts are per ROW, so an
|
|
116
135
|
* item sold on many orders is never collapsed into one sale.
|
|
@@ -8,10 +8,12 @@ exports.isMaterializedPrepTimeDurationCountable = isMaterializedPrepTimeDuration
|
|
|
8
8
|
exports.filterMaterializedPrepTimeRowsByAnchor = filterMaterializedPrepTimeRowsByAnchor;
|
|
9
9
|
exports.matchesMaterializedPrepTimeReadFilters = matchesMaterializedPrepTimeReadFilters;
|
|
10
10
|
exports.applyMaterializedPrepTimeReadFilters = applyMaterializedPrepTimeReadFilters;
|
|
11
|
+
exports.buildMaterializedPrepTimeCoverageFromCounts = buildMaterializedPrepTimeCoverageFromCounts;
|
|
11
12
|
exports.buildMaterializedPrepTimeCoverage = buildMaterializedPrepTimeCoverage;
|
|
12
13
|
exports.getMaterializedPrepTimePercentile = getMaterializedPrepTimePercentile;
|
|
13
14
|
exports.getMaterializedPrepTimeMedian = getMaterializedPrepTimeMedian;
|
|
14
15
|
exports.getMaterializedPrepTimeP90 = getMaterializedPrepTimeP90;
|
|
16
|
+
exports.summarizeMaterializedPrepTimeDurations = summarizeMaterializedPrepTimeDurations;
|
|
15
17
|
exports.groupMaterializedPrepTimeItemRows = groupMaterializedPrepTimeItemRows;
|
|
16
18
|
exports.isMaterializedPrepTimeItemSort = isMaterializedPrepTimeItemSort;
|
|
17
19
|
exports.getMaterializedPrepTimeItemSortValue = getMaterializedPrepTimeItemSortValue;
|
|
@@ -258,11 +260,23 @@ const addTotals = (totals, row) => {
|
|
|
258
260
|
else
|
|
259
261
|
totals.unmeasured += 1;
|
|
260
262
|
};
|
|
261
|
-
|
|
263
|
+
/**
|
|
264
|
+
* Coverage from COUNTS (the server-side Series aggregation groups in Mongo and
|
|
265
|
+
* therefore has no raw rows). Same math as `buildMaterializedPrepTimeCoverage`.
|
|
266
|
+
*/
|
|
267
|
+
function buildMaterializedPrepTimeCoverageFromCounts(input) {
|
|
268
|
+
const served = Math.max(0, Math.floor(Number(input.served_count) || 0));
|
|
269
|
+
const measured = Math.max(0, Math.floor(Number(input.measured_count) || 0));
|
|
270
|
+
return {
|
|
271
|
+
served_count: served,
|
|
272
|
+
measured_count: measured,
|
|
273
|
+
unmeasured_count: Math.max(0, served - measured),
|
|
274
|
+
coverage_percent: served > 0 ? round1((measured / served) * 100) : 0,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
const coverageFromTotals = (totals) => buildMaterializedPrepTimeCoverageFromCounts({
|
|
262
278
|
served_count: totals.served,
|
|
263
279
|
measured_count: totals.measured,
|
|
264
|
-
unmeasured_count: totals.unmeasured,
|
|
265
|
-
coverage_percent: totals.served > 0 ? round1((totals.measured / totals.served) * 100) : 0,
|
|
266
280
|
});
|
|
267
281
|
/** Coverage of the supplied population (measured / served, one decimal). */
|
|
268
282
|
function buildMaterializedPrepTimeCoverage(rows) {
|
|
@@ -327,15 +341,28 @@ const addMetrics = (metrics, row) => {
|
|
|
327
341
|
metrics.late += 1;
|
|
328
342
|
}
|
|
329
343
|
};
|
|
344
|
+
/**
|
|
345
|
+
* Duration statistics for a list of VALID durations (invalid values are
|
|
346
|
+
* filtered defensively). `null` for an empty population — never 0.
|
|
347
|
+
*/
|
|
348
|
+
function summarizeMaterializedPrepTimeDurations(durations) {
|
|
349
|
+
const valid = (Array.isArray(durations) ? durations : []).filter(isMaterializedPrepTimeDurationCountable);
|
|
350
|
+
if (valid.length === 0) {
|
|
351
|
+
return { avg_minutes: null, median_minutes: null, p90_minutes: null };
|
|
352
|
+
}
|
|
353
|
+
const sum = valid.reduce((total, value) => total + value, 0);
|
|
354
|
+
return {
|
|
355
|
+
avg_minutes: round1(sum / valid.length),
|
|
356
|
+
median_minutes: getMaterializedPrepTimeMedian(valid),
|
|
357
|
+
p90_minutes: getMaterializedPrepTimeP90(valid),
|
|
358
|
+
};
|
|
359
|
+
}
|
|
330
360
|
const durationStats = (durations) => {
|
|
331
|
-
const
|
|
332
|
-
if (count === 0)
|
|
333
|
-
return { avg: null, median: null, p90: null };
|
|
334
|
-
const sum = durations.reduce((total, value) => total + value, 0);
|
|
361
|
+
const summary = summarizeMaterializedPrepTimeDurations(durations);
|
|
335
362
|
return {
|
|
336
|
-
avg:
|
|
337
|
-
median:
|
|
338
|
-
p90:
|
|
363
|
+
avg: summary.avg_minutes,
|
|
364
|
+
median: summary.median_minutes,
|
|
365
|
+
p90: summary.p90_minutes,
|
|
339
366
|
};
|
|
340
367
|
};
|
|
341
368
|
const freshItemGroup = (itemId) => ({
|
|
@@ -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
|
-
|
|
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 ----
|
|
@@ -63,7 +64,7 @@ exports.MaterializedTenantPrepTimeEntriesSchema = new mongoose_1.Schema({
|
|
|
63
64
|
scope_context: {
|
|
64
65
|
type: String,
|
|
65
66
|
required: true,
|
|
66
|
-
enum: MATERIALIZED_REPORTS_1.
|
|
67
|
+
enum: MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ITEM_SCOPES,
|
|
67
68
|
},
|
|
68
69
|
scope_field: { type: String, required: true },
|
|
69
70
|
day_index: {
|
|
@@ -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
|
|
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(
|
|
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();
|
|
@@ -65,12 +65,31 @@ describe('day vs period scope classification', () => {
|
|
|
65
65
|
expect((0, materialized_prep_time_close_day_helpers_1.isMaterializedPrepTimeDayScope)(String(scope))).toBe(false);
|
|
66
66
|
}
|
|
67
67
|
});
|
|
68
|
-
it('exposes
|
|
68
|
+
it('exposes the full report periods and the month-capped item periods', () => {
|
|
69
69
|
expect(materialized_prep_time_close_day_helpers_1.MATERIALIZED_PREP_TIME_PERIOD_SCOPES.sort()).toEqual([
|
|
70
70
|
'localMonth',
|
|
71
|
+
'localQuarter',
|
|
72
|
+
'localSemester',
|
|
71
73
|
'localWeek',
|
|
72
74
|
'localWeekOfYear',
|
|
75
|
+
'localYear',
|
|
73
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
|
+
]);
|
|
82
|
+
});
|
|
83
|
+
it('enumerates the FULL report sibling days (through year) separately from the item days', () => {
|
|
84
|
+
// Item period days stay month-bounded (Sep 1..30 for this Monday).
|
|
85
|
+
const itemDays = (0, materialized_prep_time_close_day_helpers_1.getMaterializedPrepTimePeriodDayScopeFields)('2026-09-14');
|
|
86
|
+
expect(itemDays).toHaveLength(30);
|
|
87
|
+
// Report period days include the whole year (the Summary year counter fold).
|
|
88
|
+
const reportDays = (0, materialized_prep_time_close_day_helpers_1.getMaterializedPrepTimeReportPeriodDayScopeFields)('2026-09-14');
|
|
89
|
+
expect(reportDays).toHaveLength(365);
|
|
90
|
+
expect(reportDays[0]).toBe('2026-01-01');
|
|
91
|
+
expect(reportDays[reportDays.length - 1]).toBe('2026-12-31');
|
|
92
|
+
expect(reportDays).toEqual(expect.arrayContaining(itemDays));
|
|
74
93
|
});
|
|
75
94
|
it('enumerates the EXACT week / ISO-week / month sibling days', () => {
|
|
76
95
|
const days = (0, materialized_prep_time_close_day_helpers_1.getMaterializedPrepTimePeriodDayScopeFields)('2026-09-14');
|
|
@@ -110,11 +110,20 @@ describe('mergeMaterializedPrepTimeCounters', () => {
|
|
|
110
110
|
});
|
|
111
111
|
});
|
|
112
112
|
describe('compileTenantPrepTimeReports', () => {
|
|
113
|
-
it('emits the
|
|
113
|
+
it('emits the FULL report scope set with NO hour scope', () => {
|
|
114
114
|
const result = compile([order()]);
|
|
115
115
|
const scopes = result.reports.map((doc) => doc.scope_context);
|
|
116
116
|
expect(scopes.sort()).toEqual([...MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_REPORT_SCOPES].sort());
|
|
117
117
|
expect(scopes).not.toContain('localHour');
|
|
118
|
+
expect(scopes).toEqual(expect.arrayContaining(['localQuarter', 'localSemester', 'localYear']));
|
|
119
|
+
});
|
|
120
|
+
it('emits ENTRY documents only for the month-capped item scopes', () => {
|
|
121
|
+
const result = compile([order()]);
|
|
122
|
+
const entryScopes = [...new Set(result.entries.map((doc) => doc.scope_context))].sort();
|
|
123
|
+
expect(entryScopes).toEqual([...MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ITEM_SCOPES].sort());
|
|
124
|
+
expect(entryScopes).not.toContain('localQuarter');
|
|
125
|
+
expect(entryScopes).not.toContain('localSemester');
|
|
126
|
+
expect(entryScopes).not.toContain('localYear');
|
|
118
127
|
});
|
|
119
128
|
it('sets day_index ONLY on the date document', () => {
|
|
120
129
|
const result = compile([order()]);
|
|
@@ -176,8 +185,18 @@ describe('compileTenantPrepTimeReports', () => {
|
|
|
176
185
|
expect(counters.coverage.tracked_count).toBe(0);
|
|
177
186
|
expect(counters.coverage.own_anchor_count).toBe(0);
|
|
178
187
|
expect(counters.duration.count).toBe(0);
|
|
179
|
-
//
|
|
180
|
-
|
|
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
|
+
});
|
|
181
200
|
});
|
|
182
201
|
it('counts a served-but-unobserved item in coverage but NOT in duration', () => {
|
|
183
202
|
const unobserved = order({
|
|
@@ -10,19 +10,30 @@ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
|
10
10
|
* tests pin the tiling, the scope cap and the two-bucket vocabulary.
|
|
11
11
|
*/
|
|
12
12
|
describe('prep time report constants (M2)', () => {
|
|
13
|
-
it('scopes are
|
|
13
|
+
it('report scopes are the FULL Summary range with NO hour scope (decision h2)', () => {
|
|
14
14
|
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_REPORT_SCOPES).toEqual([
|
|
15
15
|
MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
16
16
|
MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK,
|
|
17
17
|
MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK_OF_YEAR,
|
|
18
18
|
MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
|
|
19
|
+
MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER,
|
|
20
|
+
MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER,
|
|
21
|
+
MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR,
|
|
19
22
|
]);
|
|
20
23
|
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_REPORT_SCOPES).not.toContain(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR);
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
expect(MATERIALIZED_REPORTS_1.
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
});
|
|
25
|
+
it('item scopes are the month-capped read range', () => {
|
|
26
|
+
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ITEM_SCOPES).toEqual([
|
|
27
|
+
MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
28
|
+
MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK,
|
|
29
|
+
MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK_OF_YEAR,
|
|
30
|
+
MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
|
|
31
|
+
]);
|
|
32
|
+
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ITEM_SCOPES).not.toContain(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER);
|
|
33
|
+
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ITEM_SCOPES).not.toContain(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER);
|
|
34
|
+
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ITEM_SCOPES).not.toContain(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR);
|
|
35
|
+
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ITEM_SCOPES).not.toContain(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR);
|
|
36
|
+
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_PREP_TIME_ITEM_MAX_READ_DAYS).toBe(31);
|
|
26
37
|
});
|
|
27
38
|
it('buckets TILE the range with no gap and no overlap', () => {
|
|
28
39
|
// Contiguous: each bucket starts exactly one minute after the previous ends.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|