feat(core-flows, order): add workflow to create change order actions (#8056)

what:

- adds workflow to create change order actions
This commit is contained in:
Riqwan Thamir
2024-07-10 12:35:03 +02:00
committed by GitHub
parent 04ec8e758f
commit bec2dcd667
37 changed files with 290 additions and 46 deletions

View File

@@ -0,0 +1,116 @@
import {
createOrderChangeActionsWorkflow,
createOrderChangeActionsWorkflowId,
createOrderChangeWorkflow,
} from "@medusajs/core-flows"
import { IOrderModuleService, OrderChangeDTO, OrderDTO } from "@medusajs/types"
import { ChangeActionType, ModuleRegistrationName } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createOrderFixture, prepareDataFixtures } from "./__fixtures__"
jest.setTimeout(50000)
medusaIntegrationTestRunner({
env: { MEDUSA_FF_MEDUSA_V2: true },
testSuite: ({ getContainer }) => {
let container
beforeAll(() => {
container = getContainer()
})
describe("Order change action workflows", () => {
let order: OrderDTO
let service: IOrderModuleService
let orderChange: OrderChangeDTO
describe("createOrderChangeActionWorkflow", () => {
beforeEach(async () => {
const fixtures = await prepareDataFixtures({ container })
order = await createOrderFixture({
container,
product: fixtures.product,
location: fixtures.location,
inventoryItem: fixtures.inventoryItem,
})
const { result } = await createOrderChangeWorkflow(container).run({
input: { order_id: order.id },
})
orderChange = result
service = container.resolve(ModuleRegistrationName.ORDER)
})
it("should successfully create an order change action", async () => {
const { result } = await createOrderChangeActionsWorkflow(
container
).run({
input: [
{
action: ChangeActionType.ITEM_ADD,
order_change_id: orderChange.id,
order_id: order.id,
},
{
action: ChangeActionType.ITEM_REMOVE,
order_change_id: orderChange.id,
order_id: order.id,
},
],
})
expect(result).toHaveLength(2)
expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
order_id: order.id,
order_change_id: orderChange.id,
action: ChangeActionType.ITEM_ADD,
}),
expect.objectContaining({
id: expect.any(String),
order_id: order.id,
order_change_id: orderChange.id,
action: ChangeActionType.ITEM_REMOVE,
}),
])
)
})
it("should delete created actions when a rollback occurs on a workflow", async () => {
const workflow = createOrderChangeActionsWorkflow(container)
workflow.appendAction("throw", createOrderChangeActionsWorkflowId, {
invoke: async function failStep() {
throw new Error(`Fail`)
},
})
const {
errors: [error],
} = await workflow.run({
input: [
{
action: ChangeActionType.ITEM_ADD,
order_change_id: orderChange.id,
order_id: order.id,
},
],
throwOnError: false,
})
const orderChange1 = await service.retrieveOrderChange(
orderChange.id,
{ relations: ["actions"] }
)
expect(error).toBeDefined()
expect(orderChange1.actions).toHaveLength(0)
})
})
})
},
})

View File

@@ -0,0 +1,34 @@
import {
CreateOrderChangeActionDTO,
IOrderModuleService,
} from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const createOrderChangeActionsStepId = "create-order-change-actions"
export const createOrderChangeActionsStep = createStep(
createOrderChangeActionsStepId,
async (data: CreateOrderChangeActionDTO[], { container }) => {
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
const created = await service.addOrderAction(data)
return new StepResponse(
created,
created.map((c) => c.id)
)
},
async (ids, { container }) => {
if (!ids?.length) {
return
}
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
await service.deleteOrderChangeActions(ids)
}
)

View File

@@ -6,6 +6,7 @@ export * from "./cancel-orders"
export * from "./cancel-return"
export * from "./complete-orders"
export * from "./create-order-change"
export * from "./create-order-change-actions"
export * from "./create-orders"
export * from "./decline-order-change"
export * from "./delete-order-change"

View File

@@ -0,0 +1,16 @@
import {
CreateOrderChangeActionDTO,
OrderChangeActionDTO,
} from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { createOrderChangeActionsStep } from "../steps"
export const createOrderChangeActionsWorkflowId = "create-order-change-actions"
export const createOrderChangeActionsWorkflow = createWorkflow(
createOrderChangeActionsWorkflowId,
(
input: WorkflowData<CreateOrderChangeActionDTO[]>
): WorkflowData<OrderChangeActionDTO[]> => {
return createOrderChangeActionsStep(input)
}
)

