@managesome/eposmatic-toolkit 0.4.51 → 0.4.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +106 -2
  2. package/esm/constants/enums/delivery.js +13 -0
  3. package/esm/helpers/materialized-reports/index.js +5 -0
  4. package/esm/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +8 -62
  5. package/esm/helpers/materialized-reports/materialized-delivery-classification-helpers.js +95 -73
  6. package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +118 -146
  7. package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +304 -71
  8. package/esm/helpers/materialized-reports/materialized-delivery-entry-pages.js +307 -0
  9. package/esm/helpers/materialized-reports/materialized-delivery-index-migration.js +103 -0
  10. package/esm/helpers/materialized-reports/materialized-delivery-rows.js +109 -0
  11. package/esm/helpers/materialized-reports/materialized-delivery-wipe-targets.js +32 -0
  12. package/esm/helpers/materialized-reports/materialized-document-size.js +65 -0
  13. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +31 -16
  14. package/esm/helpers/materialized-reports/materialized-integer-contract.js +10 -1
  15. package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +3 -91
  16. package/esm/helpers/materialized-reports/materialized-tenant-delivery-geo-helpers.js +597 -0
  17. package/esm/helpers/materialized-reports/scope-helpers.js +98 -0
  18. package/esm/helpers/materialized-reports/tenant-delivery-formatter.js +70 -20
  19. package/esm/helpers/utils/offer-item-resolution.js +39 -0
  20. package/esm/schemas/models/index.js +12 -10
  21. package/esm/schemas/models/materialized_tenant_delivery_entries.js +33 -12
  22. package/esm/schemas/models/materialized_tenant_delivery_geo_cells.js +83 -0
  23. package/esm/schemas/models/materialized_tenant_delivery_geo_scopes.js +35 -0
  24. package/esm/schemas/models/materialized_tenant_delivery_reports.js +10 -1
  25. package/esm/schemas/types/index.js +14 -12
  26. package/esm/schemas/types/materialized_tenant_delivery_geo_cells.js +1 -0
  27. package/esm/schemas/types/materialized_tenant_delivery_geo_scopes.js +1 -0
  28. package/lib/constants/MATERIALIZED_REPORTS.d.ts +95 -1
  29. package/lib/constants/MATERIALIZED_REPORTS.js +108 -4
  30. package/lib/constants/enums/delivery.d.ts +9 -0
  31. package/lib/constants/enums/delivery.js +14 -1
  32. package/lib/helpers/materialized-reports/index.d.ts +5 -0
  33. package/lib/helpers/materialized-reports/index.js +5 -0
  34. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +8 -62
  35. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +36 -9
  36. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +94 -72
  37. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +23 -26
  38. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +121 -150
  39. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +44 -5
  40. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +306 -69
  41. package/lib/helpers/materialized-reports/materialized-delivery-entry-pages.d.ts +128 -0
  42. package/lib/helpers/materialized-reports/materialized-delivery-entry-pages.js +322 -0
  43. package/lib/helpers/materialized-reports/materialized-delivery-index-migration.d.ts +48 -0
  44. package/lib/helpers/materialized-reports/materialized-delivery-index-migration.js +108 -0
  45. package/lib/helpers/materialized-reports/materialized-delivery-rows.d.ts +15 -0
  46. package/lib/helpers/materialized-reports/materialized-delivery-rows.js +115 -0
  47. package/lib/helpers/materialized-reports/materialized-delivery-wipe-targets.d.ts +17 -0
  48. package/lib/helpers/materialized-reports/materialized-delivery-wipe-targets.js +35 -0
  49. package/lib/helpers/materialized-reports/materialized-document-size.d.ts +47 -0
  50. package/lib/helpers/materialized-reports/materialized-document-size.js +73 -0
  51. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +28 -13
  52. package/lib/helpers/materialized-reports/materialized-integer-contract.js +10 -1
  53. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +2 -90
  54. package/lib/helpers/materialized-reports/materialized-tenant-delivery-geo-helpers.d.ts +186 -0
  55. package/lib/helpers/materialized-reports/materialized-tenant-delivery-geo-helpers.js +620 -0
  56. package/lib/helpers/materialized-reports/scope-helpers.d.ts +8 -0
  57. package/lib/helpers/materialized-reports/scope-helpers.js +99 -0
  58. package/lib/helpers/materialized-reports/tenant-delivery-formatter.d.ts +41 -0
  59. package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +70 -20
  60. package/lib/helpers/utils/offer-item-resolution.d.ts +28 -0
  61. package/lib/helpers/utils/offer-item-resolution.js +41 -0
  62. package/lib/schemas/models/index.d.ts +12 -10
  63. package/lib/schemas/models/index.js +12 -10
  64. package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +31 -9
  65. package/lib/schemas/models/materialized_tenant_delivery_entries.js +32 -11
  66. package/lib/schemas/models/materialized_tenant_delivery_geo_cells.d.ts +129 -0
  67. package/lib/schemas/models/materialized_tenant_delivery_geo_cells.js +86 -0
  68. package/lib/schemas/models/materialized_tenant_delivery_geo_scopes.d.ts +71 -0
  69. package/lib/schemas/models/materialized_tenant_delivery_geo_scopes.js +38 -0
  70. package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +10 -1
  71. package/lib/schemas/models/materialized_tenant_delivery_reports.js +10 -1
  72. package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +15 -15
  73. package/lib/schemas/types/index.d.ts +14 -12
  74. package/lib/schemas/types/index.js +14 -12
  75. package/lib/schemas/types/materialized_tenant_delivery_entries.d.ts +36 -4
  76. package/lib/schemas/types/materialized_tenant_delivery_geo_cells.d.ts +178 -0
  77. package/lib/schemas/types/materialized_tenant_delivery_geo_cells.js +2 -0
  78. package/lib/schemas/types/materialized_tenant_delivery_geo_scopes.d.ts +21 -0
  79. package/lib/schemas/types/materialized_tenant_delivery_geo_scopes.js +2 -0
  80. package/lib/schemas/types/materialized_tenant_delivery_reports.d.ts +49 -7
  81. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.js +56 -19
  82. package/lib/tests/materialized-reports/materialized-delivery-budgets.test.d.ts +1 -0
  83. package/lib/tests/materialized-reports/materialized-delivery-budgets.test.js +91 -0
  84. package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +271 -15
  85. package/lib/tests/materialized-reports/materialized-delivery-final-audit.test.d.ts +1 -0
  86. package/lib/tests/materialized-reports/materialized-delivery-final-audit.test.js +128 -0
  87. package/lib/tests/materialized-reports/materialized-delivery-fold.test.js +42 -5
  88. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +13 -1
  89. package/lib/tests/materialized-reports/materialized-delivery-geo.test.d.ts +1 -0
  90. package/lib/tests/materialized-reports/materialized-delivery-geo.test.js +446 -0
  91. package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +117 -25
  92. package/lib/tests/materialized-reports/materialized-delivery-practical.test.d.ts +1 -0
  93. package/lib/tests/materialized-reports/materialized-delivery-practical.test.js +344 -0
  94. package/lib/tests/materialized-reports/materialized-integer-contract.test.js +14 -0
  95. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +1 -1
  96. package/lib/tests/offer-item-resolution.test.js +57 -0
  97. package/package.json +4 -2
