Skip to content

Commit

Permalink
fix(carts): set storeKey on create cart if set
Browse files Browse the repository at this point in the history
  • Loading branch information
davidweterings committed Oct 14, 2024
1 parent b98cb37 commit a0b1e5f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
53 changes: 25 additions & 28 deletions src/repositories/cart/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type {
CartDraft,
LineItem,
} from "@commercetools/platform-sdk";
import type { CartDraft, LineItem } from "@commercetools/platform-sdk";
import { describe, expect, test } from "vitest";
import { InMemoryStorage } from "~src/storage";
import { CartRepository } from "./index";
Expand All @@ -16,60 +13,60 @@ describe("Cart repository", () => {
lastModifiedAt: "",
productType: {
typeId: "product-type",
id: "fake-product-type-id"
id: "fake-product-type-id",
},
version: 1,
id: "15fc56ba-a74e-4cf8-b4b0-bada5c101541",
masterData: {
current: {
name: {"nl-NL": "Dummy"},
slug: {"nl-NL": "Dummy"},
name: { "nl-NL": "Dummy" },
slug: { "nl-NL": "Dummy" },
categories: [],
masterVariant: {
sku: "MYSKU",
id: 1,
prices: [
{
id: 'fake-price-id',
id: "fake-price-id",
value: {
currencyCode: "EUR",
centAmount: 1000,
type: "centPrecision",
fractionDigits: 2
fractionDigits: 2,
},
country: "NL"
}
]
country: "NL",
},
],
},
variants: [],
searchKeywords: {},
},
published: false,
staged: {
name: {"nl-NL": "Dummy"},
slug: {"nl-NL": "Dummy"},
name: { "nl-NL": "Dummy" },
slug: { "nl-NL": "Dummy" },
categories: [],
masterVariant: {
sku: "MYSKU",
id: 1,
prices: [
{
id: 'fake-price-id',
id: "fake-price-id",
value: {
currencyCode: "EUR",
centAmount: 1000,
type: "centPrecision",
fractionDigits: 2
fractionDigits: 2,
},
country: "NL"
}
]
country: "NL",
},
],
},
variants: [],
searchKeywords: {},
},
hasStagedChanges: false
}
hasStagedChanges: false,
},
});

const cart: CartDraft = {
Expand Down Expand Up @@ -98,17 +95,17 @@ describe("Cart repository", () => {
variant: {
prices: [
{
id: 'fake-price-id',
id: "fake-price-id",
value: {
currencyCode: "EUR",
centAmount: 1000,
type: "centPrecision",
fractionDigits: 2
fractionDigits: 2,
},
country: "NL"
}
]
}
country: "NL",
},
],
},
} as unknown as LineItem,
],
origin: "Customer",
Expand All @@ -125,7 +122,7 @@ describe("Cart repository", () => {
shippingMode: "Single",
taxMode: "Platform",
taxRoundingMode: "HalfEven",
taxCalculationMode: "UnitPriceLevel"
taxCalculationMode: "UnitPriceLevel",
};

const ctx = { projectKey: "dummy", storeKey: "dummyStore" };
Expand Down
24 changes: 13 additions & 11 deletions src/repositories/cart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
const resource: Writable<Cart> = {
...getBaseResourceProperties(),
anonymousId: draft.anonymousId,
billingAddress: draft.billingAddress ? createAddress(
draft.billingAddress,
context.projectKey,
this._storage,
) : undefined,
billingAddress: draft.billingAddress
? createAddress(draft.billingAddress, context.projectKey, this._storage)
: undefined,
cartState: "Active",
country: draft.country,
customerEmail: draft.customerEmail,
Expand All @@ -65,11 +63,13 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
fractionDigits: 0,
},
shippingMode: "Single",
shippingAddress: draft.shippingAddress ? createAddress(
draft.shippingAddress,
context.projectKey,
this._storage,
) : undefined,
shippingAddress: draft.shippingAddress
? createAddress(
draft.shippingAddress,
context.projectKey,
this._storage,
)
: undefined,
shipping: [],
origin: draft.origin ?? "Customer",
refusedGifts: [],
Expand All @@ -80,7 +80,9 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
),
};
resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
resource.store = context.storeKey ? { typeId: 'store', key: context.storeKey } : undefined;
resource.store = context.storeKey
? { typeId: "store", key: context.storeKey }
: undefined;

return this.saveNew(context, resource);
}
Expand Down

0 comments on commit a0b1e5f

Please sign in to comment.