-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@labdigital/commercetools-mock": minor | ||
| --- | ||
|
|
||
| Add basic support for discount codes in the cart repository |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,12 +2,15 @@ import type { | |||||
| Cart, | ||||||
| CustomLineItem, | ||||||
| CustomLineItemDraft, | ||||||
| DiscountCodeInfo, | ||||||
| DiscountCodeNonApplicableError, | ||||||
| LineItem, | ||||||
| Price, | ||||||
| TaxCategory, | ||||||
| TaxCategoryReference, | ||||||
| } from "@commercetools/platform-sdk"; | ||||||
| import { v4 as uuidv4 } from "uuid"; | ||||||
| import { CommercetoolsError } from "#src/exceptions.ts"; | ||||||
| import { calculateTaxedPrice } from "#src/lib/tax.ts"; | ||||||
| import type { AbstractStorage } from "#src/storage/abstract.ts"; | ||||||
| import { | ||||||
|
|
@@ -121,3 +124,29 @@ export const createCustomLineItemFromDraft = ( | |||||
| taxedPricePortions: [], | ||||||
| }; | ||||||
| }; | ||||||
|
|
||||||
| export const createDiscountCodeInfoFromCode = ( | ||||||
| projectKey: string, | ||||||
| storage: AbstractStorage, | ||||||
| code: string, | ||||||
| ): DiscountCodeInfo => { | ||||||
| const discountCodes = storage.query(projectKey, "discount-code", { | ||||||
| where: `code="${code}"`, | ||||||
| }); | ||||||
| // Does not validate anything besides existence of the DiscountCode object | ||||||
| if (discountCodes.count === 0) { | ||||||
| throw new CommercetoolsError<DiscountCodeNonApplicableError>({ | ||||||
| code: "DiscountCodeNonApplicable", | ||||||
| message: `The discount code '${code}' was not found.`, | ||||||
| reason: "DoesNotExist", | ||||||
| discountCode: "nonexistent", | ||||||
|
||||||
| discountCode: "nonexistent", | |
| discountCode: code, |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -716,4 +716,36 @@ describe("createShippingInfo", () => { | |
| expect(result.taxedPrice!.totalGross.centAmount).toBe(1204); | ||
| expect(result.taxedPrice!.totalNet.centAmount).toBe(995); | ||
| }); | ||
|
|
||
| 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", | ||
| }, | ||
| ]); | ||
| }); | ||
|
Comment on lines
+720
to
+750
|
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import type { | |
| BusinessUnit, | ||
| Cart, | ||
| CartDraft, | ||
| DiscountCodeInfo, | ||
| GeneralError, | ||
| InvalidOperationError, | ||
| LineItem, | ||
|
|
@@ -29,6 +30,7 @@ import { CartUpdateHandler } from "./actions.ts"; | |
| import { | ||
| calculateCartTotalPrice, | ||
| createCustomLineItemFromDraft, | ||
| createDiscountCodeInfoFromCode, | ||
| selectPrice, | ||
| } from "./helpers.ts"; | ||
|
|
||
|
|
@@ -87,6 +89,20 @@ export class CartRepository extends AbstractResourceRepository<"cart"> { | |
| ), | ||
| ) ?? []; | ||
|
|
||
| // Validate that discount codes exist | ||
| const discountCodeInfo: DiscountCodeInfo[] = []; | ||
| if (draft.discountCodes?.length) { | ||
| draft.discountCodes.forEach((code) => { | ||
| discountCodeInfo.push( | ||
| createDiscountCodeInfoFromCode( | ||
| context.projectKey, | ||
| this._storage, | ||
| code, | ||
| ), | ||
| ); | ||
| }); | ||
| } | ||
|
Comment on lines
+92
to
+104
|
||
|
|
||
| const resource: Writable<Cart> = { | ||
| ...getBaseResourceProperties(), | ||
| anonymousId: draft.anonymousId, | ||
|
|
@@ -106,7 +122,7 @@ export class CartRepository extends AbstractResourceRepository<"cart"> { | |
| customerEmail: draft.customerEmail, | ||
| customLineItems, | ||
| directDiscounts: [], | ||
| discountCodes: [], | ||
| discountCodes: discountCodeInfo, | ||
| inventoryMode: "None", | ||
| itemShippingAddresses: [], | ||
| lineItems, | ||
|
|
||
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.