@managesome/eposmatic-toolkit 0.4.23 → 0.4.24

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 (46) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +83 -2
  2. package/esm/helpers/materialized-reports/index.js +3 -0
  3. package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +115 -35
  4. package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +39 -12
  5. package/esm/helpers/materialized-reports/materialized-discount-details-helpers.js +28 -0
  6. package/esm/helpers/materialized-reports/materialized-discount-source-helpers.js +428 -0
  7. package/esm/helpers/materialized-reports/tenant-discount-source-formatter.js +140 -0
  8. package/esm/schemas/models/index.js +1 -0
  9. package/esm/schemas/models/materialized_tenant_discount_entries.js +12 -0
  10. package/esm/schemas/models/materialized_tenant_discount_sources.js +165 -0
  11. package/esm/schemas/types/index.js +1 -0
  12. package/esm/schemas/types/materialized_tenant_discount_sources.js +1 -0
  13. package/lib/constants/MATERIALIZED_REPORTS.d.ts +79 -2
  14. package/lib/constants/MATERIALIZED_REPORTS.js +85 -4
  15. package/lib/helpers/materialized-reports/index.d.ts +3 -0
  16. package/lib/helpers/materialized-reports/index.js +3 -0
  17. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.d.ts +30 -0
  18. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +116 -35
  19. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.d.ts +27 -4
  20. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +38 -11
  21. package/lib/helpers/materialized-reports/materialized-discount-details-helpers.d.ts +31 -0
  22. package/lib/helpers/materialized-reports/materialized-discount-details-helpers.js +31 -0
  23. package/lib/helpers/materialized-reports/materialized-discount-source-helpers.d.ts +73 -0
  24. package/lib/helpers/materialized-reports/materialized-discount-source-helpers.js +437 -0
  25. package/lib/helpers/materialized-reports/tenant-discount-source-formatter.d.ts +79 -0
  26. package/lib/helpers/materialized-reports/tenant-discount-source-formatter.js +144 -0
  27. package/lib/helpers/types/materialized-reports-apis.d.ts +53 -1
  28. package/lib/schemas/models/index.d.ts +1 -0
  29. package/lib/schemas/models/index.js +1 -0
  30. package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +21 -6
  31. package/lib/schemas/models/materialized_tenant_discount_entries.js +12 -0
  32. package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +331 -0
  33. package/lib/schemas/models/materialized_tenant_discount_sources.js +168 -0
  34. package/lib/schemas/types/index.d.ts +1 -0
  35. package/lib/schemas/types/index.js +1 -0
  36. package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +26 -0
  37. package/lib/schemas/types/materialized_tenant_discount_sources.d.ts +128 -0
  38. package/lib/schemas/types/materialized_tenant_discount_sources.js +2 -0
  39. package/lib/tests/materialized-reports/materialized-discount-classification.test.js +22 -2
  40. package/lib/tests/materialized-reports/materialized-discount-details-helpers.test.d.ts +1 -0
  41. package/lib/tests/materialized-reports/materialized-discount-details-helpers.test.js +59 -0
  42. package/lib/tests/materialized-reports/materialized-discount-sources.test.d.ts +1 -0
  43. package/lib/tests/materialized-reports/materialized-discount-sources.test.js +483 -0
  44. package/lib/tests/materialized-reports/tenant-discount-source-formatter.test.d.ts +1 -0
  45. package/lib/tests/materialized-reports/tenant-discount-source-formatter.test.js +203 -0
  46. package/package.json +1 -1
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS = exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME = exports.DiscountSourceKeyMetricsSchema = exports.MaterializedTenantDiscountSourceSchema = exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_SCHEMA_VERSION = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ const ENUMS_1 = require("../../constants/ENUMS");
6
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
7
+ /**
8
+ * Mongoose schema for `materialized_tenant_discount_sources` — the DERIVED
9
+ * "by discount" projection of the discounts report (see
10
+ * `schemas/types/materialized_tenant_discount_sources.ts`).
11
+ *
12
+ * One document per (tenant, scope context, scope field, source key, level).
13
+ * The top-level metrics are the sum of `key_metrics`, which carries the
14
+ * `ORDER_STATUS:PLATFORM` split so the report filters stay exact.
15
+ *
16
+ * This collection is REBUILT from the post-plan entry documents in the close
17
+ * transaction. It has no iteration history of its own: the parent entry
18
+ * documents are the audit trail.
19
+ */
20
+ /**
21
+ * Version of the source-projection rules. Bump when the source identity,
22
+ * grouping or rate rule changes — historical documents must then be rebuilt
23
+ * with the backfill script.
24
+ */
25
+ exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_SCHEMA_VERSION = 1;
26
+ const DiscountSourceKeyMetricsSchema = new mongoose_1.Schema({
27
+ key: { type: String, required: true },
28
+ order_count: { type: Number, required: true, min: 0, default: 0 },
29
+ row_count: { type: Number, required: true, min: 0, default: 0 },
30
+ discount_amount: { type: Number, required: true, default: 0 },
31
+ free_item_count: { type: Number, required: true, min: 0, default: 0 },
32
+ free_item_value: { type: Number, required: true, default: 0 },
33
+ covered_amount: { type: Number, required: true, default: 0 },
34
+ percentage_row_count: { type: Number, required: true, min: 0, default: 0 },
35
+ percentage_rate_sum: { type: Number, required: true, default: 0 },
36
+ fixed_row_count: { type: Number, required: true, min: 0, default: 0 },
37
+ }, { _id: false });
38
+ exports.DiscountSourceKeyMetricsSchema = DiscountSourceKeyMetricsSchema;
39
+ exports.MaterializedTenantDiscountSourceSchema = new mongoose_1.Schema({
40
+ _id: {
41
+ type: mongoose_1.Schema.Types.ObjectId,
42
+ required: true,
43
+ immutable: true,
44
+ },
45
+ tenant_id: { type: String, required: true },
46
+ brand_id: { type: String, required: true },
47
+ scope_context: {
48
+ type: String,
49
+ required: true,
50
+ enum: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_REPORT_SCOPES,
51
+ },
52
+ scope_field: { type: String, required: true },
53
+ day_index: {
54
+ type: String,
55
+ default: null,
56
+ enum: [...ENUMS_1.ALL_DAY_INDEX, null],
57
+ },
58
+ source_key: { type: String, required: true },
59
+ source_id: { type: String, default: null },
60
+ source_name: { type: String, required: true },
61
+ source_class: {
62
+ type: String,
63
+ required: true,
64
+ enum: MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_DISCOUNT_SOURCE_CLASSES,
65
+ },
66
+ kind: { type: String, required: true, enum: MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_DISCOUNT_ENTRY_KINDS },
67
+ level: { type: String, required: true, enum: MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_DISCOUNT_LEVELS },
68
+ slot: {
69
+ type: String,
70
+ default: null,
71
+ enum: [...MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_DISCOUNT_SLOTS, null],
72
+ },
73
+ sub_kind: { type: String, required: true, enum: MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_DISCOUNT_SUB_KINDS },
74
+ rate: { type: Number, default: null },
75
+ rate_kind: {
76
+ type: String,
77
+ default: null,
78
+ enum: [...MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_DISCOUNT_RATE_KINDS, null],
79
+ },
80
+ percentage_row_count: { type: Number, required: true, min: 0, default: 0 },
81
+ percentage_rate_sum: { type: Number, required: true, default: 0 },
82
+ fixed_row_count: { type: Number, required: true, min: 0, default: 0 },
83
+ order_count: { type: Number, required: true, min: 0, default: 0 },
84
+ row_count: { type: Number, required: true, min: 0, default: 0 },
85
+ discount_amount: { type: Number, required: true, default: 0 },
86
+ free_item_count: { type: Number, required: true, min: 0, default: 0 },
87
+ free_item_value: { type: Number, required: true, default: 0 },
88
+ complimentary_full: { type: Boolean, default: null },
89
+ covered_amount: { type: Number, required: true, default: 0 },
90
+ key_metrics: {
91
+ type: [DiscountSourceKeyMetricsSchema],
92
+ required: true,
93
+ default: () => [],
94
+ },
95
+ sale_type_key_filters: {
96
+ type: [String],
97
+ required: true,
98
+ default: () => [...MATERIALIZED_REPORTS_1.DISCOUNTS_KEY_FILTERS],
99
+ validate: {
100
+ validator: (value) => value.length > 0 &&
101
+ value.every((entry) => MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_REPORT_KEY_FILTERS.includes(entry)),
102
+ message: 'sale_type_key_filters must be a non-empty subset of the known key filters',
103
+ },
104
+ },
105
+ schema_version: {
106
+ type: Number,
107
+ required: true,
108
+ immutable: true,
109
+ default: exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_SCHEMA_VERSION,
110
+ },
111
+ source_watermark: { type: mongoose_1.Schema.Types.Date, required: true },
112
+ computed_at: { type: mongoose_1.Schema.Types.Date, required: true },
113
+ createdAt: {
114
+ type: mongoose_1.Schema.Types.Date,
115
+ required: true,
116
+ default: () => new Date(),
117
+ immutable: true,
118
+ },
119
+ updatedAt: {
120
+ type: mongoose_1.Schema.Types.Date,
121
+ required: true,
122
+ default: () => new Date(),
123
+ },
124
+ }, {
125
+ timestamps: true,
126
+ versionKey: false,
127
+ minimize: false,
128
+ });
129
+ exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME = 'materialized_tenant_discount_sources';
130
+ /**
131
+ * Indexes for `materialized_tenant_discount_sources`.
132
+ *
133
+ * Compound keys are named by their field path (`field__field__field`), matching
134
+ * every other materialized collection's index constants.
135
+ *
136
+ * - `tenant_id__scope_context__scope_field__source_key__level` is the
137
+ * write-side upsert/delete key and the scope prefix every historical read
138
+ * uses.
139
+ * - `tenant_id__scope_context__scope_field__discount_amount` serves the
140
+ * "biggest source first" sort inside a scope.
141
+ */
142
+ exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_INDEXES = {
143
+ _id: { _id: 1 },
144
+ tenant_id__scope_context__scope_field__source_key__level: {
145
+ tenant_id: 1,
146
+ scope_context: 1,
147
+ scope_field: 1,
148
+ source_key: 1,
149
+ level: 1,
150
+ },
151
+ tenant_id__scope_context__scope_field__discount_amount: {
152
+ tenant_id: 1,
153
+ scope_context: 1,
154
+ scope_field: 1,
155
+ discount_amount: -1,
156
+ },
157
+ };
158
+ exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS = [
159
+ {
160
+ key: exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_INDEXES.tenant_id__scope_context__scope_field__source_key__level,
161
+ name: 'tenant_id__scope_context__scope_field__source_key__level',
162
+ unique: true,
163
+ },
164
+ {
165
+ key: exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_INDEXES.tenant_id__scope_context__scope_field__discount_amount,
166
+ name: 'tenant_id__scope_context__scope_field__discount_amount_desc',
167
+ },
168
+ ];
@@ -33,6 +33,7 @@ export * from './marketing_fingerprints';
33
33
  export * from './materialized_credit_bills';
