@bisondesk/core-sdk 1.0.592 → 1.0.593

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.
@@ -0,0 +1,82 @@
1
+ import { Categories } from '../constants.js';
2
+ import { CountryPricingRule } from '../types/country-pricing.js';
3
+ import {
4
+ convertFromBranchCurrency,
5
+ getApplicableDeliveryCost,
6
+ shouldApplyDeliveryCost,
7
+ } from './country-pricing.js';
8
+
9
+ const baseRule: CountryPricingRule = {
10
+ countryCode: 'bo',
11
+ currency: 'USD',
12
+ rate: 1.08,
13
+ deliveryCostByCategory: { [Categories.Truck]: 500 },
14
+ deliveryLocation: 'La Paz (Bolivia)',
15
+ };
16
+
17
+ describe('convertFromBranchCurrency', () => {
18
+ test('multiplies by rate', () => {
19
+ expect(convertFromBranchCurrency(1000, { rate: 1.08 })).toBe(1080);
20
+ });
21
+
22
+ test('uses Math.ceil to avoid underselling on partial cents', () => {
23
+ expect(convertFromBranchCurrency(999.5, { rate: 1.0 })).toBe(1000);
24
+ });
25
+ });
26
+
27
+ describe('shouldApplyDeliveryCost', () => {
28
+ test('returns false when origin equals rule country', () => {
29
+ expect(shouldApplyDeliveryCost({ countryCode: 'bo' }, 'bo')).toBe(false);
30
+ });
31
+
32
+ test('is case-insensitive', () => {
33
+ expect(shouldApplyDeliveryCost({ countryCode: 'bo' }, 'BO')).toBe(false);
34
+ expect(shouldApplyDeliveryCost({ countryCode: 'BO' }, 'bo')).toBe(false);
35
+ });
36
+
37
+ test('returns true when origin differs from rule country', () => {
38
+ expect(shouldApplyDeliveryCost({ countryCode: 'bo' }, 'be')).toBe(true);
39
+ });
40
+
41
+ test('returns true when origin is undefined (safe default)', () => {
42
+ expect(shouldApplyDeliveryCost({ countryCode: 'bo' }, undefined)).toBe(true);
43
+ });
44
+ });
45
+
46
+ describe('getApplicableDeliveryCost', () => {
47
+ test('returns undefined when same country, even with a configured cost', () => {
48
+ expect(getApplicableDeliveryCost(baseRule, Categories.Truck, 'bo')).toBeUndefined();
49
+ });
50
+
51
+ test('returns undefined when category is undefined', () => {
52
+ expect(getApplicableDeliveryCost(baseRule, undefined, 'be')).toBeUndefined();
53
+ });
54
+
55
+ test('returns the matching cost for a different country and known category', () => {
56
+ expect(getApplicableDeliveryCost(baseRule, Categories.Truck, 'be')).toBe(500);
57
+ });
58
+
59
+ test('returns 0 when the category is explicitly mapped to 0 (opt-in with no fee)', () => {
60
+ const zeroFeeRule: CountryPricingRule = {
61
+ countryCode: 'bo',
62
+ currency: 'USD',
63
+ rate: 1.08,
64
+ deliveryCostByCategory: { [Categories.Truck]: 0 },
65
+ deliveryLocation: 'Bolivia',
66
+ };
67
+ expect(getApplicableDeliveryCost(zeroFeeRule, Categories.Truck, 'be')).toBe(0);
68
+ });
69
+
70
+ test('returns undefined when category has no configured cost', () => {
71
+ expect(getApplicableDeliveryCost(baseRule, Categories.Van, 'be')).toBeUndefined();
72
+ });
73
+
74
+ test('returns undefined when rule has no deliveryCostByCategory at all', () => {
75
+ const pureRateRule: CountryPricingRule = {
76
+ countryCode: 'bo',
77
+ currency: 'USD',
78
+ rate: 1.08,
79
+ };
80
+ expect(getApplicableDeliveryCost(pureRateRule, Categories.Truck, 'be')).toBeUndefined();
81
+ });
82
+ });
@@ -0,0 +1,44 @@
1
+ import { Categories } from '../constants.js';
2
+ import { CountryPricingRule } from '../types/country-pricing.js';
3
+
4
+ /**
5
+ * Should the delivery cost defined by `rule` be applied for a vehicle whose
6
+ * origin branch is in `originCountry`? Returns `false` when the vehicle is
7
+ * already located in the rule's country (case-insensitive ISO-2 compare).
8
+ */
9
+ export const shouldApplyDeliveryCost = (
10
+ rule: Pick<CountryPricingRule, 'countryCode'>,
11
+ originCountry: string | undefined,
12
+ ): boolean => {
13
+ if (originCountry == null) {
14
+ return true;
15
+ }
16
+ return originCountry.toLowerCase() !== rule.countryCode.toLowerCase();
17
+ };
18
+
19
+ /**
20
+ * Look up the per-category delivery cost on the rule. Returns `undefined` when delivery is
21
+ * not applicable (same-country suppression, missing category, or category not in the rule's
22
+ * map). Returns the configured number — including `0` — when the rule explicitly covers the
23
+ * category. A `0` is a legitimate value: the operator has opted in for this category but is
24
+ * charging no fee on top (e.g. for vehicles already at the rule's `deliveryLocation`).
25
+ */
26
+ export const getApplicableDeliveryCost = (
27
+ rule: CountryPricingRule,
28
+ category: string | undefined,
29
+ originCountry: string | undefined,
30
+ ): number | undefined => {
31
+ if (!shouldApplyDeliveryCost(rule, originCountry)) {
32
+ return undefined;
33
+ }
34
+ if (category == null) {
35
+ return undefined;
36
+ }
37
+ return rule.deliveryCostByCategory?.[category as Categories];
38
+ };
39
+
40
+ /** Convert an amount in the owning branch's currency to `rule.currency`. */
41
+ export const convertFromBranchCurrency = (
42
+ branchAmount: number,
43
+ rule: Pick<CountryPricingRule, 'rate'>,
44
+ ): number => Math.ceil(branchAmount * rule.rate);