feat(core-flows,types): add workflow to update order change actions (#8080)

This commit is contained in:
Riqwan Thamir
2024-07-11 11:04:41 +02:00
committed by GitHub
parent 1789b8e315
commit 8a548cbc2f
6 changed files with 235 additions and 2 deletions

View File

@@ -4,8 +4,15 @@ import {
createOrderChangeWorkflow,
deleteOrderChangeActionsWorkflow,
deleteOrderChangeActionsWorkflowId,
updateOrderChangeActionsWorkflow,
updateOrderChangeActionsWorkflowId,
} from "@medusajs/core-flows"
import { IOrderModuleService, OrderChangeDTO, OrderDTO } from "@medusajs/types"
import {
IOrderModuleService,
OrderChangeActionDTO,
OrderChangeDTO,
OrderDTO,
} from "@medusajs/types"
import { ChangeActionType, ModuleRegistrationName } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createOrderFixture, prepareDataFixtures } from "./__fixtures__"
@@ -44,7 +51,7 @@ medusaIntegrationTestRunner({
service = container.resolve(ModuleRegistrationName.ORDER)
})
describe("createOrderChangeActionWorkflow", () => {
describe("createOrderChangeActionsWorkflow", () => {
it("should successfully create an order change action", async () => {
const { result } = await createOrderChangeActionsWorkflow(
container
@@ -212,6 +219,77 @@ medusaIntegrationTestRunner({
)
})
})
describe("updateOrderChangeActionWorkflow", () => {
let createdOrderAction: OrderChangeActionDTO
beforeEach(async () => {
const { result } = await createOrderChangeActionsWorkflow(
container
).run({
input: [
{
action: ChangeActionType.ITEM_ADD,
order_change_id: orderChange.id,
order_id: order.id,
internal_note: "existing",
},
],
})
createdOrderAction = result[0]
})
it("should successfully update an order change action", async () => {
const {
result: [updatedOrderChange],
} = await updateOrderChangeActionsWorkflow(container).run({
input: [
{
id: createdOrderAction.id,
internal_note: "new",
},
],
})
expect(updatedOrderChange).toEqual(
expect.objectContaining({
id: createdOrderAction.id,
internal_note: "new",
})
)
})
it("should rollback to original state when a future step has an error", async () => {
const workflow = updateOrderChangeActionsWorkflow(container)
workflow.appendAction("throw", updateOrderChangeActionsWorkflowId, {
invoke: async function failStep() {
throw new Error(`Fail`)
},
})
const {
errors: [error],
} = await workflow.run({
input: [
{
id: createdOrderAction.id,
internal_note: "new",
},
],
throwOnError: false,
})
const orderChangeAction = await service.retrieveOrderChangeAction(
createdOrderAction.id,
{}
)
expect(error).toBeDefined()
expect(orderChangeAction.internal_note).toEqual("existing")
})
})
})
},
})

View File

@@ -15,4 +15,5 @@ export * from "./get-item-tax-lines"
export * from "./register-fulfillment"
export * from "./register-shipment"
export * from "./set-tax-lines-for-items"
export * from "./update-order-change-actions"
export * from "./update-tax-lines"

View File

@@ -0,0 +1,42 @@
import {
IOrderModuleService,
UpdateOrderChangeActionDTO,
} from "@medusajs/types"
import {
getSelectsAndRelationsFromObjectArray,
ModuleRegistrationName,
} from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
export const updateOrderChangeActionsStepId = "update-order-change-actions"
export const updateOrderChangeActionsStep = createStep(
updateOrderChangeActionsStepId,
async (data: UpdateOrderChangeActionDTO[], { container }) => {
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
const { selects, relations } = getSelectsAndRelationsFromObjectArray(data, {
objectFields: ["metadata"],
})
const dataBeforeUpdate = await service.listOrderChangeActions(
{ id: data.map((d) => d.id) },
{ relations, select: selects }
)
const updated = await service.updateOrderChangeActions(data)
return new StepResponse(updated, dataBeforeUpdate)
},
async (dataBeforeUpdate, { container }) => {
if (!dataBeforeUpdate?.length) {
return
}
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
await service.updateOrderChangeActions(dataBeforeUpdate)
}
)

View File

@@ -16,4 +16,5 @@ export * from "./delete-order-change-actions"
export * from "./get-order-detail"
export * from "./get-orders-list"
export * from "./receive-return"
export * from "./update-order-change-actions"
export * from "./update-tax-lines"

