@zoxllc/shopify-checkout-extensions 0.0.5 → 0.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zoxllc/shopify-checkout-extensions",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "ZOX helper lib for shopify checkout extensions",
5
5
  "main": "src/index.ts",
6
6
  "directories": {
@@ -9,11 +9,7 @@ import type {
9
9
  StringComparisonType,
10
10
  } from './Qualifier';
11
11
  import type { MatchType, Selector } from './Selector';
12
- import { ConditionalDiscount } from '../lineItem/ConditionalDiscount';
13
- import { FixedItemDiscount } from '../lineItem/FixedItemDiscount';
14
- import { PercentageDiscount } from '../lineItem/PercentageDiscount';
15
12
  import { AndSelector } from './AndSelector';
16
- import { BuyXGetY } from '../lineItem/BuyXGetY';
17
13
  import {
18
14
  type CartAmountBehavior,
19
15
  CartAmountQualifier,
@@ -33,11 +29,17 @@ import {
33
29
  SaleItemSelector,
34
30
  type SaleItemSelectorMatchType,
35
31
  } from './SaleItemSelector';
32
+
33
+ import { ConditionalDiscount } from '../lineItem/ConditionalDiscount';
34
+ import { FixedItemDiscount } from '../lineItem/FixedItemDiscount';
35
+ import { PercentageDiscount } from '../lineItem/PercentageDiscount';
36
+ import { BuyXGetY } from '../lineItem/BuyXGetY';
37
+
36
38
  import { SubscriptionItemSelector } from './SubscriptionItemSelector';
37
- import { RateNameSelector } from '~/shipping/RateNameSelector';
38
- import { FixedDiscount } from '~/shipping/FixedDiscount';
39
- import { ShippingDiscount } from '~/shipping/ShippingDiscount';
40
39
  import { CountryCodeQualifier } from './CountryCodeQualifier';
40
+ import { RateNameSelector } from '../shipping/RateNameSelector';
41
+ import { FixedDiscount } from '../shipping/FixedDiscount';
42
+ import { ShippingDiscount } from '../shipping/ShippingDiscount';
41
43
 
42
44
  export type InputConfig = {
43
45
  __type: string;
@@ -1,5 +1,11 @@
1
- import type { RunInput as LineItemRunInput } from '../lineItem/api';
2
- import type { RunInput as ShippingRunInput } from '../shipping/api';
1
+ import type {
2
+ RunInput as LineItemRunInput,
3
+ Discount as DiscountItem,
4
+ } from '../lineItem/api';
5
+ import type {
6
+ RunInput as ShippingRunInput,
7
+ Discount as DiscountShipping,
8
+ } from '../shipping/api';
3
9
  import { Discount } from './DiscountInterface';
4
10
 
5
11
  type RunInput = {
@@ -80,7 +86,11 @@ export class DiscountCart {
80
86
  this.discounts = [];
81
87
  }
82
88
 
83
- addDiscount(discount: Discount) {
89
+ addShippingDiscount(discount: DiscountShipping) {
90
+ this.discounts.push(discount as Discount);
91
+ }
92
+
93
+ addDiscount(discount: DiscountItem) {
84
94
  // Figure out how much discount has been applied
85
95
  const totalDiscounts = discount.targets.reduce(
86
96
  (total, target) => {
@@ -103,10 +113,7 @@ export class DiscountCart {
103
113
  line.cost.amountPerQuantity.amount
104
114
  );
105
115
 
106
- // TODO: Make sure we're not discounting more items than are available on the line item quantity
107
-
108
- // When it's a fixed amount, calculate the items per but only if it applies to each
109
- // individual item...
116
+ // When it's a fixed amount, calculate the items per but only if it applies to each individual item...
110
117
  if (discount.value.fixedAmount) {
111
118
  if (
112
119
  discount.value.fixedAmount.appliesToEachItem
@@ -145,7 +152,7 @@ export class DiscountCart {
145
152
  this.appliedDiscountTotal
146
153
  );
147
154
 
148
- this.discounts.push(discount);
155
+ this.discounts.push(discount as Discount);
149
156
  }
150
157
 
151
158
  reset() {
@@ -3,10 +3,11 @@ import type {
3
3
  CartLine,
4
4
  } from '../lineItem/api';
5
5
 
6
- import type { Discount as ItemDiscount } from '../lineItem/api';
7
- import type { Discount as ShippingDiscount } from '~/shipping/api';
6
+ import type { Discount as DiscountItem } from '../lineItem/api';
7
+ import type { Discount as DiscountShipping } from '../shipping/api';
8
+
9
+ export type Discount = DiscountItem & DiscountShipping;
8
10
 
9
- export type Discount = ItemDiscount & ShippingDiscount;
10
11
 
11
12
  type ApplyLineItemDiscount = {
12
13
  lineItem: CartLine;
@@ -1,4 +1,4 @@
1
- import type { CartDeliveryOption } from '~/shipping/api';
1
+ import type { CartDeliveryOption } from '../shipping/api';
2
2
  import type { CartLine } from '../lineItem/api';
3
3
  import { StringComparisonType } from './Qualifier';
4
4
 
@@ -67,7 +67,7 @@ export class ShippingDiscount extends Campaign {
67
67
  const discount = this.discount.apply(
68
68
  applicableDiscounts
69
69
  );
70
- discountCart.addDiscount(discount);
70
+ discountCart.addShippingDiscount(discount);
71
71
  }
72
72
  }
73
73
  return discountCart;
@@ -33,7 +33,7 @@ describe('RateNameSelector', () => {
33
33
  const selector = new RateNameSelector(
34
34
  MatchType.DOES,
35
35
  StringComparisonType.CONTAINS,
36
- ['edex']
36
+ ['FedEx']
37
37
  );
38
38
  const result = selector.match(cartDeliveryOption);
39
39
  expect(result).toEqual(true);
@@ -102,6 +102,6 @@ describe('RateNameSelector', () => {
102
102
  const cartDeliveryOption = {
103
103
  deliveryMethodType: DeliveryMethod.Shipping,
104
104
  handle: 'test-handle',
105
- title: 'Fedex - 10% Shipping',
105
+ title: 'FedEx - 10% Shipping',
106
106
  } as CartDeliveryOption;
107
107
  });
@@ -0,0 +1,147 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import _ from 'lodash';
3
+ import {
4
+ DeliveryMethod,
5
+ Discount,
6
+ RunInput,
7
+ } from '../src/shipping/api';
8
+ import { ShippingDiscount } from '../src/shipping/ShippingDiscount';
9
+ import { MatchType } from '../src/common/Selector';
10
+ import {
11
+ NumericalComparisonType,
12
+ QualifierBehavior,
13
+ StringComparisonType,
14
+ } from '../src/common/Qualifier';
15
+ import {
16
+ CartAmountQualifier,
17
+ CartAmountBehavior,
18
+ } from '../src/common/CartAmountQualifier';
19
+ import { FixedDiscount } from '../src/shipping/FixedDiscount';
20
+ import { RateNameSelector } from '../src/shipping/RateNameSelector';
21
+ import { DiscountCart } from '../src/common/DiscountCart';
22
+
23
+ describe('ShippingDiscount', () => {
24
+ it('is true when it matches one of', () => {
25
+ const campaign = new ShippingDiscount(
26
+ QualifierBehavior.ALL,
27
+ [
28
+ new CartAmountQualifier(
29
+ CartAmountBehavior.CART,
30
+ NumericalComparisonType.GREATER_THAN_OR_EQUAL,
31
+ 50
32
+ ),
33
+ ],
34
+ new FixedDiscount(10, 'ZOXPASS Discount'),
35
+ new RateNameSelector(
36
+ MatchType.DOES,
37
+ StringComparisonType.CONTAINS,
38
+ ['FedEx']
39
+ )
40
+ );
41
+
42
+ // @ts-ignore
43
+ const discountCart = new DiscountCart(runInput.cart);
44
+ campaign.run(discountCart);
45
+
46
+ const expectedResult: Discount[] = [
47
+ {
48
+ message: 'ZOXPASS Discount',
49
+ targets: [
50
+ {
51
+ deliveryOption: {
52
+ handle: 'test-handle',
53
+ },
54
+ },
55
+ ],
56
+ value: {
57
+ fixedAmount: {
58
+ amount: 10,
59
+ },
60
+ },
61
+ },
62
+ ];
63
+
64
+ expect(discountCart.discounts).toEqual(expectedResult);
65
+ });
66
+
67
+ const runInput = {
68
+ cart: {
69
+ buyerIdentity: {
70
+ customer: {
71
+ email: 'sebi@zox.la',
72
+ isActiveSubscriber: false,
73
+ hasTags: [
74
+ {
75
+ tag: 'Active product subscription',
76
+ hasTag: true,
77
+ },
78
+ ],
79
+ },
80
+ },
81
+ cost: {
82
+ amountPerQuantity: {
83
+ amount: '8.0',
84
+ },
85
+ subtotalAmount: {
86
+ amount: '64.0',
87
+ },
88
+ totalAmount: {
89
+ amount: '64.0',
90
+ },
91
+ compareAtAmountPerQuantity: null,
92
+ },
93
+ lines: [
94
+ {
95
+ cost: {
96
+ amountPerQuantity: {
97
+ amount: '8.0',
98
+ },
99
+ subtotalAmount: {
100
+ amount: '64.0',
101
+ },
102
+ totalAmount: {
103
+ amount: '64.0',
104
+ },
105
+ compareAtAmountPerQuantity: null,
106
+ },
107
+ quantity: 8,
108
+ merchandise: {
109
+ __typename: 'ProductVariant',
110
+ id: 'gid://shopify/ProductVariant/12345',
111
+ product: {
112
+ id: 'gid://shopify/Product/67890',
113
+ productType: 'Wristband - Single',
114
+ hasTags: [
115
+ {
116
+ tag: 'meta-exclude-gift',
117
+ hasTag: false,
118
+ },
119
+ {
120
+ tag: 'mystery-3-pack',
121
+ hasTag: true,
122
+ },
123
+ ],
124
+ },
125
+ },
126
+ },
127
+ ],
128
+ deliveryGroups: [
129
+ {
130
+ deliveryOptions: [
131
+ {
132
+ deliveryMethodType: DeliveryMethod.Shipping,
133
+ handle: 'test-handle',
134
+ title: 'FedEx - 10% Shipping',
135
+ },
136
+ ],
137
+ },
138
+ ],
139
+ },
140
+ discountNode: {
141
+ metafield: {
142
+ value:
143
+ '{"campaigns":[{"__type":"ShippingDiscount","inputs":[":all",[{"__type":"CartAmountQualifier","inputs":[":cart",":greater_than_or_equal",50]}],{"__type":"FixedDiscount","inputs":[10,"ZOXPASS Discount"]},{"__type":"RateNameSelector","inputs":[":does",":contains",["FedEx"]]}]}]}',
144
+ },
145
+ },
146
+ } as unknown;
147
+ });