@managesome/eposmatic-toolkit 0.4.52 → 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 (107) 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/schemas/models/applied_order_discounts.js +103 -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/applied_order_discounts.js +1 -0
  26. package/esm/schemas/types/index.js +14 -12
  27. package/esm/schemas/types/materialized_tenant_delivery_geo_cells.js +1 -0
  28. package/esm/schemas/types/materialized_tenant_delivery_geo_scopes.js +1 -0
  29. package/lib/constants/MATERIALIZED_REPORTS.d.ts +95 -1
  30. package/lib/constants/MATERIALIZED_REPORTS.js +108 -4
  31. package/lib/constants/enums/delivery.d.ts +9 -0
  32. package/lib/constants/enums/delivery.js +14 -1
  33. package/lib/helpers/materialized-reports/index.d.ts +5 -0
  34. package/lib/helpers/materialized-reports/index.js +5 -0
  35. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +8 -62
  36. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +36 -9
  37. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +94 -72
  38. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +23 -26
  39. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +121 -150
  40. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +44 -5
  41. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +306 -69
  42. package/lib/helpers/materialized-reports/materialized-delivery-entry-pages.d.ts +128 -0
  43. package/lib/helpers/materialized-reports/materialized-delivery-entry-pages.js +322 -0
  44. package/lib/helpers/materialized-reports/materialized-delivery-index-migration.d.ts +48 -0
  45. package/lib/helpers/materialized-reports/materialized-delivery-index-migration.js +108 -0
  46. package/lib/helpers/materialized-reports/materialized-delivery-rows.d.ts +15 -0
  47. package/lib/helpers/materialized-reports/materialized-delivery-rows.js +115 -0
  48. package/lib/helpers/materialized-reports/materialized-delivery-wipe-targets.d.ts +17 -0
  49. package/lib/helpers/materialized-reports/materialized-delivery-wipe-targets.js +35 -0
  50. package/lib/helpers/materialized-reports/materialized-document-size.d.ts +47 -0
  51. package/lib/helpers/materialized-reports/materialized-document-size.js +73 -0
  52. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +28 -13
  53. package/lib/helpers/materialized-reports/materialized-integer-contract.js +10 -1
  54. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +2 -90
  55. package/lib/helpers/materialized-reports/materialized-tenant-delivery-geo-helpers.d.ts +186 -0
  56. package/lib/helpers/materialized-reports/materialized-tenant-delivery-geo-helpers.js +620 -0
  57. package/lib/helpers/materialized-reports/scope-helpers.d.ts +8 -0
  58. package/lib/helpers/materialized-reports/scope-helpers.js +99 -0
  59. package/lib/helpers/materialized-reports/tenant-delivery-formatter.d.ts +41 -0
  60. package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +70 -20
  61. package/lib/helpers/order/payment-collection.d.ts +25 -0
  62. package/lib/helpers/order/payment-collection.js +51 -0
  63. package/lib/schemas/models/applied_order_discounts.d.ts +80 -0
  64. package/lib/schemas/models/applied_order_discounts.js +106 -0
  65. package/lib/schemas/models/index.d.ts +12 -10
  66. package/lib/schemas/models/index.js +12 -10
  67. package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +31 -9
  68. package/lib/schemas/models/materialized_tenant_delivery_entries.js +32 -11
  69. package/lib/schemas/models/materialized_tenant_delivery_geo_cells.d.ts +129 -0
  70. package/lib/schemas/models/materialized_tenant_delivery_geo_cells.js +86 -0
  71. package/lib/schemas/models/materialized_tenant_delivery_geo_scopes.d.ts +71 -0
  72. package/lib/schemas/models/materialized_tenant_delivery_geo_scopes.js +38 -0
  73. package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +10 -1
  74. package/lib/schemas/models/materialized_tenant_delivery_reports.js +10 -1
  75. package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +15 -15
  76. package/lib/schemas/types/applied_order_discounts.d.ts +22 -0
  77. package/lib/schemas/types/applied_order_discounts.js +2 -0
  78. package/lib/schemas/types/index.d.ts +14 -12
  79. package/lib/schemas/types/index.js +14 -12
  80. package/lib/schemas/types/materialized_tenant_delivery_entries.d.ts +36 -4
  81. package/lib/schemas/types/materialized_tenant_delivery_geo_cells.d.ts +178 -0
  82. package/lib/schemas/types/materialized_tenant_delivery_geo_cells.js +2 -0
  83. package/lib/schemas/types/materialized_tenant_delivery_geo_scopes.d.ts +21 -0
  84. package/lib/schemas/types/materialized_tenant_delivery_geo_scopes.js +2 -0
  85. package/lib/schemas/types/materialized_tenant_delivery_reports.d.ts +49 -7
  86. package/lib/tests/materialized-reports/analytics-growth-helpers.test.d.ts +1 -0
  87. package/lib/tests/materialized-reports/analytics-overview-helpers.test.d.ts +1 -0
  88. package/lib/tests/materialized-reports/analytics-performance-helpers.test.d.ts +1 -0
  89. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.js +56 -19
  90. package/lib/tests/materialized-reports/materialized-delivery-budgets.test.d.ts +1 -0
  91. package/lib/tests/materialized-reports/materialized-delivery-budgets.test.js +91 -0
  92. package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +271 -15
  93. package/lib/tests/materialized-reports/materialized-delivery-final-audit.test.d.ts +1 -0
  94. package/lib/tests/materialized-reports/materialized-delivery-final-audit.test.js +128 -0
  95. package/lib/tests/materialized-reports/materialized-delivery-fold.test.js +42 -5
  96. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +13 -1
  97. package/lib/tests/materialized-reports/materialized-delivery-geo.test.d.ts +1 -0
  98. package/lib/tests/materialized-reports/materialized-delivery-geo.test.js +446 -0
  99. package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +117 -25
  100. package/lib/tests/materialized-reports/materialized-delivery-practical.test.d.ts +1 -0
  101. package/lib/tests/materialized-reports/materialized-delivery-practical.test.js +344 -0
  102. package/lib/tests/materialized-reports/materialized-integer-contract.test.js +14 -0
  103. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +1 -1
  104. package/lib/tests/offer-item-resolution.test.js +1 -4
  105. package/lib/tests/payment-collection-timestamps.test.d.ts +1 -0
  106. package/lib/tests/payment-collection-timestamps.test.js +71 -0
  107. package/package.json +4 -2