View File

@@ -0,0 +1,16 @@
import {
OrderChangeActionDTO,
UpdateOrderChangeActionDTO,
} from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { updateOrderChangeActionsStep } from "../steps"
export const updateOrderChangeActionsWorkflowId = "update-order-change-actions"
export const updateOrderChangeActionsWorkflow = createWorkflow(
updateOrderChangeActionsWorkflowId,
(
input: WorkflowData<UpdateOrderChangeActionDTO[]>
): WorkflowData<OrderChangeActionDTO[]> => {
return updateOrderChangeActionsStep(input)
}
)

View File

@@ -4,6 +4,7 @@ import { IModuleService } from "../modules-sdk"
import { Context } from "../shared-context"
import {
FilterableOrderAddressProps,
FilterableOrderChangeActionProps,
FilterableOrderLineItemAdjustmentProps,
FilterableOrderLineItemProps,
FilterableOrderLineItemTaxLineProps,
@@ -59,6 +60,7 @@ import {
RegisterOrderFulfillmentDTO,
RegisterOrderShipmentDTO,
UpdateOrderAddressDTO,
UpdateOrderChangeActionDTO,
UpdateOrderChangeDTO,
UpdateOrderDTO,
UpdateOrderItemDTO,
@@ -1535,6 +1537,99 @@ export interface IOrderModuleService extends IModuleService {
sharedContext?: Context
): Promise<OrderChangeReturn>
/**
* This method retrieves a paginated list of {return type}(s) based on optional filters and configuration.
*
* @param {FilterableOrderChangeActionProps} filters - The filters to apply on the retrieved order change action.
* @param {FindConfig<OrderChangeActionDTO>} config - The configurations determining how the order is retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a order.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<OrderChangeActionDTO[]>} The list of {return type}(s).
*
* @example
* ```typescript
* const orderChangeActions = await orderModuleService.listOrderChangeActions();
* ```
*
*/
listOrderChangeActions(
filters?: FilterableOrderChangeActionProps,
config?: FindConfig<OrderChangeActionDTO>,
sharedContext?: Context
): Promise<OrderChangeActionDTO[]>
/**
* This method retrieves a {return type} by its ID.
*
* @param {string} actionId - The order change action's ID.
* @param {FindConfig<OrderChangeActionDTO>} config - The configurations determining how the order is retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a order.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<OrderChangeActionDTO>} The retrieved {return type}(s).
*
* @example
* ```typescript
* const result = await orderModuleService.retrieveOrderChangeAction("actionId123");
* ```
*
*/
retrieveOrderChangeAction(
actionId: string,
config?: FindConfig<OrderChangeActionDTO>,
sharedContext?: Context
): Promise<OrderChangeActionDTO>
updateOrderChangeActions(
data: UpdateOrderChangeActionDTO,
sharedContext?: Context
): Promise<OrderChangeActionDTO>
/**
* This method updates {return type}(s)
*
* @param {UpdateOrderChangeActionDTO[]} data - The order change action to be updated.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<OrderChangeActionDTO[]>} The updated {return type}(s).
*
* @example
* ```typescript
* // Example call to updateOrderChangeActions
*
* const updateOrderChangeActionsData: UpdateOrderChangeActionDTO[] = [{
* id: "orderchangeaction123",
* ...
* }];
*
* const result = await orderModuleService.updateOrderChangeActions(updateOrderChangeActionsData);
* ```
*
*/
updateOrderChangeActions(
data: UpdateOrderChangeActionDTO[],
sharedContext?: Context
): Promise<OrderChangeActionDTO[]>
/**
* This method updates {return type}(s)
*
* @param {UpdateOrderChangeActionDTO | UpdateOrderChangeActionDTO[]} data - The order change action d t o | order change to be updated.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<OrderChangeActionDTO | OrderChangeActionDTO[]>} The updated {return type}(s).
*
* @example
* ```typescript
* const result = await orderModuleService.updateOrderChangeActions({
* id: "orderChangeAction123",
* ...
* });
* ```
*
*/
updateOrderChangeActions(
data: UpdateOrderChangeActionDTO | UpdateOrderChangeActionDTO[],
sharedContext?: Context
): Promise<OrderChangeActionDTO | OrderChangeActionDTO[]>
addOrderAction(
data: CreateOrderChangeActionDTO,
sharedContext?: Context