commercetools-node-mock icon indicating copy to clipboard operation
commercetools-node-mock copied to clipboard

Implement missing fields for priceFromDraft function

Open Copilot opened this issue 4 months ago • 5 comments

The priceFromDraft function in src/repositories/product/helpers.ts was missing several important fields when converting a PriceDraft to a Price object. This caused incomplete price objects to be created, missing key functionality like customer group targeting, validity periods, discounts, tiered pricing, and custom fields.

Changes Made

  • customerGroup: Added mapping from CustomerGroupResourceIdentifier to CustomerGroupReference using the existing getReferenceFromResourceIdentifier helper
  • validFrom/validUntil: Added direct string mapping for price validity date ranges
  • discounted: Added mapping from DiscountedPriceDraft to DiscountedPrice with proper value conversion and ProductDiscount reference handling
  • tiers: Added mapping from PriceTierDraft[] to PriceTier[] with value conversion for quantity-based pricing
  • custom: Added mapping from CustomFieldsDraft to CustomFields using the existing createCustomFields helper

Testing

Added comprehensive unit tests in src/repositories/product/helpers.test.ts that verify:

  • Basic price creation without optional fields continues to work
  • CustomerGroup references are properly resolved and mapped
  • ValidFrom/validUntil date strings are correctly passed through
  • Discounted prices with ProductDiscount references work correctly
  • Price tiers with quantity thresholds and values are properly converted
  • Custom fields are correctly processed using the type system

All existing tests continue to pass, ensuring no regressions.

Example Usage

const priceDraft: PriceDraft = {
  value: { currencyCode: "EUR", centAmount: 1000 },
  country: "NL",
  customerGroup: { typeId: "customer-group", id: "vip-customers" },
  validFrom: "2024-01-01T00:00:00.000Z",
  validUntil: "2024-12-31T23:59:59.999Z",
  tiers: [
    { minimumQuantity: 10, value: { currencyCode: "EUR", centAmount: 900 } }
  ]
};

// Now correctly produces a complete Price object with all fields
const price = priceFromDraft(context, storage, priceDraft);

Fixes #283.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Jul 15 '25 05:07 Copilot