diff --git a/.changeset/plain-gifts-cross.md b/.changeset/plain-gifts-cross.md new file mode 100644 index 00000000..c87cd4e2 --- /dev/null +++ b/.changeset/plain-gifts-cross.md @@ -0,0 +1,5 @@ +--- +"@labdigital/commercetools-mock": patch +--- + +Implement missing fields for priceFromDraft function: customerGroup, validFrom, validUntil, tiers, and custom fields diff --git a/src/repositories/product/helpers.test.ts b/src/repositories/product/helpers.test.ts new file mode 100644 index 00000000..956b4091 --- /dev/null +++ b/src/repositories/product/helpers.test.ts @@ -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", + }, + }, + }); + }); +}); diff --git a/src/repositories/product/helpers.ts b/src/repositories/product/helpers.ts index b28facd9..1449cf56 100644 --- a/src/repositories/product/helpers.ts +++ b/src/repositories/product/helpers.ts @@ -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"; @@ -113,4 +119,20 @@ export const priceFromDraft = ( storage, ) : undefined, + customerGroup: draft.customerGroup + ? getReferenceFromResourceIdentifier( + 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), });