Skip to content

Commit

Permalink
chore: added decline order change workflow (#8041)
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan authored Jul 9, 2024
1 parent 4e183e1 commit 65527b7
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
cancelOrderChangeWorkflow,
cancelOrderChangeWorkflowId,
createOrderChangeWorkflow,
declineOrderChangeWorkflow,
declineOrderChangeWorkflowId,
deleteOrderChangeWorkflow,
deleteOrderChangeWorkflowId,
} from "@medusajs/core-flows"
Expand Down Expand Up @@ -233,6 +235,85 @@ medusaIntegrationTestRunner({
)
})
})

describe("declineOrderChangeWorkflow", () => {
let orderChange: OrderChangeDTO

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 decline an order change", async () => {
await declineOrderChangeWorkflow(container).run({
input: {
id: orderChange.id,
declined_by: "test",
},
})

const orderChange2 = await service.retrieveOrderChange(orderChange.id)

expect(orderChange2).toEqual(
expect.objectContaining({
id: expect.any(String),
declined_by: "test",
declined_at: expect.any(Date),
})
)
})

it("should rollback to its original state when step throws error", async () => {
const workflow = declineOrderChangeWorkflow(container)

workflow.appendAction("throw", declineOrderChangeWorkflowId, {
invoke: async function failStep() {
throw new Error(`Fail`)
},
})

const {
errors: [error],
} = await workflow.run({
input: {
id: orderChange.id,
declined_by: "test",
},
throwOnError: false,
})

expect(error.error).toEqual(
expect.objectContaining({
message: `Fail`,
})
)

const orderChange2 = await service.retrieveOrderChange(orderChange.id)

expect(orderChange2).toEqual(
expect.objectContaining({
id: expect.any(String),
declined_by: null,
declined_at: null,
})
)
})
})
})
},
})
45 changes: 45 additions & 0 deletions packages/core/core-flows/src/order/steps/decline-order-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
DeclineOrderChangeDTO,
IOrderModuleService,
UpdateOrderChangeDTO,
} from "@medusajs/types"
import {
getSelectsAndRelationsFromObjectArray,
ModuleRegistrationName,
} from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"

export const declineOrderChangeStepId = "decline-order-change"
export const declineOrderChangeStep = createStep(
declineOrderChangeStepId,
async (data: DeclineOrderChangeDTO, { container }) => {
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)

const { selects, relations } = getSelectsAndRelationsFromObjectArray(
[data],
{ objectFields: ["metadata"] }
)

const dataBeforeUpdate = await service.retrieveOrderChange(data.id, {
select: [...selects, "declined_at"],
relations,
})

await service.declineOrderChange(data)

return new StepResponse(void 0, dataBeforeUpdate)
},
async (rollbackData, { container }) => {
if (!rollbackData) {
return
}

const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)

await service.updateOrderChanges(rollbackData as UpdateOrderChangeDTO)
}
)
1 change: 1 addition & 0 deletions packages/core/core-flows/src/order/steps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from "./cancel-return"
export * from "./complete-orders"
export * from "./create-order-change"
export * from "./create-orders"
export * from "./decline-order-change"
export * from "./delete-order-change"
export * from "./get-item-tax-lines"
export * from "./register-fulfillment"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DeclineOrderChangeDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { declineOrderChangeStep } from "../steps"

export const declineOrderChangeWorkflowId = "decline-order-change"
export const declineOrderChangeWorkflow = createWorkflow(
declineOrderChangeWorkflowId,
(input: WorkflowData<DeclineOrderChangeDTO>): WorkflowData<void> => {
declineOrderChangeStep(input)
}
)
1 change: 1 addition & 0 deletions packages/core/core-flows/src/order/workflows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from "./create-order-change"
export * from "./create-orders"
export * from "./create-return"
export * from "./create-shipment"
export * from "./decline-order-change"
export * from "./delete-order-change"
export * from "./get-order-detail"
export * from "./get-orders-list"
Expand Down

0 comments on commit 65527b7

Please sign in to comment.