Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plain-gifts-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": patch
---

Implement missing fields for priceFromDraft function: customerGroup, validFrom, validUntil, tiers, and custom fields
226 changes: 226 additions & 0 deletions src/repositories/product/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
import type {
CustomerGroupResourceIdentifier,
PriceDraft,
PriceTierDraft,
} from "@commercetools/platform-sdk";
import { describe, expect, test } from "vitest";
import { getBaseResourceProperties } from "~src/helpers";
import { InMemoryStorage } from "~src/storage";
import type { RepositoryContext } from "../abstract";
import { priceFromDraft } from "./helpers";

describe("priceFromDraft", () => {
const context: RepositoryContext = {
projectKey: "test-project",
};
const storage = new InMemoryStorage();

test("should handle basic price draft without optional fields", () => {
const draft: PriceDraft = {
value: {
currencyCode: "EUR",
centAmount: 1000,
},
country: "NL",
};

const result = priceFromDraft(context, storage, draft);

expect(result).toMatchObject({
id: expect.any(String),
country: "NL",
value: {
type: "centPrecision",
currencyCode: "EUR",
centAmount: 1000,
fractionDigits: 2,
},
});
expect(result.key).toBeUndefined();
expect(result.channel).toBeUndefined();
expect(result.customerGroup).toBeUndefined();
});

test("should handle customerGroup field when provided", () => {
// First create a customer group in storage
const customerGroup = {
...getBaseResourceProperties(),
id: "customer-group-id",
key: "customer-group-key",
name: "Test Customer Group",
groupName: "Test Group",
};
storage.add("test-project", "customer-group", customerGroup);

const customerGroupResourceIdentifier: CustomerGroupResourceIdentifier = {
typeId: "customer-group",
id: "customer-group-id",
};

const draft: PriceDraft = {
value: {
currencyCode: "EUR",
centAmount: 1000,
},
country: "NL",
customerGroup: customerGroupResourceIdentifier,
};

const result = priceFromDraft(context, storage, draft);

expect(result).toMatchObject({
id: expect.any(String),
country: "NL",
value: {
type: "centPrecision",
currencyCode: "EUR",
centAmount: 1000,
fractionDigits: 2,
},
customerGroup: {
typeId: "customer-group",
id: "customer-group-id",
},
});
});

test("should handle validFrom and validUntil fields when provided", () => {
const draft: PriceDraft = {
value: {
currencyCode: "EUR",
centAmount: 1000,
},
country: "NL",
validFrom: "2023-01-01T00:00:00.000Z",
validUntil: "2023-12-31T23:59:59.999Z",
};

const result = priceFromDraft(context, storage, draft);

expect(result).toMatchObject({
id: expect.any(String),
country: "NL",
value: {
type: "centPrecision",
currencyCode: "EUR",
centAmount: 1000,
fractionDigits: 2,
},
validFrom: "2023-01-01T00:00:00.000Z",
validUntil: "2023-12-31T23:59:59.999Z",
});
});

test("should handle tiers field when provided", () => {
const tierDrafts: PriceTierDraft[] = [
{
minimumQuantity: 5,
value: {
currencyCode: "EUR",
centAmount: 900,
},
},
{
minimumQuantity: 10,
value: {
currencyCode: "EUR",
centAmount: 800,
},
},
];

const draft: PriceDraft = {
value: {
currencyCode: "EUR",
centAmount: 1000,
},
country: "NL",
tiers: tierDrafts,
};

const result = priceFromDraft(context, storage, draft);

expect(result).toMatchObject({
id: expect.any(String),
country: "NL",
value: {
type: "centPrecision",
currencyCode: "EUR",
centAmount: 1000,
fractionDigits: 2,
},
tiers: [
{
minimumQuantity: 5,
value: {
type: "centPrecision",
currencyCode: "EUR",
centAmount: 900,
fractionDigits: 2,
},
},
{
minimumQuantity: 10,
value: {
type: "centPrecision",
currencyCode: "EUR",
centAmount: 800,
fractionDigits: 2,
},
},
],
});
});

test("should handle custom field when provided", () => {
// First create a type in storage for custom fields
const customType = {
...getBaseResourceProperties(),
id: "custom-type-id",
key: "custom-type-key",
name: { en: "Custom Type" },
resourceTypeIds: ["price"],
fieldDefinitions: [],
};
storage.add("test-project", "type", customType);

const draft: PriceDraft = {
value: {
currencyCode: "EUR",
centAmount: 1000,
},
country: "NL",
custom: {
type: {
typeId: "type",
id: "custom-type-id",
},
fields: {
customField: "customValue",
},
},
};

const result = priceFromDraft(context, storage, draft);

expect(result).toMatchObject({
id: expect.any(String),
country: "NL",
value: {
type: "centPrecision",
currencyCode: "EUR",
centAmount: 1000,
fractionDigits: 2,
},
custom: {
type: {
typeId: "type",
id: "custom-type-id",
},
fields: {
customField: "customValue",
},
},
});
});
});
22 changes: 22 additions & 0 deletions src/repositories/product/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import type {
Asset,
AssetDraft,
ChannelReference,
CustomerGroupReference,
DiscountedPrice,
DiscountedPriceDraft,
Price,
PriceDraft,
PriceTier,
PriceTierDraft,
Product,
ProductData,
ProductDiscountReference,
ProductVariant,
ProductVariantDraft,
} from "@commercetools/platform-sdk";
Expand Down Expand Up @@ -113,4 +119,20 @@ export const priceFromDraft = (
storage,
)
: undefined,
customerGroup: draft.customerGroup
? getReferenceFromResourceIdentifier<CustomerGroupReference>(
draft.customerGroup,
context.projectKey,
storage,
)
: undefined,
validFrom: draft.validFrom,
validUntil: draft.validUntil,
tiers: draft.tiers?.map(
(tier: PriceTierDraft): PriceTier => ({
minimumQuantity: tier.minimumQuantity,
value: createTypedMoney(tier.value),
}),
),
custom: createCustomFields(draft.custom, context.projectKey, storage),
});
Loading