Skip to content

Conversation

@daanjo3
Copy link
Contributor

@daanjo3 daanjo3 commented Dec 16, 2025

Currently it is not possible to provide discountcodes on cart creation, nor use the addDiscountCode update action on the cart. As I would like to use this functionality in our repository I've created this PR.

To avoid putting a lot of work in recreating CommerceTools logic I'm not validating the to-be-added discountcodes further than purely whether their object exists in the store. So prices will not be updated nor will cart-discount rules be checked.

@changeset-bot
Copy link

changeset-bot bot commented Dec 16, 2025

🦋 Changeset detected

Latest commit: e39342a

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

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

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds basic support for discount codes in the cart repository by enabling discount codes to be provided during cart creation and implementing the addDiscountCode update action. The implementation intentionally omits complex validation logic such as cart discount rule checking or price updates, focusing solely on verifying that discount code objects exist in the store.

Key Changes

  • Added createDiscountCodeInfoFromCode helper function to validate and convert discount codes to DiscountCodeInfo objects
  • Enabled discount codes to be specified in CartDraft during cart creation
  • Implemented addDiscountCode action handler with duplicate prevention logic

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/repositories/cart/index.ts Added discount code validation and processing during cart creation
src/repositories/cart/helpers.ts Introduced createDiscountCodeInfoFromCode helper to validate discount codes and create DiscountCodeInfo objects
src/repositories/cart/actions.ts Implemented addDiscountCode action with duplicate checking
src/repositories/cart/index.test.ts Added test case for creating a cart with a discount code
.changeset/cozy-meals-rush.md Added changeset documenting the new feature

code: "DiscountCodeNonApplicable",
message: `The discount code '${code}' was not found.`,
reason: "DoesNotExist",
discountCode: "nonexistent",
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The discountCode field should contain the actual discount code that was not found, not the hardcoded string "nonexistent". This field should be set to the value of the code parameter to provide more useful error information to API consumers.

Suggested change
discountCode: "nonexistent",
discountCode: code,

Copilot uses AI. Check for mistakes.
Comment on lines +285 to +302
addDiscountCode(
context: RepositoryContext,
resource: Writable<Cart>,
{ code }: CartAddDiscountCodeAction,
) {
const info = createDiscountCodeInfoFromCode(
context.projectKey,
this._storage,
code,
);
if (
!resource.discountCodes
.map((dc) => dc.discountCode.id)
.includes(info.discountCode.id)
) {
resource.discountCodes.push(info);
}
}
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The newly added addDiscountCode action lacks test coverage. While there is a test for creating a cart with discount codes, there's no test for the addDiscountCode update action itself. Consider adding a test that creates a cart and then uses the addDiscountCode action to verify the functionality works correctly.

Copilot uses AI. Check for mistakes.
Comment on lines +720 to +750
test("create cart with discount code", async () => {
const code = storage.add("dummy", "discount-code", {
...getBaseResourceProperties(),
code: "test-1234",
cartDiscounts: [],
isActive: true,
references: [],
groups: [],
});

const cart: CartDraft = {
country: "NL",
currency: "EUR",
discountCodes: ["test-1234"],
};

const ctx = { projectKey: "dummy", storeKey: "dummyStore" };

const result = repository.create(ctx, cart);
expect(result.id).toBeDefined();

expect(result.discountCodes).toEqual([
{
discountCode: {
typeId: "discount-code",
id: code.id,
},
state: "MatchesCart",
},
]);
});
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The test only covers the happy path where the discount code exists. Consider adding a test case that verifies the error thrown when attempting to create a cart with a non-existent discount code to ensure proper error handling.

Copilot uses AI. Check for mistakes.
Comment on lines +92 to +104
// Validate that discount codes exist
const discountCodeInfo: DiscountCodeInfo[] = [];
if (draft.discountCodes?.length) {
draft.discountCodes.forEach((code) => {
discountCodeInfo.push(
createDiscountCodeInfoFromCode(
context.projectKey,
this._storage,
code,
),
);
});
}
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

When creating a cart with duplicate discount codes in the discountCodes array, the current implementation will create duplicate entries in the cart's discountCodes array. Consider deduplicating the discount codes during cart creation, similar to how the addDiscountCode action prevents duplicates.

Copilot uses AI. Check for mistakes.
@mvantellingen mvantellingen merged commit 06315c6 into labd:main Dec 30, 2025
17 checks passed
@github-actions github-actions bot mentioned this pull request Dec 30, 2025
mvantellingen pushed a commit that referenced this pull request Dec 30, 2025
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @labdigital/[email protected]

### Minor Changes

- [#365](#365)
[`ef7ef13`](ef7ef13)
Thanks [@robertmoelker](https://github.com/robertmoelker)! - Add
HighPrecisionMoney support

- [#360](#360)
[`06315c6`](06315c6)
Thanks [@daanjo3](https://github.com/daanjo3)! - Add basic support for
discount codes in the cart repository

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

2 participants