-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-flows,link-modules,modules-sdk): add cart <> promotion link…
… as source of truth (#6561) what: - adds promotion cart link - update steps to create and remove links
- Loading branch information
Showing
14 changed files
with
272 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@medusajs/link-modules": patch | ||
"@medusajs/modules-sdk": patch | ||
"@medusajs/core-flows": patch | ||
--- | ||
|
||
feat(core-flows,link-modules,modules-sdk): add cart <> promotion link as source of truth |
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
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
58 changes: 16 additions & 42 deletions
58
packages/core-flows/src/definition/cart/steps/get-actions-to-compute-from-promotions.ts
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
107 changes: 107 additions & 0 deletions
107
packages/core-flows/src/definition/cart/steps/update-cart-promotions.ts
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,107 @@ | ||
import { | ||
LinkModuleUtils, | ||
ModuleRegistrationName, | ||
Modules, | ||
} from "@medusajs/modules-sdk" | ||
import { IPromotionModuleService } from "@medusajs/types" | ||
import { PromotionActions } from "@medusajs/utils" | ||
import { StepResponse, createStep } from "@medusajs/workflows-sdk" | ||
|
||
interface StepInput { | ||
id: string | ||
promo_codes?: string[] | ||
action?: | ||
| PromotionActions.ADD | ||
| PromotionActions.REMOVE | ||
| PromotionActions.REPLACE | ||
} | ||
|
||
export const updateCartPromotionsStepId = "update-cart-promotions" | ||
export const updateCartPromotionsStep = createStep( | ||
updateCartPromotionsStepId, | ||
async (data: StepInput, { container }) => { | ||
const { promo_codes = [], id, action = PromotionActions.ADD } = data | ||
|
||
const remoteLink = container.resolve(LinkModuleUtils.REMOTE_LINK) | ||
const remoteQuery = container.resolve(LinkModuleUtils.REMOTE_QUERY) | ||
const promotionService = container.resolve<IPromotionModuleService>( | ||
ModuleRegistrationName.PROMOTION | ||
) | ||
|
||
const existingCartPromotionLinks = await remoteQuery({ | ||
cart_promotion: { | ||
__args: { cart_id: [id] }, | ||
fields: ["cart_id", "promotion_id"], | ||
}, | ||
}) | ||
|
||
const promotionLinkMap = new Map<string, any>( | ||
existingCartPromotionLinks.map((link) => [link.promotion_id, link]) | ||
) | ||
|
||
const promotions = await promotionService.list( | ||
{ code: promo_codes }, | ||
{ select: ["id"] } | ||
) | ||
|
||
const linksToCreate: any[] = [] | ||
const linksToDismiss: any[] = [] | ||
|
||
for (const promotion of promotions) { | ||
const linkObject = { | ||
[Modules.CART]: { cart_id: id }, | ||
[Modules.PROMOTION]: { promotion_id: promotion.id }, | ||
} | ||
|
||
if ([PromotionActions.ADD, PromotionActions.REPLACE].includes(action)) { | ||
linksToCreate.push(linkObject) | ||
} | ||
|
||
if (action === PromotionActions.REMOVE) { | ||
const link = promotionLinkMap.get(promotion.id) | ||
|
||
if (link) { | ||
linksToDismiss.push(linkObject) | ||
} | ||
} | ||
} | ||
|
||
if (action === PromotionActions.REPLACE) { | ||
for (const link of existingCartPromotionLinks) { | ||
linksToDismiss.push({ | ||
[Modules.CART]: { cart_id: link.cart_id }, | ||
[Modules.PROMOTION]: { promotion_id: link.promotion_id }, | ||
}) | ||
} | ||
} | ||
|
||
const linksToDismissPromise = linksToDismiss.length | ||
? remoteLink.dismiss(linksToDismiss) | ||
: [] | ||
|
||
const linksToCreatePromise = linksToCreate.length | ||
? remoteLink.create(linksToCreate) | ||
: [] | ||
|
||
const [_, createdLinks] = await Promise.all([ | ||
linksToDismissPromise, | ||
linksToCreatePromise, | ||
]) | ||
|
||
return new StepResponse(null, { | ||
createdLinkIds: createdLinks.map((link) => link.id), | ||
dismissedLinks: linksToDismiss, | ||
}) | ||
}, | ||
async (revertData, { container }) => { | ||
const remoteLink = container.resolve(LinkModuleUtils.REMOTE_LINK) | ||
|
||
if (revertData?.dismissedLinks?.length) { | ||
await remoteLink.create(revertData.dismissedLinks) | ||
} | ||
|
||
if (revertData?.createdLinkIds?.length) { | ||
await remoteLink.delete(revertData.createdLinkIds) | ||
} | ||
} | ||
) |
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
Oops, something went wrong.