@managesome/eposmatic-toolkit 0.4.24 → 0.4.26

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.
@@ -31,10 +31,38 @@ const LEVEL_LABELS = {
31
31
  ITEM: 'Item',
32
32
  PAYMENT: 'Payment',
33
33
  };
34
- const formatRate = (rate) => {
34
+ /**
35
+ * THE rate normalisation rule for discount source rows.
36
+ *
37
+ * A source row's `rate` is the average of the percentage rates of its
38
+ * percentage rows, copied verbatim from the order's discount details. The
39
+ * WRITE side has no single unit for that field: the `discounts` collection
40
+ * stores a percentage value in CENTS (`value` is an `IPrice`, so 50% is
41
+ * stored as 5000) and some writers copy that raw number into the order's
42
+ * discount details, while others (manual quick discounts, payment-type
43
+ * discounts) store the plain percent (25 for 25%). The stored source row
44
+ * therefore carries whichever form the writer used.
45
+ *
46
+ * THE RULE (one rule, applied at the display boundary):
47
+ * - a rate ABOVE 100 is CENTS-SCALED -> divide by 100 (5000 -> 50%);
48
+ * - a rate of 100 or below is ALREADY a percentage -> use as-is (15 -> 15%).
49
+ *
50
+ * A real percentage discount can never exceed 100 (the POS clamps it), so the
51
+ * >100 boundary separates the two forms for every value the platform can
52
+ * produce, except exactly 100, which is read as 100%.
53
+ *
54
+ * This is a DISPLAY rule only: it never rewrites a stored document.
55
+ */
56
+ export function normalizeDiscountSourceRatePercent(rate) {
35
57
  if (rate == null || !Number.isFinite(rate))
58
+ return null;
59
+ return rate > 100 ? rate / 100 : rate;
60
+ }
61
+ const formatRate = (rate) => {
62
+ const percent = normalizeDiscountSourceRatePercent(rate);
63
+ if (percent == null)
36
64
  return '—';
37
- return `${Math.round(rate * 100) / 100}%`;
65
+ return `${Math.round(percent * 100) / 100}%`;
38
66
  };
39
67
  const rateKindLabel = (rateKind) => {
40
68
  switch (rateKind) {
@@ -64,7 +92,9 @@ const makeRow = (source, shareBasis) => {
64
92
  level: source.level,
65
93
  level_label: levelLabel,
66
94
  type_label: kindLabel === '—' ? levelLabel : `${levelLabel} · ${kindLabel}`,
95
+ rate_kind_label: kindLabel,
67
96
  rate: source.rate,
97
+ rate_percent: normalizeDiscountSourceRatePercent(source.rate),
68
98
  rate_label: source.rate_kind === MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED ? 'mixed' : formatRate(source.rate),
69
99
  order_count: source.order_count,
70
100
  discount_amount: amount,
@@ -35,10 +35,14 @@ export const TenantHrShiftScheduleSchema = new Schema({
35
35
  start_time: {
36
36
  type: Number,
37
37
  required: true,
38
+ min: 0,
39
+ max: 86399,
38
40
  },
39
41
  end_time: {
40
42
  type: Number,
41
43
  required: true,
44
+ min: 0,
45
+ max: 86399,
42
46
  },
43
47
  position: {
44
48
  type: String,
@@ -28,10 +28,14 @@ export const TenantHrShiftSchema = new Schema({
28
28
  start_time: {
29
29
  type: Number,
30
30
  required: true,
31
+ min: 0,
32
+ max: 86399,
31
33
  },
32
34
  end_time: {
33
35
  type: Number,
34
36
  required: true,
37
+ min: 0,
38
+ max: 86399,
35
39
  },
36
40
  grace_period_minutes: {
37
41
  type: Number,
@@ -19,7 +19,12 @@ export interface IFormattedDiscountSourceRow {
19
19
  level: string;
20
20
  level_label: string;
21
21
  type_label: string;
22
+ /** The rate kind as its own label ("Percentage" / "Fixed" / "Mixed" / "—"). */
23
+ rate_kind_label: string;
24
+ /** RAW stored rate (cents-scaled or plain percent) — never normalised. */
22
25
  rate: number | null;
26
+ /** Normalised display percentage (see `normalizeDiscountSourceRatePercent`). */
27
+ rate_percent: number | null;
23
28
  rate_label: string;
24
29
  order_count: number;
25
30
  discount_amount: number;
@@ -64,6 +69,29 @@ export interface IDiscountSourceShareReconciliation {
64
69
  show_overage: boolean;
65
70
  }
66
71
  export declare function getDiscountSourceShareReconciliation(summaryTotal: number | null | undefined, sourceTotal: number): IDiscountSourceShareReconciliation;
72
+ /**
73
+ * THE rate normalisation rule for discount source rows.
74
+ *
75
+ * A source row's `rate` is the average of the percentage rates of its
76
+ * percentage rows, copied verbatim from the order's discount details. The
77
+ * WRITE side has no single unit for that field: the `discounts` collection
78
+ * stores a percentage value in CENTS (`value` is an `IPrice`, so 50% is
79
+ * stored as 5000) and some writers copy that raw number into the order's
80
+ * discount details, while others (manual quick discounts, payment-type
81
+ * discounts) store the plain percent (25 for 25%). The stored source row
82
+ * therefore carries whichever form the writer used.
83
+ *
84
+ * THE RULE (one rule, applied at the display boundary):
85
+ * - a rate ABOVE 100 is CENTS-SCALED -> divide by 100 (5000 -> 50%);
86
+ * - a rate of 100 or below is ALREADY a percentage -> use as-is (15 -> 15%).
87
+ *
88
+ * A real percentage discount can never exceed 100 (the POS clamps it), so the
89
+ * >100 boundary separates the two forms for every value the platform can
90
+ * produce, except exactly 100, which is read as 100%.
91
+ *
92
+ * This is a DISPLAY rule only: it never rewrites a stored document.
93
+ */
94
+ export declare function normalizeDiscountSourceRatePercent(rate: number | null | undefined): number | null;
67
95
  /**
68
96
  * Formats one or more tenants' source records into the page model.
69
97
  *
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getDiscountSourceShareReconciliation = getDiscountSourceShareReconciliation;
4
+ exports.normalizeDiscountSourceRatePercent = normalizeDiscountSourceRatePercent;
4
5
  exports.getFormattedTenantDiscountSources = getFormattedTenantDiscountSources;
5
6
  const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
6
7
  const price_conversion_1 = require("../utils/price-conversion");
@@ -35,10 +36,38 @@ const LEVEL_LABELS = {
35
36
  ITEM: 'Item',
36
37
  PAYMENT: 'Payment',
37
38
  };
38
- const formatRate = (rate) => {
39
+ /**
40
+ * THE rate normalisation rule for discount source rows.
41
+ *
42
+ * A source row's `rate` is the average of the percentage rates of its
43
+ * percentage rows, copied verbatim from the order's discount details. The
44
+ * WRITE side has no single unit for that field: the `discounts` collection
45
+ * stores a percentage value in CENTS (`value` is an `IPrice`, so 50% is
46
+ * stored as 5000) and some writers copy that raw number into the order's
47
+ * discount details, while others (manual quick discounts, payment-type
48
+ * discounts) store the plain percent (25 for 25%). The stored source row
49
+ * therefore carries whichever form the writer used.
50
+ *
51
+ * THE RULE (one rule, applied at the display boundary):
52
+ * - a rate ABOVE 100 is CENTS-SCALED -> divide by 100 (5000 -> 50%);
53
+ * - a rate of 100 or below is ALREADY a percentage -> use as-is (15 -> 15%).
54
+ *
55
+ * A real percentage discount can never exceed 100 (the POS clamps it), so the
56
+ * >100 boundary separates the two forms for every value the platform can
57
+ * produce, except exactly 100, which is read as 100%.
58
+ *
59
+ * This is a DISPLAY rule only: it never rewrites a stored document.
60
+ */
61
+ function normalizeDiscountSourceRatePercent(rate) {
39
62
  if (rate == null || !Number.isFinite(rate))
63
+ return null;
64
+ return rate > 100 ? rate / 100 : rate;
65
+ }
66
+ const formatRate = (rate) => {
67
+ const percent = normalizeDiscountSourceRatePercent(rate);
68
+ if (percent == null)
40
69
  return '—';
41
- return `${Math.round(rate * 100) / 100}%`;
70
+ return `${Math.round(percent * 100) / 100}%`;
42
71
  };
43
72
  const rateKindLabel = (rateKind) => {
44
73
  switch (rateKind) {
@@ -68,7 +97,9 @@ const makeRow = (source, shareBasis) => {
68
97
  level: source.level,
69
98
  level_label: levelLabel,
70
99
  type_label: kindLabel === '—' ? levelLabel : `${levelLabel} · ${kindLabel}`,
100
+ rate_kind_label: kindLabel,
71
101
  rate: source.rate,
102
+ rate_percent: normalizeDiscountSourceRatePercent(source.rate),
72
103
  rate_label: source.rate_kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED ? 'mixed' : formatRate(source.rate),
73
104
  order_count: source.order_count,
74
105
  discount_amount: amount,
@@ -38,10 +38,14 @@ exports.TenantHrShiftScheduleSchema = new mongoose_1.Schema({
38
38
  start_time: {
39
39
  type: Number,
40
40
  required: true,
41
+ min: 0,
42
+ max: 86399,
41
43
  },
42
44
  end_time: {
43
45
  type: Number,
44
46
  required: true,
47
+ min: 0,
48
+ max: 86399,
45
49
  },
46
50
  position: {
47
51
  type: String,
@@ -31,10 +31,14 @@ exports.TenantHrShiftSchema = new mongoose_1.Schema({
31
31
  start_time: {
32
32
  type: Number,
33
33
  required: true,
34
+ min: 0,
35
+ max: 86399,
34
36
  },
35
37
  end_time: {
36
38
  type: Number,
37
39
  required: true,
40
+ min: 0,
41
+ max: 86399,
38
42
  },
39
43
  grace_period_minutes: {
40
44
  type: Number,
@@ -97,6 +97,32 @@ describe('getFormattedTenantDiscountSources', () => {
97
97
  expect(formatted.sources.find((source) => source.source_key === 'discount:a')?.rate_label).toBe('15%');
98
98
  expect(formatted.sources.find((source) => source.source_key === 'manual:item')?.rate_label).toBe('mixed');
99
99
  expect(formatted.sources.find((source) => source.source_key === 'price_override:item')?.rate_label).toBe('—');
100
+ // The rate kind has its own label so a view can show it without parsing.
101
+ expect(formatted.sources.find((source) => source.source_key === 'discount:a')?.rate_kind_label).toBe('Percentage');
102
+ expect(formatted.sources.find((source) => source.source_key === 'manual:item')?.rate_kind_label).toBe('Mixed');
103
+ expect(formatted.sources.find((source) => source.source_key === 'price_override:item')?.rate_kind_label).toBe('Fixed');
104
+ });
105
+ it('normalises a CENTS-SCALED rate (5000) to the intended percentage', () => {
106
+ const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
107
+ report([row({ rate: 5000, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE })]),
108
+ ]);
109
+ const source = formatted.sources[0];
110
+ expect(source.rate_label).toBe('50%');
111
+ expect(source.rate_percent).toBe(50);
112
+ // The RAW stored value stays untouched on the row (display-only rule).
113
+ expect(source.rate).toBe(5000);
114
+ });
115
+ it('keeps "mixed" and "—" when normalising (the rate kind wins)', () => {
116
+ const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
117
+ report([
118
+ row({ source_key: 'discount:a', rate: 5000, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED }),
119
+ row({ source_key: 'discount:b', rate: null, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE }),
120
+ row({ source_key: 'discount:c', rate: null, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.FIXED }),
121
+ ]),
122
+ ]);
123
+ expect(formatted.sources.find((source) => source.source_key === 'discount:a')?.rate_label).toBe('mixed');
124
+ expect(formatted.sources.find((source) => source.source_key === 'discount:b')?.rate_label).toBe('—');
125
+ expect(formatted.sources.find((source) => source.source_key === 'discount:c')?.rate_label).toBe('—');
100
126
  });
101
127
  it('folds multiple tenants into ONE page', () => {
102
128
  const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
@@ -177,6 +203,44 @@ describe('getFormattedTenantDiscountSources', () => {
177
203
  expect((0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([null])).toBeNull();
178
204
  });
179
205
  });
206
+ describe('normalizeDiscountSourceRatePercent', () => {
207
+ /**
208
+ * THE RULE: a stored rate ABOVE 100 is cents-scaled (50% is stored as 5000)
209
+ * and is divided by 100; a rate of 100 or below is already a percentage.
210
+ */
211
+ it('divides a cents-scaled rate by 100', () => {
212
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(5000)).toBe(50);
213
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(2000)).toBe(20);
214
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(1500)).toBe(15);
215
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(10000)).toBe(100);
216
+ });
217
+ it('keeps a plain percentage as-is', () => {
218
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(100)).toBe(100);
219
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(15)).toBe(15);
220
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(1)).toBe(1);
221
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(0)).toBe(0);
222
+ });
223
+ it('returns null for a missing or non-finite rate', () => {
224
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(null)).toBeNull();
225
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(undefined)).toBeNull();
226
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(Number.NaN)).toBeNull();
227
+ expect((0, tenant_discount_source_formatter_1.normalizeDiscountSourceRatePercent)(Number.POSITIVE_INFINITY)).toBeNull();
228
+ });
229
+ it('renders the label of every production rate shape', () => {
230
+ const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
231
+ report([
232
+ row({ source_key: 'a', rate: 5000, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE }),
233
+ row({ source_key: 'b', rate: 2000, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE }),
234
+ row({ source_key: 'c', rate: 1500, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE }),
235
+ row({ source_key: 'd', rate: 100, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE }),
236
+ row({ source_key: 'e', rate: 15, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE }),
237
+ row({ source_key: 'f', rate: null, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE }),
238
+ ]),
239
+ ]);
240
+ const labels = Object.fromEntries(formatted.sources.map((source) => [source.source_key, source.rate_label]));
241
+ expect(labels).toEqual({ a: '50%', b: '20%', c: '15%', d: '100%', e: '15%', f: '—' });
242
+ });
243
+ });
180
244
  describe('getDiscountSourceShareReconciliation', () => {
181
245
  it('reports an unattributed GAP when summary > source', () => {
182
246
  const result = (0, tenant_discount_source_formatter_1.getDiscountSourceShareReconciliation)(3000, 1500);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.24",
3
+ "version": "0.4.26",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",