@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.
- package/esm/constants/MATERIALIZED_REPORTS.js +83 -2
- package/esm/helpers/materialized-reports/index.js +3 -0
- package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +115 -35
- package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +39 -12
- package/esm/helpers/materialized-reports/materialized-discount-details-helpers.js +28 -0
- package/esm/helpers/materialized-reports/materialized-discount-source-helpers.js +428 -0
- package/esm/helpers/materialized-reports/tenant-discount-source-formatter.js +140 -0
- package/esm/schemas/models/index.js +1 -0
- package/esm/schemas/models/materialized_tenant_discount_entries.js +12 -0
- package/esm/schemas/models/materialized_tenant_discount_sources.js +165 -0
- package/esm/schemas/types/index.js +1 -0
- package/esm/schemas/types/materialized_tenant_discount_sources.js +1 -0
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +79 -2
- package/lib/constants/MATERIALIZED_REPORTS.js +85 -4
- package/lib/helpers/materialized-reports/index.d.ts +3 -0
- package/lib/helpers/materialized-reports/index.js +3 -0
- package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.d.ts +30 -0
- package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +116 -35
- package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.d.ts +27 -4
- package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +38 -11
- package/lib/helpers/materialized-reports/materialized-discount-details-helpers.d.ts +31 -0
- package/lib/helpers/materialized-reports/materialized-discount-details-helpers.js +31 -0
- package/lib/helpers/materialized-reports/materialized-discount-source-helpers.d.ts +73 -0
- package/lib/helpers/materialized-reports/materialized-discount-source-helpers.js +437 -0
- package/lib/helpers/materialized-reports/tenant-discount-source-formatter.d.ts +79 -0
- package/lib/helpers/materialized-reports/tenant-discount-source-formatter.js +144 -0
- package/lib/helpers/types/materialized-reports-apis.d.ts +53 -1
- package/lib/schemas/models/index.d.ts +1 -0
- package/lib/schemas/models/index.js +1 -0
- package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +21 -6
- package/lib/schemas/models/materialized_tenant_discount_entries.js +12 -0
- package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +331 -0
- package/lib/schemas/models/materialized_tenant_discount_sources.js +168 -0
- package/lib/schemas/types/index.d.ts +1 -0
- package/lib/schemas/types/index.js +1 -0
- package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +26 -0
- package/lib/schemas/types/materialized_tenant_discount_sources.d.ts +128 -0
- package/lib/schemas/types/materialized_tenant_discount_sources.js +2 -0
- package/lib/tests/materialized-reports/materialized-discount-classification.test.js +22 -2
- package/lib/tests/materialized-reports/materialized-discount-details-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-discount-details-helpers.test.js +59 -0
- package/lib/tests/materialized-reports/materialized-discount-sources.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-discount-sources.test.js +483 -0
- package/lib/tests/materialized-reports/tenant-discount-source-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/tenant-discount-source-formatter.test.js +203 -0
- package/package.json +1 -1
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
4
|
+
const materialized_discount_accumulator_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-accumulator-helpers");
|
|
5
|
+
const materialized_discount_source_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-source-helpers");
|
|
6
|
+
/**
|
|
7
|
+
* "By discount" SOURCE projection (M1) — the grouping that answers "which
|
|
8
|
+
* discount was this?" instead of "which generated amount was it?".
|
|
9
|
+
*
|
|
10
|
+
* The cases below are the taxonomy the report must keep distinct: collection,
|
|
11
|
+
* manual order/item, payment-type, price override, catalog auto, loyalty
|
|
12
|
+
* offer/gift card/wallet, voucher, free item, full/partial complimentary and
|
|
13
|
+
* unattributed.
|
|
14
|
+
*/
|
|
15
|
+
const CONTEXT = {
|
|
16
|
+
local_date: '2026-09-15',
|
|
17
|
+
local_hour: 18,
|
|
18
|
+
local_hour_label: '2026-09-15 18',
|
|
19
|
+
};
|
|
20
|
+
const KEY = 'COMPLETED:POS_WALK-IN';
|
|
21
|
+
const entry = (over = {}) => ({
|
|
22
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
23
|
+
slot: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL,
|
|
24
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL,
|
|
25
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER,
|
|
26
|
+
entry_key: 'order:special',
|
|
27
|
+
order_id: 'order-1',
|
|
28
|
+
order_number: 1,
|
|
29
|
+
item_id: null,
|
|
30
|
+
catalog_item_id: null,
|
|
31
|
+
catalog_auto: null,
|
|
32
|
+
item_name: null,
|
|
33
|
+
payment_id: null,
|
|
34
|
+
rate: 10,
|
|
35
|
+
amount: 500,
|
|
36
|
+
is_percentage: true,
|
|
37
|
+
item_value: null,
|
|
38
|
+
discount_id: null,
|
|
39
|
+
name: null,
|
|
40
|
+
reference_number: null,
|
|
41
|
+
reason: null,
|
|
42
|
+
complimentary_full: null,
|
|
43
|
+
covered_amount: null,
|
|
44
|
+
order_amount: null,
|
|
45
|
+
staff_id: null,
|
|
46
|
+
staff_name: null,
|
|
47
|
+
order_taker_id: null,
|
|
48
|
+
order_taker_name: null,
|
|
49
|
+
order_status: 'COMPLETED',
|
|
50
|
+
platform: 'POS_WALK-IN',
|
|
51
|
+
created_at: new Date('2026-09-15T13:00:00.000Z'),
|
|
52
|
+
local_hour: '2026-09-15 18',
|
|
53
|
+
...over,
|
|
54
|
+
});
|
|
55
|
+
const scopeInput = (rows) => ({
|
|
56
|
+
tenantId: 'tenant-a',
|
|
57
|
+
brandId: 'brand-a',
|
|
58
|
+
scopeContext: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
59
|
+
scopeField: '2026-09-15',
|
|
60
|
+
sections: { [KEY]: { rows } },
|
|
61
|
+
computedAt: new Date('2026-09-15T23:00:00.000Z'),
|
|
62
|
+
});
|
|
63
|
+
describe('discount source identity', () => {
|
|
64
|
+
it('keys a collection discount by its discount _id and keeps its name', () => {
|
|
65
|
+
const id = '69f1c56ef9c86581b0ae9e36';
|
|
66
|
+
const identity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
67
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION,
|
|
68
|
+
discount_id: id,
|
|
69
|
+
name: 'UBL Bank 15%',
|
|
70
|
+
}));
|
|
71
|
+
expect(identity.source_key).toBe(`discount:${id}`);
|
|
72
|
+
expect(identity.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COLLECTION);
|
|
73
|
+
expect(identity.source_name).toBe('UBL Bank 15%');
|
|
74
|
+
expect(identity.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER);
|
|
75
|
+
});
|
|
76
|
+
it('groups every manual order amount under ONE source', () => {
|
|
77
|
+
const a = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({ discount_id: 'MANUAL_50%', rate: 50, name: 'Discount' }));
|
|
78
|
+
const b = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({ discount_id: 'MANUAL_Rs 500', rate: 500, name: 'Apply Discount', is_percentage: false }));
|
|
79
|
+
expect(a.source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.MANUAL_ORDER);
|
|
80
|
+
expect(b.source_key).toBe(a.source_key);
|
|
81
|
+
expect(a.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.MANUAL);
|
|
82
|
+
expect(a.source_name).toBe('Manual / quick discount');
|
|
83
|
+
});
|
|
84
|
+
it('separates item manual from order manual', () => {
|
|
85
|
+
const identity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
86
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
87
|
+
item_id: 'item-1',
|
|
88
|
+
discount_id: 'MANUAL_25%',
|
|
89
|
+
rate: 25,
|
|
90
|
+
}));
|
|
91
|
+
expect(identity.source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.MANUAL_ITEM);
|
|
92
|
+
expect(identity.source_name).toBe('Manual item discount');
|
|
93
|
+
});
|
|
94
|
+
it('separates a payment-type discount from a manual one by its reason', () => {
|
|
95
|
+
expect((0, materialized_discount_source_helpers_1.getPaymentTypeFromDiscountReason)('Discount applied for VISA CARD')).toBe('VISA CARD');
|
|
96
|
+
expect((0, materialized_discount_source_helpers_1.normalizeDiscountSourceKeyPart)('VISA CARD')).toBe('visa_card');
|
|
97
|
+
const identity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
98
|
+
discount_id: 'MANUAL_10%',
|
|
99
|
+
rate: 10,
|
|
100
|
+
name: 'MANUAL_10%',
|
|
101
|
+
reason: 'Discount applied for VISA CARD',
|
|
102
|
+
}));
|
|
103
|
+
expect(identity.source_key).toBe('payment_type:visa_card');
|
|
104
|
+
expect(identity.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.PAYMENT_TYPE);
|
|
105
|
+
expect(identity.source_name).toBe('Visa Card discount');
|
|
106
|
+
});
|
|
107
|
+
it('groups a price override under one item source', () => {
|
|
108
|
+
const identity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
109
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
110
|
+
item_id: 'item-1',
|
|
111
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.PRICE_OVERRIDE,
|
|
112
|
+
discount_id: 'MANUAL_SINGLE_Rs 150',
|
|
113
|
+
name: 'Manual Single Price',
|
|
114
|
+
rate: 150,
|
|
115
|
+
is_percentage: false,
|
|
116
|
+
}));
|
|
117
|
+
expect(identity.source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.PRICE_OVERRIDE_ITEM);
|
|
118
|
+
expect(identity.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.PRICE_OVERRIDE);
|
|
119
|
+
});
|
|
120
|
+
it('recognises catalog auto from the CURRENT marker, not the id shape', () => {
|
|
121
|
+
const catalogItemId = '69f1c56ef9c86581b0ae9e36';
|
|
122
|
+
const current = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
123
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
124
|
+
item_id: 'order-item-1',
|
|
125
|
+
catalog_item_id: catalogItemId,
|
|
126
|
+
catalog_auto: true,
|
|
127
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM,
|
|
128
|
+
discount_id: catalogItemId,
|
|
129
|
+
name: 'Special Discount',
|
|
130
|
+
}));
|
|
131
|
+
expect(current.source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.CATALOG_AUTO);
|
|
132
|
+
expect(current.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM);
|
|
133
|
+
// No legacy id-shape fallback: the same catalog id on a COLLECTION row is
|
|
134
|
+
// a collection. Pre-release periods are replayed, not read through this.
|
|
135
|
+
const noMarker = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
136
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
137
|
+
item_id: 'order-item-1',
|
|
138
|
+
catalog_item_id: catalogItemId,
|
|
139
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION,
|
|
140
|
+
discount_id: catalogItemId,
|
|
141
|
+
name: 'Special Discount',
|
|
142
|
+
}));
|
|
143
|
+
expect(noMarker.source_key).toBe(`discount:${catalogItemId}`);
|
|
144
|
+
});
|
|
145
|
+
it('keys loyalty offers, gift cards and wallet cashback separately', () => {
|
|
146
|
+
const offer = '6aa472763689e5f08dd0599f';
|
|
147
|
+
const giftCard = '6aa472763689e5f08dd059a0';
|
|
148
|
+
const offerIdentity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
149
|
+
slot: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY,
|
|
150
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER,
|
|
151
|
+
discount_id: offer,
|
|
152
|
+
name: 'Z2 Party 10% Offer',
|
|
153
|
+
rate: 10,
|
|
154
|
+
}));
|
|
155
|
+
expect(offerIdentity.source_key).toBe(`loyalty:offer:${offer}`);
|
|
156
|
+
expect(offerIdentity.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY);
|
|
157
|
+
const giftIdentity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
158
|
+
slot: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY,
|
|
159
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER,
|
|
160
|
+
discount_id: giftCard,
|
|
161
|
+
name: 'Birthday Card',
|
|
162
|
+
reference_number: 'GC-123',
|
|
163
|
+
}));
|
|
164
|
+
expect(giftIdentity.source_key).toBe(`loyalty:gift_card:${giftCard}`);
|
|
165
|
+
expect(giftIdentity.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD);
|
|
166
|
+
const walletIdentity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
167
|
+
slot: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY,
|
|
168
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.WALLET_CASHBACK,
|
|
169
|
+
discount_id: 'wallet_cashback_redemption',
|
|
170
|
+
name: 'Wallet Cashback',
|
|
171
|
+
reference_number: 'WALLET_CASHBACK',
|
|
172
|
+
is_percentage: false,
|
|
173
|
+
}));
|
|
174
|
+
expect(walletIdentity.source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.WALLET_CASHBACK);
|
|
175
|
+
});
|
|
176
|
+
it('keys a voucher by its offer id (fallback: the code)', () => {
|
|
177
|
+
const offer = '6a14165680f2052bbbcbe9b7';
|
|
178
|
+
expect((0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
179
|
+
slot: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.VOUCHER,
|
|
180
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER,
|
|
181
|
+
discount_id: offer,
|
|
182
|
+
name: 'RS150 OFF',
|
|
183
|
+
reference_number: 'RS150',
|
|
184
|
+
is_percentage: false,
|
|
185
|
+
})).source_key).toBe(`voucher:${offer}`);
|
|
186
|
+
expect((0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
187
|
+
slot: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.VOUCHER,
|
|
188
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER,
|
|
189
|
+
discount_id: null,
|
|
190
|
+
reference_number: 'RS150',
|
|
191
|
+
})).source_key).toBe('voucher:RS150');
|
|
192
|
+
});
|
|
193
|
+
it('classifies complimentary rows as full/partial/unknown', () => {
|
|
194
|
+
const base = {
|
|
195
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY,
|
|
196
|
+
slot: null,
|
|
197
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
|
|
198
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT,
|
|
199
|
+
amount: 0,
|
|
200
|
+
rate: null,
|
|
201
|
+
is_percentage: null,
|
|
202
|
+
};
|
|
203
|
+
expect((0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({ ...base, entry_key: 'payment:1', complimentary_full: true, covered_amount: 1000 })).source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_FULL);
|
|
204
|
+
expect((0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({ ...base, entry_key: 'payment:2', complimentary_full: false, covered_amount: 400 })).source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_PARTIAL);
|
|
205
|
+
expect((0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({ ...base, entry_key: 'payment:3', complimentary_full: null })).source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_UNKNOWN);
|
|
206
|
+
});
|
|
207
|
+
it('classifies a CATALOG_ITEM row from its current sub_kind', () => {
|
|
208
|
+
// The current classifier writes CATALOG_ITEM; the explicit catalog_auto
|
|
209
|
+
// marker is the primary signal, but the current sub_kind is equivalent.
|
|
210
|
+
const identity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
211
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
212
|
+
item_id: '6abce4ef6d1de61c4383a59d', // order item _id
|
|
213
|
+
catalog_item_id: null,
|
|
214
|
+
catalog_auto: null,
|
|
215
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM,
|
|
216
|
+
discount_id: '69f1c56ef9c86581b0ae9e36', // catalog item _id
|
|
217
|
+
name: 'Special Discount',
|
|
218
|
+
}));
|
|
219
|
+
expect(identity.source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.CATALOG_AUTO);
|
|
220
|
+
expect(identity.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.CATALOG_ITEM);
|
|
221
|
+
});
|
|
222
|
+
it('does NOT guess catalog auto from a COLLECTION row id or name', () => {
|
|
223
|
+
// The row looks exactly like a collection discount (24-hex id, no marker).
|
|
224
|
+
// The correct answer is COLLECTION; guessing from the display name or the
|
|
225
|
+
// id shape would mislabel a real collection. Pre-release periods are
|
|
226
|
+
// replayed, so this shape is not a supported compatibility path.
|
|
227
|
+
const identity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
228
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
229
|
+
item_id: '6abce4ef6d1de61c4383a59d',
|
|
230
|
+
catalog_item_id: null,
|
|
231
|
+
catalog_auto: null,
|
|
232
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION,
|
|
233
|
+
discount_id: '69f1c56ef9c86581b0ae9e36',
|
|
234
|
+
name: 'Special Discount',
|
|
235
|
+
}));
|
|
236
|
+
expect(identity.source_key).toBe(`discount:69f1c56ef9c86581b0ae9e36`);
|
|
237
|
+
expect(identity.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COLLECTION);
|
|
238
|
+
});
|
|
239
|
+
it('falls back to unattributed when no usable id exists', () => {
|
|
240
|
+
const identity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
241
|
+
slot: null,
|
|
242
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.UNATTRIBUTED,
|
|
243
|
+
discount_id: null,
|
|
244
|
+
}));
|
|
245
|
+
expect(identity.source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.UNATTRIBUTED);
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
describe('buildDiscountSourcesForScope', () => {
|
|
249
|
+
it('groups two manual order rows into one source with distinct orders', () => {
|
|
250
|
+
const docs = (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)(scopeInput([
|
|
251
|
+
entry({ order_id: 'o1', discount_id: 'MANUAL_50%', rate: 50, amount: 500 }),
|
|
252
|
+
entry({ order_id: 'o2', discount_id: 'MANUAL_Rs 200', rate: 200, is_percentage: false, amount: 200 }),
|
|
253
|
+
]));
|
|
254
|
+
expect(docs).toHaveLength(1);
|
|
255
|
+
expect(docs[0].source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.MANUAL_ORDER);
|
|
256
|
+
expect(docs[0].order_count).toBe(2);
|
|
257
|
+
expect(docs[0].row_count).toBe(2);
|
|
258
|
+
expect(docs[0].discount_amount).toBe(700);
|
|
259
|
+
expect(docs[0].rate_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED);
|
|
260
|
+
expect(docs[0].rate).toBe(50);
|
|
261
|
+
});
|
|
262
|
+
it('counts an order ONCE per source even with several item rows', () => {
|
|
263
|
+
const docs = (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)(scopeInput([
|
|
264
|
+
entry({
|
|
265
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
266
|
+
order_id: 'o1',
|
|
267
|
+
item_id: 'i1',
|
|
268
|
+
entry_key: 'item:i1:special',
|
|
269
|
+
discount_id: 'MANUAL_25%',
|
|
270
|
+
rate: 25,
|
|
271
|
+
amount: 250,
|
|
272
|
+
}),
|
|
273
|
+
entry({
|
|
274
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
275
|
+
order_id: 'o1',
|
|
276
|
+
item_id: 'i2',
|
|
277
|
+
entry_key: 'item:i2:special',
|
|
278
|
+
discount_id: 'MANUAL_25%',
|
|
279
|
+
rate: 25,
|
|
280
|
+
amount: 250,
|
|
281
|
+
}),
|
|
282
|
+
]));
|
|
283
|
+
expect(docs).toHaveLength(1);
|
|
284
|
+
expect(docs[0].order_count).toBe(1);
|
|
285
|
+
expect(docs[0].row_count).toBe(2);
|
|
286
|
+
expect(docs[0].discount_amount).toBe(500);
|
|
287
|
+
});
|
|
288
|
+
it('marks a source FREE_ITEM only when EVERY row is a free item', () => {
|
|
289
|
+
const free = entry({
|
|
290
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.FREE_ITEM,
|
|
291
|
+
item_id: 'i1',
|
|
292
|
+
entry_key: 'item:i1:special',
|
|
293
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
294
|
+
rate: 100,
|
|
295
|
+
amount: 500,
|
|
296
|
+
item_value: 500,
|
|
297
|
+
});
|
|
298
|
+
const allFree = (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)(scopeInput([free]));
|
|
299
|
+
expect(allFree[0].kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.FREE_ITEM);
|
|
300
|
+
expect(allFree[0].free_item_count).toBe(1);
|
|
301
|
+
expect(allFree[0].free_item_value).toBe(500);
|
|
302
|
+
expect(allFree[0].discount_amount).toBe(500);
|
|
303
|
+
const mixed = (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)(scopeInput([
|
|
304
|
+
free,
|
|
305
|
+
entry({
|
|
306
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
307
|
+
order_id: 'o2',
|
|
308
|
+
item_id: 'i2',
|
|
309
|
+
entry_key: 'item:i2:special',
|
|
310
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
311
|
+
rate: 50,
|
|
312
|
+
amount: 100,
|
|
313
|
+
}),
|
|
314
|
+
]));
|
|
315
|
+
expect(mixed[0].kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT);
|
|
316
|
+
expect(mixed[0].free_item_count).toBe(1);
|
|
317
|
+
expect(mixed[0].discount_amount).toBe(600);
|
|
318
|
+
});
|
|
319
|
+
it('carries the complimentary coverage but never discount money', () => {
|
|
320
|
+
const docs = (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)(scopeInput([
|
|
321
|
+
entry({
|
|
322
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY,
|
|
323
|
+
slot: null,
|
|
324
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT,
|
|
325
|
+
entry_key: 'payment:p1',
|
|
326
|
+
amount: 0,
|
|
327
|
+
rate: null,
|
|
328
|
+
is_percentage: null,
|
|
329
|
+
complimentary_full: true,
|
|
330
|
+
covered_amount: 1000,
|
|
331
|
+
order_amount: 1000,
|
|
332
|
+
}),
|
|
333
|
+
]));
|
|
334
|
+
expect(docs).toHaveLength(1);
|
|
335
|
+
expect(docs[0].source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_FULL);
|
|
336
|
+
expect(docs[0].discount_amount).toBe(0);
|
|
337
|
+
expect(docs[0].covered_amount).toBe(1000);
|
|
338
|
+
expect(docs[0].order_count).toBe(1);
|
|
339
|
+
});
|
|
340
|
+
it('keeps key_metrics and the top-level totals in lockstep', () => {
|
|
341
|
+
const docs = (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)({
|
|
342
|
+
...scopeInput([
|
|
343
|
+
entry({ order_id: 'o1', discount_id: 'MANUAL_10%', amount: 100 }),
|
|
344
|
+
entry({ order_id: 'o2', discount_id: 'MANUAL_10%', amount: 200 }),
|
|
345
|
+
]),
|
|
346
|
+
sections: {
|
|
347
|
+
'COMPLETED:POS_WALK-IN': { rows: [entry({ order_id: 'o1', amount: 100 })] },
|
|
348
|
+
'COMPLETED:WEB': { rows: [entry({ order_id: 'o2', amount: 200 })] },
|
|
349
|
+
},
|
|
350
|
+
});
|
|
351
|
+
expect(docs[0].key_metrics).toHaveLength(2);
|
|
352
|
+
expect(docs[0].key_metrics.reduce((sum, metric) => sum + metric.discount_amount, 0)).toBe(300);
|
|
353
|
+
expect(docs[0].key_metrics.reduce((sum, metric) => sum + metric.order_count, 0)).toBe(2);
|
|
354
|
+
expect(docs[0].discount_amount).toBe(300);
|
|
355
|
+
expect(docs[0].order_count).toBe(2);
|
|
356
|
+
});
|
|
357
|
+
it('reconciles Σ source.discount_amount to Σ entry.amount (excluding comp)', () => {
|
|
358
|
+
const rows = [
|
|
359
|
+
entry({ order_id: 'o1', amount: 500, discount_id: 'MANUAL_50%' }),
|
|
360
|
+
entry({
|
|
361
|
+
order_id: 'o2',
|
|
362
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
363
|
+
item_id: 'i1',
|
|
364
|
+
entry_key: 'item:i1:special',
|
|
365
|
+
amount: 250,
|
|
366
|
+
discount_id: 'MANUAL_25%',
|
|
367
|
+
}),
|
|
368
|
+
entry({
|
|
369
|
+
order_id: 'o3',
|
|
370
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY,
|
|
371
|
+
slot: null,
|
|
372
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT,
|
|
373
|
+
entry_key: 'payment:p1',
|
|
374
|
+
amount: 0,
|
|
375
|
+
complimentary_full: true,
|
|
376
|
+
covered_amount: 900,
|
|
377
|
+
}),
|
|
378
|
+
];
|
|
379
|
+
const docs = (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)(scopeInput(rows));
|
|
380
|
+
const sourceTotal = docs.reduce((sum, doc) => sum + doc.discount_amount, 0);
|
|
381
|
+
const entryTotal = rows.reduce((sum, row) => (row.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY ? sum : sum + row.amount), 0);
|
|
382
|
+
expect(sourceTotal).toBe(entryTotal);
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
describe('complimentary full/partial accumulator', () => {
|
|
386
|
+
const baseOrder = (over = {}) => ({
|
|
387
|
+
_id: 'order-1',
|
|
388
|
+
tenant_id: 'tenant-a',
|
|
389
|
+
platform: 'POS_WALK-IN',
|
|
390
|
+
order_status: 'COMPLETED',
|
|
391
|
+
order_number: 1,
|
|
392
|
+
categories: [],
|
|
393
|
+
payments: [],
|
|
394
|
+
order_discount_details: null,
|
|
395
|
+
total_discount_applied: 0,
|
|
396
|
+
grand_total: 1000,
|
|
397
|
+
amount_earned: 1000,
|
|
398
|
+
amount_collected: 0,
|
|
399
|
+
createdAt: new Date('2026-09-15T13:00:00.000Z'),
|
|
400
|
+
updatedAt: new Date('2026-09-15T13:00:00.000Z'),
|
|
401
|
+
staff_details: null,
|
|
402
|
+
order_taker_details: null,
|
|
403
|
+
wallet_redemptions: null,
|
|
404
|
+
...over,
|
|
405
|
+
});
|
|
406
|
+
const payment = (over = {}) => ({
|
|
407
|
+
_id: 'p1',
|
|
408
|
+
payment_type: 'COMPLIMENTARY',
|
|
409
|
+
payment_status: 'SETTLED',
|
|
410
|
+
amount_due: 1000,
|
|
411
|
+
amount_paid: 1000,
|
|
412
|
+
amount_refunded: 0,
|
|
413
|
+
card_details: null,
|
|
414
|
+
payment_id: null,
|
|
415
|
+
...over,
|
|
416
|
+
});
|
|
417
|
+
it('marks a full comp (all COMPLIMENTARY, covers the order)', () => {
|
|
418
|
+
const result = (0, materialized_discount_accumulator_helpers_1.accumulateOrderDiscounts)(baseOrder({ payments: [payment()] }), CONTEXT);
|
|
419
|
+
const comp = result.entries.find((row) => row.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY);
|
|
420
|
+
expect(comp.complimentary_full).toBe(true);
|
|
421
|
+
expect(comp.covered_amount).toBe(1000);
|
|
422
|
+
expect(comp.order_amount).toBe(1000);
|
|
423
|
+
});
|
|
424
|
+
it('marks a single underpaid COMPLIMENTARY as partial (no other tender)', () => {
|
|
425
|
+
const result = (0, materialized_discount_accumulator_helpers_1.accumulateOrderDiscounts)(baseOrder({
|
|
426
|
+
payments: [
|
|
427
|
+
payment({ amount_due: 1000, amount_paid: 400 }),
|
|
428
|
+
{
|
|
429
|
+
_id: 'p2',
|
|
430
|
+
payment_type: 'UNPAID',
|
|
431
|
+
payment_status: 'PENDING',
|
|
432
|
+
amount_due: 600,
|
|
433
|
+
amount_paid: 0,
|
|
434
|
+
amount_refunded: 0,
|
|
435
|
+
card_details: null,
|
|
436
|
+
payment_id: null,
|
|
437
|
+
},
|
|
438
|
+
],
|
|
439
|
+
}), CONTEXT);
|
|
440
|
+
const comp = result.entries.find((row) => row.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY);
|
|
441
|
+
expect(comp.complimentary_full).toBe(false);
|
|
442
|
+
expect(comp.covered_amount).toBe(400);
|
|
443
|
+
});
|
|
444
|
+
it('marks a comp mixed with another paid tender as partial', () => {
|
|
445
|
+
const result = (0, materialized_discount_accumulator_helpers_1.accumulateOrderDiscounts)(baseOrder({
|
|
446
|
+
payments: [
|
|
447
|
+
payment({ amount_due: 1000, amount_paid: 400 }),
|
|
448
|
+
{
|
|
449
|
+
_id: 'p2',
|
|
450
|
+
payment_type: 'CASH',
|
|
451
|
+
payment_status: 'SETTLED',
|
|
452
|
+
amount_due: 600,
|
|
453
|
+
amount_paid: 600,
|
|
454
|
+
amount_refunded: 0,
|
|
455
|
+
card_details: null,
|
|
456
|
+
payment_id: 'pay-2',
|
|
457
|
+
},
|
|
458
|
+
],
|
|
459
|
+
}), CONTEXT);
|
|
460
|
+
const comp = result.entries.find((row) => row.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY);
|
|
461
|
+
expect(comp.complimentary_full).toBe(false);
|
|
462
|
+
expect(comp.covered_amount).toBe(400);
|
|
463
|
+
});
|
|
464
|
+
it('ignores a zero-amount other tender when deciding full vs partial', () => {
|
|
465
|
+
const result = (0, materialized_discount_accumulator_helpers_1.accumulateOrderDiscounts)(baseOrder({
|
|
466
|
+
payments: [
|
|
467
|
+
payment(),
|
|
468
|
+
{
|
|
469
|
+
_id: 'p2',
|
|
470
|
+
payment_type: 'CASH',
|
|
471
|
+
payment_status: 'SETTLED',
|
|
472
|
+
amount_due: 0,
|
|
473
|
+
amount_paid: 0,
|
|
474
|
+
amount_refunded: 0,
|
|
475
|
+
card_details: null,
|
|
476
|
+
payment_id: 'pay-2',
|
|
477
|
+
},
|
|
478
|
+
],
|
|
479
|
+
}), CONTEXT);
|
|
480
|
+
const comp = result.entries.find((row) => row.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY);
|
|
481
|
+
expect(comp.complimentary_full).toBe(true);
|
|
482
|
+
});
|
|
483
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|