commercetools-node-mock
commercetools-node-mock copied to clipboard
Implement missing fields for priceFromDraft function
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
CustomerGroupResourceIdentifiertoCustomerGroupReferenceusing the existinggetReferenceFromResourceIdentifierhelper - validFrom/validUntil: Added direct string mapping for price validity date ranges
- discounted: Added mapping from
DiscountedPriceDrafttoDiscountedPricewith proper value conversion and ProductDiscount reference handling - tiers: Added mapping from
PriceTierDraft[]toPriceTier[]with value conversion for quantity-based pricing - custom: Added mapping from
CustomFieldsDrafttoCustomFieldsusing the existingcreateCustomFieldshelper
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.