@labdigital/commercetools-mock 2.59.0 → 2.59.1
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 +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +176 -130
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/repositories/cart/index.ts +11 -137
- package/src/repositories/order/index.test.ts +307 -0
- package/src/repositories/order/index.ts +97 -2
- package/src/shipping.ts +157 -0
package/package.json
CHANGED
|
@@ -23,19 +23,16 @@ import { v4 as uuidv4 } from "uuid";
|
|
|
23
23
|
import type { Config } from "~src/config";
|
|
24
24
|
import { CommercetoolsError } from "~src/exceptions";
|
|
25
25
|
import { getBaseResourceProperties } from "~src/helpers";
|
|
26
|
-
import {
|
|
26
|
+
import {
|
|
27
|
+
createShippingInfoFromMethod,
|
|
28
|
+
getShippingMethodsMatchingCart,
|
|
29
|
+
} from "~src/shipping";
|
|
27
30
|
import type { Writable } from "~src/types";
|
|
28
31
|
import {
|
|
29
32
|
AbstractResourceRepository,
|
|
30
33
|
type RepositoryContext,
|
|
31
34
|
} from "../abstract";
|
|
32
|
-
import {
|
|
33
|
-
createAddress,
|
|
34
|
-
createCentPrecisionMoney,
|
|
35
|
-
createCustomFields,
|
|
36
|
-
createTypedMoney,
|
|
37
|
-
roundDecimal,
|
|
38
|
-
} from "../helpers";
|
|
35
|
+
import { createAddress, createCustomFields } from "../helpers";
|
|
39
36
|
import { CartUpdateHandler } from "./actions";
|
|
40
37
|
import {
|
|
41
38
|
calculateCartTotalPrice,
|
|
@@ -284,15 +281,6 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
|
|
|
284
281
|
throw new Error("External tax rate is not supported");
|
|
285
282
|
}
|
|
286
283
|
|
|
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
284
|
// Bit of a hack: calling this checks that the resource identifier is
|
|
297
285
|
// valid (i.e. id xor key) and that the shipping method exists.
|
|
298
286
|
this._storage.getByResourceIdentifier<"shipping-method">(
|
|
@@ -327,126 +315,12 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
|
|
|
327
315
|
});
|
|
328
316
|
}
|
|
329
317
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
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),
|
|
318
|
+
// Use the shared shipping info creation logic
|
|
319
|
+
return createShippingInfoFromMethod(
|
|
320
|
+
context,
|
|
321
|
+
this._storage,
|
|
322
|
+
resource,
|
|
323
|
+
method,
|
|
351
324
|
);
|
|
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
325
|
}
|
|
452
326
|
}
|
|
@@ -2,6 +2,7 @@ import { beforeEach } from "node:test";
|
|
|
2
2
|
import type {
|
|
3
3
|
Cart,
|
|
4
4
|
LineItem,
|
|
5
|
+
Order,
|
|
5
6
|
OrderImportDraft,
|
|
6
7
|
} from "@commercetools/platform-sdk";
|
|
7
8
|
import { describe, expect, test } from "vitest";
|
|
@@ -187,6 +188,8 @@ describe("Order repository", () => {
|
|
|
187
188
|
expect(result.taxRoundingMode).toEqual(cart.taxRoundingMode);
|
|
188
189
|
expect(result.totalPrice).toEqual(cart.totalPrice);
|
|
189
190
|
expect(result.store).toEqual(cart.store);
|
|
191
|
+
// Test that shippingInfo is copied from cart to order
|
|
192
|
+
expect(result.shippingInfo).toEqual(cart.shippingInfo);
|
|
190
193
|
});
|
|
191
194
|
|
|
192
195
|
test("create order in store", async () => {
|
|
@@ -510,4 +513,308 @@ describe("Order repository", () => {
|
|
|
510
513
|
repository.import('dummy', draft)
|
|
511
514
|
})
|
|
512
515
|
*/
|
|
516
|
+
|
|
517
|
+
describe("shippingInfo functionality", () => {
|
|
518
|
+
test("createShippingInfo creates basic shipping info", () => {
|
|
519
|
+
// Create a zone for Netherlands
|
|
520
|
+
const zone = {
|
|
521
|
+
...getBaseResourceProperties(),
|
|
522
|
+
id: "zone-nl",
|
|
523
|
+
name: "Netherlands Zone",
|
|
524
|
+
locations: [
|
|
525
|
+
{
|
|
526
|
+
country: "NL",
|
|
527
|
+
},
|
|
528
|
+
],
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
// Create a shipping method first
|
|
532
|
+
const shippingMethod = {
|
|
533
|
+
...getBaseResourceProperties(),
|
|
534
|
+
id: "shipping-method-123",
|
|
535
|
+
name: "Express Shipping",
|
|
536
|
+
active: true,
|
|
537
|
+
isDefault: false,
|
|
538
|
+
taxCategory: {
|
|
539
|
+
typeId: "tax-category" as const,
|
|
540
|
+
id: "tax-category-123",
|
|
541
|
+
},
|
|
542
|
+
zoneRates: [
|
|
543
|
+
{
|
|
544
|
+
zone: {
|
|
545
|
+
typeId: "zone" as const,
|
|
546
|
+
id: "zone-nl",
|
|
547
|
+
obj: zone,
|
|
548
|
+
},
|
|
549
|
+
shippingRates: [
|
|
550
|
+
{
|
|
551
|
+
price: {
|
|
552
|
+
type: "centPrecision" as const,
|
|
553
|
+
currencyCode: "EUR",
|
|
554
|
+
centAmount: 500,
|
|
555
|
+
fractionDigits: 2,
|
|
556
|
+
},
|
|
557
|
+
tiers: [],
|
|
558
|
+
},
|
|
559
|
+
],
|
|
560
|
+
},
|
|
561
|
+
],
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
const taxCategory = {
|
|
565
|
+
...getBaseResourceProperties(),
|
|
566
|
+
id: "tax-category-123",
|
|
567
|
+
name: "Standard Tax",
|
|
568
|
+
rates: [
|
|
569
|
+
{
|
|
570
|
+
name: "Standard VAT",
|
|
571
|
+
amount: 0.21,
|
|
572
|
+
country: "NL",
|
|
573
|
+
includedInPrice: true,
|
|
574
|
+
},
|
|
575
|
+
],
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
storage.add("dummy", "zone", zone);
|
|
579
|
+
storage.add("dummy", "shipping-method", shippingMethod);
|
|
580
|
+
storage.add("dummy", "tax-category", taxCategory);
|
|
581
|
+
|
|
582
|
+
const order: Order = {
|
|
583
|
+
...getBaseResourceProperties(),
|
|
584
|
+
orderNumber: "order-123",
|
|
585
|
+
orderState: "Open",
|
|
586
|
+
origin: "Customer",
|
|
587
|
+
customLineItems: [],
|
|
588
|
+
lineItems: [],
|
|
589
|
+
totalPrice: {
|
|
590
|
+
type: "centPrecision",
|
|
591
|
+
currencyCode: "EUR",
|
|
592
|
+
centAmount: 1000,
|
|
593
|
+
fractionDigits: 2,
|
|
594
|
+
},
|
|
595
|
+
lastMessageSequenceNumber: 0,
|
|
596
|
+
refusedGifts: [],
|
|
597
|
+
shipping: [],
|
|
598
|
+
shippingMode: "Single",
|
|
599
|
+
shippingAddress: {
|
|
600
|
+
id: "address-123",
|
|
601
|
+
country: "NL",
|
|
602
|
+
firstName: "John",
|
|
603
|
+
lastName: "Doe",
|
|
604
|
+
streetName: "Main Street",
|
|
605
|
+
streetNumber: "123",
|
|
606
|
+
postalCode: "1234AB",
|
|
607
|
+
city: "Amsterdam",
|
|
608
|
+
},
|
|
609
|
+
syncInfo: [],
|
|
610
|
+
taxCalculationMode: "UnitPriceLevel",
|
|
611
|
+
taxMode: "Platform",
|
|
612
|
+
taxRoundingMode: "HalfEven",
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
const ctx = { projectKey: "dummy" };
|
|
616
|
+
const result = repository.createShippingInfo(ctx, order, {
|
|
617
|
+
typeId: "shipping-method",
|
|
618
|
+
id: "shipping-method-123",
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
expect(result).toBeDefined();
|
|
622
|
+
expect(result.shippingMethod?.id).toBe("shipping-method-123");
|
|
623
|
+
expect(result.shippingMethodName).toBe("Express Shipping");
|
|
624
|
+
expect(result.price.currencyCode).toBe("EUR");
|
|
625
|
+
expect(result.price.centAmount).toBe(500);
|
|
626
|
+
expect(result.shippingMethodState).toBe("MatchesCart");
|
|
627
|
+
expect(result.deliveries).toEqual([]);
|
|
628
|
+
expect(result.taxCategory?.id).toBe("tax-category-123");
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
test("import order with shippingInfo", () => {
|
|
632
|
+
// Create required resources
|
|
633
|
+
const zone = {
|
|
634
|
+
...getBaseResourceProperties(),
|
|
635
|
+
id: "zone-de",
|
|
636
|
+
name: "Germany Zone",
|
|
637
|
+
locations: [
|
|
638
|
+
{
|
|
639
|
+
country: "DE",
|
|
640
|
+
},
|
|
641
|
+
],
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
const shippingMethod = {
|
|
645
|
+
...getBaseResourceProperties(),
|
|
646
|
+
id: "shipping-method-456",
|
|
647
|
+
name: "Standard Shipping",
|
|
648
|
+
active: true,
|
|
649
|
+
isDefault: false,
|
|
650
|
+
taxCategory: {
|
|
651
|
+
typeId: "tax-category" as const,
|
|
652
|
+
id: "tax-category-456",
|
|
653
|
+
},
|
|
654
|
+
zoneRates: [
|
|
655
|
+
{
|
|
656
|
+
zone: {
|
|
657
|
+
typeId: "zone" as const,
|
|
658
|
+
id: "zone-de",
|
|
659
|
+
obj: zone,
|
|
660
|
+
},
|
|
661
|
+
shippingRates: [
|
|
662
|
+
{
|
|
663
|
+
price: {
|
|
664
|
+
type: "centPrecision" as const,
|
|
665
|
+
currencyCode: "EUR",
|
|
666
|
+
centAmount: 500,
|
|
667
|
+
fractionDigits: 2,
|
|
668
|
+
},
|
|
669
|
+
tiers: [],
|
|
670
|
+
},
|
|
671
|
+
],
|
|
672
|
+
},
|
|
673
|
+
],
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
const taxCategory = {
|
|
677
|
+
...getBaseResourceProperties(),
|
|
678
|
+
id: "tax-category-456",
|
|
679
|
+
name: "Standard Tax",
|
|
680
|
+
rates: [
|
|
681
|
+
{
|
|
682
|
+
name: "Standard VAT",
|
|
683
|
+
amount: 0.19,
|
|
684
|
+
country: "DE",
|
|
685
|
+
includedInPrice: true,
|
|
686
|
+
},
|
|
687
|
+
],
|
|
688
|
+
};
|
|
689
|
+
|
|
690
|
+
storage.add("dummy", "zone", zone);
|
|
691
|
+
storage.add("dummy", "shipping-method", shippingMethod);
|
|
692
|
+
storage.add("dummy", "tax-category", taxCategory);
|
|
693
|
+
|
|
694
|
+
const draft: OrderImportDraft = {
|
|
695
|
+
orderNumber: "imported-order-123",
|
|
696
|
+
totalPrice: {
|
|
697
|
+
currencyCode: "EUR",
|
|
698
|
+
centAmount: 2000,
|
|
699
|
+
},
|
|
700
|
+
shippingAddress: {
|
|
701
|
+
country: "DE",
|
|
702
|
+
firstName: "Max",
|
|
703
|
+
lastName: "Mustermann",
|
|
704
|
+
streetName: "Hauptstraße",
|
|
705
|
+
streetNumber: "1",
|
|
706
|
+
postalCode: "10115",
|
|
707
|
+
city: "Berlin",
|
|
708
|
+
},
|
|
709
|
+
shippingInfo: {
|
|
710
|
+
shippingMethodName: "Standard Shipping",
|
|
711
|
+
price: {
|
|
712
|
+
currencyCode: "EUR",
|
|
713
|
+
centAmount: 500,
|
|
714
|
+
},
|
|
715
|
+
shippingRate: {
|
|
716
|
+
price: {
|
|
717
|
+
currencyCode: "EUR",
|
|
718
|
+
centAmount: 500,
|
|
719
|
+
},
|
|
720
|
+
tiers: [],
|
|
721
|
+
},
|
|
722
|
+
shippingMethod: {
|
|
723
|
+
typeId: "shipping-method",
|
|
724
|
+
id: "shipping-method-456",
|
|
725
|
+
},
|
|
726
|
+
taxCategory: {
|
|
727
|
+
typeId: "tax-category",
|
|
728
|
+
id: "tax-category-456",
|
|
729
|
+
},
|
|
730
|
+
taxRate: {
|
|
731
|
+
name: "Standard VAT",
|
|
732
|
+
amount: 0.19,
|
|
733
|
+
country: "DE",
|
|
734
|
+
includedInPrice: true,
|
|
735
|
+
},
|
|
736
|
+
shippingMethodState: "MatchesCart",
|
|
737
|
+
deliveries: [
|
|
738
|
+
{
|
|
739
|
+
key: "delivery-1",
|
|
740
|
+
items: [],
|
|
741
|
+
parcels: [
|
|
742
|
+
{
|
|
743
|
+
key: "parcel-1",
|
|
744
|
+
measurements: {
|
|
745
|
+
heightInMillimeter: 100,
|
|
746
|
+
lengthInMillimeter: 200,
|
|
747
|
+
widthInMillimeter: 150,
|
|
748
|
+
weightInGram: 500,
|
|
749
|
+
},
|
|
750
|
+
items: [],
|
|
751
|
+
},
|
|
752
|
+
],
|
|
753
|
+
},
|
|
754
|
+
],
|
|
755
|
+
},
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
const ctx = { projectKey: "dummy" };
|
|
759
|
+
const result = repository.import(ctx, draft);
|
|
760
|
+
|
|
761
|
+
expect(result.shippingInfo).toBeDefined();
|
|
762
|
+
expect(result.shippingInfo?.shippingMethodName).toBe("Standard Shipping");
|
|
763
|
+
expect(result.shippingInfo?.price.centAmount).toBe(500);
|
|
764
|
+
expect(result.shippingInfo?.shippingMethod?.id).toBe(
|
|
765
|
+
"shipping-method-456",
|
|
766
|
+
);
|
|
767
|
+
expect(result.shippingInfo?.taxCategory?.id).toBe("tax-category-456");
|
|
768
|
+
expect(result.shippingInfo?.taxRate?.amount).toBe(0.19);
|
|
769
|
+
// Note: deliveries from import drafts are not currently supported in native implementation
|
|
770
|
+
expect(result.shippingInfo?.deliveries).toEqual([]);
|
|
771
|
+
});
|
|
772
|
+
|
|
773
|
+
test("createShippingInfo throws error for non-existent shipping method", () => {
|
|
774
|
+
const order: Order = {
|
|
775
|
+
...getBaseResourceProperties(),
|
|
776
|
+
orderNumber: "order-456",
|
|
777
|
+
orderState: "Open",
|
|
778
|
+
origin: "Customer",
|
|
779
|
+
customLineItems: [],
|
|
780
|
+
lineItems: [],
|
|
781
|
+
totalPrice: {
|
|
782
|
+
type: "centPrecision",
|
|
783
|
+
currencyCode: "USD",
|
|
784
|
+
centAmount: 1500,
|
|
785
|
+
fractionDigits: 2,
|
|
786
|
+
},
|
|
787
|
+
lastMessageSequenceNumber: 0,
|
|
788
|
+
refusedGifts: [],
|
|
789
|
+
shipping: [],
|
|
790
|
+
shippingMode: "Single",
|
|
791
|
+
shippingAddress: {
|
|
792
|
+
id: "address-456",
|
|
793
|
+
country: "US",
|
|
794
|
+
firstName: "Jane",
|
|
795
|
+
lastName: "Smith",
|
|
796
|
+
streetName: "Broadway",
|
|
797
|
+
streetNumber: "456",
|
|
798
|
+
postalCode: "10001",
|
|
799
|
+
city: "New York",
|
|
800
|
+
state: "NY",
|
|
801
|
+
},
|
|
802
|
+
syncInfo: [],
|
|
803
|
+
taxCalculationMode: "UnitPriceLevel",
|
|
804
|
+
taxMode: "Platform",
|
|
805
|
+
taxRoundingMode: "HalfEven",
|
|
806
|
+
};
|
|
807
|
+
|
|
808
|
+
const ctx = { projectKey: "dummy" };
|
|
809
|
+
|
|
810
|
+
expect(() => {
|
|
811
|
+
repository.createShippingInfo(ctx, order, {
|
|
812
|
+
typeId: "shipping-method",
|
|
813
|
+
id: "non-existent-shipping-method",
|
|
814
|
+
});
|
|
815
|
+
}).toThrow(
|
|
816
|
+
/The shipping method with ID 'non-existent-shipping-method' is not allowed/,
|
|
817
|
+
);
|
|
818
|
+
});
|
|
819
|
+
});
|
|
513
820
|
});
|
|
@@ -2,21 +2,35 @@ import assert from "node:assert";
|
|
|
2
2
|
import type {
|
|
3
3
|
Cart,
|
|
4
4
|
CartReference,
|
|
5
|
+
CentPrecisionMoney,
|
|
5
6
|
CustomLineItem,
|
|
6
7
|
CustomLineItemImportDraft,
|
|
7
8
|
GeneralError,
|
|
9
|
+
InvalidOperationError,
|
|
8
10
|
LineItem,
|
|
9
11
|
LineItemImportDraft,
|
|
12
|
+
MissingTaxRateForCountryError,
|
|
10
13
|
Order,
|
|
11
14
|
OrderFromCartDraft,
|
|
12
15
|
OrderImportDraft,
|
|
13
16
|
Product,
|
|
14
17
|
ProductPagedQueryResponse,
|
|
15
18
|
ProductVariant,
|
|
19
|
+
ShippingInfo,
|
|
20
|
+
ShippingMethodDoesNotMatchCartError,
|
|
21
|
+
ShippingMethodReference,
|
|
22
|
+
TaxPortion,
|
|
23
|
+
TaxedItemPrice,
|
|
16
24
|
} from "@commercetools/platform-sdk";
|
|
25
|
+
import { Decimal } from "decimal.js/decimal";
|
|
17
26
|
import type { Config } from "~src/config";
|
|
18
27
|
import { CommercetoolsError } from "~src/exceptions";
|
|
19
28
|
import { generateRandomString, getBaseResourceProperties } from "~src/helpers";
|
|
29
|
+
import {
|
|
30
|
+
createShippingInfoFromMethod,
|
|
31
|
+
getShippingMethodsMatchingCart,
|
|
32
|
+
} from "~src/shipping";
|
|
33
|
+
import type { Writable } from "~src/types";
|
|
20
34
|
import type { RepositoryContext } from "../abstract";
|
|
21
35
|
import { AbstractResourceRepository, type QueryParams } from "../abstract";
|
|
22
36
|
import {
|
|
@@ -26,6 +40,7 @@ import {
|
|
|
26
40
|
createPrice,
|
|
27
41
|
createTypedMoney,
|
|
28
42
|
resolveStoreReference,
|
|
43
|
+
roundDecimal,
|
|
29
44
|
} from "../helpers";
|
|
30
45
|
import { OrderUpdateHandler } from "./actions";
|
|
31
46
|
|
|
@@ -84,6 +99,7 @@ export class OrderRepository extends AbstractResourceRepository<"order"> {
|
|
|
84
99
|
refusedGifts: [],
|
|
85
100
|
shipping: cart.shipping,
|
|
86
101
|
shippingAddress: cart.shippingAddress,
|
|
102
|
+
shippingInfo: cart.shippingInfo,
|
|
87
103
|
shippingMode: cart.shippingMode,
|
|
88
104
|
syncInfo: [],
|
|
89
105
|
taxCalculationMode: cart.taxCalculationMode,
|
|
@@ -100,7 +116,7 @@ export class OrderRepository extends AbstractResourceRepository<"order"> {
|
|
|
100
116
|
import(context: RepositoryContext, draft: OrderImportDraft): Order {
|
|
101
117
|
// TODO: Check if order with given orderNumber already exists
|
|
102
118
|
assert(this, "OrderRepository not valid");
|
|
103
|
-
const resource: Order = {
|
|
119
|
+
const resource: Writable<Order> = {
|
|
104
120
|
...getBaseResourceProperties(),
|
|
105
121
|
|
|
106
122
|
billingAddress: createAddress(
|
|
@@ -132,7 +148,7 @@ export class OrderRepository extends AbstractResourceRepository<"order"> {
|
|
|
132
148
|
refusedGifts: [],
|
|
133
149
|
shippingMode: "Single",
|
|
134
150
|
shipping: [],
|
|
135
|
-
|
|
151
|
+
shippingInfo: undefined,
|
|
136
152
|
store: resolveStoreReference(
|
|
137
153
|
draft.store,
|
|
138
154
|
context.projectKey,
|
|
@@ -151,6 +167,33 @@ export class OrderRepository extends AbstractResourceRepository<"order"> {
|
|
|
151
167
|
|
|
152
168
|
totalPrice: createCentPrecisionMoney(draft.totalPrice),
|
|
153
169
|
};
|
|
170
|
+
|
|
171
|
+
// Set shipping info after resource is created
|
|
172
|
+
if (draft.shippingInfo?.shippingMethod) {
|
|
173
|
+
const { ...shippingMethodRef } = draft.shippingInfo.shippingMethod;
|
|
174
|
+
|
|
175
|
+
// get id when reference is by key only
|
|
176
|
+
if (shippingMethodRef.key && !shippingMethodRef.id) {
|
|
177
|
+
const shippingMethod =
|
|
178
|
+
this._storage.getByResourceIdentifier<"shipping-method">(
|
|
179
|
+
context.projectKey,
|
|
180
|
+
shippingMethodRef,
|
|
181
|
+
);
|
|
182
|
+
if (!shippingMethod) {
|
|
183
|
+
throw new CommercetoolsError<GeneralError>({
|
|
184
|
+
code: "General",
|
|
185
|
+
message: `A shipping method with key '${shippingMethodRef.key}' does not exist.`,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
shippingMethodRef.id = shippingMethod.id;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
resource.shippingInfo = this.createShippingInfo(context, resource, {
|
|
192
|
+
typeId: "shipping-method",
|
|
193
|
+
id: shippingMethodRef.id as string,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
154
197
|
return this.saveNew(context, resource);
|
|
155
198
|
}
|
|
156
199
|
|
|
@@ -272,4 +315,56 @@ export class OrderRepository extends AbstractResourceRepository<"order"> {
|
|
|
272
315
|
|
|
273
316
|
return;
|
|
274
317
|
}
|
|
318
|
+
|
|
319
|
+
createShippingInfo(
|
|
320
|
+
context: RepositoryContext,
|
|
321
|
+
resource: Writable<Order>,
|
|
322
|
+
shippingMethodRef: ShippingMethodReference,
|
|
323
|
+
): ShippingInfo {
|
|
324
|
+
const cartLikeForMatching: Writable<Cart> = {
|
|
325
|
+
...resource,
|
|
326
|
+
cartState: "Active" as const,
|
|
327
|
+
inventoryMode: "None" as const,
|
|
328
|
+
itemShippingAddresses: [],
|
|
329
|
+
priceRoundingMode: resource.taxRoundingMode || "HalfEven",
|
|
330
|
+
taxMode: resource.taxMode || "Platform",
|
|
331
|
+
taxCalculationMode: resource.taxCalculationMode || "LineItemLevel",
|
|
332
|
+
taxRoundingMode: resource.taxRoundingMode || "HalfEven",
|
|
333
|
+
discountCodes: resource.discountCodes || [],
|
|
334
|
+
directDiscounts: resource.directDiscounts || [],
|
|
335
|
+
shippingInfo: undefined,
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const shippingMethods = getShippingMethodsMatchingCart(
|
|
339
|
+
context,
|
|
340
|
+
this._storage,
|
|
341
|
+
cartLikeForMatching,
|
|
342
|
+
{
|
|
343
|
+
expand: ["zoneRates[*].zone"],
|
|
344
|
+
},
|
|
345
|
+
);
|
|
346
|
+
|
|
347
|
+
const method = shippingMethods.results.find(
|
|
348
|
+
(candidate) => candidate.id === shippingMethodRef.id,
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
if (!method) {
|
|
352
|
+
throw new CommercetoolsError<ShippingMethodDoesNotMatchCartError>({
|
|
353
|
+
code: "ShippingMethodDoesNotMatchCart",
|
|
354
|
+
message: `The shipping method with ID '${shippingMethodRef.id}' is not allowed for the order with ID '${resource.id}'.`,
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const baseShippingInfo = createShippingInfoFromMethod(
|
|
359
|
+
context,
|
|
360
|
+
this._storage,
|
|
361
|
+
resource,
|
|
362
|
+
method,
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
return {
|
|
366
|
+
...baseShippingInfo,
|
|
367
|
+
deliveries: [],
|
|
368
|
+
};
|
|
369
|
+
}
|
|
275
370
|
}
|