-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathcart-functions.ts
More file actions
279 lines (254 loc) · 7.11 KB
/
cart-functions.ts
File metadata and controls
279 lines (254 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* External dependencies
*/
import debugFactory from 'debug';
/**
* Internal dependencies
*/
import { getEmptyResponseCart } from './empty-carts';
import type { TempResponseCart } from './shopping-cart-endpoint';
import type {
CartLocation,
RequestCart,
RequestCartProduct,
ResponseCart,
ResponseCartProduct,
} from './types';
const debug = debugFactory( 'shopping-cart:cart-functions' );
let lastUUID = 100;
const emptyResponseCart = getEmptyResponseCart();
function convertResponseCartProductToRequestCartProduct(
product: ResponseCartProduct | RequestCartProduct
): RequestCartProduct {
const { product_slug, meta, product_id, extra, volume, quantity } = product;
return {
product_slug,
meta,
volume,
quantity,
product_id,
extra,
};
}
export function convertResponseCartToRequestCart( {
products,
currency,
locale,
coupon,
is_coupon_applied,
tax,
}: TempResponseCart ): RequestCart {
let requestCartTax = null;
if ( tax.location.country_code || tax.location.postal_code || tax.location.subdivision_code ) {
requestCartTax = {
location: {
country_code: tax.location.country_code,
postal_code: tax.location.postal_code,
subdivision_code: tax.location.subdivision_code,
},
};
}
return {
products: products.map( convertResponseCartProductToRequestCartProduct ),
currency,
locale,
coupon,
is_coupon_applied,
temporary: false,
tax: requestCartTax,
extra: '', // This property doesn't appear to be used for anything
};
}
export function convertTempResponseCartToResponseCart( cart: TempResponseCart ): ResponseCart {
return {
...cart,
products: cart.products.filter( isValidResponseCartProduct ),
};
}
function isValidResponseCartProduct(
product: RequestCartProduct | ResponseCartProduct
): product is ResponseCartProduct {
return 'uuid' in product;
}
export function removeItemFromResponseCart(
cart: TempResponseCart,
uuidToRemove: string
): TempResponseCart {
return {
...cart,
products: cart.products.filter( ( product ) => {
return isValidResponseCartProduct( product ) ? product.uuid !== uuidToRemove : true;
} ),
};
}
export function addCouponToResponseCart(
cart: TempResponseCart,
couponToAdd: string
): TempResponseCart {
return {
...cart,
coupon: couponToAdd,
is_coupon_applied: false,
};
}
export function removeCouponFromResponseCart( cart: TempResponseCart ): TempResponseCart {
return {
...cart,
coupon: '',
is_coupon_applied: false,
};
}
export function addLocationToResponseCart(
cart: TempResponseCart,
location: CartLocation
): TempResponseCart {
return {
...cart,
tax: {
...cart.tax,
location: {
country_code: location.countryCode || undefined,
postal_code: location.postalCode || undefined,
subdivision_code: location.subdivisionCode || undefined,
},
},
};
}
export function doesCartLocationDifferFromResponseCartLocation(
cart: TempResponseCart,
location: CartLocation
): boolean {
const { countryCode, postalCode, subdivisionCode } = location;
const isMissing = ( value: null | undefined | string ) => value === null || value === undefined;
if ( ! isMissing( countryCode ) && cart.tax.location.country_code !== countryCode ) {
return true;
}
if ( ! isMissing( postalCode ) && cart.tax.location.postal_code !== postalCode ) {
return true;
}
if ( ! isMissing( subdivisionCode ) && cart.tax.location.subdivision_code !== subdivisionCode ) {
return true;
}
return false;
}
export function convertRawResponseCartToResponseCart(
rawResponseCart: Partial< ResponseCart >
): ResponseCart {
if ( typeof rawResponseCart !== 'object' || rawResponseCart === null ) {
return emptyResponseCart;
}
// If tax.location is an empty PHP associative array, it will be JSON serialized to [] but we need {}
let taxLocation = {};
if ( rawResponseCart.tax?.location ) {
if ( Array.isArray( rawResponseCart.tax.location ) ) {
taxLocation = {};
} else {
taxLocation = rawResponseCart.tax.location;
}
}
const rawProducts =
rawResponseCart.products?.length && Array.isArray( rawResponseCart.products )
? rawResponseCart.products
: [];
return {
...emptyResponseCart,
...rawResponseCart,
tax: {
location: taxLocation,
display_taxes: rawResponseCart.tax?.display_taxes ?? false,
},
// Add uuid to products returned by the server
products: rawProducts.filter( isRealProduct ).map( ( product ) => {
return {
...product,
uuid: product.product_slug + lastUUID++,
};
} ),
};
}
function isRealProduct( serverCartItem: ResponseCartProduct ): boolean {
// Credits are reported separately, so we do not need to include the pseudo-product in the line items.
if ( serverCartItem.product_slug === 'wordpress-com-credits' ) {
return false;
}
return true;
}
function shouldProductReplaceCart(
product: RequestCartProduct,
responseCart: TempResponseCart
): boolean {
const doesCartHaveRenewals = responseCart.products.some(
( cartProduct ) => cartProduct.extra?.purchaseType === 'renewal'
);
if (
! doesCartHaveRenewals &&
product.extra?.purchaseType === 'renewal' &&
product.product_slug !== 'domain_redemption'
) {
// adding a renewal replaces the cart unless it is a privacy protection (comment copied from original code from old checkout; is domain_redemption really privacy protection?)
return true;
}
if ( doesCartHaveRenewals && product.extra?.purchaseType !== 'renewal' ) {
// all items should replace the cart if the cart contains a renewal
return true;
}
return false;
}
function shouldProductsReplaceCart(
products: RequestCartProduct[],
responseCart: TempResponseCart
): boolean {
return products.some( ( product ) => shouldProductReplaceCart( product, responseCart ) );
}
export function addItemsToResponseCart(
responseCart: TempResponseCart,
products: RequestCartProduct[]
): TempResponseCart {
if ( shouldProductsReplaceCart( products, responseCart ) ) {
debug( 'items should replace response cart', products );
return replaceAllItemsInResponseCart( responseCart, products );
}
debug( 'items should not replace response cart', products );
return {
...responseCart,
products: [ ...responseCart.products, ...products ],
};
}
export function replaceAllItemsInResponseCart(
responseCart: TempResponseCart,
products: RequestCartProduct[]
): TempResponseCart {
return {
...responseCart,
products: [ ...products ],
};
}
export function replaceItemInResponseCart(
cart: TempResponseCart,
uuidToReplace: string,
productPropertiesToChange: Partial< RequestCartProduct >
): TempResponseCart {
return {
...cart,
products: cart.products.map( ( item ) => {
if ( isValidResponseCartProduct( item ) && item.uuid === uuidToReplace ) {
return { ...item, ...productPropertiesToChange };
}
return item;
} ),
};
}
export function doesResponseCartContainProductMatching(
responseCart: TempResponseCart,
productProperties: Partial< ResponseCartProduct >
): boolean {
return responseCart.products.some( ( product ) => {
return (
isValidResponseCartProduct( product ) &&
Object.keys( productProperties ).every( ( key ) => {
const typedKey = key as keyof ResponseCartProduct;
return product[ typedKey ] === productProperties[ typedKey ];
} )
);
} );
}