-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: promotions discounts v2 #6575
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
822bbdc
feat: add the promotion-discounts plugin
vanpho93 4c936b7
fix: fix query and mutation tests fail
vanpho93 b798517
feat: add rules for promotion trigger (uncompleted)
vanpho93 5ebb6ac
feat: inclusion and exclusion for discount item
vanpho93 0c979c9
fix: make getEligibleItems shorter
vanpho93 0eac0d9
feat: refactor promotion discount plugin
vanpho93 6271a1a
fix: update pnpm lock
vanpho93 03b90cb
feat: add test for promotion discounts
vanpho93 f59a564
fix: fix integration mutation test fail
vanpho93 64030bd
fix: calculate percentage discount
vanpho93 1baccb1
fix: split order discount for cart items
vanpho93 01b77aa
fix: fix calculate the order total
vanpho93 0cd9287
fix: fix test fail for order plugin
vanpho93 47bf3c7
feat: update the promotion data on sample-data plugin
vanpho93 719002a
feat: add integration test for promotions
vanpho93 f4cf8fd
feat: update lockfile
vanpho93 cbf069f
fix: fix sample-data records
brent-hoover cc6a9c2
fix: fix typo on the tests
vanpho93 bc2451b
chore: rename variable
vanpho93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
268 changes: 268 additions & 0 deletions
268
apps/reaction/tests/integration/api/mutations/checkout/promotionCheckout.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
import decodeOpaqueIdForNamespace from "@reactioncommerce/api-utils/decodeOpaqueIdForNamespace.js"; | ||
import importAsString from "@reactioncommerce/api-utils/importAsString.js"; | ||
import Factory from "/tests/util/factory.js"; | ||
import getCommonData from "../checkout/checkoutTestsCommon.js"; | ||
|
||
const AnonymousCartByCartIdQuery = importAsString("../checkout/AnonymousCartByCartIdQuery.graphql"); | ||
const SetEmailOnAnonymousCart = importAsString("../checkout/SetEmailOnAnonymousCartMutation.graphql"); | ||
|
||
let anonymousCartByCartQuery; | ||
let availablePaymentMethods; | ||
let createCart; | ||
let encodeProductOpaqueId; | ||
let internalVariantIds; | ||
let opaqueProductId; | ||
let opaqueShopId; | ||
let placeOrder; | ||
let selectFulfillmentOptionForGroup; | ||
let setEmailOnAnonymousCart; | ||
let setShippingAddressOnCart; | ||
let testApp; | ||
let updateFulfillmentOptionsForGroup; | ||
let mockPromotion; | ||
|
||
beforeAll(async () => { | ||
({ | ||
availablePaymentMethods, | ||
createCart, | ||
encodeProductOpaqueId, | ||
internalVariantIds, | ||
opaqueProductId, | ||
opaqueShopId, | ||
placeOrder, | ||
selectFulfillmentOptionForGroup, | ||
setShippingAddressOnCart, | ||
testApp, | ||
updateFulfillmentOptionsForGroup | ||
} = getCommonData()); | ||
|
||
anonymousCartByCartQuery = testApp.mutate(AnonymousCartByCartIdQuery); | ||
setEmailOnAnonymousCart = testApp.mutate(SetEmailOnAnonymousCart); | ||
|
||
const now = new Date(); | ||
mockPromotion = Factory.Promotion.makeOne({ | ||
actions: [ | ||
{ | ||
actionKey: "discounts", | ||
actionParameters: { | ||
discountType: "order", | ||
discountCalculationType: "percentage", | ||
discountValue: 50 | ||
} | ||
} | ||
], | ||
triggers: [ | ||
{ | ||
triggerKey: "offers", | ||
triggerParameters: { | ||
name: "50 percent off your entire order when you spend more then $100", | ||
conditions: { | ||
all: [ | ||
{ | ||
fact: "totalItemAmount", | ||
operator: "greaterThanInclusive", | ||
value: 100 | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
triggerType: "implicit", | ||
promotionType: "order-discount", | ||
startDate: now, | ||
endDate: new Date(now.getTime() + 1000 * 60 * 60 * 24 * 7), | ||
enabled: true, | ||
shopId: decodeOpaqueIdForNamespace("reaction/shop")(opaqueShopId) | ||
}); | ||
|
||
await testApp.collections.Promotions.insertOne(mockPromotion); | ||
}); | ||
|
||
// There is no need to delete any test data from collections because | ||
// testApp.stop() will drop the entire test database. Each integration | ||
// test file gets its own test database. | ||
afterAll(() => testApp.stop()); | ||
|
||
describe("Promotions", () => { | ||
let cartToken; | ||
let opaqueCartId; | ||
let opaqueCartProductVariantId; | ||
let opaqueFulfillmentGroupId; | ||
let opaqueFulfillmentMethodId; | ||
let latestCartSummary; | ||
|
||
beforeAll(async () => { | ||
opaqueCartProductVariantId = encodeProductOpaqueId(internalVariantIds[1]); | ||
await testApp.clearLoggedInUser(); | ||
}); | ||
|
||
const shippingAddress = { | ||
address1: "12345 Drive Lane", | ||
city: "The city", | ||
country: "USA", | ||
firstName: "FName", | ||
fullName: "FName LName", | ||
isBillingDefault: false, | ||
isCommercial: false, | ||
isShippingDefault: false, | ||
lastName: "LName", | ||
phone: "5555555555", | ||
postal: "97878", | ||
region: "CA" | ||
}; | ||
|
||
test("create a new cart", async () => { | ||
const result = await createCart({ | ||
createCartInput: { | ||
shopId: opaqueShopId, | ||
items: { | ||
price: { | ||
amount: 19.99, | ||
currencyCode: "USD" | ||
}, | ||
productConfiguration: { | ||
productId: opaqueProductId, | ||
productVariantId: opaqueCartProductVariantId | ||
}, | ||
quantity: 6 | ||
} | ||
} | ||
}); | ||
|
||
cartToken = result.createCart.token; | ||
opaqueCartId = result.createCart.cart._id; | ||
}); | ||
|
||
test("set email on anonymous cart", async () => { | ||
const result = await setEmailOnAnonymousCart({ | ||
input: { | ||
cartId: opaqueCartId, | ||
cartToken, | ||
email: "test@email.com" | ||
} | ||
}); | ||
|
||
opaqueCartId = result.setEmailOnAnonymousCart.cart._id; | ||
}); | ||
|
||
test("set shipping address on cart", async () => { | ||
const result = await setShippingAddressOnCart({ | ||
input: { | ||
cartId: opaqueCartId, | ||
cartToken, | ||
address: { | ||
address1: "12345 Drive Lane", | ||
city: "The city", | ||
country: "USA", | ||
firstName: "FName", | ||
fullName: "FName LName", | ||
lastName: "LName", | ||
phone: "5555555555", | ||
postal: "97878", | ||
region: "CA" | ||
} | ||
} | ||
}); | ||
|
||
opaqueFulfillmentGroupId = result.setShippingAddressOnCart.cart.checkout.fulfillmentGroups[0]._id; | ||
}); | ||
|
||
test("get available fulfillment options", async () => { | ||
const result = await updateFulfillmentOptionsForGroup({ | ||
input: { | ||
cartId: opaqueCartId, | ||
cartToken, | ||
fulfillmentGroupId: opaqueFulfillmentGroupId | ||
} | ||
}); | ||
|
||
const option = result.updateFulfillmentOptionsForGroup.cart.checkout.fulfillmentGroups[0].availableFulfillmentOptions[0]; | ||
opaqueFulfillmentMethodId = option.fulfillmentMethod._id; | ||
}); | ||
|
||
test("select the `Standard mockMethod` fulfillment option", async () => { | ||
const result = await selectFulfillmentOptionForGroup({ | ||
input: { | ||
cartId: opaqueCartId, | ||
cartToken, | ||
fulfillmentGroupId: opaqueFulfillmentGroupId, | ||
fulfillmentMethodId: opaqueFulfillmentMethodId | ||
} | ||
}); | ||
|
||
latestCartSummary = result.selectFulfillmentOptionForGroup.cart.checkout.summary; | ||
}); | ||
|
||
test("place an order with discount and get the correct values", async () => { | ||
let result; | ||
|
||
const paymentMethods = await availablePaymentMethods({ | ||
shopId: opaqueShopId | ||
}); | ||
|
||
const paymentMethodName = paymentMethods.availablePaymentMethods[0].name; | ||
|
||
const { anonymousCartByCartId: anonymousCart } = await anonymousCartByCartQuery({ | ||
cartId: opaqueCartId, | ||
cartToken | ||
}); | ||
|
||
try { | ||
result = await placeOrder({ | ||
input: { | ||
order: { | ||
cartId: opaqueCartId, | ||
currencyCode: "USD", | ||
email: anonymousCart.email, | ||
fulfillmentGroups: [ | ||
{ | ||
data: { | ||
shippingAddress | ||
}, | ||
items: [ | ||
{ | ||
price: 19.99, | ||
productConfiguration: { | ||
productId: opaqueProductId, | ||
productVariantId: opaqueCartProductVariantId | ||
}, | ||
quantity: 6 | ||
} | ||
], | ||
selectedFulfillmentMethodId: opaqueFulfillmentMethodId, | ||
shopId: opaqueShopId, | ||
type: "shipping", | ||
totalPrice: latestCartSummary.total.amount | ||
} | ||
], | ||
shopId: opaqueShopId | ||
}, | ||
payments: [ | ||
{ | ||
amount: latestCartSummary.total.amount, | ||
method: paymentMethodName | ||
} | ||
] | ||
} | ||
}); | ||
} catch (error) { | ||
expect(error).toBeUndefined(); | ||
return; | ||
} | ||
|
||
const orderId = decodeOpaqueIdForNamespace("reaction/order")(result.placeOrder.orders[0]._id); | ||
const newOrder = await testApp.collections.Orders.findOne({ _id: orderId }); | ||
|
||
expect(newOrder.shipping[0].invoice.total).toEqual(62.47); | ||
expect(newOrder.shipping[0].invoice.discounts).toEqual(59.97); | ||
expect(newOrder.shipping[0].invoice.subtotal).toEqual(119.94); | ||
|
||
expect(newOrder.shipping[0].items[0].quantity).toEqual(6); | ||
expect(newOrder.shipping[0].items[0].discounts).toHaveLength(1); | ||
expect(newOrder.shipping[0].items[0].discount).toEqual(59.97); | ||
|
||
expect(newOrder.appliedPromotions[0]._id).toEqual(mockPromotion._id); | ||
expect(newOrder.discounts).toHaveLength(1); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These are set up like separate tests but in fact there is only one test, which should describe what it's testing, e.g. "place an order with discount and get the correct values"