Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 15, 2025

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.

Copy link

changeset-bot bot commented Jul 15, 2025

🦋 Changeset detected

Latest commit: 06da874

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@labdigital/commercetools-mock Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Copilot Copilot AI changed the title [WIP] Implement missing fields for prices Implement missing fields for priceFromDraft function Jul 15, 2025
@Copilot Copilot AI requested a review from mvantellingen July 15, 2025 05:37
Copilot finished work on behalf of mvantellingen July 15, 2025 05:37
: undefined,
validFrom: draft.validFrom,
validUntil: draft.validUntil,
discounted: draft.discounted
Copy link
Member

@mvantellingen mvantellingen Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Is this available on the draft? I think it is calculated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right! The discounted field should be calculated by the platform when ProductDiscounts are applied, not directly mapped from the draft. I've removed this field mapping from the priceFromDraft function. Commit: 4c83c8b

@mvantellingen
Copy link
Member

@copilot see my comment, and add a missing changeset file please

Copy link
Contributor Author

Copilot AI commented Jul 16, 2025

@copilot see my comment, and add a missing changeset file please

Addressed the discounted field issue and added the missing changeset file. Commit: 4c83c8b

@Copilot Copilot AI requested a review from mvantellingen July 16, 2025 14:58
Copilot finished work on behalf of mvantellingen July 16, 2025 14:58
@mvantellingen
Copy link
Member

@copilot that changelog item does not make sense

Copy link
Contributor Author

Copilot AI commented Aug 18, 2025

@copilot that changelog item does not make sense

You're right! Fixed the changeset to properly describe the main feature - implementing missing fields for the priceFromDraft function rather than just mentioning the discounted field removal. Commit: ca9c8f7

Copilot finished work on behalf of mvantellingen August 18, 2025 11:04
@mvantellingen mvantellingen marked this pull request as ready for review August 18, 2025 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement missing fields for prices
2 participants