View File

@@ -6,6 +6,7 @@ export * from "./cancel-return"
export * from "./complete-orders"
export * from "./create-fulfillment"
export * from "./create-order-change"
export * from "./create-order-change-actions"
export * from "./create-orders"
export * from "./create-return"
export * from "./create-shipment"

View File

@@ -1544,6 +1544,42 @@ export interface IOrderModuleService extends IModuleService {
sharedContext?: Context
): Promise<OrderChangeActionDTO[]>
/**
* This method deletes {return type} by its ID.
*
* @param {string[]} actionId - The list of {summary}
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<void>} Resolves when {summary}
*
* @example
* ```typescript
* await orderModuleService.deleteOrderActions(["12345abc", "67890def"]);
* ```
*
*/
deleteOrderChangeActions(
actionId: string[],
sharedContext?: Context
): Promise<void>
/**
* This method deletes {return type} by its ID.
*
* @param {string} orderId - The order action's ID.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<void>} Resolves when {summary}
*
* @example
* ```typescript
* await orderModuleService.deleteOrderActions("orderActionId");
* ```
*
*/
deleteOrderChangeActions(
actionId: string,
sharedContext?: Context
): Promise<void>
softDeleteAddresses<TReturnableLinkableKeys extends string = string>(
ids: string[],
config?: SoftDeleteReturn<TReturnableLinkableKeys>,

View File

@@ -1,3 +1,3 @@
export * from "./events"
export * from "./order-change-action"
export * from "./status"

View File

@@ -4,9 +4,8 @@ import {
CreateOrderDTO,
IOrderModuleService,
} from "@medusajs/types"
import { BigNumber, Modules } from "@medusajs/utils"
import { BigNumber, ChangeActionType, Modules } from "@medusajs/utils"
import { moduleIntegrationTestRunner } from "medusa-test-utils"
import { ChangeActionType } from "../../src/utils"
jest.setTimeout(100000)

View File

@@ -1,5 +1,6 @@
import { ChangeActionType } from "@medusajs/utils"
import { OrderChangeEvent } from "../../../../types"
import { ChangeActionType, calculateOrderChange } from "../../../../utils"
import { calculateOrderChange } from "../../../../utils"
describe("Order Exchange - Actions", function () {
const originalOrder = {

View File

@@ -1,5 +1,6 @@
import { ChangeActionType } from "@medusajs/utils"
import { OrderChangeEvent } from "../../../../types"
import { ChangeActionType, calculateOrderChange } from "../../../../utils"
import { calculateOrderChange } from "../../../../utils"
describe("Order Return - Actions", function () {
const originalOrder = {

View File

@@ -3,8 +3,7 @@ import {
CreateOrderChangeActionDTO,
OrderTypes,
} from "@medusajs/types"
import { promiseAll } from "@medusajs/utils"
import { ChangeActionType } from "../../utils"
import { ChangeActionType, promiseAll } from "@medusajs/utils"
async function createOrderChange(
service,

View File

@@ -3,8 +3,7 @@ import {
CreateOrderChangeActionDTO,
OrderTypes,
} from "@medusajs/types"
import { promiseAll } from "@medusajs/utils"
import { ChangeActionType } from "../../utils"
import { ChangeActionType, promiseAll } from "@medusajs/utils"
async function createOrderChange(
service,

View File

@@ -1,5 +1,5 @@
import { Context, OrderTypes } from "@medusajs/types"
import { ChangeActionType } from "../../utils"
import { ChangeActionType } from "@medusajs/utils"
export async function cancelFulfillment(
this: any,

View File

@@ -3,8 +3,7 @@ import {
CreateOrderChangeActionDTO,
OrderTypes,
} from "@medusajs/types"
import { promiseAll } from "@medusajs/utils"
import { ChangeActionType } from "../../utils"
import { ChangeActionType, promiseAll } from "@medusajs/utils"
async function createOrderChange(
service,

View File

@@ -4,6 +4,7 @@ import {
OrderTypes,
} from "@medusajs/types"
import {
ChangeActionType,
ClaimType,
ReturnStatus,
getShippingMethodsTotals,
@@ -12,7 +13,6 @@ import {
} from "@medusajs/utils"
import { ClaimItem, OrderClaim, Return, ReturnItem } from "@models"
import { OrderChangeType } from "@types"
import { ChangeActionType } from "../../utils"
function createClaimAndReturnEntities(em, data, order) {
const claimReference = em.create(OrderClaim, {

View File

@@ -4,6 +4,7 @@ import {
OrderTypes,
} from "@medusajs/types"
import {
ChangeActionType,
ReturnStatus,
getShippingMethodsTotals,
isString,
@@ -11,7 +12,6 @@ import {
} from "@medusajs/utils"
import { ExchangeItem, OrderExchange, Return, ReturnItem } from "@models"
import { OrderChangeType } from "@types"
import { ChangeActionType } from "../../utils"
function createExchangeAndReturnEntities(em, data, order) {
const exchangeReference = em.create(OrderExchange, {

View File

@@ -4,6 +4,7 @@ import {
OrderTypes,
} from "@medusajs/types"
import {
ChangeActionType,
ReturnStatus,
getShippingMethodsTotals,
isDefined,
@@ -12,7 +13,6 @@ import {
} from "@medusajs/utils"
import { Return, ReturnItem } from "@models"
import { OrderChangeType } from "@types"
import { ChangeActionType } from "../../utils"
function createReturnReference(em, data, order) {
return em.create(Return, {

View File

@@ -1,7 +1,11 @@
import { Context, OrderTypes } from "@medusajs/types"
import { MathBN, ReturnStatus, promiseAll } from "@medusajs/utils"
import {
ChangeActionType,
MathBN,
ReturnStatus,
promiseAll,
} from "@medusajs/utils"
import { OrderChangeType } from "@types"
import { ChangeActionType } from "../../utils"
function createReturnItems(data) {
return data.items.map((item) => ({

View File

@@ -1,5 +1,5 @@
import { Context, OrderTypes } from "@medusajs/types"
import { ChangeActionType } from "../../utils"
import { ChangeActionType } from "@medusajs/utils"
export async function registerFulfillment(
this: any,

View File

@@ -3,7 +3,7 @@ import {
CreateOrderChangeActionDTO,
OrderTypes,
} from "@medusajs/types"
import { ChangeActionType } from "../../utils"
import { ChangeActionType } from "@medusajs/utils"
export async function registerShipment(
this: any,

View File

@@ -2233,6 +2233,7 @@ export default class OrderModuleService<
includeActions: boolean,
sharedContext?: Context
): Promise<any> {
orderChangeIds = deduplicate(orderChangeIds)
const options = {
select: [
"id",

View File

@@ -1,5 +1,9 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import {
ChangeActionType,
MathBN,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
import {
setActionReference,

View File

@@ -1,5 +1,9 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import {
ChangeActionType,
MathBN,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
import {
setActionReference,

View File

@@ -1,4 +1,4 @@
import { ChangeActionType } from "../action-key"
import { ChangeActionType } from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
OrderChangeProcessing.registerActionType(ChangeActionType.CANCEL, {

View File

@@ -1,5 +1,9 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import {
ChangeActionType,
MathBN,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
import {
setActionReference,

View File

@@ -1,6 +1,10 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import {
ChangeActionType,
MathBN,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { VirtualOrder } from "@types"
import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
import {
setActionReference,

View File

@@ -1,6 +1,10 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import {
ChangeActionType,
MathBN,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { VirtualOrder } from "@types"
import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
import {
setActionReference,

View File

@@ -1,6 +1,10 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import {
ChangeActionType,
MathBN,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { EVENT_STATUS } from "@types"
import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
import {
setActionReference,

View File

@@ -1,11 +1,11 @@
import {
ChangeActionType,
MathBN,
MedusaError,
isDefined,
transformPropertiesToBigNumber,
} from "@medusajs/utils"
import { EVENT_STATUS } from "@types"
import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
import {
setActionReference,

View File

@@ -1,5 +1,9 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import {
ChangeActionType,
MathBN,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
import {
setActionReference,

View File

@@ -1,5 +1,9 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import {
ChangeActionType,
MathBN,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
import {
setActionReference,

View File

@@ -1,5 +1,9 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import {
ChangeActionType,
MathBN,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
import {
setActionReference,

View File

@@ -1,5 +1,4 @@
import { MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import { ChangeActionType, MedusaError, isDefined } from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
import { setActionReference } from "../set-action-reference"

View File

@@ -1,5 +1,4 @@
import { MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import { ChangeActionType, MedusaError, isDefined } from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_REMOVE, {

View File

@@ -1,5 +1,9 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import {
ChangeActionType,
MathBN,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
import {
setActionReference,

View File

@@ -1,4 +1,3 @@
export * from "./action-key"
export * from "./actions"
export * from "./apply-order-changes"
export * from "./calculate-order-change"