chore(core-flows): favor runAsStep (#8540)

This commit is contained in:
Carlos R. L. Rodrigues
2024-08-12 06:21:11 -03:00
committed by GitHub
parent f14f398685
commit 6ffe85aa3d
23 changed files with 113 additions and 319 deletions
@@ -1,33 +0,0 @@
import { PromotionActions } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { updateCartPromotionsWorkflow } from "../workflows/update-cart-promotions"
export interface RefreshCartPromotionsStepInput {
id: string
promo_codes?: string[]
action?:
| PromotionActions.ADD
| PromotionActions.REMOVE
| PromotionActions.REPLACE
}
export const refreshCartPromotionsStepId = "refresh-cart-promotions"
/**
* This step refreshes the promotions of a cart.
*/
export const refreshCartPromotionsStep = createStep(
refreshCartPromotionsStepId,
async (data: RefreshCartPromotionsStepInput, { container }) => {
const { promo_codes = [], id, action = PromotionActions.ADD } = data
await updateCartPromotionsWorkflow(container).run({
input: {
action,
promoCodes: promo_codes,
cartId: id,
},
})
return new StepResponse(null)
}
)
@@ -1,38 +0,0 @@
import {
CartLineItemDTO,
CartShippingMethodDTO,
CartWorkflowDTO,
} from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { updateTaxLinesWorkflow } from "../workflows/update-tax-lines"
export interface UpdateTaxLinesStepInput {
cart_or_cart_id: CartWorkflowDTO | string
items?: CartLineItemDTO[]
shipping_methods?: CartShippingMethodDTO[]
force_tax_calculation?: boolean
}
export const updateTaxLinesStepId = "update-tax-lines-step"
/**
* This step updates tax lines of line items and shipping methods.
*/
export const updateTaxLinesStep = createStep(
updateTaxLinesStepId,
async (input: UpdateTaxLinesStepInput, { container }) => {
const { transaction } = await updateTaxLinesWorkflow(container).run({
input,
})
return new StepResponse(null, { transaction })
},
async (flow, { container }) => {
if (!flow) {
return
}
await updateTaxLinesWorkflow(container).cancel({
transaction: flow.transaction,
})
}
)
@@ -9,9 +9,9 @@ import {
removeShippingMethodFromCartStep,
validateCartShippingOptionsStep,
} from "../steps"
import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions"
import { updateTaxLinesStep } from "../steps/update-tax-lines"
import { cartFieldsForRefreshSteps } from "../utils/fields"
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
import { updateTaxLinesWorkflow } from "./update-tax-lines"
export interface AddShippingMethodToCartWorkflowInput {
cart_id: string
@@ -99,11 +99,17 @@ export const addShippingMethodToWorkflow = createWorkflow(
shipping_methods: shippingMethodInput,
})
updateTaxLinesStep({
cart_or_cart_id: input.cart_id,
shipping_methods: shippingMethodsToAdd,
updateTaxLinesWorkflow.runAsStep({
input: {
cart_or_cart_id: input.cart_id,
shipping_methods: shippingMethodsToAdd,
},
})
refreshCartPromotionsStep({ id: input.cart_id })
updateCartPromotionsWorkflow.runAsStep({
input: {
cart_id: input.cart_id,
},
})
}
)
@@ -16,8 +16,6 @@ import {
refreshCartShippingMethodsStep,
updateLineItemsStep,
} from "../steps"
import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions"
import { updateTaxLinesStep } from "../steps/update-tax-lines"
import { validateVariantPricesStep } from "../steps/validate-variant-prices"
import {
cartFieldsForRefreshSteps,
@@ -26,6 +24,8 @@ import {
import { prepareLineItemData } from "../utils/prepare-line-item-data"
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
import { updateTaxLinesWorkflow } from "./update-tax-lines"
export const addToCartWorkflowId = "add-to-cart"
/**
@@ -117,10 +117,19 @@ export const addToCartWorkflow = createWorkflow(
parallelize(
refreshCartShippingMethodsStep({ cart }),
updateTaxLinesStep({ cart_or_cart_id: input.cart.id, items })
updateTaxLinesWorkflow.runAsStep({
input: {
cart_or_cart_id: input.cart.id,
items,
},
})
)
refreshCartPromotionsStep({ id: input.cart.id })
updateCartPromotionsWorkflow.runAsStep({
input: {
cart_id: input.cart.id,
},
})
refreshPaymentCollectionForCartWorkflow.runAsStep({
input: {
@@ -1,8 +1,4 @@
import {
AdditionalData,
CartDTO,
CreateCartWorkflowInputDTO,
} from "@medusajs/types"
import { AdditionalData, CreateCartWorkflowInputDTO } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
import {
WorkflowData,
@@ -20,13 +16,13 @@ import {
findSalesChannelStep,
getVariantPriceSetsStep,
} from "../steps"
import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions"
import { updateTaxLinesStep } from "../steps/update-tax-lines"
import { validateVariantPricesStep } from "../steps/validate-variant-prices"
import { productVariantsFields } from "../utils/fields"
import { prepareLineItemData } from "../utils/prepare-line-item-data"
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
import { updateTaxLinesWorkflow } from "./update-tax-lines"
// TODO: The createCartWorkflow are missing the following steps:
// - Refresh/delete shipping methods (fulfillment module)
@@ -151,11 +147,17 @@ export const createCartWorkflow = createWorkflow(
const carts = createCartsStep([cartToCreate])
const cart = transform({ carts }, (data) => data.carts?.[0])
updateTaxLinesStep({ cart_or_cart_id: cart.id })
updateTaxLinesWorkflow.runAsStep({
input: {
cart_or_cart_id: cart.id,
},
})
refreshCartPromotionsStep({
id: cart.id,
promo_codes: input.promo_codes,
updateCartPromotionsWorkflow.runAsStep({
input: {
cart_id: cart.id,
promo_codes: input.promo_codes,
},
})
refreshPaymentCollectionForCartWorkflow.runAsStep({
@@ -18,8 +18,8 @@ import { updateCartPromotionsStep } from "../steps/update-cart-promotions"
import { cartFieldsForRefreshSteps } from "../utils/fields"
export type UpdateCartPromotionsWorkflowInput = {
promoCodes: string[]
cartId: string
cart_id: string
promo_codes?: string[]
action?:
| PromotionActions.ADD
| PromotionActions.REMOVE
@@ -32,17 +32,19 @@ export const updateCartPromotionsWorkflowId = "update-cart-promotions"
*/
export const updateCartPromotionsWorkflow = createWorkflow(
updateCartPromotionsWorkflowId,
(input: WorkflowData<UpdateCartPromotionsWorkflowInput>): WorkflowData<void> => {
(
input: WorkflowData<UpdateCartPromotionsWorkflowInput>
): WorkflowData<void> => {
const cart = useRemoteQueryStep({
entry_point: "cart",
fields: cartFieldsForRefreshSteps,
variables: { id: input.cartId },
variables: { id: input.cart_id },
list: false,
})
const promotionCodesToApply = getPromotionCodesToApply({
cart: cart,
promo_codes: input.promoCodes,
promo_codes: input.promo_codes ?? [],
action: input.action || PromotionActions.ADD,
})
@@ -69,7 +71,7 @@ export const updateCartPromotionsWorkflow = createWorkflow(
shippingMethodAdjustmentsToCreate,
}),
updateCartPromotionsStep({
id: input.cartId,
id: input.cart_id,
promo_codes: computedPromotionCodes,
action: PromotionActions.REPLACE,
})
@@ -16,10 +16,10 @@ import {
refreshCartShippingMethodsStep,
updateCartsStep,
} from "../steps"
import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions"
import { updateTaxLinesStep } from "../steps/update-tax-lines"
import { cartFieldsForRefreshSteps } from "../utils/fields"
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
import { updateTaxLinesWorkflow } from "./update-tax-lines"
export const updateCartWorkflowId = "update-cart"
/**
@@ -87,13 +87,19 @@ export const updateCartWorkflow = createWorkflow(
parallelize(
refreshCartShippingMethodsStep({ cart }),
updateTaxLinesStep({ cart_or_cart_id: carts[0].id })
updateTaxLinesWorkflow.runAsStep({
input: {
cart_or_cart_id: carts[0].id,
},
})
)
refreshCartPromotionsStep({
id: input.id,
promo_codes: input.promo_codes,
action: PromotionActions.REPLACE,
updateCartPromotionsWorkflow.runAsStep({
input: {
cart_id: input.id,
promo_codes: input.promo_codes,
action: PromotionActions.REPLACE,
},
})
refreshPaymentCollectionForCartWorkflow.runAsStep({
@@ -8,7 +8,6 @@ import {
import { useRemoteQueryStep } from "../../../common/steps/use-remote-query"
import { updateLineItemsStepWithSelector } from "../../line-item/steps"
import { refreshCartShippingMethodsStep } from "../steps"
import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions"
import { validateVariantPricesStep } from "../steps/validate-variant-prices"
import {
cartFieldsForRefreshSteps,
@@ -16,6 +15,7 @@ import {
} from "../utils/fields"
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
// TODO: The UpdateLineItemsWorkflow are missing the following steps:
// - Validate shipping methods for new items (fulfillment module)
@@ -94,7 +94,11 @@ export const updateLineItemInCartWorkflow = createWorkflow(
refreshCartShippingMethodsStep({ cart })
refreshCartPromotionsStep({ id: input.cart.id })
updateCartPromotionsWorkflow.runAsStep({
input: {
cart_id: input.cart.id,
},
})
refreshPaymentCollectionForCartWorkflow.runAsStep({
input: { cart_id: input.cart.id },
@@ -1,5 +1,5 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { refreshCartPromotionsStep } from "../../cart/steps/refresh-cart-promotions"
import { updateCartPromotionsWorkflow } from "../../cart/workflows/update-cart-promotions"
import { deleteLineItemsStep } from "../steps/delete-line-items"
export type DeleteLineItemsWorkflowInput = { cart_id: string; ids: string[] }
@@ -18,6 +18,10 @@ export const deleteLineItemsWorkflow = createWorkflow(
(input: WorkflowData<DeleteLineItemsWorkflowInput>) => {
deleteLineItemsStep(input.ids)
refreshCartPromotionsStep({ id: input.cart_id })
updateCartPromotionsWorkflow.runAsStep({
input: {
cart_id: input.cart_id,
},
})
}
)
@@ -34,4 +34,3 @@ export * from "./set-tax-lines-for-items"
export * from "./update-order-change-actions"
export * from "./update-order-exchanges"
export * from "./update-shipping-methods"
export * from "./update-tax-lines"
@@ -1,34 +0,0 @@
import { OrderLineItemDTO, OrderShippingMethodDTO } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { updateOrderTaxLinesWorkflow } from "../workflows/update-tax-lines"
export interface UpdateOrderTaxLinesStepInput {
order_id: string
items?: OrderLineItemDTO[]
shipping_methods?: OrderShippingMethodDTO[]
force_tax_calculation?: boolean
}
export const updateOrderTaxLinesStepId = "update-order-tax-lines-step"
/**
* This step updates tax lines for an order's line items and shipping methods.
*/
export const updateOrderTaxLinesStep = createStep(
updateOrderTaxLinesStepId,
async (input: UpdateOrderTaxLinesStepInput, { container }) => {
const { transaction } = await updateOrderTaxLinesWorkflow(container).run({
input,
})
return new StepResponse(null, { transaction })
},
async (flow, { container }) => {
if (!flow) {
return
}
await updateOrderTaxLinesWorkflow(container).cancel({
transaction: flow.transaction,
})
}
)
@@ -16,9 +16,10 @@ import { getVariantPriceSetsStep } from "../../definition/cart/steps/get-variant
import { validateVariantPricesStep } from "../../definition/cart/steps/validate-variant-prices"
import { prepareLineItemData } from "../../definition/cart/utils/prepare-line-item-data"
import { confirmVariantInventoryWorkflow } from "../../definition/cart/workflows/confirm-variant-inventory"
import { createOrdersStep, updateOrderTaxLinesStep } from "../steps"
import { createOrdersStep } from "../steps"
import { productVariantsFields } from "../utils/fields"
import { prepareCustomLineItemData } from "../utils/prepare-custom-line-item-data"
import { updateOrderTaxLinesWorkflow } from "./update-tax-lines"
function prepareLineItems(data) {
const items = (data.input.items ?? []).map((item) => {
@@ -172,14 +173,12 @@ export const createOrdersWorkflow = createWorkflow(
const orders = createOrdersStep([orderToCreate])
const order = transform({ orders }, (data) => data.orders?.[0])
/* TODO: Implement Order promotions
refreshOrderPromotionsStep({
id: order.id,
promo_codes: input.promo_codes,
updateOrderTaxLinesWorkflow.runAsStep({
input: {
order_id: order.id,
},
})
*/
updateOrderTaxLinesStep({ order_id: order.id })
const orderCreated = createHook("orderCreated", {
order,
additional_data: input.additional_data,
@@ -1,26 +0,0 @@
import { CreatePriceListPricesWorkflowDTO } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { createPriceListPricesWorkflow } from "../workflows/create-price-list-prices"
export const createPriceListPricesWorkflowStepId =
"create-price-list-prices-workflow-step"
/**
* This step creates prices for price lists.
*/
export const createPriceListPricesWorkflowStep = createStep(
createPriceListPricesWorkflowStepId,
async (data: CreatePriceListPricesWorkflowDTO[], { container }) => {
const { transaction, result: created } =
await createPriceListPricesWorkflow(container).run({ input: { data } })
return new StepResponse(created, transaction)
},
async (transaction, { container }) => {
if (!transaction) {
return
}
await createPriceListPricesWorkflow(container).cancel({ transaction })
}
)
@@ -1,12 +1,9 @@
export * from "./create-price-list-prices"
export * from "./create-price-list-prices-workflow"
export * from "./create-price-lists"
export * from "./delete-price-lists"
export * from "./get-existing-price-lists-price-ids"
export * from "./remove-price-list-prices"
export * from "./remove-price-list-prices-workflow"
export * from "./update-price-list-prices"
export * from "./update-price-list-prices-workflow"
export * from "./update-price-lists"
export * from "./validate-price-lists"
export * from "./validate-variant-price-links"
@@ -1,25 +0,0 @@
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { removePriceListPricesWorkflow } from "../workflows/remove-price-list-prices"
export const removePriceListPricesWorkflowStepId =
"remove-price-list-prices-workflow"
/**
* This step removes prices from price lists.
*/
export const removePriceListPricesWorkflowStep = createStep(
removePriceListPricesWorkflowStepId,
async (ids: string[], { container }) => {
const { transaction, result: updated } =
await removePriceListPricesWorkflow(container).run({ input: { ids } })
return new StepResponse(updated, transaction)
},
async (transaction, { container }) => {
if (!transaction) {
return
}
await removePriceListPricesWorkflow(container).cancel({ transaction })
}
)
@@ -1,26 +0,0 @@
import { UpdatePriceListPricesWorkflowDTO } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { updatePriceListPricesWorkflow } from "../workflows/update-price-list-prices"
export const updatePriceListPricesWorkflowStepId =
"update-price-list-prices-workflow"
/**
* This step updates price lists' prices.
*/
export const updatePriceListPricesWorkflowStep = createStep(
updatePriceListPricesWorkflowStepId,
async (data: UpdatePriceListPricesWorkflowDTO[], { container }) => {
const { transaction, result: updated } =
await updatePriceListPricesWorkflow(container).run({ input: { data } })
return new StepResponse(updated, transaction)
},
async (transaction, { container }) => {
if (!transaction) {
return
}
await updatePriceListPricesWorkflow(container).cancel({ transaction })
}
)
@@ -9,9 +9,9 @@ import {
parallelize,
transform,
} from "@medusajs/workflows-sdk"
import { createPriceListPricesWorkflowStep } from "../steps/create-price-list-prices-workflow"
import { removePriceListPricesWorkflowStep } from "../steps/remove-price-list-prices-workflow"
import { updatePriceListPricesWorkflowStep } from "../steps/update-price-list-prices-workflow"
import { createPriceListPricesWorkflow } from "./create-price-list-prices"
import { removePriceListPricesWorkflow } from "./remove-price-list-prices"
import { updatePriceListPricesWorkflow } from "./update-price-list-prices"
export const batchPriceListPricesWorkflowId = "batch-price-list-prices"
/**
@@ -33,9 +33,21 @@ export const batchPriceListPricesWorkflow = createWorkflow(
])
const [created, updated, deleted] = parallelize(
createPriceListPricesWorkflowStep(createInput),
updatePriceListPricesWorkflowStep(updateInput),
removePriceListPricesWorkflowStep(input.data.delete)
createPriceListPricesWorkflow.runAsStep({
input: {
data: createInput,
},
}),
updatePriceListPricesWorkflow.runAsStep({
input: {
data: updateInput,
},
}),
removePriceListPricesWorkflow.runAsStep({
input: {
ids: input.data.delete,
},
})
)
return new WorkflowResponse(
@@ -1,36 +0,0 @@
import { AddPromotionRulesWorkflowDTO } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { createPromotionRulesWorkflow } from "../workflows/create-promotion-rules"
export const createPromotionRulesWorkflowStepId =
"create-promotion-rules-workflow"
/**
* This step creates promotion rules using the {@link createPromotionRulesWorkflow}.
*/
export const createPromotionRulesWorkflowStep = createStep(
createPromotionRulesWorkflowStepId,
async (data: AddPromotionRulesWorkflowDTO, { container }) => {
const {
transaction,
result: created,
errors,
} = await createPromotionRulesWorkflow(container).run({
input: data,
throwOnError: false,
})
if (errors?.length) {
throw errors[0].error
}
return new StepResponse(created, transaction)
},
async (transaction, { container }) => {
if (!transaction) {
return
}
await createPromotionRulesWorkflow(container).cancel({ transaction })
}
)
@@ -1,36 +0,0 @@
import { UpdatePromotionRulesWorkflowDTO } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { updatePromotionRulesWorkflow } from "../workflows/update-promotion-rules"
export const updatePromotionRulesWorkflowStepId =
"update-promotion-rules-workflow"
/**
* This step updates promotion rules using the {@link updatePromotionRulesWorkflow}.
*/
export const updatePromotionRulesWorkflowStep = createStep(
updatePromotionRulesWorkflowStepId,
async (data: UpdatePromotionRulesWorkflowDTO, { container }) => {
const {
transaction,
result: updated,
errors,
} = await updatePromotionRulesWorkflow(container).run({
input: data,
throwOnError: false,
})
if (errors?.length) {
throw errors[0].error
}
return new StepResponse(updated, transaction)
},
async (transaction, { container }) => {
if (!transaction) {
return
}
await updatePromotionRulesWorkflow(container).cancel({ transaction })
}
)
@@ -5,6 +5,7 @@ import {
PromotionRuleDTO,
UpdatePromotionRuleDTO,
} from "@medusajs/types"
import { RuleType } from "@medusajs/utils"
import {
WorkflowData,
WorkflowResponse,
@@ -12,10 +13,9 @@ import {
parallelize,
transform,
} from "@medusajs/workflows-sdk"
import { RuleType } from "@medusajs/utils"
import { createPromotionRulesWorkflowStep } from "../steps/create-promotion-rules-workflow"
import { updatePromotionRulesWorkflowStep } from "../steps/update-promotion-rules-workflow"
import { deletePromotionRulesWorkflowStep } from "../steps/delete-promotion-rules-workflow"
import { createPromotionRulesWorkflow } from "./create-promotion-rules"
import { updatePromotionRulesWorkflow } from "./update-promotion-rules"
export const batchPromotionRulesWorkflowId = "batch-promotion-rules"
/**
@@ -46,8 +46,12 @@ export const batchPromotionRulesWorkflow = createWorkflow(
}))
const [created, updated, deleted] = parallelize(
createPromotionRulesWorkflowStep(createInput),
updatePromotionRulesWorkflowStep(updateInput),
createPromotionRulesWorkflow.runAsStep({
input: createInput,
}),
updatePromotionRulesWorkflow.runAsStep({
input: updateInput,
}),
deletePromotionRulesWorkflowStep(deleteInput)
)
@@ -14,4 +14,3 @@ export * from "./receive-return"
export * from "./request-item-return"
export * from "./shipping-method"
export * from "./update-return"
@@ -30,6 +30,11 @@ export interface OrderExchangeAddNewItemWorkflowInput {
items: NewItem[]
}
export interface OrderEditAddNewItemWorkflowInput {
order_id: string
items: NewItem[]
}
export interface OrderAddLineItemWorkflowInput {
order_id: string
items: NewItem[]
@@ -16,8 +16,8 @@ export const POST = async (
await workflow.run({
input: {
promoCodes: payload.promo_codes,
cartId: req.params.id,
promo_codes: payload.promo_codes,
cart_id: req.params.id,
action: PromotionActions.ADD,
},
})
@@ -40,8 +40,8 @@ export const DELETE = async (
await workflow.run({
input: {
promoCodes: payload.promo_codes,
cartId: req.params.id,
promo_codes: payload.promo_codes,
cart_id: req.params.id,
action: PromotionActions.REMOVE,
},
})