@labdigital/commercetools-mock 2.57.1 → 2.58.0
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/dist/index.d.ts +26 -23
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +149 -132
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/repositories/cart/actions.ts +9 -168
- package/src/repositories/cart/index.test.ts +376 -2
- package/src/repositories/cart/index.ts +203 -3
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BusinessUnit,
|
|
3
|
+
CentPrecisionMoney,
|
|
3
4
|
InvalidOperationError,
|
|
5
|
+
MissingTaxRateForCountryError,
|
|
6
|
+
ShippingMethodDoesNotMatchCartError,
|
|
4
7
|
} from "@commercetools/platform-sdk";
|
|
5
8
|
import type {
|
|
6
9
|
Cart,
|
|
@@ -12,17 +15,27 @@ import type {
|
|
|
12
15
|
LineItemDraft,
|
|
13
16
|
Product,
|
|
14
17
|
ProductPagedQueryResponse,
|
|
18
|
+
TaxPortion,
|
|
19
|
+
TaxedItemPrice,
|
|
15
20
|
} from "@commercetools/platform-sdk";
|
|
21
|
+
import { Decimal } from "decimal.js/decimal";
|
|
16
22
|
import { v4 as uuidv4 } from "uuid";
|
|
17
23
|
import type { Config } from "~src/config";
|
|
18
24
|
import { CommercetoolsError } from "~src/exceptions";
|
|
19
25
|
import { getBaseResourceProperties } from "~src/helpers";
|
|
26
|
+
import { getShippingMethodsMatchingCart } from "~src/shipping";
|
|
20
27
|
import type { Writable } from "~src/types";
|
|
21
28
|
import {
|
|
22
29
|
AbstractResourceRepository,
|
|
23
30
|
type RepositoryContext,
|
|
24
31
|
} from "../abstract";
|
|
25
|
-
import {
|
|
32
|
+
import {
|
|
33
|
+
createAddress,
|
|
34
|
+
createCentPrecisionMoney,
|
|
35
|
+
createCustomFields,
|
|
36
|
+
createTypedMoney,
|
|
37
|
+
roundDecimal,
|
|
38
|
+
} from "../helpers";
|
|
26
39
|
import { CartUpdateHandler } from "./actions";
|
|
27
40
|
import {
|
|
28
41
|
calculateCartTotalPrice,
|
|
@@ -33,7 +46,7 @@ import {
|
|
|
33
46
|
export class CartRepository extends AbstractResourceRepository<"cart"> {
|
|
34
47
|
constructor(config: Config) {
|
|
35
48
|
super("cart", config);
|
|
36
|
-
this.actions = new CartUpdateHandler(this._storage);
|
|
49
|
+
this.actions = new CartUpdateHandler(this._storage, this);
|
|
37
50
|
}
|
|
38
51
|
|
|
39
52
|
create(context: RepositoryContext, draft: CartDraft): Cart {
|
|
@@ -128,6 +141,7 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
|
|
|
128
141
|
)
|
|
129
142
|
: undefined,
|
|
130
143
|
shipping: [],
|
|
144
|
+
shippingInfo: undefined,
|
|
131
145
|
origin: draft.origin ?? "Customer",
|
|
132
146
|
refusedGifts: [],
|
|
133
147
|
custom: createCustomFields(
|
|
@@ -139,7 +153,18 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
|
|
|
139
153
|
resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
|
|
140
154
|
resource.store = context.storeKey
|
|
141
155
|
? { typeId: "store", key: context.storeKey }
|
|
142
|
-
:
|
|
156
|
+
: draft.store?.key
|
|
157
|
+
? { typeId: "store", key: draft.store.key }
|
|
158
|
+
: undefined;
|
|
159
|
+
|
|
160
|
+
// Set shipping info after resource is created
|
|
161
|
+
if (draft.shippingMethod) {
|
|
162
|
+
resource.shippingInfo = this.createShippingInfo(
|
|
163
|
+
context,
|
|
164
|
+
resource,
|
|
165
|
+
draft.shippingMethod,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
143
168
|
|
|
144
169
|
return this.saveNew(context, resource);
|
|
145
170
|
}
|
|
@@ -249,4 +274,179 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
|
|
|
249
274
|
),
|
|
250
275
|
};
|
|
251
276
|
};
|
|
277
|
+
|
|
278
|
+
createShippingInfo(
|
|
279
|
+
context: RepositoryContext,
|
|
280
|
+
resource: Writable<Cart>,
|
|
281
|
+
shippingMethodRef: NonNullable<CartDraft["shippingMethod"]>,
|
|
282
|
+
): NonNullable<Cart["shippingInfo"]> {
|
|
283
|
+
if (resource.taxMode === "External") {
|
|
284
|
+
throw new Error("External tax rate is not supported");
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const country = resource.shippingAddress?.country;
|
|
288
|
+
|
|
289
|
+
if (!country) {
|
|
290
|
+
throw new CommercetoolsError<InvalidOperationError>({
|
|
291
|
+
code: "InvalidOperation",
|
|
292
|
+
message: `The cart with ID '${resource.id}' does not have a shipping address set.`,
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Bit of a hack: calling this checks that the resource identifier is
|
|
297
|
+
// valid (i.e. id xor key) and that the shipping method exists.
|
|
298
|
+
this._storage.getByResourceIdentifier<"shipping-method">(
|
|
299
|
+
context.projectKey,
|
|
300
|
+
shippingMethodRef,
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
// getShippingMethodsMatchingCart does the work of determining whether the
|
|
304
|
+
// shipping method is allowed for the cart, and which shipping rate to use
|
|
305
|
+
const shippingMethods = getShippingMethodsMatchingCart(
|
|
306
|
+
context,
|
|
307
|
+
this._storage,
|
|
308
|
+
resource,
|
|
309
|
+
{
|
|
310
|
+
expand: ["zoneRates[*].zone"],
|
|
311
|
+
},
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
const method = shippingMethods.results.find((candidate) =>
|
|
315
|
+
shippingMethodRef.id
|
|
316
|
+
? candidate.id === shippingMethodRef.id
|
|
317
|
+
: candidate.key === shippingMethodRef.key,
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
// Not finding the method in the results means it's not allowed, since
|
|
321
|
+
// getShippingMethodsMatchingCart only returns allowed methods and we
|
|
322
|
+
// already checked that the method exists.
|
|
323
|
+
if (!method) {
|
|
324
|
+
throw new CommercetoolsError<ShippingMethodDoesNotMatchCartError>({
|
|
325
|
+
code: "ShippingMethodDoesNotMatchCart",
|
|
326
|
+
message: `The shipping method with ${shippingMethodRef.id ? `ID '${shippingMethodRef.id}'` : `key '${shippingMethodRef.key}'`} is not allowed for the cart with ID '${resource.id}'.`,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const taxCategory = this._storage.getByResourceIdentifier<"tax-category">(
|
|
331
|
+
context.projectKey,
|
|
332
|
+
method.taxCategory,
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
// TODO: match state in addition to country
|
|
336
|
+
const taxRate = taxCategory.rates.find((rate) => rate.country === country);
|
|
337
|
+
|
|
338
|
+
if (!taxRate) {
|
|
339
|
+
throw new CommercetoolsError<MissingTaxRateForCountryError>({
|
|
340
|
+
code: "MissingTaxRateForCountry",
|
|
341
|
+
message: `Tax category '${taxCategory.id}' is missing a tax rate for country '${country}'.`,
|
|
342
|
+
taxCategoryId: taxCategory.id,
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// There should only be one zone rate matching the address, since
|
|
347
|
+
// Locations cannot be assigned to more than one zone.
|
|
348
|
+
// See https://docs.commercetools.com/api/projects/zones#location
|
|
349
|
+
const zoneRate = method.zoneRates.find((rate) =>
|
|
350
|
+
rate.zone.obj?.locations.some((loc) => loc.country === country),
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
if (!zoneRate) {
|
|
354
|
+
// This shouldn't happen because getShippingMethodsMatchingCart already
|
|
355
|
+
// filtered out shipping methods without any zones matching the address
|
|
356
|
+
throw new Error("Zone rate not found");
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Shipping rates are defined by currency, and getShippingMethodsMatchingCart
|
|
360
|
+
// also matches on currency, so there should only be one in the array.
|
|
361
|
+
// See https://docs.commercetools.com/api/projects/shippingMethods#zonerate
|
|
362
|
+
const shippingRate = zoneRate.shippingRates[0];
|
|
363
|
+
if (!shippingRate) {
|
|
364
|
+
// This shouldn't happen because getShippingMethodsMatchingCart already
|
|
365
|
+
// filtered out shipping methods without any matching rates
|
|
366
|
+
throw new Error("Shipping rate not found");
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const shippingRateTier = shippingRate.tiers.find((tier) => tier.isMatching);
|
|
370
|
+
if (shippingRateTier && shippingRateTier.type !== "CartValue") {
|
|
371
|
+
throw new Error("Non-CartValue shipping rate tier is not supported");
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
let shippingPrice = shippingRateTier
|
|
375
|
+
? createCentPrecisionMoney(shippingRateTier.price)
|
|
376
|
+
: shippingRate.price;
|
|
377
|
+
|
|
378
|
+
// Handle freeAbove: if cart total is above the freeAbove threshold, shipping is free
|
|
379
|
+
if (
|
|
380
|
+
shippingRate.freeAbove &&
|
|
381
|
+
shippingRate.freeAbove.currencyCode ===
|
|
382
|
+
resource.totalPrice.currencyCode &&
|
|
383
|
+
resource.totalPrice.centAmount >= shippingRate.freeAbove.centAmount
|
|
384
|
+
) {
|
|
385
|
+
shippingPrice = {
|
|
386
|
+
...shippingPrice,
|
|
387
|
+
centAmount: 0,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// Calculate tax amounts
|
|
392
|
+
const totalGross: CentPrecisionMoney = taxRate.includedInPrice
|
|
393
|
+
? shippingPrice
|
|
394
|
+
: {
|
|
395
|
+
...shippingPrice,
|
|
396
|
+
centAmount: roundDecimal(
|
|
397
|
+
new Decimal(shippingPrice.centAmount).mul(1 + taxRate.amount),
|
|
398
|
+
resource.taxRoundingMode,
|
|
399
|
+
).toNumber(),
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const totalNet: CentPrecisionMoney = taxRate.includedInPrice
|
|
403
|
+
? {
|
|
404
|
+
...shippingPrice,
|
|
405
|
+
centAmount: roundDecimal(
|
|
406
|
+
new Decimal(shippingPrice.centAmount).div(1 + taxRate.amount),
|
|
407
|
+
resource.taxRoundingMode,
|
|
408
|
+
).toNumber(),
|
|
409
|
+
}
|
|
410
|
+
: shippingPrice;
|
|
411
|
+
|
|
412
|
+
const taxPortions: TaxPortion[] = [
|
|
413
|
+
{
|
|
414
|
+
name: taxRate.name,
|
|
415
|
+
rate: taxRate.amount,
|
|
416
|
+
amount: {
|
|
417
|
+
...shippingPrice,
|
|
418
|
+
centAmount: totalGross.centAmount - totalNet.centAmount,
|
|
419
|
+
},
|
|
420
|
+
},
|
|
421
|
+
];
|
|
422
|
+
|
|
423
|
+
const totalTax: CentPrecisionMoney = {
|
|
424
|
+
...shippingPrice,
|
|
425
|
+
centAmount: taxPortions.reduce(
|
|
426
|
+
(acc, portion) => acc + portion.amount.centAmount,
|
|
427
|
+
0,
|
|
428
|
+
),
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const taxedPrice: TaxedItemPrice = {
|
|
432
|
+
totalNet,
|
|
433
|
+
totalGross,
|
|
434
|
+
taxPortions,
|
|
435
|
+
totalTax,
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
return {
|
|
439
|
+
shippingMethod: {
|
|
440
|
+
typeId: "shipping-method" as const,
|
|
441
|
+
id: method.id,
|
|
442
|
+
},
|
|
443
|
+
shippingMethodName: method.name,
|
|
444
|
+
price: shippingPrice,
|
|
445
|
+
shippingRate,
|
|
446
|
+
taxedPrice,
|
|
447
|
+
taxRate,
|
|
448
|
+
taxCategory: method.taxCategory,
|
|
449
|
+
shippingMethodState: "MatchesCart",
|
|
450
|
+
};
|
|
451
|
+
}
|
|
252
452
|
}
|