feat(core-flows,types,promotion): register promotion campaign usage upon cart completion (#8970)
* feat(core-flows,types,promotion): register promotion campaign usage upon cart completion * Apply suggestions from code review Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oli Juhl
parent
2a055b71ef
commit
5d7179b7d0
@@ -1,11 +1,15 @@
|
||||
import { OrderDTO } from "@medusajs/types"
|
||||
import {
|
||||
CartWorkflowDTO,
|
||||
OrderDTO,
|
||||
UsageComputedActions,
|
||||
} from "@medusajs/types"
|
||||
import { Modules, OrderStatus, OrderWorkflowEvents } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
createRemoteLinkStep,
|
||||
@@ -14,6 +18,7 @@ import {
|
||||
} from "../../common"
|
||||
import { createOrdersStep } from "../../order/steps/create-orders"
|
||||
import { authorizePaymentSessionStep } from "../../payment/steps/authorize-payment-session"
|
||||
import { registerUsageStep } from "../../promotion/steps/register-usage"
|
||||
import { updateCartsStep, validateCartPaymentsStep } from "../steps"
|
||||
import { reserveInventoryStep } from "../steps/reserve-inventory"
|
||||
import { validateCartStep } from "../steps/validate-cart"
|
||||
@@ -61,7 +66,8 @@ export const completeCartWorkflow = createWorkflow(
|
||||
(data) => {
|
||||
const allItems: any[] = []
|
||||
const allVariants: any[] = []
|
||||
data.cart.items.forEach((item) => {
|
||||
|
||||
data.cart?.items?.forEach((item) => {
|
||||
allItems.push({
|
||||
id: item.id,
|
||||
variant_id: item.variant_id,
|
||||
@@ -188,6 +194,39 @@ export const completeCartWorkflow = createWorkflow(
|
||||
})
|
||||
)
|
||||
|
||||
const promotionUsage = transform(
|
||||
{ cart },
|
||||
({ cart }: { cart: CartWorkflowDTO }) => {
|
||||
const promotionUsage: UsageComputedActions[] = []
|
||||
|
||||
const itemAdjustments = (cart.items ?? [])
|
||||
.map((item) => item.adjustments ?? [])
|
||||
.flat(1)
|
||||
|
||||
const shippingAdjustments = (cart.shipping_methods ?? [])
|
||||
.map((item) => item.adjustments ?? [])
|
||||
.flat(1)
|
||||
|
||||
for (const adjustment of itemAdjustments) {
|
||||
promotionUsage.push({
|
||||
amount: adjustment.amount,
|
||||
code: adjustment.code!,
|
||||
})
|
||||
}
|
||||
|
||||
for (const adjustment of shippingAdjustments) {
|
||||
promotionUsage.push({
|
||||
amount: adjustment.amount,
|
||||
code: adjustment.code!,
|
||||
})
|
||||
}
|
||||
|
||||
return promotionUsage
|
||||
}
|
||||
)
|
||||
|
||||
registerUsageStep(promotionUsage)
|
||||
|
||||
return new WorkflowResponse(order)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { IPromotionModuleService, UsageComputedActions } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const registerUsageStepId = "register-usage"
|
||||
/**
|
||||
* This step registers usage for promotion campaigns
|
||||
*/
|
||||
export const registerUsageStep = createStep(
|
||||
registerUsageStepId,
|
||||
async (data: UsageComputedActions[], { container }) => {
|
||||
if (!data.length) {
|
||||
return new StepResponse(null, [])
|
||||
}
|
||||
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
await promotionModule.registerUsage(data)
|
||||
|
||||
return new StepResponse(null, data)
|
||||
},
|
||||
async (revertData, { container }) => {
|
||||
if (!revertData?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
await promotionModule.revertUsage(revertData)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user