@managesome/eposmatic-toolkit 0.4.32 → 0.4.35
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 +22 -4
- package/esm/constants/enums/offers.js +0 -6
- package/esm/helpers/materialized-reports/index.js +1 -0
- package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
- package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +12 -8
- package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +141 -10
- package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +7 -3
- package/esm/helpers/materialized-reports/materialized-prep-time-read-helpers.js +638 -0
- package/esm/helpers/types/materialized-reports-apis.js +10 -0
- package/esm/schemas/models/index.js +0 -1
- package/esm/schemas/models/materialized_tenant_prep_time_entries.js +2 -2
- package/esm/schemas/types/index.js +0 -1
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +17 -4
- package/lib/constants/MATERIALIZED_REPORTS.js +24 -6
- package/lib/constants/enums/offers.d.ts +0 -5
- package/lib/constants/enums/offers.js +1 -7
- package/lib/helpers/materialized-reports/index.d.ts +1 -0
- package/lib/helpers/materialized-reports/index.js +1 -0
- package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +12 -8
- package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +22 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +142 -9
- 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 +198 -0
- package/lib/helpers/materialized-reports/materialized-prep-time-read-helpers.js +666 -0
- package/lib/helpers/types/materialized-reports-apis.d.ts +145 -1
- package/lib/helpers/types/materialized-reports-apis.js +11 -0
- package/lib/schemas/models/index.d.ts +0 -1
- package/lib/schemas/models/index.js +0 -1
- package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +6 -6
- package/lib/schemas/models/materialized_tenant_prep_time_entries.js +1 -1
- package/lib/schemas/types/index.d.ts +0 -1
- package/lib/schemas/types/index.js +0 -1
- package/lib/schemas/types/materialized_tenant_prep_time_reports.d.ts +7 -5
- package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +28 -5
- package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +33 -1
- package/lib/tests/materialized-reports/materialized-prep-time-compile.test.js +10 -1
- package/lib/tests/materialized-reports/materialized-prep-time-constants.test.js +14 -1
- package/lib/tests/materialized-reports/materialized-prep-time-duration-consistency.test.js +135 -0
- package/lib/tests/materialized-reports/materialized-prep-time-plan.test.js +106 -0
- package/lib/tests/materialized-reports/materialized-prep-time-read-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-prep-time-read-helpers.test.js +620 -0
- package/lib/tests/offers-matrix.test.d.ts +1 -0
- package/lib/tests/offers-matrix.test.js +401 -0
- package/package.json +1 -1
- package/esm/schemas/models/rewards_usage.js +0 -61
- package/lib/schemas/models/rewards_usage.d.ts +0 -53
- package/lib/schemas/models/rewards_usage.js +0 -64
- package/lib/schemas/types/rewards_usage.d.ts +0 -13
- package/lib/schemas/types/rewards_usage.js +0 -2
- /package/{esm/schemas/types/rewards_usage.js → lib/tests/materialized-reports/materialized-prep-time-duration-consistency.test.d.ts} +0 -0
|
@@ -417,12 +417,16 @@ export const MATERIALIZED_DELIVERY_AREA_LIMIT = 50;
|
|
|
417
417
|
/* Prep time report (M2) */
|
|
418
418
|
/* ======================================================================== */
|
|
419
419
|
/**
|
|
420
|
-
* Scopes materialized by `materialized_tenant_prep_time_reports
|
|
420
|
+
* Scopes materialized by `materialized_tenant_prep_time_reports` — the SUMMARY
|
|
421
|
+
* range (and the writer's COUNTER scopes).
|
|
421
422
|
*
|
|
422
423
|
* NO `localHour` (decision h2): the tracked population per hour is 2-3 items in
|
|
423
|
-
* the dev stores, so an hourly prep time would be noise.
|
|
424
|
-
*
|
|
425
|
-
*
|
|
424
|
+
* the dev stores, so an hourly prep time would be noise.
|
|
425
|
+
*
|
|
426
|
+
* The Summary is CHEAP for a year because it reads these pre-aggregated counters,
|
|
427
|
+
* not item rows. The itemized reads are a DIFFERENT, month-capped list
|
|
428
|
+
* (`MATERIALIZED_PREP_TIME_ITEM_SCOPES`); the writer therefore materializes the
|
|
429
|
+
* full counter range but only month-capped ENTRY rows / flat period rows.
|
|
426
430
|
*/
|
|
427
431
|
export const MATERIALIZED_PREP_TIME_REPORT_SCOPES = [
|
|
428
432
|
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
@@ -433,6 +437,20 @@ export const MATERIALIZED_PREP_TIME_REPORT_SCOPES = [
|
|
|
433
437
|
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER,
|
|
434
438
|
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR,
|
|
435
439
|
];
|
|
440
|
+
/**
|
|
441
|
+
* Scopes the ITEMIZED prep reads support — Items, Categories and the Details
|
|
442
|
+
* drill-down. Capped at `LOCAL_MONTH` so item rows can never grow into a
|
|
443
|
+
* quarter/year document; the service additionally enforces a 31-effective-day
|
|
444
|
+
* cap (see `MATERIALIZED_PREP_TIME_ITEM_MAX_READ_DAYS`).
|
|
445
|
+
*/
|
|
446
|
+
export const MATERIALIZED_PREP_TIME_ITEM_SCOPES = [
|
|
447
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
448
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK,
|
|
449
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK_OF_YEAR,
|
|
450
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
|
|
451
|
+
];
|
|
452
|
+
/** Max EFFECTIVE days (after weekday/date refinement) an item read may cover. */
|
|
453
|
+
export const MATERIALIZED_PREP_TIME_ITEM_MAX_READ_DAYS = 31;
|
|
436
454
|
/** Ordered key filters of the prep time report (`COMPLETED:POS_WALK-IN`). */
|
|
437
455
|
export const PREP_TIME_KEY_FILTERS = [
|
|
438
456
|
MATERIALIZED_REPORT_KEY_FILTER_ENUM.ORDER_STATUS,
|
|
@@ -65,12 +65,6 @@ export var PATHWAY_TO_UNLOCK_TYPE_ENUM;
|
|
|
65
65
|
PATHWAY_TO_UNLOCK_TYPE_ENUM["POINTS"] = "POINTS";
|
|
66
66
|
})(PATHWAY_TO_UNLOCK_TYPE_ENUM || (PATHWAY_TO_UNLOCK_TYPE_ENUM = {}));
|
|
67
67
|
export const ALL_PATHWAY_TO_UNLOCK_TYPES = Object.values(PATHWAY_TO_UNLOCK_TYPE_ENUM);
|
|
68
|
-
export var REWARDS_USAGE_REF_TYPE_ENUM;
|
|
69
|
-
(function (REWARDS_USAGE_REF_TYPE_ENUM) {
|
|
70
|
-
REWARDS_USAGE_REF_TYPE_ENUM["VOUCHER"] = "VOUCHER";
|
|
71
|
-
REWARDS_USAGE_REF_TYPE_ENUM["GIFT_CARD"] = "GIFT_CARD";
|
|
72
|
-
})(REWARDS_USAGE_REF_TYPE_ENUM || (REWARDS_USAGE_REF_TYPE_ENUM = {}));
|
|
73
|
-
export const ALL_REWARDS_USAGE_REF_TYPES = Object.values(REWARDS_USAGE_REF_TYPE_ENUM);
|
|
74
68
|
/* ================><================= */
|
|
75
69
|
export var RATING_TYPE_ENUM;
|
|
76
70
|
(function (RATING_TYPE_ENUM) {
|
|
@@ -26,6 +26,7 @@ export * from './materialized-cash-compile-helpers';
|
|
|
26
26
|
export * from './materialized-prep-time-classification-helpers';
|
|
27
27
|
export * from './materialized-prep-time-close-day-helpers';
|
|
28
28
|
export * from './materialized-prep-time-compile-helpers';
|
|
29
|
+
export * from './materialized-prep-time-read-helpers';
|
|
29
30
|
export * from './materialized-report-drilldown-rows';
|
|
30
31
|
export * from './materialized-reports-read-helpers';
|
|
31
32
|
export * from './materialized-reports-scope-group-helpers';
|
|
@@ -3,6 +3,8 @@ import { MATERIALIZED_TENANT_DISCOUNT_ENTRIES_INDEX_OPTIONS, MATERIALIZED_TENANT
|
|
|
3
3
|
import { MATERIALIZED_TENANT_DISCOUNT_REPORTS_INDEX_OPTIONS, MATERIALIZED_TENANT_DISCOUNT_REPORTS_COLLECTION_NAME, } from '../../schemas/models/materialized_tenant_discount_reports';
|
|
4
4
|
import { MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS, MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME, } from '../../schemas/models/materialized_tenant_discount_sources';
|
|
5
5
|
import { MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME, MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_reports_iterations';
|
|
6
|
+
import { MATERIALIZED_TENANT_PREP_TIME_ENTRIES_COLLECTION_NAME, MATERIALIZED_TENANT_PREP_TIME_ENTRIES_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_prep_time_entries';
|
|
7
|
+
import { MATERIALIZED_TENANT_PREP_TIME_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_PREP_TIME_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_prep_time_reports';
|
|
6
8
|
import { MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_sales_overview_reports';
|
|
7
9
|
import { MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME, MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS, MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME, MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS, } from '../../schemas/models/materialized_report_drilldown_rows';
|
|
8
10
|
import { TENANT_CALENDAR_COLLECTION_NAME, TENANT_CALENDAR_INDEX_OPTIONS } from '../../schemas/models/tenant_calendar';
|
|
@@ -25,6 +27,8 @@ const INDEXES = {
|
|
|
25
27
|
[MATERIALIZED_TENANT_DISCOUNT_REPORTS_COLLECTION_NAME]: MATERIALIZED_TENANT_DISCOUNT_REPORTS_INDEX_OPTIONS,
|
|
26
28
|
[MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_NAME]: MATERIALIZED_TENANT_DISCOUNT_ENTRIES_INDEX_OPTIONS,
|
|
27
29
|
[MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME]: MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS,
|
|
30
|
+
[MATERIALIZED_TENANT_PREP_TIME_REPORTS_COLLECTION_NAME]: MATERIALIZED_TENANT_PREP_TIME_REPORTS_INDEX_OPTIONS,
|
|
31
|
+
[MATERIALIZED_TENANT_PREP_TIME_ENTRIES_COLLECTION_NAME]: MATERIALIZED_TENANT_PREP_TIME_ENTRIES_INDEX_OPTIONS,
|
|
28
32
|
[MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME]: MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS,
|
|
29
33
|
[MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME]: MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS,
|
|
30
34
|
[MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME]: MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS,
|
|
@@ -142,14 +142,18 @@ export function accumulateOrderPrepTime({ order, context, tenantDefaultMinutes =
|
|
|
142
142
|
counters.coverage.own_anchor_count += 1;
|
|
143
143
|
// RECONSTRUCTED ready instant - the source field ready_at does not exist.
|
|
144
144
|
readyAt = new Date(prepStartedAt.getTime() + minutes * 60000);
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
//
|
|
151
|
-
|
|
152
|
-
|
|
145
|
+
// ONE canonical duration population: a duration is VALID only when it
|
|
146
|
+
// is finite, >= 0 and <= the outlier cap. Valid durations alone feed
|
|
147
|
+
// count / sum / min / max / buckets / instant count (and therefore
|
|
148
|
+
// avg = sum/count). An artefact must never understate the average by
|
|
149
|
+
// sitting in the denominator while being excluded from the numerator.
|
|
150
|
+
// Coverage still counts the MEASURED row (own_anchor_count above).
|
|
151
|
+
if (minutes >= 0 && minutes <= MATERIALIZED_PREP_TIME_MAX_MINUTES) {
|
|
152
|
+
counters.duration.count += 1;
|
|
153
|
+
if (minutes === 0)
|
|
154
|
+
counters.duration.instant_count += 1;
|
|
155
|
+
const bucket = resolveMaterializedPrepTimeBucket(minutes);
|
|
156
|
+
counters.duration.buckets[bucket] = (counters.duration.buckets[bucket] ?? 0) + 1;
|
|
153
157
|
counters.duration.sum += minutes;
|
|
154
158
|
if (counters.duration.count === 1 || minutes < counters.duration.min) {
|
|
155
159
|
counters.duration.min = minutes;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
|
-
import { MATERIALIZED_PREP_TIME_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { MATERIALIZED_PREP_TIME_ITEM_SCOPES, MATERIALIZED_PREP_TIME_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
3
|
import { freshMaterializedPrepTimeCounters } from './materialized-prep-time-classification-helpers';
|
|
4
4
|
import { mergeMaterializedPrepTimeCounters } from './materialized-prep-time-compile-helpers';
|
|
5
5
|
import { buildMaterializedTenantReportIteration } from './materialized-tenant-reports-iterations-helpers';
|
|
6
|
-
import { getMaterializedPeriodScopeFields } from './scope-helpers';
|
|
6
|
+
import { getMaterializedPeriodScopeFields, parseMaterializedLocalDate, } from './scope-helpers';
|
|
7
7
|
/**
|
|
8
8
|
* M2 close-day helpers.
|
|
9
9
|
*
|
|
@@ -154,6 +154,117 @@ export function foldMaterializedPrepTimePeriodRows({ periodScopeField, periodSco
|
|
|
154
154
|
}
|
|
155
155
|
/** The period scope contexts (everything except the day scope). */
|
|
156
156
|
export const MATERIALIZED_PREP_TIME_PERIOD_SCOPES = MATERIALIZED_PREP_TIME_REPORT_SCOPES.filter((scope) => !isMaterializedPrepTimeDayScope(String(scope)));
|
|
157
|
+
/** Period scopes that carry ENTRY rows / flat rows (month-capped). */
|
|
158
|
+
export const MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES = MATERIALIZED_PREP_TIME_ITEM_SCOPES.filter((scope) => !isMaterializedPrepTimeDayScope(String(scope)));
|
|
159
|
+
/* ------------------------------------------------------------------------ */
|
|
160
|
+
/* Period sibling-day enumeration */
|
|
161
|
+
/* ------------------------------------------------------------------------ */
|
|
162
|
+
const pad2 = (value) => String(value).padStart(2, '0');
|
|
163
|
+
const toLocalDateKey = (date) => `${date.getUTCFullYear()}-${pad2(date.getUTCMonth() + 1)}-${pad2(date.getUTCDate())}`;
|
|
164
|
+
const addUtcDays = (date, amount) => new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate() + amount));
|
|
165
|
+
const addMonthDays = (year, month, count, days) => {
|
|
166
|
+
if (!Number.isInteger(year) || !Number.isInteger(month) || month < 1 || month > 12)
|
|
167
|
+
return false;
|
|
168
|
+
for (let offset = 0; offset < count; offset += 1) {
|
|
169
|
+
const date = new Date(Date.UTC(year, month - 1 + offset, 1));
|
|
170
|
+
const monthYear = date.getUTCFullYear();
|
|
171
|
+
const monthNumber = date.getUTCMonth() + 1;
|
|
172
|
+
const last = new Date(Date.UTC(monthYear, monthNumber, 0)).getUTCDate();
|
|
173
|
+
for (let day = 1; day <= last; day += 1) {
|
|
174
|
+
days.add(`${monthYear}-${pad2(monthNumber)}-${pad2(day)}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return true;
|
|
178
|
+
};
|
|
179
|
+
const addPeriodDays = (scope, scopeField, days) => {
|
|
180
|
+
switch (scope) {
|
|
181
|
+
case MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK: {
|
|
182
|
+
const parts = parseMaterializedLocalDate(scopeField);
|
|
183
|
+
if (parts == null)
|
|
184
|
+
return false;
|
|
185
|
+
const start = new Date(Date.UTC(parts.year, parts.month - 1, parts.day));
|
|
186
|
+
if (start.getUTCDay() !== 0)
|
|
187
|
+
return false;
|
|
188
|
+
for (let offset = 0; offset < 7; offset += 1)
|
|
189
|
+
days.add(toLocalDateKey(addUtcDays(start, offset)));
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
case MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK_OF_YEAR: {
|
|
193
|
+
const match = /^(\d{4})-W(\d{2})$/.exec(scopeField);
|
|
194
|
+
if (match == null)
|
|
195
|
+
return false;
|
|
196
|
+
const weekYear = Number(match[1]);
|
|
197
|
+
const week = Number(match[2]);
|
|
198
|
+
if (!Number.isInteger(week) || week < 1 || week > 53)
|
|
199
|
+
return false;
|
|
200
|
+
const jan4 = new Date(Date.UTC(weekYear, 0, 4));
|
|
201
|
+
const isoWeekday = jan4.getUTCDay() || 7;
|
|
202
|
+
const weekOneMonday = addUtcDays(jan4, 1 - isoWeekday);
|
|
203
|
+
const start = addUtcDays(weekOneMonday, (week - 1) * 7);
|
|
204
|
+
for (let offset = 0; offset < 7; offset += 1)
|
|
205
|
+
days.add(toLocalDateKey(addUtcDays(start, offset)));
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
case MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH: {
|
|
209
|
+
const match = /^(\d{4})-(\d{2})$/.exec(scopeField);
|
|
210
|
+
if (match == null)
|
|
211
|
+
return false;
|
|
212
|
+
return addMonthDays(Number(match[1]), Number(match[2]), 1, days);
|
|
213
|
+
}
|
|
214
|
+
case MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER: {
|
|
215
|
+
const match = /^(\d{4})-Q([1-4])$/.exec(scopeField);
|
|
216
|
+
if (match == null)
|
|
217
|
+
return false;
|
|
218
|
+
return addMonthDays(Number(match[1]), (Number(match[2]) - 1) * 3 + 1, 3, days);
|
|
219
|
+
}
|
|
220
|
+
case MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER: {
|
|
221
|
+
const match = /^(\d{4})-S([12])$/.exec(scopeField);
|
|
222
|
+
if (match == null)
|
|
223
|
+
return false;
|
|
224
|
+
return addMonthDays(Number(match[1]), (Number(match[2]) - 1) * 6 + 1, 6, days);
|
|
225
|
+
}
|
|
226
|
+
case MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR: {
|
|
227
|
+
const match = /^(\d{4})$/.exec(scopeField);
|
|
228
|
+
if (match == null)
|
|
229
|
+
return false;
|
|
230
|
+
return addMonthDays(Number(match[1]), 1, 12, days);
|
|
231
|
+
}
|
|
232
|
+
default:
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
const enumeratePeriodDays = (localDate, scopes) => {
|
|
237
|
+
const periodFields = getMaterializedPeriodScopeFields(localDate);
|
|
238
|
+
if (periodFields == null)
|
|
239
|
+
return null;
|
|
240
|
+
const days = new Set([localDate]);
|
|
241
|
+
for (const scope of scopes) {
|
|
242
|
+
const scopeField = periodFields[scope];
|
|
243
|
+
if (typeof scopeField !== 'string' || scopeField.length === 0)
|
|
244
|
+
return null;
|
|
245
|
+
if (!addPeriodDays(scope, scopeField, days))
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
return [...days].sort();
|
|
249
|
+
};
|
|
250
|
+
/**
|
|
251
|
+
* Every `localDate` label whose DAY entry document can contribute to a
|
|
252
|
+
* month-capped period (week / ISO week / month). The worker uses this to fetch
|
|
253
|
+
* exactly the sibling day entries the entry fold needs — never a full-period
|
|
254
|
+
* entry document and never a year-wide scan.
|
|
255
|
+
*/
|
|
256
|
+
export function getMaterializedPrepTimePeriodDayScopeFields(localDate) {
|
|
257
|
+
return enumeratePeriodDays(localDate, MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Every `localDate` label whose DAY counter document can contribute to ANY
|
|
261
|
+
* report period the closing day rolls into, including quarter / semester /
|
|
262
|
+
* year. Used by the worker's SUMMARY counter fold only; entry rows stay
|
|
263
|
+
* month-capped.
|
|
264
|
+
*/
|
|
265
|
+
export function getMaterializedPrepTimeReportPeriodDayScopeFields(localDate) {
|
|
266
|
+
return enumeratePeriodDays(localDate, MATERIALIZED_PREP_TIME_PERIOD_SCOPES);
|
|
267
|
+
}
|
|
157
268
|
/** True when a stored prep counter document equals the incoming one (skip write). */
|
|
158
269
|
export function needsMaterializedPrepTimeReportWrite(existing, incoming) {
|
|
159
270
|
if (existing == null || incoming == null)
|
|
@@ -194,6 +305,7 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
194
305
|
if (reports.some((doc) => doc == null || doc.tenant_id !== tenantId))
|
|
195
306
|
return null;
|
|
196
307
|
const computedAt = input.computedAt ?? new Date();
|
|
308
|
+
const force = input.force === true;
|
|
197
309
|
const existing = input.existing ?? [];
|
|
198
310
|
const existingEntries = input.existingEntries ?? [];
|
|
199
311
|
const dateDoc = reports.find((doc) => doc.scope_context === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE) ?? null;
|
|
@@ -344,14 +456,14 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
344
456
|
sale_type_key_filters: existingDoc?.sale_type_key_filters ?? reports[0]?.sale_type_key_filters ?? [],
|
|
345
457
|
prep_time_counters: counters,
|
|
346
458
|
prep_time_counter_rows: counterRows,
|
|
347
|
-
iteration: existingDoc
|
|
459
|
+
iteration: existingDoc == null ? 1 : (existingDoc.iteration ?? 1) + 1,
|
|
348
460
|
schema_version: existingDoc?.schema_version ?? 1,
|
|
349
461
|
source_watermark: watermark ?? new Date(computedAt),
|
|
350
462
|
computed_at: computedAt,
|
|
351
463
|
createdAt: existingDoc?.createdAt ?? computedAt,
|
|
352
464
|
updatedAt: computedAt,
|
|
353
465
|
};
|
|
354
|
-
if (existingDoc != null && !needsMaterializedPrepTimeReportWrite(existingDoc, report)) {
|
|
466
|
+
if (!force && existingDoc != null && !needsMaterializedPrepTimeReportWrite(existingDoc, report)) {
|
|
355
467
|
skippedDocuments.push(existingDoc);
|
|
356
468
|
continue;
|
|
357
469
|
}
|
|
@@ -361,11 +473,20 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
361
473
|
// ---- the DAY scope: written straight from the compile --------------------
|
|
362
474
|
for (const doc of compiledDateDocs) {
|
|
363
475
|
const existingDoc = existingByScope.get(prepScopeKey(doc)) ?? null;
|
|
364
|
-
|
|
476
|
+
const report = {
|
|
477
|
+
...doc,
|
|
478
|
+
// A reclose REPLACES the stored day document: preserving its _id and
|
|
479
|
+
// createdAt keeps the identity + immutable fields stable (Mongo rejects a
|
|
480
|
+
// replace that alters _id).
|
|
481
|
+
_id: existingDoc?._id ?? doc._id,
|
|
482
|
+
createdAt: existingDoc?.createdAt ?? doc.createdAt,
|
|
483
|
+
iteration: existingDoc == null ? 1 : (existingDoc.iteration ?? 1) + 1,
|
|
484
|
+
};
|
|
485
|
+
if (!force && existingDoc != null && !needsMaterializedPrepTimeReportWrite(existingDoc, report)) {
|
|
365
486
|
skippedDocuments.push(existingDoc);
|
|
366
487
|
continue;
|
|
367
488
|
}
|
|
368
|
-
if (!addWrite(
|
|
489
|
+
if (!addWrite(report))
|
|
369
490
|
return null;
|
|
370
491
|
}
|
|
371
492
|
// ---- entry documents ------------------------------------------------------
|
|
@@ -399,7 +520,11 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
399
520
|
continue;
|
|
400
521
|
staleEntryDocuments.push(doc);
|
|
401
522
|
}
|
|
523
|
+
const itemPeriodScopes = new Set(MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES);
|
|
402
524
|
for (const { scope, field } of touchedPeriods.values()) {
|
|
525
|
+
// ENTRY documents are month-capped; quarter/semester/year are COUNTER only.
|
|
526
|
+
if (!itemPeriodScopes.has(String(scope)))
|
|
527
|
+
continue;
|
|
403
528
|
const sections = foldMaterializedPrepTimePeriodRows({
|
|
404
529
|
periodScope: scope,
|
|
405
530
|
periodScopeField: field,
|
|
@@ -441,14 +566,14 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
441
566
|
day_index: null,
|
|
442
567
|
sale_type_key_filters: existingDoc?.sale_type_key_filters ?? entries[0]?.sale_type_key_filters ?? [],
|
|
443
568
|
prep_time_entries: sections,
|
|
444
|
-
iteration: existingDoc
|
|
569
|
+
iteration: existingDoc == null ? 1 : (existingDoc.iteration ?? 1) + 1,
|
|
445
570
|
schema_version: existingDoc?.schema_version ?? 1,
|
|
446
571
|
source_watermark: entryWatermark ?? new Date(computedAt),
|
|
447
572
|
computed_at: computedAt,
|
|
448
573
|
createdAt: existingDoc?.createdAt ?? computedAt,
|
|
449
574
|
updatedAt: computedAt,
|
|
450
575
|
};
|
|
451
|
-
if (existingDoc != null && !needsMaterializedPrepTimeEntriesWrite(existingDoc, report)) {
|
|
576
|
+
if (!force && existingDoc != null && !needsMaterializedPrepTimeEntriesWrite(existingDoc, report)) {
|
|
452
577
|
skippedEntryDocuments.push(existingDoc);
|
|
453
578
|
continue;
|
|
454
579
|
}
|
|
@@ -459,11 +584,17 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
459
584
|
if (doc.scope_context !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
|
|
460
585
|
continue;
|
|
461
586
|
const existingDoc = existingEntriesByScope.get(prepScopeKey(doc)) ?? null;
|
|
462
|
-
|
|
587
|
+
const report = {
|
|
588
|
+
...doc,
|
|
589
|
+
_id: existingDoc?._id ?? doc._id,
|
|
590
|
+
createdAt: existingDoc?.createdAt ?? doc.createdAt,
|
|
591
|
+
iteration: existingDoc == null ? 1 : (existingDoc.iteration ?? 1) + 1,
|
|
592
|
+
};
|
|
593
|
+
if (!force && existingDoc != null && !needsMaterializedPrepTimeEntriesWrite(existingDoc, report)) {
|
|
463
594
|
skippedEntryDocuments.push(existingDoc);
|
|
464
595
|
continue;
|
|
465
596
|
}
|
|
466
|
-
if (!addEntryWrite(
|
|
597
|
+
if (!addEntryWrite(report))
|
|
467
598
|
return null;
|
|
468
599
|
}
|
|
469
600
|
return {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
|
-
import { MATERIALIZED_PREP_TIME_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, PREP_TIME_KEY_FILTERS, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { MATERIALIZED_PREP_TIME_ITEM_SCOPES, MATERIALIZED_PREP_TIME_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, PREP_TIME_KEY_FILTERS, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
3
|
import { accumulateOrderPrepTime, freshMaterializedPrepTimeCounters, } from './materialized-prep-time-classification-helpers';
|
|
4
4
|
import { getDayIndexFromLocalDate, getMaterializedPeriodScopeFields } from './scope-helpers';
|
|
5
5
|
/**
|
|
@@ -86,6 +86,7 @@ export function compileTenantPrepTimeReports(input) {
|
|
|
86
86
|
return null;
|
|
87
87
|
const dayIndex = getDayIndexFromLocalDate(localDate);
|
|
88
88
|
const at = computedAt ?? new Date();
|
|
89
|
+
const itemScopes = new Set(MATERIALIZED_PREP_TIME_ITEM_SCOPES);
|
|
89
90
|
const byScope = new Map();
|
|
90
91
|
const scopeContextOf = new Map();
|
|
91
92
|
const bucketFor = (scope, scopeField, key) => {
|
|
@@ -138,7 +139,10 @@ export function compileTenantPrepTimeReports(input) {
|
|
|
138
139
|
continue;
|
|
139
140
|
const bucket = bucketFor(scope, scopeField, key);
|
|
140
141
|
mergeMaterializedPrepTimeCounters(bucket.counters, result.counters);
|
|
141
|
-
|
|
142
|
+
// ENTRY rows / flat rows are month-capped; the Summary counters go to the
|
|
143
|
+
// full report range. Never accumulate quarter/semester/year item rows.
|
|
144
|
+
if (itemScopes.has(scope))
|
|
145
|
+
bucket.rows.push(...result.entries);
|
|
142
146
|
foldWatermark(bucket, watermark);
|
|
143
147
|
}
|
|
144
148
|
}
|
|
@@ -179,7 +183,7 @@ export function compileTenantPrepTimeReports(input) {
|
|
|
179
183
|
createdAt: at,
|
|
180
184
|
updatedAt: at,
|
|
181
185
|
});
|
|
182
|
-
if (Object.keys(entrySections).length > 0) {
|
|
186
|
+
if (itemScopes.has(scopeContext) && Object.keys(entrySections).length > 0) {
|
|
183
187
|
entries.push({
|
|
184
188
|
_id: new mongoose.Types.ObjectId().toHexString(),
|
|
185
189
|
tenant_id: tenantId,
|