-
Notifications
You must be signed in to change notification settings - Fork 23
Add basic support for discount codes in the cart repository #360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic support for discount codes in the cart repository #360
Conversation
🦋 Changeset detectedLatest 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 |
There was a problem hiding this 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
createDiscountCodeInfoFromCodehelper function to validate and convert discount codes toDiscountCodeInfoobjects - Enabled discount codes to be specified in
CartDraftduring cart creation - Implemented
addDiscountCodeaction 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", |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
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.
| discountCode: "nonexistent", | |
| discountCode: code, |
| 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); | ||
| } | ||
| } |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
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.
| 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", | ||
| }, | ||
| ]); | ||
| }); |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
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.
| // Validate that discount codes exist | ||
| const discountCodeInfo: DiscountCodeInfo[] = []; | ||
| if (draft.discountCodes?.length) { | ||
| draft.discountCodes.forEach((code) => { | ||
| discountCodeInfo.push( | ||
| createDiscountCodeInfoFromCode( | ||
| context.projectKey, | ||
| this._storage, | ||
| code, | ||
| ), | ||
| ); | ||
| }); | ||
| } |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
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.
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>
Currently it is not possible to provide discountcodes on cart creation, nor use the
addDiscountCodeupdate 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.