@@ -1,10 +1,13 @@
1
- import { MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
2
1
  import mongoose from 'mongoose';
3
- import { DELIVERY_KEY_FILTERS, MATERIALIZED_DELIVERY_REPORT_SCOPES, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
4
- import { buildMaterializedTenantReportIteration } from './materialized-tenant-reports-iterations-helpers';
5
- import { getMaterializedPeriodScopeFields } from './scope-helpers';
6
- import { freshDeliveryCounters } from './materialized-delivery-classification-helpers';
2
+ import { DELIVERY_KEY_FILTERS, MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DELIVERY_REPORT_MAX_BSON_BYTES, MATERIALIZED_DELIVERY_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
3
+ import { MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_delivery_reports';
7
4
  import { mergeMaterializedDeliveryCounters } from './materialized-delivery-accumulator-helpers';
5
+ import { freshDeliveryCounters } from './materialized-delivery-classification-helpers';
6
+ import { pruneDocumentDeliveryAreas, pruneDocumentDeliveryRiders } from './materialized-delivery-compile-helpers';
7
+ import { deliveryEntryPageOf, materializedDeliveryEntryPageKey } from './materialized-delivery-entry-pages';
8
+ import { assertMaterializedDocumentSize } from './materialized-document-size';
9
+ import { buildMaterializedTenantReportIteration } from './materialized-tenant-reports-iterations-helpers';
10
+ import { enumerateMaterializedPeriodDays, getMaterializedPeriodScopeFields } from './scope-helpers';
8
11
  /**
9
12
  * M4 close-day helpers.
10
13
  *
@@ -20,50 +23,27 @@ import { mergeMaterializedDeliveryCounters } from './materialized-delivery-accum
20
23
  * A folded document that inherited the STORED document watermark would look unchanged to
21
24
  * the write guard and be skipped forever, freezing the period at its first closed day.
22
25
  * That bug shipped once in the prep time report; it is designed out here from the start.
26
+ *
27
+ * ## ENTRY pages are NOT folded
28
+ *
29
+ * `materialized_tenant_delivery_entries` is page-partitioned by source day (the discounts
30
+ * split). The plan never reads, merges or folds another day's page: the caller fetches
31
+ * exactly the closing day's pages, each is replaced by its page identity, and a fetched
32
+ * page the day no longer owns is deleted. There is no quarter/semester/year entry scope.
23
33
  */
24
- /** Period scopes this report materializes (everything but the day scope). */
34
+ /** Period COUNTER scopes this report materializes (everything but the day scope). */
25
35
  export const MATERIALIZED_DELIVERY_PERIOD_SCOPES = MATERIALIZED_DELIVERY_REPORT_SCOPES.filter((scope) => String(scope) !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
26
- /** True when the scope is the plain day scope. */
27
- export const isMaterializedDeliveryDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
28
- const deliveryScopeKey = (doc) => String(doc.scope_context) + '|' + String(doc.scope_field);
29
36
  /**
30
- * Merges TWO period entry sections, keyed on `entry_key`.
31
- *
32
- * Rows are deduplicated by identity: a source delivery belongs to exactly one day, so the
33
- * same `entry_key` twice means the fold saw it twice.
37
+ * Every `localDate` label whose DAY counter document can contribute to a REPORT period
38
+ * the closing day rolls into - week through YEAR. The worker uses it to fetch exactly
39
+ * the sibling DAY counters the period re-fold needs.
34
40
  */
35
- export function mergeMaterializedDeliveryEntrySections(target, source) {
36
- const rowsByKey = new Map();
37
- const seenByKey = new Map();
38
- const absorb = (input) => {
39
- if (input == null)
40
- return;
41
- for (const [key, section] of Object.entries(input)) {
42
- const rows = rowsByKey.get(key) ?? [];
43
- const seen = seenByKey.get(key) ?? new Set();
44
- for (const row of section?.rows ?? []) {
45
- const identity = String(row.entry_key ?? row.delivery_id ?? '');
46
- if (identity !== '') {
47
- if (seen.has(identity))
48
- continue;
49
- seen.add(identity);
50
- }
51
- rows.push(row);
52
- }
53
- rowsByKey.set(key, rows);
54
- seenByKey.set(key, seen);
55
- }
56
- };
57
- absorb(target);
58
- absorb(source);
59
- const out = {};
60
- for (const [key, rows] of rowsByKey) {
61
- rows.sort((a, b) => new Date(b.delivered_at ?? b.created_at).getTime() - new Date(a.delivered_at ?? a.created_at).getTime() ||
62
- (String(a.delivery_id) < String(b.delivery_id) ? 1 : -1));
63
- out[key] = { rows };
64
- }
65
- return out;
41
+ export function getMaterializedDeliveryReportPeriodDayScopeFields(localDate) {
42
+ return enumerateMaterializedPeriodDays(localDate, MATERIALIZED_DELIVERY_PERIOD_SCOPES);
66
43
  }
44
+ /** True when the scope is the plain day scope. */
45
+ export const isMaterializedDeliveryDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
46
+ const deliveryScopeKey = (doc) => String(doc.scope_context) + '|' + String(doc.scope_field);
67
47
  /** True when the stored document already carries this exact content. */
68
48
  export function needsMaterializedDeliveryReportWrite(existing, next) {
69
49
  if (existing == null)
@@ -102,25 +82,6 @@ export function foldMaterializedDeliveryPeriodCounters(input) {
102
82
  }
103
83
  return folded;
104
84
  }
105
- /** Folds the statement rows of every day document belonging to one period scope. */
106
- export function foldMaterializedDeliveryPeriodRows(input) {
107
- const { periodScope, periodScopeField, dayDocuments } = input;
108
- let merged = {};
109
- let offApp = 0;
110
- for (const dayDocument of dayDocuments ?? []) {
111
- if (!isMaterializedDeliveryDayScope(String(dayDocument?.scope_context)))
112
- continue;
113
- const fields = getMaterializedPeriodScopeFields(String(dayDocument.scope_field));
114
- if (fields == null)
115
- continue;
116
- if (fields[periodScope] !== periodScopeField)
117
- continue;
118
- merged = mergeMaterializedDeliveryEntrySections(merged, dayDocument.delivery_entries ?? null);
119
- // Off-app orders cannot be rows, so the period total is a SUM of the day counts.
120
- offApp += Number(dayDocument.off_app_order_count) || 0;
121
- }
122
- return { sections: merged, offApp };
123
- }
124
85
  /**
125
86
  * Builds the close-day plan for ONE tenant-day.
126
87
  *
@@ -138,6 +99,7 @@ export function buildTenantDeliveryCloseDayPlan(input) {
138
99
  if (typeof reason !== 'string' || reason.length === 0)
139
100
  return null;
140
101
  const computedAt = input.computedAt ?? new Date();
102
+ const force = input.force === true;
141
103
  const compiledReports = (reports ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
142
104
  const compiledEntries = (entries ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
143
105
  const storedReports = (input.existing ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
@@ -155,9 +117,14 @@ export function buildTenantDeliveryCloseDayPlan(input) {
155
117
  const storedReportsByScope = new Map();
156
118
  for (const doc of storedReports)
157
119
  storedReportsByScope.set(deliveryScopeKey(doc), doc);
158
- const storedEntriesByScope = new Map();
159
- for (const doc of storedEntries)
160
- storedEntriesByScope.set(deliveryScopeKey(doc), doc);
120
+ const storedEntriesByPage = new Map();
121
+ for (const doc of storedEntries) {
122
+ // Legacy documents without a page are inconsistent input - the caller must only
123
+ // fetch page-partitioned documents (the reader keeps no legacy fallback).
124
+ if (typeof doc.page !== 'string' || doc.page === '')
125
+ return null;
126
+ storedEntriesByPage.set(materializedDeliveryEntryPageKey(doc), doc);
127
+ }
161
128
  const addWrite = (report) => {
162
129
  const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
163
130
  const iteration = buildMaterializedTenantReportIteration(persisted, {
@@ -190,23 +157,24 @@ export function buildTenantDeliveryCloseDayPlan(input) {
190
157
  _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
191
158
  iteration: Number(existing?.iteration ?? 0) + 1,
192
159
  };
193
- if (needsMaterializedDeliveryReportWrite(existing, next))
160
+ if (force || needsMaterializedDeliveryReportWrite(existing, next))
194
161
  addWrite(next);
195
162
  else
196
163
  skippedDocuments.push(existing);
197
164
  }
198
- const compiledDateEntries = compiledEntries.find((doc) => isMaterializedDeliveryDayScope(String(doc.scope_context)));
199
- if (compiledDateEntries != null) {
200
- const existing = storedEntriesByScope.get(deliveryScopeKey(compiledDateEntries));
201
- const next = {
202
- ...compiledDateEntries,
203
- _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
204
- iteration: Number(existing?.iteration ?? 0) + 1,
205
- };
206
- if (needsMaterializedDeliveryEntriesWrite(existing, next))
207
- addEntryWrite(next);
208
- else
209
- skippedEntryDocuments.push(existing);
165
+ // ---- ENTRY PAGES: exactly the pages the closing day owns ----
166
+ // A page is replaced wholesale; a page with no compiled counterpart is stale and is
167
+ // deleted. No other day's page is in `existingEntries`, so nothing else is touched.
168
+ const compiledEntriesByPage = new Map();
169
+ for (const doc of compiledEntries) {
170
+ const expectedPage = deliveryEntryPageOf(doc.scope_context, localDate);
171
+ if (expectedPage == null || (doc.page !== expectedPage && !String(doc.page ?? '').startsWith(expectedPage + '#'))) {
172
+ return null;
173
+ }
174
+ const key = materializedDeliveryEntryPageKey(doc);
175
+ if (compiledEntriesByPage.has(key))
176
+ return null; // duplicate page - data issue
177
+ compiledEntriesByPage.set(key, doc);
210
178
  }
211
179
  // ---- DAY deletion: a stored day doc that no longer compiled ----
212
180
  const compiledDateKeys = new Set(compiledReports
@@ -223,28 +191,12 @@ export function buildTenantDeliveryCloseDayPlan(input) {
223
191
  staleDocuments.push(doc);
224
192
  removedDates.push(String(doc.scope_field));
225
193
  }
226
- const compiledDateEntryKeys = new Set(compiledEntries
227
- .filter((doc) => isMaterializedDeliveryDayScope(String(doc.scope_context)))
228
- .map((doc) => deliveryScopeKey(doc)));
229
- for (const doc of storedEntries) {
230
- if (!isMaterializedDeliveryDayScope(String(doc.scope_context)))
231
- continue;
232
- if (String(doc.scope_field) !== localDate)
233
- continue;
234
- if (compiledDateEntryKeys.has(deliveryScopeKey(doc)))
235
- continue;
236
- staleEntryDocuments.push(doc);
237
- }
238
194
  // ---- PERIOD re-fold ----
239
195
  const nextDayDocs = storedReports.filter((doc) => isMaterializedDeliveryDayScope(String(doc.scope_context)) && String(doc.scope_field) !== localDate);
240
196
  if (compiledDateDoc != null) {
241
197
  nextDayDocs.push(writes.find((w) => isMaterializedDeliveryDayScope(String(w.report.scope_context)))?.report ??
242
198
  compiledDateDoc);
243
199
  }
244
- const nextDayEntryDocs = storedEntries.filter((doc) => isMaterializedDeliveryDayScope(String(doc.scope_context)) && String(doc.scope_field) !== localDate);
245
- const compiledDateEntryDoc = entryWrites.find((w) => isMaterializedDeliveryDayScope(String(w.report.scope_context)))?.report;
246
- if (compiledDateEntryDoc != null)
247
- nextDayEntryDocs.push(compiledDateEntryDoc);
248
200
  const touchedPeriods = new Map();
249
201
  const noteDay = (day) => {
250
202
  const fields = getMaterializedPeriodScopeFields(day);
@@ -261,11 +213,15 @@ export function buildTenantDeliveryCloseDayPlan(input) {
261
213
  for (const day of removedDates)
262
214
  noteDay(day);
263
215
  for (const { scope, field } of touchedPeriods.values()) {
264
- const folded = foldMaterializedDeliveryPeriodCounters({
216
+ let folded = foldMaterializedDeliveryPeriodCounters({
265
217
  periodScope: scope,
266
218
  periodScopeField: field,
267
219
  dayDocuments: nextDayDocs,
268
220
  });
221
+ // Each DAY is bounded to top-N cities + riders. The period fold unions 28-31 such
222
+ // bounds, so re-bound BOTH maps at the DOCUMENT level after the merge.
223
+ folded = pruneDocumentDeliveryAreas(folded);
224
+ pruneDocumentDeliveryRiders(folded);
269
225
  const existing = storedReportsByScope.get(scope + '|' + field);
270
226
  const keys = Object.keys(folded).sort();
271
227
  if (keys.length === 0) {
@@ -286,71 +242,87 @@ export function buildTenantDeliveryCloseDayPlan(input) {
286
242
  scope_context: scope,
287
243
  scope_field: field,
288
244
  day_index: null,
289
- sale_type_key_filters: existing?.sale_type_key_filters ?? [...DELIVERY_KEY_FILTERS],
245
+ // Page-model documents carry the CURRENT key dimensions. Never inherit a
246
+ // pre-page (v1) filter array: the reader's `$all` gate would keep the period
247
+ // document invisible forever.
248
+ sale_type_key_filters: [...DELIVERY_KEY_FILTERS],
290
249
  delivery_counters: counters,
291
250
  delivery_counter_rows: counterRows,
292
251
  iteration: existing?.iteration,
293
- schema_version: existing?.schema_version ?? 1,
294
- // FROM THE FOLDED DAYS - never the stored watermark.
295
- source_watermark: watermarkOfDays(nextDayDocs, existing?.source_watermark ?? computedAt),
252
+ // A folded period is always a CURRENT-shape document. v1 data must be wiped and
253
+ // re-closed; it is never upgraded in place.
254
+ schema_version: compiledDateDoc?.schema_version ?? MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION,
255
+ // FROM THE DAYS THAT BELONG TO THIS PERIOD - never the stored watermark, and never
256
+ // an unrelated day from another period. Using every sibling day would rewrite a
257
+ // period whenever any other month moved.
258
+ source_watermark: watermarkOfDays(nextDayDocs.filter((dayDoc) => {
259
+ const fields = getMaterializedPeriodScopeFields(String(dayDoc?.scope_field));
260
+ return fields != null && fields[scope] === field;
261
+ }), existing?.source_watermark ?? computedAt),
296
262
  computed_at: computedAt,
297
263
  createdAt: existing?.createdAt ?? computedAt,
298
264
  updatedAt: computedAt,
299
265
  };
300
266
  const next = { ...report, iteration: Number(existing?.iteration ?? 0) + 1 };
301
- if (needsMaterializedDeliveryReportWrite(existing, next))
267
+ if (force || needsMaterializedDeliveryReportWrite(existing, next))
302
268
  addWrite(next);
303
269
  else
304
270
  skippedDocuments.push(existing);
305
271
  }
306
- for (const { scope, field } of touchedPeriods.values()) {
307
- const { sections, offApp } = foldMaterializedDeliveryPeriodRows({
308
- periodScope: scope,
309
- periodScopeField: field,
310
- dayDocuments: nextDayEntryDocs,
311
- });
312
- const existing = storedEntriesByScope.get(scope + '|' + field);
313
- const keys = Object.keys(sections);
314
- if (keys.length === 0 && offApp === 0) {
315
- if (existing != null)
316
- staleEntryDocuments.push(existing);
317
- continue;
318
- }
319
- let watermark = existing?.source_watermark ?? null;
320
- for (const dayDoc of nextDayEntryDocs) {
321
- const fields = getMaterializedPeriodScopeFields(String(dayDoc.scope_field));
322
- if (fields == null)
323
- continue;
324
- if (fields[scope] !== field)
325
- continue;
326
- const time = new Date(dayDoc.source_watermark ?? 0).getTime();
327
- if (Number.isNaN(time))
328
- continue;
329
- if (watermark == null || time > new Date(watermark).getTime())
330
- watermark = new Date(time);
331
- }
332
- const report = {
272
+ // ---- entry PAGE writes (replace by page identity) ----
273
+ for (const compiled of compiledEntriesByPage.values()) {
274
+ const key = materializedDeliveryEntryPageKey(compiled);
275
+ const existing = storedEntriesByPage.get(key) ?? null;
276
+ const next = {
277
+ ...compiled,
333
278
  _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
334
- tenant_id: tenantId,
335
- brand_id: existing?.brand_id ?? compiledDateEntries?.brand_id ?? '',
336
- scope_context: scope,
337
- scope_field: field,
338
- day_index: null,
339
- sale_type_key_filters: existing?.sale_type_key_filters ?? [...DELIVERY_KEY_FILTERS],
340
- delivery_entries: Object.keys(sections).length === 0 ? null : sections,
341
- off_app_order_count: offApp,
342
- iteration: existing?.iteration,
343
- schema_version: existing?.schema_version ?? 1,
344
- source_watermark: watermark ?? computedAt,
345
- computed_at: computedAt,
346
- createdAt: existing?.createdAt ?? computedAt,
347
- updatedAt: computedAt,
279
+ iteration: Number(existing?.iteration ?? 0) + 1,
348
280
  };
349
- const next = { ...report, iteration: Number(existing?.iteration ?? 0) + 1 };
350
- if (needsMaterializedDeliveryEntriesWrite(existing, next))
351
- addEntryWrite(next);
352
- else
281
+ if (existing == null || force || needsMaterializedDeliveryEntriesWrite(existing, next)) {
282
+ if (!addEntryWrite(next))
283
+ return null;
284
+ }
285
+ else {
353
286
  skippedEntryDocuments.push(existing);
287
+ }
288
+ }
289
+ // Anything fetched but not recompiled is a page this day no longer owns (an emptied
290
+ // day, or a row set that disappeared): delete exactly that page.
291
+ for (const doc of storedEntriesByPage.values()) {
292
+ if (compiledEntriesByPage.has(materializedDeliveryEntryPageKey(doc)))
293
+ continue;
294
+ staleEntryDocuments.push(doc);
295
+ }
296
+ // BSON BUDGETS: a counter fold or one day's page can exceed its budget even when the
297
+ // compile looked fine. Fail here, naming the collection + day + scope, instead of at
298
+ // bulkWrite inside the close.
299
+ for (const write of writes) {
300
+ assertMaterializedDocumentSize(write.report, {
301
+ label: 'materialized_tenant_delivery_reports:' +
302
+ tenantId +
303
+ ':' +
304
+ localDate +
305
+ ':' +
306
+ String(write.report.scope_context) +
307
+ ':' +
308
+ write.report.scope_field,
309
+ maxBytes: MATERIALIZED_DELIVERY_REPORT_MAX_BSON_BYTES,
310
+ });
311
+ }
312
+ for (const write of entryWrites) {
313
+ assertMaterializedDocumentSize(write.report, {
314
+ label: 'materialized_tenant_delivery_entries:' +
315
+ tenantId +
316
+ ':' +
317
+ localDate +
318
+ ':' +
319
+ String(write.report.scope_context) +
320
+ ':' +
321
+ write.report.scope_field +
322
+ ':page=' +
323
+ write.report.page,
324
+ maxBytes: MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES,
325
+ });
354
326
  }
355
327
  return {
356
328
  writes,