forked from reactioncommerce/reaction
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add check stackability for the shipping discount
Signed-off-by: vanpho93 <[email protected]>
- Loading branch information
Showing
4 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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
28 changes: 28 additions & 0 deletions
28
...ages/api-plugin-promotions-discounts/src/discountTypes/shipping/checkShippingStackable.js
This file contains 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,28 @@ | ||
/* eslint-disable no-await-in-loop */ | ||
import _ from "lodash"; | ||
|
||
/** | ||
* @summary check if a promotion is applicable to a cart | ||
* @param {Object} context - The application context | ||
* @param {Object} shipping - The cart we are trying to apply the promotion to | ||
* @param {Object} discount - The promotion we are trying to apply | ||
* @returns {Promise<Boolean>} - Whether the promotion is applicable to the shipping | ||
*/ | ||
export default async function checkShippingStackable(context, shipping, discount) { | ||
const { promotions } = context; | ||
const stackabilityByKey = _.keyBy(promotions.stackabilities, "key"); | ||
|
||
for (const appliedDiscount of shipping.discounts) { | ||
if (!appliedDiscount.stackability) continue; | ||
|
||
const stackHandler = stackabilityByKey[discount.stackability.key]; | ||
const appliedStackHandler = stackabilityByKey[appliedDiscount.stackability.key]; | ||
|
||
const stackResult = await stackHandler.handler(context, null, { promotion: discount, appliedPromotion: appliedDiscount }); | ||
const appliedStackResult = await appliedStackHandler.handler(context, {}, { promotion: appliedDiscount, appliedPromotion: discount }); | ||
|
||
if (!stackResult || !appliedStackResult) return false; | ||
} | ||
|
||
return true; | ||
} |
45 changes: 45 additions & 0 deletions
45
...api-plugin-promotions-discounts/src/discountTypes/shipping/checkShippingStackable.test.js
This file contains 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,45 @@ | ||
import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js"; | ||
import checkShippingStackable from "./checkShippingStackable.js"; | ||
|
||
test("should returns true if no the current discount is stackable", async () => { | ||
const shipping = { | ||
discounts: [ | ||
{ | ||
stackability: { key: "all" } | ||
} | ||
] | ||
}; | ||
const discount = { | ||
stackability: { key: "all" } | ||
}; | ||
|
||
mockContext.promotions = { | ||
stackabilities: [{ key: "all", handler: () => true }] | ||
}; | ||
|
||
const result = await checkShippingStackable(mockContext, shipping, discount); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
test("should returns false if the current discount is not stackable", async () => { | ||
const shipping = { | ||
discounts: [ | ||
{ | ||
stackability: { key: "all" } | ||
} | ||
] | ||
}; | ||
const discount = { | ||
stackability: { key: "none" } | ||
}; | ||
|
||
mockContext.promotions = { | ||
stackabilities: [ | ||
{ key: "all", handler: () => true }, | ||
{ key: "none", handler: () => false } | ||
] | ||
}; | ||
|
||
const result = await checkShippingStackable(mockContext, shipping, discount); | ||
expect(result).toBe(false); | ||
}); |