34
34
  export * from './materialized_tenant_discount_entries';
35
35
  export * from './materialized_tenant_discount_reports';
36
+ export * from './materialized_tenant_discount_sources';
36
37
  export * from './materialized_tenant_cash_entries';
37
38
  export * from './materialized_tenant_delivery_entries';
38
39
  export * from './materialized_tenant_delivery_reports';
@@ -49,6 +49,7 @@ __exportStar(require("./marketing_fingerprints"), exports);
49
49
  __exportStar(require("./materialized_credit_bills"), exports);
50
50
  __exportStar(require("./materialized_tenant_discount_entries"), exports);
51
51
  __exportStar(require("./materialized_tenant_discount_reports"), exports);
52
+ __exportStar(require("./materialized_tenant_discount_sources"), exports);
52
53
  __exportStar(require("./materialized_tenant_cash_entries"), exports);
53
54
  __exportStar(require("./materialized_tenant_delivery_entries"), exports);
54
55
  __exportStar(require("./materialized_tenant_delivery_reports"), exports);
@@ -31,6 +31,22 @@ export interface IMaterializedDiscountEntry {
31
31
  order_id: string;
32
32
  order_number?: number | null;
33
33
  item_id?: string | null;
34
+ /**
35
+ * The CATALOG item id (`orderItem.item_id`) — distinct from `item_id`, which
36
+ * is the ORDER ITEM's `_id` and is part of `entry_key`.
37
+ *
38
+ * The catalog-auto discount writes the catalog item id as its
39
+ * `special_discount_id`, and a catalog id is usually a 24-hex ObjectId — so
40
+ * without this field a catalog-auto row is indistinguishable from a
41
+ * collection discount after the fact. Null for order/payment rows.
42
+ */
43
+ catalog_item_id?: string | null;
44
+ /**
45
+ * True when the row is the item's own catalog/special/half-half auto
46
+ * discount. Written by the accumulator because the id alone cannot always
47
+ * prove it (a specials/half-half setup id is not the catalog item id).
48
+ */
49
+ catalog_auto?: boolean | null;
34
50
  item_name?: string | null;
35
51
  payment_id?: string | null;
36
52
  /** Raw slot value (PERCENT for percentage rows) — not money. */
@@ -44,6 +60,16 @@ export interface IMaterializedDiscountEntry {
44
60
  name: string | null;
45
61
  reference_number: string | null;
46
62
  reason: string | null;
63
+ /**
64
+ * COMPLIMENTARY rows only: true when every tender on the order is
65
+ * COMPLIMENTARY and it covers the order; false when comp was mixed with
66
+ * another tender; null for a row written before this metadata existed.
67
+ */
68
+ complimentary_full?: boolean | null;
69
+ /** COMPLIMENTARY rows only: the amount this tender covered (cents). */
70
+ covered_amount?: number | null;
71
+ /** COMPLIMENTARY rows only: the order amount used for the full/partial test. */
72
+ order_amount?: number | null;
47
73
  staff_id: string | null;
48
74
  staff_name: string | null;
49
75
  order_taker_id: string | null;
@@ -0,0 +1,128 @@
1
+ import { DAY_INDEX_ENUM } from '../../constants/ENUMS';
2
+ import { MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM, MATERIALIZED_DISCOUNT_LEVEL_ENUM, MATERIALIZED_DISCOUNT_RATE_KIND_ENUM, MATERIALIZED_DISCOUNT_SLOT_ENUM, MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM, MATERIALIZED_REPORT_KEY_FILTER_ENUM, MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
3
+ /**
4
+ * Document types for `materialized_tenant_discount_sources` — the DERIVED
5
+ * "by discount" projection of the discounts report.
6
+ *
7
+ * ## What a source is
8
+ *
9
+ * One document is one discount SOURCE at one LEVEL, inside one tenant scope:
10
+ * a collection discount, the order/item manual buckets, a payment-type
11
+ * discount, a price override, the catalog-auto bucket, an offer, a gift card,
12
+ * wallet cashback, a voucher, a complimentary tender, or the unattributed
13
+ * bucket.
14
+ *
15
+ * ## It is a projection, not a second ledger
16
+ *
17
+ * Sources are rebuilt from the POST-PLAN entry documents in the SAME
18
+ * transaction as the counters/entries/flat projection. The parent entry
19
+ * documents and their iteration history remain the audit trail; this
20
+ * collection has no independent iteration history. Deleting every source
21
+ * document loses no information — replaying the closed tenant-day rebuilds them
22
+ * from the entry documents in the same transaction.
23
+ *
24
+ * ## Identity
25
+ *
26
+ * `(tenant_id, scope_context, scope_field, source_key, level)` is unique.
27
+ * `source_key` never encodes a generated amount: manual discounts are one
28
+ * bucket per level (the rows keep their individual rates), and a collection is
29
+ * keyed by its `discounts` `_id`, not by its name.
30
+ *
31
+ * ## Money
32
+ *
33
+ * `discount_amount` is RESOLVED money in CENTS — the same money the entry
34
+ * rows carry, which is also what `discount_counters.amount` sums. A source
35
+ * document never invents an allocation; it groups rows that already have one.
36
+ * `Σ source.discount_amount` (excluding COMPLIMENTARY) therefore equals the
37
+ * report's total discount for the same selection.
38
+ *
39
+ * `order_count` is DISTINCT ORDERS per source+level. It is only meaningful
40
+ * within one scope: summing it across overlapping scopes would double-count
41
+ * an order that appears in a day, its week and its month. Read disjoint scope
42
+ * groups only (the same rule the counter read enforces).
43
+ */
44
+ export interface IMaterializedDiscountSourceKeyMetrics {
45
+ /** The `ORDER_STATUS:PLATFORM` key the rows were stored under. */
46
+ key: string;
47
+ /** Distinct orders for this source+level inside this key. */
48
+ order_count: number;
49
+ /** Discount rows / occurrences (a comp payment is one row). */
50
+ row_count: number;
51
+ /** Resolved discount money (CENTS). Zero for COMPLIMENTARY. */
52
+ discount_amount: number;
53
+ /** 100 %-item rows (a subset of `row_count`). */
54
+ free_item_count: number;
55
+ /** Σ the discounted items' own value (CENTS). */
56
+ free_item_value: number;
57
+ /** Amount covered by a COMPLIMENTARY tender (CENTS). */
58
+ covered_amount: number;
59
+ /** Percentage rows (numerator for the displayed average rate). */
60
+ percentage_row_count: number;
61
+ /** Σ RAW percentage values (denominator is `percentage_row_count`). */
62
+ percentage_rate_sum: number;
63
+ /** Fixed-amount rows. */
64
+ fixed_row_count: number;
65
+ }
66
+ /**
67
+ * One source document — tenant × scope × source × level.
68
+ */
69
+ export interface IMaterializedTenantDiscountSource {
70
+ _id: string;
71
+ tenant_id: string;
72
+ brand_id: string;
73
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM;
74
+ scope_field: string;
75
+ /** Denormalized weekday — only `localHour` / `localDate` documents. */
76
+ day_index?: DAY_INDEX_ENUM | null;
77
+ /** Stable reporting identity of the source (see the module docs). */
78
+ source_key: string;
79
+ /** `discounts` / offer / voucher id when the source has one; else null. */
80
+ source_id: string | null;
81
+ /** Human label: configured discount name, offer name, or a bucket label. */
82
+ source_name: string;
83
+ source_class: MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM;
84
+ /** Primary row kind of the source. */
85
+ kind: MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
86
+ level: MATERIALIZED_DISCOUNT_LEVEL_ENUM;
87
+ slot: MATERIALIZED_DISCOUNT_SLOT_ENUM | null;
88
+ sub_kind: MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
89
+ /** Average percentage rate; null for a fixed-only or mixed source. */
90
+ rate: number | null;
91
+ /** Whether the rows behind this source are percentage, fixed or mixed. */
92
+ rate_kind: MATERIALIZED_DISCOUNT_RATE_KIND_ENUM | null;
93
+ percentage_row_count: number;
94
+ percentage_rate_sum: number;
95
+ fixed_row_count: number;
96
+ /** Distinct orders (per scope; never sum overlapping scopes). */
97
+ order_count: number;
98
+ /** Discount rows / occurrences. */
99
+ row_count: number;
100
+ /** Resolved discount money (CENTS). Zero for COMPLIMENTARY. */
101
+ discount_amount: number;
102
+ /** 100 %-item rows (a subset of `row_count`). */
103
+ free_item_count: number;
104
+ /** Σ the discounted items' own value (CENTS). */
105
+ free_item_value: number;
106
+ /**
107
+ * Complimentary only: `true` when every tender on the order is
108
+ * COMPLIMENTARY and it covers the order, `false` when comp was mixed with
109
+ * another tender, `null` when the historical row predates the metadata.
110
+ */
111
+ complimentary_full: boolean | null;
112
+ /** Complimentary tender coverage (CENTS) — never a discount. */
113
+ covered_amount: number;
114
+ /**
115
+ * The same metrics split by `ORDER_STATUS:PLATFORM`, so the platform /
116
+ * order-status filters stay exact. The top-level fields are the SUM of this
117
+ * array; both are written from one accumulator pass, so they cannot drift.
118
+ */
119
+ key_metrics: IMaterializedDiscountSourceKeyMetrics[];
120
+ /** `ORDER_STATUS:PLATFORM` grammar of `key_metrics[].key`. */
121
+ sale_type_key_filters: MATERIALIZED_REPORT_KEY_FILTER_ENUM[];
122
+ schema_version: number;
123
+ /** Max source entry watermark folded into this document. */
124
+ source_watermark: Date;
125
+ computed_at: Date;
126
+ createdAt: Date;
127
+ updatedAt: Date;
128
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -43,6 +43,8 @@ const BASHA = {
43
43
  itemSpecialCount: 22,
44
44
  itemSpecialCollectionRaw: 0,
45
45
  itemSpecialCollectionResolved: 0,
46
+ itemSpecialCatalogRaw: 0,
47
+ itemSpecialCatalogResolved: 0,
46
48
  freeItemCount: 11,
47
49
  freeItemValue: 11230,
48
50
  complimentaryPayments: 4,
@@ -68,8 +70,10 @@ const BRUZO = {
68
70
  itemSpecialRaw: 100328,
69
71
  itemSpecialResolved: 100437.15,
70
72
  itemSpecialCount: 304,
71
- itemSpecialCollectionRaw: 100293,
72
- itemSpecialCollectionResolved: 100293,
73
+ itemSpecialCollectionRaw: 0,
74
+ itemSpecialCollectionResolved: 0,
75
+ itemSpecialCatalogRaw: 100293,
76
+ itemSpecialCatalogResolved: 100293,
73
77
  freeItemCount: 0,
74
78
  freeItemValue: 0,
75
79
  complimentaryPayments: 41,
@@ -187,6 +191,11 @@ describe('materialized discounts — accumulator vs the independent analysis', (
187
191
  expect(round2(counters.item_special.count)).toBe(expected.itemSpecialCount);
188
192
  expect(round2(counters.item_special.collection_amount)).toBe(expected.itemSpecialCollectionRaw);
189
193
  expect(round2(counters.item_special.collection_resolved_amount)).toBe(expected.itemSpecialCollectionResolved);
194
+ // Catalog-auto rows write the CATALOG ITEM id, which is a 24-hex ObjectId.
195
+ // They used to be booked as COLLECTION; the caller now supplies the item
196
+ // ids so the two are separated (bruzo: 302 rows / 100_293).
197
+ expect(round2(counters.item_special.catalog_item_amount)).toBe(expected.itemSpecialCatalogRaw);
198
+ expect(round2(counters.item_special.catalog_item_resolved_amount)).toBe(expected.itemSpecialCatalogResolved);
190
199
  expect(round2(counters.item_special.resolved_amount)).toBe(round2(counters.item_level_amount));
191
200
  // Free items are a SUBSET of the item rows (anchor G), never added to them
192
201
  expect(round2(counters.free_items.count)).toBe(expected.freeItemCount);
@@ -213,6 +222,17 @@ describe('materialized discounts — sub-kind classifier (synthesized coverage)'
213
222
  expect((0, materialized_discount_classification_helpers_1.classifyDiscountSubKind)(S.VOUCHER, '6a14165680f2052bbbcbe9b7')).toBe(K.OFFER);
214
223
  expect((0, materialized_discount_classification_helpers_1.classifyDiscountSubKind)(S.VOUCHER, 'wallet_cashback_redemption')).toBe(K.WALLET_CASHBACK);
215
224
  expect((0, materialized_discount_classification_helpers_1.classifyDiscountSubKind)(S.SPECIAL, null)).toBe(K.UNATTRIBUTED);
225
+ // The catalog item id is ALSO a 24-hex ObjectId. With the item context the
226
+ // classifier separates it from a collection discount; without context the
227
+ // shape-only answer stays COLLECTION (historical rows rely on the backfill).
228
+ expect((0, materialized_discount_classification_helpers_1.classifyDiscountSubKind)(S.SPECIAL, '69f1c56ef9c86581b0ae9e36')).toBe(K.COLLECTION);
229
+ expect((0, materialized_discount_classification_helpers_1.classifyDiscountSubKind)(S.SPECIAL, '69f1c56ef9c86581b0ae9e36', {
230
+ catalog_item_id: '69f1c56ef9c86581b0ae9e36',
231
+ })).toBe(K.CATALOG_ITEM);
232
+ // A gift-card id is an ObjectId too; the wallet claim is the evidence.
233
+ expect((0, materialized_discount_classification_helpers_1.classifyDiscountSubKind)(S.LOYALTY, '6aa472763689e5f08dd0599f', {
234
+ gift_card_ids: ['6aa472763689e5f08dd0599f'],
235
+ })).toBe(K.GIFT_CARD);
216
236
  });
217
237
  it('resolves a PERCENTAGE slot to the row money, never to the percent', () => {
218
238
  const details = {
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
+ const materialized_discount_details_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-details-helpers");
5
+ /**
6
+ * GAP-1 regression: the drill-down footer must never add discount money and
7
+ * comp coverage into one Σ, and the comp breakdown must only appear when the
8
+ * current view actually includes comp rows.
9
+ */
10
+ describe('getDiscountDetailsLoadedTotals', () => {
11
+ const rows = [
12
+ { kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT, amount: 500 },
13
+ { kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.FREE_ITEM, amount: 250 },
14
+ {
15
+ kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY,
16
+ amount: 0,
17
+ covered_amount: 900,
18
+ },
19
+ {
20
+ kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY,
21
+ amount: 0,
22
+ covered_amount: 100,
23
+ },
24
+ ];
25
+ it('separates discount money from comp coverage in the ALL view', () => {
26
+ const totals = (0, materialized_discount_details_helpers_1.getDiscountDetailsLoadedTotals)(rows, 'ALL');
27
+ expect(totals.discount_cents).toBe(750);
28
+ expect(totals.comp_cents).toBe(1000);
29
+ expect(totals.comp_count).toBe(2);
30
+ expect(totals.row_count).toBe(4);
31
+ expect(totals.primary_cents).toBe(750);
32
+ expect(totals.primary_label).toBe('discounts');
33
+ expect(totals.include_comp_breakdown).toBe(true);
34
+ });
35
+ it('shows only the comp coverage in the COMPLIMENTARY view', () => {
36
+ const totals = (0, materialized_discount_details_helpers_1.getDiscountDetailsLoadedTotals)(rows, MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY);
37
+ expect(totals.primary_cents).toBe(1000);
38
+ expect(totals.primary_label).toBe('covered');
39
+ expect(totals.include_comp_breakdown).toBe(false);
40
+ });
41
+ it('shows only discount money (no comp breakdown) in the DISCOUNT view', () => {
42
+ const totals = (0, materialized_discount_details_helpers_1.getDiscountDetailsLoadedTotals)(rows, MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT);
43
+ expect(totals.primary_cents).toBe(750);
44
+ expect(totals.primary_label).toBe('discounts');
45
+ // Only the ALL view can contain both kinds; the kind tabs are filtered
46
+ // server-side, so they never append a cross-kind breakdown.
47
+ expect(totals.include_comp_breakdown).toBe(false);
48
+ });
49
+ it('is safe for an empty / malformed row list', () => {
50
+ expect((0, materialized_discount_details_helpers_1.getDiscountDetailsLoadedTotals)([], 'ALL')).toMatchObject({
51
+ discount_cents: 0,
52
+ comp_cents: 0,
53
+ comp_count: 0,
54
+ row_count: 0,
55
+ include_comp_breakdown: false,
56
+ });
57
+ expect((0, materialized_discount_details_helpers_1.getDiscountDetailsLoadedTotals)(null, 'ALL').primary_cents).toBe(0);
58
+ });
59
+ });