Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa): order changes endpoint #8728

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,17 @@ medusaIntegrationTestRunner({

expect(result.total).toEqual(34)
expect(result.items.length).toEqual(1)

result = (
await api.get(
`/admin/orders/${orderId}/changes?change_type=edit`,
adminHeaders
)
).data.order_changes

expect(result[0].actions).toHaveLength(3)
expect(result[0].status).toEqual("confirmed")
expect(result[0].confirmed_by).toEqual(expect.stringContaining("user_"))
})
})
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const cancelOrderChangeStep = createStep(
"exchange_id",
"version",
"canceled_at",
"cancelled_by"
"canceled_by"
)

const dataBeforeUpdate = await service.retrieveOrderChange(data.id, {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/core-flows/src/order/steps/cancel-orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createStep, StepResponse } from "@medusajs/workflows-sdk"

export type CancelOrdersStepInput = {
orderIds: string[]
canceled_by?: string
}

export const cancelOrdersStepId = "cancel-orders"
Expand Down Expand Up @@ -35,6 +36,7 @@ export const cancelOrdersStep = createStep(
id: order.id,
status: prevData.status,
canceled_at: null,
canceled_by: null,
}
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createStep, StepResponse } from "@medusajs/workflows-sdk"
export type ConfirmOrderChangesInput = {
orderId: string
changes: OrderChangeDTO[]
confirmed_by?: string
}