@@ -1,23 +1,31 @@
1
+ import { MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_COLLECTION_NAME } from '../../constants/MATERIALIZED_REPORTS';
1
2
  import { MATERIALIZED_TENANT_CREDIT_BILLS_COLLECTION_NAME, MATERIALIZED_TENANT_CREDIT_BILLS_INDEX_OPTIONS, } from '../../schemas/models/materialized_credit_bills';
2
- import { MATERIALIZED_TENANT_DISCOUNT_ENTRIES_INDEX_OPTIONS, MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_NAME, } from '../../schemas/models/materialized_tenant_discount_entries';
3
- import { MATERIALIZED_TENANT_DISCOUNT_REPORTS_INDEX_OPTIONS, MATERIALIZED_TENANT_DISCOUNT_REPORTS_COLLECTION_NAME, } from '../../schemas/models/materialized_tenant_discount_reports';
4
- import { MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS, MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME, } from '../../schemas/models/materialized_tenant_discount_sources';
5
- import { MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME, MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_reports_iterations';
3
+ 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';
4
+ import { MATERIALIZED_TENANT_DELIVERY_ENTRIES_COLLECTION_NAME, MATERIALIZED_TENANT_DELIVERY_ENTRIES_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_delivery_entries';
5
+ import { MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_COLLECTION_NAME, MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_delivery_geo_cells';
6
+ import { MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_INDEX_OPTIONS } from '../../schemas/models/materialized_tenant_delivery_geo_scopes';
7
+ import { MATERIALIZED_TENANT_DELIVERY_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_DELIVERY_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_delivery_reports';
8
+ import { MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_NAME, MATERIALIZED_TENANT_DISCOUNT_ENTRIES_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_discount_entries';
9
+ import { MATERIALIZED_TENANT_DISCOUNT_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_DISCOUNT_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_discount_reports';
10
+ import { MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME, MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_discount_sources';
6
11
  import { MATERIALIZED_TENANT_PREP_TIME_ENTRIES_COLLECTION_NAME, MATERIALIZED_TENANT_PREP_TIME_ENTRIES_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_prep_time_entries';
7
12
  import { MATERIALIZED_TENANT_PREP_TIME_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_PREP_TIME_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_prep_time_reports';
13
+ import { MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME, MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_reports_iterations';
8
14
  import { MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_sales_overview_reports';
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';
10
- import { TENANT_CALENDAR_COLLECTION_NAME, TENANT_CALENDAR_INDEX_OPTIONS } from '../../schemas/models/tenant_calendar';
11
15
  import { TENANT_ACTIVE_JOBS_COLLECTION_NAME, TENANT_ACTIVE_JOBS_INDEX_OPTIONS, } from '../../schemas/models/tenant_active_jobs';
16
+ import { TENANT_CALENDAR_COLLECTION_NAME, TENANT_CALENDAR_INDEX_OPTIONS } from '../../schemas/models/tenant_calendar';
12
17
  /**
13
- * BOOTSTRAP the indexes the materialized reads/writes assume.
18
+ * MANUAL index helper for the materialized reads/writes.
19
+ *
20
+ * The services run with `autoIndex: false`, and the model schemas deliberately do not
21
+ * declare these compound indexes. This module is available for an explicit maintenance
22
+ * run / tests, but it is NOT called automatically after `mongoose.connect` by
23
+ * items/workers/operations. Indexes are an OWNER DEPLOY STEP, applied with the migration
24
+ * scripts before the first close/read. Do not describe this as a runtime bootstrap.
14
25
  *
15
- * The services run with `autoIndex: false`, and the model schemas deliberately
16
- * do not declare these compound indexes. Until now the only creator was a
17
- * manual simulation script; a fresh deployment therefore failed every
18
- * `.hint()` read and could create duplicate scope/year documents. This helper
19
- * is called after `mongoose.connect` by items/workers/operations and fails the
20
- * startup if an expected unique index is missing afterwards.
26
+ * For the Delivery page model, use
27
+ * `simulations/migrate-delivery-report-indexes.ts --apply` (it also drops the legacy
28
+ * entry index and creates the deliveries source and geo-scope indexes).
21
29
  *
22
30
  * It creates indexes with `createIndexes` (never drops unrelated indexes).
23
31
  */
@@ -30,6 +38,10 @@ const INDEXES = {
30
38
  [MATERIALIZED_TENANT_PREP_TIME_REPORTS_COLLECTION_NAME]: MATERIALIZED_TENANT_PREP_TIME_REPORTS_INDEX_OPTIONS,
31
39
  [MATERIALIZED_TENANT_PREP_TIME_ENTRIES_COLLECTION_NAME]: MATERIALIZED_TENANT_PREP_TIME_ENTRIES_INDEX_OPTIONS,
32
40
  [MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME]: MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS,
41
+ [MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_COLLECTION_NAME]: MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_INDEX_OPTIONS,
42
+ [MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_COLLECTION_NAME]: MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_INDEX_OPTIONS,
43
+ [MATERIALIZED_TENANT_DELIVERY_ENTRIES_COLLECTION_NAME]: MATERIALIZED_TENANT_DELIVERY_ENTRIES_INDEX_OPTIONS,
44
+ [MATERIALIZED_TENANT_DELIVERY_REPORTS_COLLECTION_NAME]: MATERIALIZED_TENANT_DELIVERY_REPORTS_INDEX_OPTIONS,
33
45
  [MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME]: MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS,
34
46
  [MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME]: MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS,
35
47
  [TENANT_CALENDAR_COLLECTION_NAME]: TENANT_CALENDAR_INDEX_OPTIONS,
@@ -48,9 +60,12 @@ export async function ensureMaterializedReportIndexes(connection, options = {})
48
60
  }
49
61
  const collection = db.collection(collectionName);
50
62
  const indexes = definitions.filter((definition) => definition.name !== '_id_');
51
- // One-time migration for the old discount-entry identity index. A page
52
- // design cannot coexist with a unique index that omits `page`.
53
- if (collectionName === MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_NAME) {
63
+ // One-time migration for a per-SCOPE entry identity index. A PAGE design cannot
64
+ // coexist with a unique index that omits `page` (all period pages share
65
+ // `day_index: null`, so the legacy index would reject the second page of a month).
66
+ // Discounts and Delivery entries have the same migration.
67
+ if (collectionName === MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_NAME ||
68
+ collectionName === MATERIALIZED_TENANT_DELIVERY_ENTRIES_COLLECTION_NAME) {
54
69
  const existing = await collection
55
70
  .listIndexes()
56
71
  .toArray()
@@ -44,6 +44,15 @@ const IDENTIFIER_FIELD = /^(scope_field|day_index|key|_id|.*_id)$/;
44
44
  * numeric-looking string when it sits in a field that is not one of these.
45
45
  */
46
46
  const TEXT_FIELD = /(name|label|title|reason|note|description|reference|reference_number|code|currency|platform|status|type|source_key|order_number)$/i;
47
+ /**
48
+ * GEOMETRY leaves are the one exception to the integer-only contract.
49
+ *
50
+ * `location.coordinates` is a GeoJSON `[lng, lat]` pair - a POSITION, not a stored
51
+ * measurement. Rounding it to an integer would move a heat cell's centroid by up to a
52
+ * degree, and the 2dsphere index needs the real value. Every OTHER numeric leaf of a
53
+ * materialized document (counts, cents, minutes, basis points) stays integer-only.
54
+ */
55
+ const GEOMETRY_COORDINATE_PATH = /(^|\.)coordinates\[\d+\]$/;
47
56
  /**
48
57
  * BSON values: only IDENTITY types are ignored. A numeric BSON value
49
58
  * (Decimal128 / Long / Double / Int32) is storage of a number and must obey the
@@ -106,7 +115,7 @@ export function findNonIntegerNumericLeaves(document) {
106
115
  violations.push({ path, value: node, reason: 'non-finite' });
107
116
  return;
108
117
  }
109
- if (!Number.isInteger(node)) {
118
+ if (!Number.isInteger(node) && !GEOMETRY_COORDINATE_PATH.test(path)) {
110
119
  violations.push({ path, value: node, reason: 'decimal' });
111
120
  }
112
121
  return;
@@ -3,7 +3,7 @@ import { MATERIALIZED_PREP_TIME_ITEM_SCOPES, MATERIALIZED_PREP_TIME_REPORT_SCOPE
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, parseMaterializedLocalDate, } from './scope-helpers';
6
+ import { enumerateMaterializedPeriodDays, getMaterializedPeriodScopeFields, } from './scope-helpers';
7
7
  /**
8
8
  * M2 close-day helpers.
9
9
  *
@@ -159,94 +159,6 @@ export const MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES = MATERIALIZED_PREP_TIME_
159
159
  /* ------------------------------------------------------------------------ */
160
160
  /* Period sibling-day enumeration */
161
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
162
  /**
251
163
  * Every `localDate` label whose DAY entry document can contribute to a
252
164
  * month-capped period (week / ISO week / month). The worker uses this to fetch
@@ -254,7 +166,7 @@ const enumeratePeriodDays = (localDate, scopes) => {
254
166
  * entry document and never a year-wide scan.
255
167
  */
256
168
  export function getMaterializedPrepTimePeriodDayScopeFields(localDate) {
257
- return enumeratePeriodDays(localDate, MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES);
169
+ return enumerateMaterializedPeriodDays(localDate, MATERIALIZED_PREP_TIME_ITEM_PERIOD_SCOPES);
258
170
  }
259
171
  /**
260
172
  * Every `localDate` label whose DAY counter document can contribute to ANY
@@ -263,7 +175,7 @@ export function getMaterializedPrepTimePeriodDayScopeFields(localDate) {
263
175
  * month-capped.
264
176
  */
265
177
  export function getMaterializedPrepTimeReportPeriodDayScopeFields(localDate) {
266
- return enumeratePeriodDays(localDate, MATERIALIZED_PREP_TIME_PERIOD_SCOPES);
178
+ return enumerateMaterializedPeriodDays(localDate, MATERIALIZED_PREP_TIME_PERIOD_SCOPES);
267
179
  }
268
180
  /** True when a stored prep counter document equals the incoming one (skip write). */
269
181
  export function needsMaterializedPrepTimeReportWrite(existing, incoming) {