@managesome/eposmatic-toolkit 0.3.10 → 0.4.0
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/ENUMS.js +17 -1988
- package/esm/constants/MATERIALIZED_REPORTS.js +105 -0
- package/esm/constants/enums/cash.js +45 -0
- package/esm/constants/enums/communications.js +329 -0
- package/esm/constants/enums/delivery.js +35 -0
- package/esm/constants/enums/geo.js +150 -0
- package/esm/constants/enums/hr.js +130 -0
- package/esm/constants/enums/internal.js +132 -0
- package/esm/constants/enums/items.js +126 -0
- package/esm/constants/enums/misc.js +35 -0
- package/esm/constants/enums/offers.js +102 -0
- package/esm/constants/enums/order.js +135 -0
- package/esm/constants/enums/payments.js +186 -0
- package/esm/constants/enums/platform.js +215 -0
- package/esm/constants/enums/printing.js +107 -0
- package/esm/constants/enums/users.js +245 -0
- package/esm/constants/enums/wallet.js +32 -0
- package/esm/helpers/materialized-reports/index.js +5 -0
- package/esm/helpers/materialized-reports/key-helpers.js +59 -0
- package/esm/helpers/materialized-reports/materialized-tenant-reports-iterations-helpers.js +60 -0
- package/esm/helpers/materialized-reports/scope-helpers.js +107 -0
- package/esm/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +251 -0
- package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +860 -0
- package/esm/helpers/store/cart-actions.js +1 -1
- package/esm/helpers/store/common-tenant-purchase-order-actions.js +7 -7
- package/esm/helpers/store/common-warehouse-purchase-order-actions.js +1 -1
- package/esm/helpers/store/common-warehouse-purchase-order-rfq-actions.js +1 -1
- package/esm/helpers/store/common-warehouse-sale-order-actions.js +1 -1
- package/esm/helpers/store/common-warehouse-sale-order-rfq-actions.js +1 -1
- package/esm/helpers/store/offline-ordering.js +1 -1
- package/esm/helpers/store/tenant-purchase-orders-cart-actions.js +1 -1
- package/esm/helpers/store/warehouse-purchase-order-rfq-cart-actions.js +1 -1
- package/esm/helpers/store/warehouse-purchase-orders-cart-actions.js +1 -1
- package/esm/helpers/store/warehouse-sale-order-rfq-cart-actions.js +1 -1
- package/esm/helpers/store/warehouse-sale-orders-cart-actions.js +1 -1
- package/esm/helpers/utils/cancellation-helpers.js +7 -0
- package/esm/helpers/utils/common.js +0 -46
- package/esm/helpers/utils/getFormFieldsDiff.js +1 -1
- package/esm/helpers/utils/index.js +2 -0
- package/esm/helpers/utils/phone-helpers.js +40 -0
- package/esm/helpers/utils/receipt-engine.js +3 -1
- package/esm/helpers/utils/specials-helpers.js +1 -1
- package/esm/helpers/utils/void-items-report-helpers.js +2 -1
- package/esm/schemas/models/materialized_tenant_reports_iterations.js +82 -0
- package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +399 -0
- package/esm/schemas/types/materialized_tenant_reports_iterations.js +1 -0
- package/esm/schemas/types/materialized_tenant_sales_overview_reports.js +1 -0
- package/lib/constants/ENUMS.d.ts +15 -1275
- package/lib/constants/ENUMS.js +31 -1994
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +96 -0
- package/lib/constants/MATERIALIZED_REPORTS.js +108 -0
- package/lib/constants/enums/cash.d.ts +35 -0
- package/lib/constants/enums/cash.js +48 -0
- package/lib/constants/enums/communications.d.ts +79 -0
- package/lib/constants/enums/communications.js +332 -0
- package/lib/constants/enums/delivery.d.ts +26 -0
- package/lib/constants/enums/delivery.js +38 -0
- package/lib/constants/enums/geo.d.ts +133 -0
- package/lib/constants/enums/geo.js +153 -0
- package/lib/constants/enums/hr.d.ts +105 -0
- package/lib/constants/enums/hr.js +133 -0
- package/lib/constants/enums/internal.d.ts +104 -0
- package/lib/constants/enums/internal.js +135 -0
- package/lib/constants/enums/items.d.ts +94 -0
- package/lib/constants/enums/items.js +129 -0
- package/lib/constants/enums/misc.d.ts +25 -0
- package/lib/constants/enums/misc.js +38 -0
- package/lib/constants/enums/offers.d.ts +80 -0
- package/lib/constants/enums/offers.js +105 -0
- package/lib/constants/enums/order.d.ts +108 -0
- package/lib/constants/enums/order.js +138 -0
- package/lib/constants/enums/payments.d.ts +129 -0
- package/lib/constants/enums/payments.js +189 -0
- package/lib/constants/enums/platform.d.ts +100 -0
- package/lib/constants/enums/platform.js +218 -0
- package/lib/constants/enums/printing.d.ts +89 -0
- package/lib/constants/enums/printing.js +110 -0
- package/lib/constants/enums/users.d.ts +142 -0
- package/lib/constants/enums/users.js +248 -0
- package/lib/constants/enums/wallet.d.ts +26 -0
- package/lib/constants/enums/wallet.js +35 -0
- package/lib/helpers/materialized-reports/index.d.ts +5 -0
- package/lib/helpers/materialized-reports/index.js +21 -0
- package/lib/helpers/materialized-reports/key-helpers.d.ts +35 -0
- package/lib/helpers/materialized-reports/key-helpers.js +64 -0
- package/lib/helpers/materialized-reports/materialized-tenant-reports-iterations-helpers.d.ts +66 -0
- package/lib/helpers/materialized-reports/materialized-tenant-reports-iterations-helpers.js +63 -0
- package/lib/helpers/materialized-reports/scope-helpers.d.ts +57 -0
- package/lib/helpers/materialized-reports/scope-helpers.js +114 -0
- package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.d.ts +125 -0
- package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +256 -0
- package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.d.ts +223 -0
- package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +875 -0
- package/lib/helpers/store/cart-actions.js +6 -6
- package/lib/helpers/store/common-tenant-purchase-order-actions.d.ts +2 -2
- package/lib/helpers/store/common-tenant-purchase-order-actions.js +10 -10
- package/lib/helpers/store/common-warehouse-purchase-order-actions.js +4 -4
- package/lib/helpers/store/common-warehouse-purchase-order-rfq-actions.js +4 -4
- package/lib/helpers/store/common-warehouse-sale-order-actions.js +4 -4
- package/lib/helpers/store/common-warehouse-sale-order-rfq-actions.js +4 -4
- package/lib/helpers/store/offline-ordering.js +2 -2
- package/lib/helpers/store/tenant-purchase-orders-cart-actions.js +2 -2
- package/lib/helpers/store/warehouse-purchase-order-rfq-cart-actions.js +2 -2
- package/lib/helpers/store/warehouse-purchase-orders-cart-actions.js +2 -2
- package/lib/helpers/store/warehouse-sale-order-rfq-cart-actions.js +2 -2
- package/lib/helpers/store/warehouse-sale-orders-cart-actions.js +2 -2
- package/lib/helpers/utils/cancellation-helpers.d.ts +1 -0
- package/lib/helpers/utils/cancellation-helpers.js +13 -0
- package/lib/helpers/utils/common.d.ts +0 -6
- package/lib/helpers/utils/common.js +1 -54
- package/lib/helpers/utils/getFormFieldsDiff.js +5 -2
- package/lib/helpers/utils/index.d.ts +2 -0
- package/lib/helpers/utils/index.js +2 -0
- package/lib/helpers/utils/phone-helpers.d.ts +5 -0
- package/lib/helpers/utils/phone-helpers.js +46 -0
- package/lib/helpers/utils/receipt-engine.js +4 -2
- package/lib/helpers/utils/specials-helpers.js +2 -2
- package/lib/helpers/utils/void-items-report-helpers.js +3 -2
- package/lib/schemas/models/materialized_tenant_reports_iterations.d.ts +80 -0
- package/lib/schemas/models/materialized_tenant_reports_iterations.js +85 -0
- package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +569 -0
- package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +402 -0
- package/lib/schemas/types/materialized_tenant_reports_iterations.d.ts +62 -0
- package/lib/schemas/types/materialized_tenant_reports_iterations.js +2 -0
- package/lib/schemas/types/materialized_tenant_sales_overview_reports.d.ts +242 -0
- package/lib/schemas/types/materialized_tenant_sales_overview_reports.js +3 -0
- package/lib/tests/materialized-reports/fixtures.d.ts +81 -0
- package/lib/tests/materialized-reports/fixtures.js +87 -0
- package/lib/tests/materialized-reports/fixtures.test.d.ts +1 -0
- package/lib/tests/materialized-reports/fixtures.test.js +123 -0
- package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-helpers.test.js +75 -0
- package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-schema.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-schema.test.js +73 -0
- package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +207 -0
- package/lib/tests/materialized-reports/sales-overview-baseline.test.d.ts +1 -0
- package/lib/tests/materialized-reports/sales-overview-baseline.test.js +326 -0
- package/lib/tests/materialized-reports/sales-overview-reference.d.ts +63 -0
- package/lib/tests/materialized-reports/sales-overview-reference.js +131 -0
- package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.js +335 -0
- package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +885 -0
- package/package.json +5 -2
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { REPORT_SALE_TYPE_ENUM } from '../../constants';
|
|
2
|
+
import { MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
|
+
import { buildMaterializedTenantReportIteration } from './materialized-tenant-reports-iterations-helpers';
|
|
4
|
+
import { getMaterializedHourScopeField, getMaterializedPeriodScopeFields } from './scope-helpers';
|
|
5
|
+
import { normalizeMaterializedSaleTypeMetrics, updateMaterializedTenantSalesOverviewReport, } from './tenant-sales-overview-helpers';
|
|
6
|
+
/** Scopes written wholesale for the compiled day (replacement semantics). */
|
|
7
|
+
const CLOSE_DAY_DAY_SCOPES = [
|
|
8
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
|
|
9
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
10
|
+
];
|
|
11
|
+
/** Scopes that accumulate day deltas (subtract the stored delta, add the fresh one). */
|
|
12
|
+
const CLOSE_DAY_ACCUMULATED_SCOPES = [
|
|
13
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK,
|
|
14
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK_OF_YEAR,
|
|
15
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
|
|
16
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER,
|
|
17
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER,
|
|
18
|
+
MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR,
|
|
19
|
+
];
|
|
20
|
+
const materializedScopeKey = (doc) => `${doc.scope_context}|${doc.scope_field}`;
|
|
21
|
+
const materializedCreditBillKey = (orderId, paymentId) => `${orderId}|${paymentId}`;
|
|
22
|
+
const hasMaterializedMetrics = (doc) => Object.keys(normalizeMaterializedSaleTypeMetrics(doc.sale_type_metrics)).length > 0;
|
|
23
|
+
/**
|
|
24
|
+
* Every stored scope the fan-out of one local day needs: the six period
|
|
25
|
+
* scopes + `localDate` of that day and all 24 `localHour` buckets (`null` for
|
|
26
|
+
* a malformed date). A bill's hour is unknowable from the received entry
|
|
27
|
+
* alone, so settlement patches always consider the whole day.
|
|
28
|
+
*/
|
|
29
|
+
export function getMaterializedTenantSalesOverviewScopeRefsForDay(localDate) {
|
|
30
|
+
const periodFields = getMaterializedPeriodScopeFields(localDate);
|
|
31
|
+
if (periodFields == null)
|
|
32
|
+
return null;
|
|
33
|
+
const refs = [];
|
|
34
|
+
for (const scopeContext of Object.keys(periodFields)) {
|
|
35
|
+
refs.push({
|
|
36
|
+
scope_context: scopeContext,
|
|
37
|
+
scope_field: periodFields[scopeContext],
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
for (let hour = 0; hour < 24; hour++) {
|
|
41
|
+
const scopeField = getMaterializedHourScopeField(localDate, hour);
|
|
42
|
+
if (scopeField == null)
|
|
43
|
+
return null;
|
|
44
|
+
refs.push({ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, scope_field: scopeField });
|
|
45
|
+
}
|
|
46
|
+
return refs;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Watermark guard of the write path: does the stored document need a
|
|
50
|
+
* structural write for the incoming compiled version?
|
|
51
|
+
*
|
|
52
|
+
* `false` (skip) when the incoming `source_watermark` is not newer than the
|
|
53
|
+
* stored one and the schema version + key-filter grammar match. Watermarks are
|
|
54
|
+
* monotonic (`updateMaterializedTenantSalesOverviewReport` never regresses
|
|
55
|
+
* them). A missing stored document always needs a write.
|
|
56
|
+
*/
|
|
57
|
+
export function needsMaterializedTenantSalesOverviewReportWrite(existing, incoming) {
|
|
58
|
+
if (existing == null || incoming == null)
|
|
59
|
+
return true;
|
|
60
|
+
if (existing.schema_version !== incoming.schema_version)
|
|
61
|
+
return true;
|
|
62
|
+
if ((existing.sale_type_key_filters ?? []).join(':') !== (incoming.sale_type_key_filters ?? []).join(':')) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
const existingTime = new Date(existing.source_watermark ?? 0).getTime();
|
|
66
|
+
const incomingTime = new Date(incoming.source_watermark ?? 0).getTime();
|
|
67
|
+
if (Number.isNaN(existingTime) || Number.isNaN(incomingTime))
|
|
68
|
+
return true;
|
|
69
|
+
return incomingTime > existingTime;
|
|
70
|
+
}
|
|
71
|
+
/** Would a received-payment patch change the stored bill? (settlement/note/received) */
|
|
72
|
+
const creditBillWouldChange = (bill, collected) => {
|
|
73
|
+
const received = Math.max(bill.received_amount ?? 0, collected.received_amount ?? 0);
|
|
74
|
+
if (received !== (bill.received_amount ?? 0))
|
|
75
|
+
return true;
|
|
76
|
+
const note = bill.credit_note ?? collected.credit_note;
|
|
77
|
+
if ((note ?? null) !== (bill.credit_note ?? null))
|
|
78
|
+
return true;
|
|
79
|
+
return bill.settled_at == null && received >= (bill.amount ?? 0);
|
|
80
|
+
};
|
|
81
|
+
/** Does the document hold an issued bill that one of the collected payments would change? */
|
|
82
|
+
const documentHoldsUnpatchedCreditBill = (doc, collectedByBill) => {
|
|
83
|
+
for (const keyMetrics of Object.values(normalizeMaterializedSaleTypeMetrics(doc.sale_type_metrics))) {
|
|
84
|
+
for (const bill of keyMetrics?.[REPORT_SALE_TYPE_ENUM.CREDIT_BILLS]?.issued?.bills ?? []) {
|
|
85
|
+
const collected = collectedByBill.get(materializedCreditBillKey(bill.order_id, bill.payment_id));
|
|
86
|
+
if (collected != null && creditBillWouldChange(bill, collected))
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Builds the close-day write plan for ONE tenant-day (see the module docs).
|
|
94
|
+
* Returns `null` (never throws) when the input is inconsistent — compiled
|
|
95
|
+
* docs of another tenant, duplicate compiled scope documents, a malformed
|
|
96
|
+
* `localDate`, duplicate STORED rows for one scope key (missing unique index
|
|
97
|
+
* — a data issue to fix, never silently deduplicated), or a merge conflict
|
|
98
|
+
* with a stored document (schema / brand).
|
|
99
|
+
*/
|
|
100
|
+
export function buildTenantSalesOverviewCloseDayPlan(input) {
|
|
101
|
+
if (input == null)
|
|
102
|
+
return null;
|
|
103
|
+
const { tenantId, compile, reason } = input;
|
|
104
|
+
if (typeof tenantId !== 'string' || tenantId.length === 0)
|
|
105
|
+
return null;
|
|
106
|
+
if (compile == null)
|
|
107
|
+
return null;
|
|
108
|
+
if (typeof reason !== 'string' || reason.length === 0)
|
|
109
|
+
return null;
|
|
110
|
+
const compiledReports = compile.materializedReports ?? [];
|
|
111
|
+
if (compiledReports.some((doc) => doc == null || doc.tenant_id !== tenantId))
|
|
112
|
+
return null;
|
|
113
|
+
const computedAt = input.computedAt ?? new Date();
|
|
114
|
+
const force = input.force === true;
|
|
115
|
+
const compiledByScope = new Map();
|
|
116
|
+
for (const doc of compiledReports) {
|
|
117
|
+
const key = materializedScopeKey(doc);
|
|
118
|
+
if (compiledByScope.has(key))
|
|
119
|
+
return null; // duplicate scope doc — data issue
|
|
120
|
+
compiledByScope.set(key, doc);
|
|
121
|
+
}
|
|
122
|
+
const compiledDateDoc = compiledReports.find((doc) => doc.scope_context === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE) ?? null;
|
|
123
|
+
if (compiledDateDoc == null)
|
|
124
|
+
return null;
|
|
125
|
+
const localDate = compiledDateDoc.scope_field;
|
|
126
|
+
if (getMaterializedTenantSalesOverviewScopeRefsForDay(localDate) == null)
|
|
127
|
+
return null;
|
|
128
|
+
const existingByScope = new Map();
|
|
129
|
+
for (const doc of input.existing ?? []) {
|
|
130
|
+
if (doc == null || doc.tenant_id !== tenantId)
|
|
131
|
+
continue;
|
|
132
|
+
const key = materializedScopeKey(doc);
|
|
133
|
+
const prior = existingByScope.get(key);
|
|
134
|
+
if (prior == null) {
|
|
135
|
+
existingByScope.set(key, doc);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
// Repeating the SAME document (overlapping fetch refs) is fine; two
|
|
139
|
+
// different rows for one scope key mean the unique index is missing.
|
|
140
|
+
// String comparison keeps hydrated ObjectId `_id`s compatible with plain ids.
|
|
141
|
+
if (String(prior._id) !== String(doc._id))
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const storedDateDoc = existingByScope.get(materializedScopeKey(compiledDateDoc)) ?? null;
|
|
145
|
+
const dayChanged = force || needsMaterializedTenantSalesOverviewReportWrite(storedDateDoc, compiledDateDoc);
|
|
146
|
+
const dayDeltaEmpty = !hasMaterializedMetrics(compiledDateDoc);
|
|
147
|
+
const writes = [];
|
|
148
|
+
const skippedDocuments = [];
|
|
149
|
+
const createdPeriodDocuments = [];
|
|
150
|
+
const addStructuralWrite = (report) => {
|
|
151
|
+
const iteration = buildMaterializedTenantReportIteration(report, {
|
|
152
|
+
reportType: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_OVERVIEW,
|
|
153
|
+
reason,
|
|
154
|
+
computedAt,
|
|
155
|
+
});
|
|
156
|
+
if (iteration == null)
|
|
157
|
+
return false;
|
|
158
|
+
writes.push({ report, iteration });
|
|
159
|
+
return true;
|
|
160
|
+
};
|
|
161
|
+
for (const compiledDoc of compiledReports) {
|
|
162
|
+
const existing = existingByScope.get(materializedScopeKey(compiledDoc)) ?? null;
|
|
163
|
+
if (CLOSE_DAY_DAY_SCOPES.includes(compiledDoc.scope_context)) {
|
|
164
|
+
if (!dayChanged) {
|
|
165
|
+
if (existing != null)
|
|
166
|
+
skippedDocuments.push(existing);
|
|
167
|
+
// Partial-failure repair: the day is up to date but this scope doc was never persisted.
|
|
168
|
+
else if (!addStructuralWrite(compiledDoc))
|
|
169
|
+
return null;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (existing == null) {
|
|
173
|
+
if (!addStructuralWrite(compiledDoc))
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
const updated = updateMaterializedTenantSalesOverviewReport(existing, { replacement: compiledDoc }, { computedAt });
|
|
178
|
+
if (updated == null || !addStructuralWrite(updated))
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (!CLOSE_DAY_ACCUMULATED_SCOPES.includes(compiledDoc.scope_context))
|
|
184
|
+
return null; // unknown scope — data issue
|
|
185
|
+
if (existing == null) {
|
|
186
|
+
if (dayDeltaEmpty)
|
|
187
|
+
continue; // nothing to fold into a (new) period document
|
|
188
|
+
if (!addStructuralWrite(compiledDoc))
|
|
189
|
+
return null;
|
|
190
|
+
createdPeriodDocuments.push({
|
|
191
|
+
scope_context: compiledDoc.scope_context,
|
|
192
|
+
scope_field: compiledDoc.scope_field,
|
|
193
|
+
});
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (!dayChanged) {
|
|
197
|
+
skippedDocuments.push(existing);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (dayDeltaEmpty && storedDateDoc == null) {
|
|
201
|
+
skippedDocuments.push(existing); // first close of an order-less day — nothing to fold
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const updated = updateMaterializedTenantSalesOverviewReport(existing, { subtract: storedDateDoc != null ? [storedDateDoc] : [], additions: [compiledDoc] }, { computedAt });
|
|
205
|
+
if (updated == null || !addStructuralWrite(updated))
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
// Orders deleted / moved shrank the day's active hours — drop the stale docs.
|
|
209
|
+
const staleHourDocuments = dayChanged
|
|
210
|
+
? Array.from(existingByScope.values()).filter((doc) => doc.scope_context === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR &&
|
|
211
|
+
typeof doc.scope_field === 'string' &&
|
|
212
|
+
doc.scope_field.startsWith(`${localDate} `) &&
|
|
213
|
+
!compiledByScope.has(materializedScopeKey(doc)))
|
|
214
|
+
: [];
|
|
215
|
+
// Collected bills patch the ISSUE-day docs that still hold the issued
|
|
216
|
+
// ledger. Compute the patches against the POST-write documents — a period
|
|
217
|
+
// doc that both accumulates the day and holds an earlier issued bill must
|
|
218
|
+
// keep the accumulation in its patched version (persist patches AFTER
|
|
219
|
+
// writes).
|
|
220
|
+
const postWriteByScope = new Map();
|
|
221
|
+
for (const doc of existingByScope.values())
|
|
222
|
+
postWriteByScope.set(materializedScopeKey(doc), doc);
|
|
223
|
+
for (const write of writes)
|
|
224
|
+
postWriteByScope.set(materializedScopeKey(write.report), write.report);
|
|
225
|
+
for (const stale of staleHourDocuments)
|
|
226
|
+
postWriteByScope.delete(materializedScopeKey(stale));
|
|
227
|
+
const receivedPatches = [];
|
|
228
|
+
const receivedPayments = compile.received_payments ?? [];
|
|
229
|
+
if (receivedPayments.length > 0) {
|
|
230
|
+
const collectedByBill = new Map();
|
|
231
|
+
for (const collected of receivedPayments) {
|
|
232
|
+
collectedByBill.set(materializedCreditBillKey(collected.order_id, collected.payment_id), collected);
|
|
233
|
+
}
|
|
234
|
+
for (const doc of postWriteByScope.values()) {
|
|
235
|
+
if (!documentHoldsUnpatchedCreditBill(doc, collectedByBill))
|
|
236
|
+
continue;
|
|
237
|
+
const patched = updateMaterializedTenantSalesOverviewReport(doc, { received_payments: receivedPayments }, { computedAt });
|
|
238
|
+
if (patched == null)
|
|
239
|
+
return null;
|
|
240
|
+
const iteration = buildMaterializedTenantReportIteration(patched, {
|
|
241
|
+
reportType: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_OVERVIEW,
|
|
242
|
+
reason,
|
|
243
|
+
computedAt,
|
|
244
|
+
});
|
|
245
|
+
if (iteration == null)
|
|
246
|
+
return null;
|
|
247
|
+
receivedPatches.push({ report: patched, iteration });
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return { writes, staleHourDocuments, receivedPatches, skippedDocuments, createdPeriodDocuments };
|
|
251
|
+
}
|