/**
Expand All @@ -15,7 +16,10 @@ export const confirmOrderChanges = createStep(
async (input: ConfirmOrderChangesInput, { container }) => {
const orderModuleService = container.resolve(ModuleRegistrationName.ORDER)
await orderModuleService.confirmOrderChange(
input.changes.map((action) => action.id)
input.changes.map((action) => ({
id: action.id,
confirmed_by: input.confirmed_by,
}))
)

return new StepResponse(null, input.orderId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const cancelOrderClaimWorkflow = createWorkflow(
cancelOrderClaimStep({
claim_id: orderClaim.id,
order_id: orderClaim.order_id,
canceled_by: input.canceled_by,
}),
deleteReservationsByLineItemsStep(lineItemIds)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import {
ReturnStatus,
} from "@medusajs/utils"
import {
WorkflowResponse,
createStep,
createWorkflow,
parallelize,
transform,
when,
WorkflowResponse,
} from "@medusajs/workflows-sdk"
import { reserveInventoryStep } from "../../../cart/steps/reserve-inventory"
import { prepareConfirmInventoryInput } from "../../../cart/utils/prepare-confirm-inventory-input"
Expand All @@ -39,6 +39,7 @@ import { createOrUpdateOrderPaymentCollectionWorkflow } from "../create-or-updat

export type ConfirmClaimRequestWorkflowInput = {
claim_id: string
confirmed_by?: string
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const cancelOrderExchangeWorkflow = createWorkflow(
cancelOrderExchangeStep({
exchange_id: orderExchange.id,
order_id: orderExchange.order_id,
canceled_by: input.canceled_by,
}),
deleteReservationsByLineItemsStep(lineItemIds)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { createOrUpdateOrderPaymentCollectionWorkflow } from "../create-or-updat

export type ConfirmExchangeRequestWorkflowInput = {
exchange_id: string
confirmed_by?: string
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
createWorkflow,
transform,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common"
import { reserveInventoryStep } from "../../../cart/steps/reserve-inventory"
import { prepareConfirmInventoryInput } from "../../../cart/utils/prepare-confirm-inventory-input"
import { useRemoteQueryStep } from "../../../common"
import { previewOrderChangeStep } from "../../steps"
import { confirmOrderChanges } from "../../steps/confirm-order-changes"
import {
Expand All @@ -24,6 +24,7 @@ import { createOrUpdateOrderPaymentCollectionWorkflow } from "../create-or-updat

export type ConfirmOrderEditRequestWorkflowInput = {
order_id: string
confirmed_by?: string
}

/**
Expand Down Expand Up @@ -99,7 +100,11 @@ export const confirmOrderEditRequestWorkflow = createWorkflow(

const orderPreview = previewOrderChangeStep(order.id)

confirmOrderChanges({ changes: [orderChange], orderId: order.id })
confirmOrderChanges({
changes: [orderChange],
orderId: order.id,
confirmed_by: input.confirmed_by,
})

const orderItems = useRemoteQueryStep({
entry_point: "order",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { createOrUpdateOrderPaymentCollectionWorkflow } from "../create-or-updat

export type OrderEditRequestWorkflowInput = {
order_id: string
requested_by?: string
}

/**
Expand Down Expand Up @@ -73,6 +74,8 @@ export const requestOrderEditRequestWorkflow = createWorkflow(
{
id: orderChange.id,
status: OrderChangeStatus.REQUESTED,
requested_at: new Date(),
requested_by: input.requested_by,
},
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const cancelReturnWorkflow = createWorkflow(
cancelOrderReturnStep({
return_id: orderReturn.id,
order_id: orderReturn.order_id,
canceled_by: input.canceled_by,
})
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {

export type ConfirmReceiveReturnRequestWorkflowInput = {
return_id: string
confirmed_by?: string
}

/**
Expand Down Expand Up @@ -292,7 +293,11 @@ export const confirmReturnReceiveWorkflow = createWorkflow(
parallelize(
updateReturnsStep([updateReturn]),
updateReturnItemsStep(updateReturnItem),
confirmOrderChanges({ changes: [orderChange], orderId: order.id }),
confirmOrderChanges({
changes: [orderChange],
orderId: order.id,
confirmed_by: input.confirmed_by,
}),
adjustInventoryLevelsStep(inventoryAdjustment)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { createOrUpdateOrderPaymentCollectionWorkflow } from "../create-or-updat

export type ConfirmReturnRequestWorkflowInput = {
return_id: string
confirmed_by?: string
}

/**
Expand Down Expand Up @@ -278,7 +279,11 @@ export const confirmReturnRequestWorkflow = createWorkflow(
requested_at: new Date(),
},
]),
confirmOrderChanges({ changes: [orderChange], orderId: order.id })
confirmOrderChanges({
changes: [orderChange],
orderId: order.id,
confirmed_by: input.confirmed_by,
})
)

createOrUpdateOrderPaymentCollectionWorkflow.runAsStep({
Expand Down
7 changes: 6 additions & 1 deletion packages/core/types/src/http/order/admin/responses.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { PaginatedResponse } from "../../common"
import { BaseOrderChange } from "../common"
import { AdminOrder, AdminOrderPreview } from "./entities"

export interface AdminOrderResponse {
order: AdminOrder
}

export interface AdminOrderChangesResponse {
order_changes: BaseOrderChange[]
}

export type AdminOrderListResponse = PaginatedResponse<{
orders: AdminOrder[]
}>
Expand All @@ -19,4 +24,4 @@ export interface AdminDraftOrderResponse {

export type AdminDraftOrderListResponse = PaginatedResponse<{
draft_orders: AdminOrder
}>
}>
3 changes: 3 additions & 0 deletions packages/core/types/src/order/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,7 @@ export interface UpdateOrderExchangeWithSelectorDTO {
}
export interface CancelOrderReturnDTO extends BaseOrderBundledActionsDTO {
return_id: string
canceled_by?: string
}

/**
Expand Down Expand Up @@ -1981,6 +1982,7 @@ export interface CreateOrderClaimDTO extends BaseOrderBundledActionsDTO {

export interface CancelOrderClaimDTO extends BaseOrderBundledActionsDTO {
claim_id: string
canceled_by?: string
}

/**
Expand Down Expand Up @@ -2031,6 +2033,7 @@ export interface CreateOrderExchangeDTO extends BaseOrderBundledActionsDTO {

export interface CancelOrderExchangeDTO extends BaseOrderBundledActionsDTO {
exchange_id: string
canceled_by?: string
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/core/types/src/workflow/order/cancel-claim.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface CancelOrderClaimWorkflowInput {
claim_id: string
no_notification?: boolean
canceled_by?: string
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface CancelOrderExchangeWorkflowInput {
exchange_id: string
no_notification?: boolean
canceled_by?: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export interface CancelOrderFulfillmentWorkflowInput {
order_id: string
fulfillment_id: string
no_notification?: boolean
canceled_by?: string
}
1 change: 1 addition & 0 deletions packages/core/types/src/workflow/order/cancel-order.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface CancelOrderWorkflowInput {
order_id: string
no_notification?: boolean
canceled_by?: string
}
1 change: 1 addition & 0 deletions packages/core/types/src/workflow/order/cancel-return.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface CancelReturnWorkflowInput {
return_id: string
no_notification?: boolean
canceled_by?: string
}
3 changes: 2 additions & 1 deletion packages/medusa/src/api/admin/claims/[id]/cancel/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { cancelOrderClaimWorkflow } from "@medusajs/core-flows"
import { HttpTypes } from "@medusajs/types"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { AdminPostCancelClaimReqSchemaType } from "../../validators"
import { HttpTypes } from "@medusajs/types"

export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostCancelClaimReqSchemaType>,
Expand All @@ -17,6 +17,7 @@ export const POST = async (
input: {
...req.validatedBody,
claim_id: id,
canceled_by: req.auth_context.actor_id,
},
})

Expand Down
7 changes: 5 additions & 2 deletions packages/medusa/src/api/admin/claims/[id]/request/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
cancelBeginOrderClaimWorkflow,
confirmClaimRequestWorkflow,
} from "@medusajs/core-flows"
import { HttpTypes } from "@medusajs/types"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
Expand All @@ -11,7 +12,6 @@ import {
MedusaResponse,
} from "../../../../../types/routing"
import { defaultAdminDetailsReturnFields } from "../../../returns/query-config"
import { HttpTypes } from "@medusajs/types"

export const POST = async (
req: AuthenticatedMedusaRequest,
Expand All @@ -22,7 +22,10 @@ export const POST = async (
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)

const { result } = await confirmClaimRequestWorkflow(req.scope).run({
input: { claim_id: id },
input: {
claim_id: id,
confirmed_by: req.auth_context.actor_id,
},
})

const queryObject = remoteQueryObjectFromString({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { cancelOrderExchangeWorkflow } from "@medusajs/core-flows"
import { HttpTypes } from "@medusajs/types"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { AdminPostCancelExchangeReqSchemaType } from "../../validators"
import { HttpTypes } from "@medusajs/types"

export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostCancelExchangeReqSchemaType>,
Expand All @@ -17,6 +17,7 @@ export const POST = async (
input: {
...req.validatedBody,
exchange_id: id,
canceled_by: req.auth_context.actor_id,
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
cancelBeginOrderExchangeWorkflow,
confirmExchangeRequestWorkflow,
} from "@medusajs/core-flows"
import { DeleteResponse, HttpTypes } from "@medusajs/types"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
Expand All @@ -11,7 +12,6 @@ import {
MedusaResponse,
} from "../../../../../types/routing"
import { defaultAdminDetailsReturnFields } from "../../../returns/query-config"
import { DeleteResponse, HttpTypes } from "@medusajs/types"

export const POST = async (
req: AuthenticatedMedusaRequest,
Expand All @@ -22,7 +22,10 @@ export const POST = async (
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)

const { result } = await confirmExchangeRequestWorkflow(req.scope).run({
input: { exchange_id: id },
input: {
exchange_id: id,
confirmed_by: req.auth_context.actor_id,
},
})

const queryObject = remoteQueryObjectFromString({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export const POST = async (
const { id } = req.params

const { result } = await confirmOrderEditRequestWorkflow(req.scope).run({
input: { order_id: id },
input: {
order_id: id,
confirmed_by: req.auth_context.actor_id,
},
})

res.json({
Expand Down
Loading
Loading