@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,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
4
|
+
const tenant_discount_source_formatter_1 = require("../../helpers/materialized-reports/tenant-discount-source-formatter");
|
|
5
|
+
/**
|
|
6
|
+
* "By discount" formatter — money (cents → decimals), share basis (comp is NOT
|
|
7
|
+
* a discount), rate labels, and the separate complimentary section.
|
|
8
|
+
*/
|
|
9
|
+
const row = (over = {}) => ({
|
|
10
|
+
source_key: 'discount:a',
|
|
11
|
+
source_id: 'a',
|
|
12
|
+
source_name: 'UBL Bank 15%',
|
|
13
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COLLECTION,
|
|
14
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT,
|
|
15
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER,
|
|
16
|
+
slot: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL,
|
|
17
|
+
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION,
|
|
18
|
+
rate: 15,
|
|
19
|
+
rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE,
|
|
20
|
+
order_count: 10,
|
|
21
|
+
row_count: 10,
|
|
22
|
+
discount_amount: 100000,
|
|
23
|
+
free_item_count: 0,
|
|
24
|
+
free_item_value: 0,
|
|
25
|
+
complimentary_full: null,
|
|
26
|
+
covered_amount: 0,
|
|
27
|
+
...over,
|
|
28
|
+
});
|
|
29
|
+
const report = (sources) => ({
|
|
30
|
+
tenant_id: 'tenant-a',
|
|
31
|
+
brand_id: 'brand-a',
|
|
32
|
+
max_updated_at: new Date('2026-09-30T23:00:00.000Z'),
|
|
33
|
+
matched_documents: 30,
|
|
34
|
+
sale_type_key_filters: ['ORDER_STATUS', 'PLATFORM'],
|
|
35
|
+
sources,
|
|
36
|
+
});
|
|
37
|
+
describe('getFormattedTenantDiscountSources', () => {
|
|
38
|
+
it('converts cents, computes share over DISCOUNTS only and sorts by money', () => {
|
|
39
|
+
const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
|
|
40
|
+
report([
|
|
41
|
+
row({ source_key: 'discount:a', discount_amount: 100000 }),
|
|
42
|
+
row({
|
|
43
|
+
source_key: 'manual:order',
|
|
44
|
+
source_name: 'Manual / quick discount',
|
|
45
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.MANUAL,
|
|
46
|
+
discount_amount: 50000,
|
|
47
|
+
rate: null,
|
|
48
|
+
rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED,
|
|
49
|
+
}),
|
|
50
|
+
row({
|
|
51
|
+
source_key: 'complimentary:full',
|
|
52
|
+
source_name: 'Fully complimentary',
|
|
53
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COMPLIMENTARY,
|
|
54
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY,
|
|
55
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT,
|
|
56
|
+
discount_amount: 0,
|
|
57
|
+
order_count: 3,
|
|
58
|
+
rate: null,
|
|
59
|
+
rate_kind: null,
|
|
60
|
+
complimentary_full: true,
|
|
61
|
+
covered_amount: 30000,
|
|
62
|
+
}),
|
|
63
|
+
]),
|
|
64
|
+
]);
|
|
65
|
+
expect(formatted.sources).toHaveLength(2);
|
|
66
|
+
expect(formatted.sources[0].discount_amount).toBe(1000);
|
|
67
|
+
expect(formatted.sources[0].share_percent).toBe(66.67);
|
|
68
|
+
expect(formatted.sources[1].discount_amount).toBe(500);
|
|
69
|
+
expect(formatted.sources[1].share_percent).toBe(33.33);
|
|
70
|
+
expect(formatted.totals.discount_amount).toBe(1500);
|
|
71
|
+
expect(formatted.totals.source_count).toBe(2);
|
|
72
|
+
expect(formatted.top_source?.source_name).toBe('UBL Bank 15%');
|
|
73
|
+
// Complimentary is separate and never part of the share basis.
|
|
74
|
+
expect(formatted.complimentary).toHaveLength(1);
|
|
75
|
+
expect(formatted.complimentary[0].discount_amount).toBe(0);
|
|
76
|
+
expect(formatted.complimentary[0].covered_amount).toBe(300);
|
|
77
|
+
expect(formatted.totals.complimentary_covered_amount).toBe(300);
|
|
78
|
+
expect(formatted.totals.complimentary_full_count).toBe(3);
|
|
79
|
+
expect(formatted.sources.every((source) => source.source_class !== 'COMPLIMENTARY')).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
it('renders rate labels: percentage, mixed and fixed', () => {
|
|
82
|
+
const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
|
|
83
|
+
report([
|
|
84
|
+
row({ rate: 15, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE }),
|
|
85
|
+
row({
|
|
86
|
+
source_key: 'manual:item',
|
|
87
|
+
rate: null,
|
|
88
|
+
rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED,
|
|
89
|
+
}),
|
|
90
|
+
row({
|
|
91
|
+
source_key: 'price_override:item',
|
|
92
|
+
rate: null,
|
|
93
|
+
rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.FIXED,
|
|
94
|
+
}),
|
|
95
|
+
]),
|
|
96
|
+
]);
|
|
97
|
+
expect(formatted.sources.find((source) => source.source_key === 'discount:a')?.rate_label).toBe('15%');
|
|
98
|
+
expect(formatted.sources.find((source) => source.source_key === 'manual:item')?.rate_label).toBe('mixed');
|
|
99
|
+
expect(formatted.sources.find((source) => source.source_key === 'price_override:item')?.rate_label).toBe('—');
|
|
100
|
+
});
|
|
101
|
+
it('folds multiple tenants into ONE page', () => {
|
|
102
|
+
const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
|
|
103
|
+
report([row({ discount_amount: 1000 })]),
|
|
104
|
+
{
|
|
105
|
+
...report([row({ discount_amount: 2000, order_count: 5 })]),
|
|
106
|
+
tenant_id: 'tenant-b',
|
|
107
|
+
},
|
|
108
|
+
]);
|
|
109
|
+
expect(formatted.tenant_ids.sort()).toEqual(['tenant-a', 'tenant-b']);
|
|
110
|
+
expect(formatted.totals.discount_amount).toBe(30);
|
|
111
|
+
});
|
|
112
|
+
it('uses the page summary total as the shared share basis when provided', () => {
|
|
113
|
+
const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
|
|
114
|
+
report([
|
|
115
|
+
row({ source_key: 'discount:a', discount_amount: 100000 }),
|
|
116
|
+
row({ source_key: 'manual:order', discount_amount: 50000 }),
|
|
117
|
+
]),
|
|
118
|
+
], { share_basis: 3000 });
|
|
119
|
+
// 1000 / 3000 and 500 / 3000 — NOT 66.7 / 33.3 of the source sum.
|
|
120
|
+
expect(formatted.sources.find((source) => source.source_key === 'discount:a')?.share_percent).toBe(33.33);
|
|
121
|
+
expect(formatted.sources.find((source) => source.source_key === 'manual:order')?.share_percent).toBe(16.67);
|
|
122
|
+
});
|
|
123
|
+
it('uses the source total when source == summary', () => {
|
|
124
|
+
const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
|
|
125
|
+
report([
|
|
126
|
+
row({ source_key: 'discount:a', discount_amount: 100000 }),
|
|
127
|
+
row({ source_key: 'manual:order', discount_amount: 50000 }),
|
|
128
|
+
]),
|
|
129
|
+
], { share_basis: 1500 });
|
|
130
|
+
expect(formatted.sources.map((source) => source.share_percent)).toEqual([66.67, 33.33]);
|
|
131
|
+
});
|
|
132
|
+
it('uses the larger SOURCE total when source > summary (no share above 100%)', () => {
|
|
133
|
+
const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
|
|
134
|
+
report([
|
|
135
|
+
row({ source_key: 'discount:a', discount_amount: 100000 }),
|
|
136
|
+
row({ source_key: 'manual:order', discount_amount: 50000 }),
|
|
137
|
+
]),
|
|
138
|
+
],
|
|
139
|
+
// The summary claims only $5, but the source rows total $1,500.
|
|
140
|
+
{ share_basis: 5 });
|
|
141
|
+
expect(formatted.sources.map((source) => source.share_percent)).toEqual([66.67, 33.33]);
|
|
142
|
+
expect(formatted.sources.every((source) => source.share_percent <= 100)).toBe(true);
|
|
143
|
+
});
|
|
144
|
+
it('returns a comp-only page with NO discount sources (GAP-2 shape)', () => {
|
|
145
|
+
const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
|
|
146
|
+
report([
|
|
147
|
+
row({
|
|
148
|
+
source_key: 'complimentary:full',
|
|
149
|
+
source_name: 'Fully complimentary',
|
|
150
|
+
source_class: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COMPLIMENTARY,
|
|
151
|
+
kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY,
|
|
152
|
+
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT,
|
|
153
|
+
discount_amount: 0,
|
|
154
|
+
order_count: 3,
|
|
155
|
+
rate: null,
|
|
156
|
+
rate_kind: null,
|
|
157
|
+
complimentary_full: true,
|
|
158
|
+
covered_amount: 30000,
|
|
159
|
+
}),
|
|
160
|
+
]),
|
|
161
|
+
]);
|
|
162
|
+
expect(formatted.sources).toEqual([]);
|
|
163
|
+
expect(formatted.complimentary).toHaveLength(1);
|
|
164
|
+
expect(formatted.totals.discount_amount).toBe(0);
|
|
165
|
+
expect(formatted.has_data).toBe(true);
|
|
166
|
+
expect(formatted.top_source).toBeNull();
|
|
167
|
+
});
|
|
168
|
+
it('returns an empty page for an empty read', () => {
|
|
169
|
+
const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([]);
|
|
170
|
+
expect(formatted.has_data).toBe(false);
|
|
171
|
+
expect(formatted.sources).toEqual([]);
|
|
172
|
+
expect(formatted.complimentary).toEqual([]);
|
|
173
|
+
expect(formatted.totals.discount_amount).toBe(0);
|
|
174
|
+
});
|
|
175
|
+
it('rejects an unformattable input instead of throwing', () => {
|
|
176
|
+
expect((0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)(null)).toBeNull();
|
|
177
|
+
expect((0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([null])).toBeNull();
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
describe('getDiscountSourceShareReconciliation', () => {
|
|
181
|
+
it('reports an unattributed GAP when summary > source', () => {
|
|
182
|
+
const result = (0, tenant_discount_source_formatter_1.getDiscountSourceShareReconciliation)(3000, 1500);
|
|
183
|
+
expect(result.share_basis).toBe(3000);
|
|
184
|
+
expect(result.gap).toBe(1500);
|
|
185
|
+
expect(result.overage).toBe(0);
|
|
186
|
+
expect(result.show_gap).toBe(true);
|
|
187
|
+
expect(result.show_overage).toBe(false);
|
|
188
|
+
});
|
|
189
|
+
it('is normal when summary == source', () => {
|
|
190
|
+
const result = (0, tenant_discount_source_formatter_1.getDiscountSourceShareReconciliation)(1500, 1500);
|
|
191
|
+
expect(result.share_basis).toBe(1500);
|
|
192
|
+
expect(result.show_gap).toBe(false);
|
|
193
|
+
expect(result.show_overage).toBe(false);
|
|
194
|
+
});
|
|
195
|
+
it('reports an OVERAGE when source > summary and uses the larger basis', () => {
|
|
196
|
+
const result = (0, tenant_discount_source_formatter_1.getDiscountSourceShareReconciliation)(5, 1500);
|
|
197
|
+
expect(result.share_basis).toBe(1500);
|
|
198
|
+
expect(result.gap).toBe(0);
|
|
199
|
+
expect(result.overage).toBe(1495);
|
|
200
|
+
expect(result.show_gap).toBe(false);
|
|
201
|
+
expect(result.show_overage).toBe(true);
|
|
202
|
+
});
|
|
203
|
+
});
|