chore(order): cancel order (#7586)
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
import { FulfillmentWorkflow } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
import { updateFulfillmentWorkflow } from "../workflows/update-fulfillment"
|
||||
|
||||
export const updateFulfillmentWorkflowStepId = "update-fulfillment-workflow"
|
||||
export const updateFulfillmentWorkflowStep = createStep(
|
||||
updateFulfillmentWorkflowStepId,
|
||||
async (
|
||||
data: FulfillmentWorkflow.UpdateFulfillmentWorkflowInput,
|
||||
{ container }
|
||||
) => {
|
||||
const {
|
||||
transaction,
|
||||
result: updated,
|
||||
errors,
|
||||
} = await updateFulfillmentWorkflow(container).run({
|
||||
input: data,
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
return new StepResponse(updated, transaction)
|
||||
},
|
||||
|
||||
async (transaction, { container }) => {
|
||||
if (!transaction) {
|
||||
return
|
||||
}
|
||||
|
||||
await updateFulfillmentWorkflow(container).cancel({ transaction })
|
||||
}
|
||||
)
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { validateShipmentStep } from "../steps"
|
||||
import { updateFulfillmentWorkflowStep } from "../steps/update-fulfillment-workflow"
|
||||
import { updateFulfillmentWorkflow } from "./update-fulfillment"
|
||||
|
||||
export const createShipmentWorkflowId = "create-shipment-workflow"
|
||||
export const createShipmentWorkflow = createWorkflow(
|
||||
@@ -20,6 +20,8 @@ export const createShipmentWorkflow = createWorkflow(
|
||||
shipped_at: new Date(),
|
||||
}))
|
||||
|
||||
updateFulfillmentWorkflowStep(update)
|
||||
updateFulfillmentWorkflow.runAsStep({
|
||||
input: update,
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IOrderModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type CompleteOrdersStepInput = {
|
||||
orderIds: string[]
|
||||
}
|
||||
|
||||
export const cancelOrdersStepId = "cancel-orders"
|
||||
export const cancelOrdersStep = createStep(
|
||||
cancelOrdersStepId,
|
||||
async (data: CompleteOrdersStepInput, { container }) => {
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
const orders = await service.list(
|
||||
{
|
||||
id: data.orderIds,
|
||||
},
|
||||
{
|
||||
select: ["id", "status"],
|
||||
}
|
||||
)
|
||||
|
||||
const canceled = await service.cancel(data.orderIds)
|
||||
return new StepResponse(
|
||||
canceled,
|
||||
canceled.map((order) => {
|
||||
const prevData = orders.find((o) => o.id === order.id)!
|
||||
return {
|
||||
id: order.id,
|
||||
status: prevData.status,
|
||||
canceled_at: null,
|
||||
}
|
||||
})
|
||||
)
|
||||
},
|
||||
async (canceled, { container }) => {
|
||||
if (!canceled?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
await service.update(canceled)
|
||||
}
|
||||
)
|
||||
@@ -14,13 +14,23 @@ export const completeOrdersStep = createStep(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
const orders = await service.list(
|
||||
{
|
||||
id: data.orderIds,
|
||||
},
|
||||
{
|
||||
select: ["id", "status"],
|
||||
}
|
||||
)
|
||||
|
||||
const completed = await service.completeOrder(data.orderIds)
|
||||
return new StepResponse(
|
||||
completed,
|
||||
completed.map((store) => {
|
||||
completed.map((order) => {
|
||||
const prevData = orders.find((o) => o.id === order.id)!
|
||||
return {
|
||||
id: store.id,
|
||||
status: store.status,
|
||||
id: order.id,
|
||||
status: prevData.status,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from "./archive-orders"
|
||||
export * from "./cancel-orders"
|
||||
export * from "./complete-orders"
|
||||
export * from "./create-orders"
|
||||
export * from "./get-item-tax-lines"
|
||||
|
||||
@@ -5,7 +5,7 @@ export function throwIfOrderIsCancelled({ order }: { order: OrderDTO }) {
|
||||
if (order.status === OrderStatus.CANCELED) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Order with id ${order.id} has been cancelled.`
|
||||
`Order with id ${order.id} has been canceled.`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
import {
|
||||
FulfillmentDTO,
|
||||
OrderDTO,
|
||||
OrderWorkflow,
|
||||
PaymentCollectionDTO,
|
||||
} from "@medusajs/types"
|
||||
import { MedusaError, deepFlatMap } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
import { cancelPaymentStep } from "../../payment/steps"
|
||||
import { deleteReservationsByLineItemsStep } from "../../reservation/steps"
|
||||
import { cancelOrdersStep } from "../steps/cancel-orders"
|
||||
import { throwIfOrderIsCancelled } from "../utils/order-validation"
|
||||
|
||||
const validateOrder = createStep(
|
||||
"validate-order",
|
||||
({
|
||||
order,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
input: OrderWorkflow.CancelOrderWorkflowInput
|
||||
}) => {
|
||||
const order_ = order as OrderDTO & {
|
||||
payment_collections: PaymentCollectionDTO[]
|
||||
fulfillments: FulfillmentDTO[]
|
||||
}
|
||||
|
||||
throwIfOrderIsCancelled({ order })
|
||||
|
||||
let refunds = 0
|
||||
let captures = 0
|
||||
|
||||
deepFlatMap(order_, "payment_collections.payments", ({ payments }) => {
|
||||
refunds += payments?.refunds?.length ?? 0
|
||||
captures += payments?.captures?.length ?? 0
|
||||
})
|
||||
|
||||
if (captures > 0) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
"Order with payment capture(s) cannot be canceled"
|
||||
)
|
||||
}
|
||||
|
||||
if (refunds > 0) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
"Order with payment refund(s) cannot be canceled"
|
||||
)
|
||||
}
|
||||
|
||||
const throwErrorIf = (
|
||||
arr: unknown[],
|
||||
pred: (obj: any) => boolean,
|
||||
type: string
|
||||
) => {
|
||||
if (arr?.filter(pred).length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
`All ${type} must be canceled before canceling an order`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const notCanceled = (o) => !o.canceled_at
|
||||
|
||||
throwErrorIf(order_.fulfillments, notCanceled, "fulfillments")
|
||||
/*
|
||||
TODO: relationship between order and returns, swaps, claims
|
||||
|
||||
throwErrorIf(
|
||||
order_.returns,
|
||||
(ret) => ret.status !== "canceled",
|
||||
"returns"
|
||||
)
|
||||
throwErrorIf(order_.swaps, notCanceled, "swaps")
|
||||
throwErrorIf(order_.claims, notCanceled, "claims")
|
||||
*/
|
||||
}
|
||||
)
|
||||
|
||||
export const cancelOrderWorkflowId = "cancel-order"
|
||||
export const cancelOrderWorkflow = createWorkflow(
|
||||
cancelOrderWorkflowId,
|
||||
(
|
||||
input: WorkflowData<OrderWorkflow.CancelOrderWorkflowInput>
|
||||
): WorkflowData<void> => {
|
||||
const order: OrderDTO & { fulfillments: FulfillmentDTO[] } =
|
||||
useRemoteQueryStep({
|
||||
entry_point: "orders",
|
||||
fields: [
|
||||
"id",
|
||||
"status",
|
||||
"items.id",
|
||||
"fulfillments.canceled_at",
|
||||
"payment_collections.payments.id",
|
||||
"payment_collections.payments.refunds.id",
|
||||
"payment_collections.payments.captures.id",
|
||||
],
|
||||
variables: { id: input.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
})
|
||||
|
||||
validateOrder({ order, input })
|
||||
|
||||
const lineItemIds = transform({ order }, ({ order }) => {
|
||||
return order.items?.map((i) => i.id)
|
||||
})
|
||||
deleteReservationsByLineItemsStep(lineItemIds)
|
||||
|
||||
const paymentIds = transform({ order }, ({ order }) => {
|
||||
return deepFlatMap(
|
||||
order,
|
||||
"payment_collections.payments",
|
||||
({ payments }) => {
|
||||
return payments?.id
|
||||
}
|
||||
)
|
||||
})
|
||||
cancelPaymentStep({ paymentIds })
|
||||
|
||||
cancelOrdersStep({ orderIds: [order.id] })
|
||||
}
|
||||
)
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from "./archive-orders"
|
||||
export * from "./cancel-order"
|
||||
export * from "./cancel-order-fulfillment"
|
||||
export * from "./complete-orders"
|
||||
export * from "./create-fulfillment"
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPaymentModuleService, Logger } from "@medusajs/types"
|
||||
import { ContainerRegistrationKeys, promiseAll } from "@medusajs/utils"
|
||||
import { createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type StepInput = {
|
||||
paymentIds: string | string[]
|
||||
}
|
||||
|
||||
export const cancelPaymentStepId = "cancel-payment-step"
|
||||
export const cancelPaymentStep = createStep(
|
||||
cancelPaymentStepId,
|
||||
async (input: StepInput, { container }) => {
|
||||
const logger = container.resolve<Logger>(ContainerRegistrationKeys.LOGGER)
|
||||
const paymentModule = container.resolve<IPaymentModuleService>(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const paymentIds = Array.isArray(input.paymentIds)
|
||||
? input.paymentIds
|
||||
: [input.paymentIds]
|
||||
|
||||
const promises: Promise<any>[] = []
|
||||
for (const id of paymentIds) {
|
||||
promises.push(
|
||||
paymentModule.cancelPayment(id).catch((e) => {
|
||||
logger.error(
|
||||
`Error was thrown trying to cancel payment - ${id} - ${e}`
|
||||
)
|
||||
})
|
||||
)
|
||||
}
|
||||
await promiseAll(promises)
|
||||
}
|
||||
)
|
||||
@@ -1,3 +1,3 @@
|
||||
export * from "./cancel-payment"
|
||||
export * from "./capture-payment"
|
||||
export * from "./refund-payment"
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IInventoryServiceNext } from "@medusajs/types"
|
||||
|
||||
export const deleteReservationsByLineItemsStepId =
|
||||
"delete-reservations-by-line-items"
|
||||
export const deleteReservationsByLineItemsStep = createStep(
|
||||
deleteReservationsByLineItemsStepId,
|
||||
async (ids: string[], { container }) => {
|
||||
const service = container.resolve<IInventoryServiceNext>(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
await service.deleteReservationItemsByLineItem(ids)
|
||||
|
||||
return new StepResponse(void 0, ids)
|
||||
},
|
||||
async (prevIds, { container }) => {
|
||||
if (!prevIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IInventoryServiceNext>(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
await service.restoreReservationItemsByLineItem(prevIds)
|
||||
}
|
||||
)
|
||||
@@ -1,7 +1,7 @@
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { IInventoryServiceNext } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IInventoryServiceNext } from "@medusajs/types"
|
||||
|
||||
export const deleteReservationsStepId = "delete-reservations"
|
||||
export const deleteReservationsStep = createStep(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./create-reservations"
|
||||
export * from "./delete-reservations"
|
||||
export * from "./delete-reservations-by-line-items"
|
||||
export * from "./update-reservations"
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { deleteReservationsByLineItemsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
export const deleteReservationsByLineItemsWorkflowId =
|
||||
"delete-reservations-by-line-items"
|
||||
export const deleteReservationsByLineItemsWorkflow = createWorkflow(
|
||||
deleteReservationsByLineItemsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
return deleteReservationsByLineItemsStep(input.ids)
|
||||
}
|
||||
)
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./create-reservations"
|
||||
export * from "./delete-reservations"
|
||||
export * from "./delete-reservations-by-line-items"
|
||||
export * from "./update-reservations"
|
||||
|
||||
@@ -280,7 +280,7 @@ describe("Medusa Modules", () => {
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
expect(moduleC).rejects.toThrow(
|
||||
await expect(moduleC).rejects.toThrow(
|
||||
"Module moduleKey already have a 'main' registered."
|
||||
)
|
||||
})
|
||||
@@ -314,7 +314,7 @@ describe("Medusa Modules", () => {
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
expect(moduleC).rejects.toThrow(
|
||||
await expect(moduleC).rejects.toThrow(
|
||||
"Module moduleKey already registed as 'module_alias'. Please choose a different alias."
|
||||
)
|
||||
})
|
||||
|
||||
@@ -110,7 +110,7 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
expect(
|
||||
await expect(
|
||||
moduleLoader({ container, moduleResolutions, logger })
|
||||
).rejects.toThrow("Loaders for module TestService failed: loader")
|
||||
})
|
||||
@@ -136,7 +136,7 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
expect(
|
||||
await expect(
|
||||
moduleLoader({ container, moduleResolutions, logger })
|
||||
).rejects.toThrow(
|
||||
"No service found in module TestService. Make sure your module exports a service."
|
||||
@@ -165,7 +165,7 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
expect(
|
||||
await expect(
|
||||
moduleLoader({ container, moduleResolutions, logger })
|
||||
).rejects.toThrow(
|
||||
"No service found in module TestService. Make sure your module exports a service."
|
||||
|
||||
@@ -793,7 +793,7 @@ describe("RemoteJoiner", () => {
|
||||
fields: ["id", "name", "email"],
|
||||
}
|
||||
|
||||
expect(newJoiner.query(queryWithAlias)).rejects.toThrowError(
|
||||
await expect(newJoiner.query(queryWithAlias)).rejects.toThrowError(
|
||||
`Service with alias "user" was not found.`
|
||||
)
|
||||
})
|
||||
@@ -825,6 +825,8 @@ describe("RemoteJoiner", () => {
|
||||
throwIfKeyNotFound: true,
|
||||
})
|
||||
|
||||
expect(dataNotFound).rejects.toThrowError("order id not found: ord_1234556")
|
||||
await expect(dataNotFound).rejects.toThrowError(
|
||||
"order id not found: ord_1234556"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -733,6 +733,33 @@ export interface IInventoryServiceNext extends IModuleService {
|
||||
context?: Context
|
||||
): Promise<void>
|
||||
|
||||
/**
|
||||
* This method is used to restore the reservation items associated with a line item or multiple line items that were deleted.
|
||||
*
|
||||
* @param {string | string[]} lineItemId - The ID(s) of the line item(s).
|
||||
* @param {SharedContext} context - A context used to share re9sources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<void>} Resolves when the reservation items are successfully deleted.
|
||||
*
|
||||
* @example
|
||||
* import {
|
||||
* initialize as initializeInventoryModule,
|
||||
* } from "@medusajs/inventory"
|
||||
*
|
||||
* async function restoreReservationItemsByLineItem (
|
||||
* lineItemIds: string[]
|
||||
* ) {
|
||||
* const inventoryModule = await initializeInventoryModule({})
|
||||
*
|
||||
* await inventoryModule.restoreReservationItemsByLineItem(
|
||||
* lineItemIds
|
||||
* )
|
||||
* }
|
||||
*/
|
||||
restoreReservationItemsByLineItem(
|
||||
lineItemId: string | string[],
|
||||
context?: Context
|
||||
): Promise<void>
|
||||
|
||||
/**
|
||||
* This method deletes reservation items by their IDs.
|
||||
*
|
||||
|
||||
@@ -875,6 +875,10 @@ export interface OrderDTO {
|
||||
* Holds custom data in key-value pairs.
|
||||
*/
|
||||
metadata?: Record<string, unknown> | null
|
||||
/**
|
||||
* When the order was canceled.
|
||||
*/
|
||||
canceled_at?: string | Date
|
||||
/**
|
||||
* When the order was created.
|
||||
*/
|
||||
|
||||
@@ -395,6 +395,11 @@ export interface CreateOrderReturnDTO extends BaseOrderBundledActionsDTO {
|
||||
shipping_method: Omit<CreateOrderShippingMethodDTO, "order_id"> | string
|
||||
}
|
||||
|
||||
export interface CancelOrderReturnDTO {
|
||||
order_id: string
|
||||
return_id: string
|
||||
}
|
||||
|
||||
export interface ReceiveOrderReturnDTO extends BaseOrderBundledActionsDTO {}
|
||||
|
||||
/** ORDER bundled action flows */
|
||||
|
||||
@@ -1491,6 +1491,9 @@ export interface IOrderModuleService extends IModuleService {
|
||||
completeOrder(orderId: string[], sharedContext?: Context): Promise<OrderDTO[]>
|
||||
completeOrder(orderId: string, sharedContext?: Context): Promise<OrderDTO>
|
||||
|
||||
cancel(orderId: string[], sharedContext?: Context): Promise<OrderDTO[]>
|
||||
cancel(orderId: string, sharedContext?: Context): Promise<OrderDTO>
|
||||
|
||||
// Bundled flows
|
||||
registerFulfillment(
|
||||
data: RegisterOrderFulfillmentDTO,
|
||||
@@ -1512,6 +1515,13 @@ export interface IOrderModuleService extends IModuleService {
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
|
||||
/*
|
||||
cancelReturn(
|
||||
returnData: CancelOrderReturnDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
*/
|
||||
|
||||
receiveReturn(
|
||||
returnData: ReceiveOrderReturnDTO,
|
||||
sharedContext?: Context
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface CancelOrderWorkflowInput {
|
||||
order_id: string
|
||||
no_notification?: boolean
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from "./cancel-fulfillment"
|
||||
export * from "./cancel-order"
|
||||
export * from "./create-fulfillment"
|
||||
export * from "./create-return-order"
|
||||
export * from "./create-shipment"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { cancelOrderWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
@@ -15,7 +16,13 @@ export const GET = async (
|
||||
|
||||
const variables = { id: req.params.id }
|
||||
|
||||
// TODO: cancel order - v1.x - packages/medusa/src/api/routes/admin/orders/cancel-order.ts
|
||||
const input = {
|
||||
order_id: req.params.id,
|
||||
}
|
||||
|
||||
await cancelOrderWorkflow(req.scope).run({
|
||||
input,
|
||||
})
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "order",
|
||||
|
||||
+1
-6
@@ -22,15 +22,10 @@ export const POST = async (
|
||||
order_id: req.params.id,
|
||||
}
|
||||
|
||||
const { errors } = await cancelOrderFulfillmentWorkflow(req.scope).run({
|
||||
await cancelOrderFulfillmentWorkflow(req.scope).run({
|
||||
input,
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "order",
|
||||
variables,
|
||||
|
||||
+1
-6
@@ -24,15 +24,10 @@ export const POST = async (
|
||||
labels: req.validatedBody.labels ?? [],
|
||||
}
|
||||
|
||||
const { errors } = await createOrderShipmentWorkflow(req.scope).run({
|
||||
await createOrderShipmentWorkflow(req.scope).run({
|
||||
input,
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "order",
|
||||
variables,
|
||||
|
||||
@@ -22,15 +22,10 @@ export const POST = async (
|
||||
order_id: req.params.id,
|
||||
}
|
||||
|
||||
const { errors } = await createOrderFulfillmentWorkflow(req.scope).run({
|
||||
await createOrderFulfillmentWorkflow(req.scope).run({
|
||||
input,
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "order",
|
||||
variables,
|
||||
|
||||
+1
-1
@@ -13,6 +13,6 @@ export default async function orderNotifier({
|
||||
}
|
||||
|
||||
export const config: SubscriberConfig = {
|
||||
event: ["order.placed", "order.cancelled", "order.completed"],
|
||||
event: ["order.placed", "order.canceled", "order.completed"],
|
||||
context: { subscriberId: "order-notifier" },
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ describe("SubscriberLoader", () => {
|
||||
)
|
||||
|
||||
expect(eventBusServiceMock.subscribe).toHaveBeenCalledWith(
|
||||
"order.cancelled",
|
||||
"order.canceled",
|
||||
expect.any(Function),
|
||||
{
|
||||
subscriberId: "order-notifier",
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import crypto from "crypto"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IApiKeyModuleService } from "@medusajs/types"
|
||||
import { ApiKeyType } from "@medusajs/utils"
|
||||
import crypto from "crypto"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import {
|
||||
createSecretKeyFixture,
|
||||
createPublishableKeyFixture,
|
||||
createSecretKeyFixture,
|
||||
} from "../__fixtures__"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
@@ -88,7 +88,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should only allow creating one active token", async function () {
|
||||
expect(
|
||||
await expect(
|
||||
service.create([createSecretKeyFixture, createSecretKeyFixture])
|
||||
).rejects.toThrow(
|
||||
"You can only create one secret key at a time. You tried to create 2 secret keys."
|
||||
|
||||
+21
-2
@@ -96,7 +96,7 @@ moduleIntegrationTestRunner({
|
||||
quantity: 3,
|
||||
})
|
||||
|
||||
expect(reserveMoreThanInStock).rejects.toThrow(
|
||||
await expect(reserveMoreThanInStock).rejects.toThrow(
|
||||
`Not enough stock available for item ${inventoryItem.id} at location location-1`
|
||||
)
|
||||
|
||||
@@ -415,7 +415,7 @@ moduleIntegrationTestRunner({
|
||||
])
|
||||
})
|
||||
|
||||
it("deleted reseravation items by line item", async () => {
|
||||
it("deleted reseravation items by line item and restore them", async () => {
|
||||
const reservationsPreDeleted = await service.listReservationItems({
|
||||
line_item_id: "line-item-id",
|
||||
})
|
||||
@@ -440,6 +440,25 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
expect(reservationsPostDeleted).toEqual([])
|
||||
|
||||
await service.restoreReservationItemsByLineItem("line-item-id")
|
||||
|
||||
const reservationsPostRestored = await service.listReservationItems({
|
||||
line_item_id: "line-item-id",
|
||||
})
|
||||
|
||||
expect(reservationsPostRestored).toEqual([
|
||||
expect.objectContaining({
|
||||
location_id: "location-1",
|
||||
quantity: 2,
|
||||
line_item_id: "line-item-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
location_id: "location-1",
|
||||
quantity: 2,
|
||||
line_item_id: "line-item-id",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("adjusts inventory levels accordingly when removing reservations by line item", async () => {
|
||||
|
||||
@@ -1,17 +1,8 @@
|
||||
import {
|
||||
Context,
|
||||
CreateInventoryLevelInput,
|
||||
DAL,
|
||||
SharedContext,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
} from "@medusajs/utils"
|
||||
import { Context } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
|
||||
import { InventoryLevel } from "../models/inventory-level"
|
||||
import { InventoryLevelRepository } from "@repositories"
|
||||
import { InventoryLevel } from "../models/inventory-level"
|
||||
|
||||
type InjectedDependencies = {
|
||||
inventoryLevelRepository: InventoryLevelRepository
|
||||
|
||||
@@ -15,11 +15,11 @@ import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
InventoryEvents,
|
||||
isDefined,
|
||||
isString,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
isDefined,
|
||||
isString,
|
||||
partitionArray,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
@@ -781,6 +781,43 @@ export default class InventoryModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes reservation items by line item
|
||||
* @param lineItemId - the id of the line item associated with the reservation item
|
||||
* @param context
|
||||
*/
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
async restoreReservationItemsByLineItem(
|
||||
lineItemId: string | string[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<void> {
|
||||
const reservations: InventoryNext.ReservationItemDTO[] =
|
||||
await this.listReservationItems({ line_item_id: lineItemId }, {}, context)
|
||||
|
||||
await this.reservationItemService_.restore(
|
||||
{ line_item_id: lineItemId },
|
||||
context
|
||||
)
|
||||
|
||||
await this.adjustInventoryLevelsForReservationsRestore(
|
||||
reservations,
|
||||
context
|
||||
)
|
||||
|
||||
context.messageAggregator?.saveRawMessageData(
|
||||
reservations.map((reservationItem) => ({
|
||||
eventName: InventoryEvents.reservation_item_created,
|
||||
service: this.constructor.name,
|
||||
action: CommonEvents.CREATED,
|
||||
object: "reservation-item",
|
||||
context,
|
||||
data: { id: reservationItem.id },
|
||||
}))
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjusts the inventory level for a given inventory item and location.
|
||||
* @param inventoryItemId - the id of the inventory item
|
||||
@@ -1040,6 +1077,30 @@ export default class InventoryModuleService<
|
||||
reservations: ReservationItemDTO[],
|
||||
context: Context
|
||||
): Promise<void> {
|
||||
await this.adjustInventoryLevelsForReservations_(
|
||||
reservations,
|
||||
true,
|
||||
context
|
||||
)
|
||||
}
|
||||
|
||||
private async adjustInventoryLevelsForReservationsRestore(
|
||||
reservations: ReservationItemDTO[],
|
||||
context: Context
|
||||
): Promise<void> {
|
||||
await this.adjustInventoryLevelsForReservations_(
|
||||
reservations,
|
||||
false,
|
||||
context
|
||||
)
|
||||
}
|
||||
|
||||
private async adjustInventoryLevelsForReservations_(
|
||||
reservations: ReservationItemDTO[],
|
||||
isDelete: boolean,
|
||||
context: Context
|
||||
): Promise<void> {
|
||||
const multiplier = isDelete ? -1 : 1
|
||||
const inventoryLevels = await this.ensureInventoryLevels(
|
||||
reservations.map((r) => ({
|
||||
inventory_item_id: r.inventory_item_id,
|
||||
@@ -1055,8 +1116,8 @@ export default class InventoryModuleService<
|
||||
const inventoryLevelMap = acc.get(curr.inventory_item_id) ?? new Map()
|
||||
|
||||
const adjustment = inventoryLevelMap.has(curr.location_id)
|
||||
? inventoryLevelMap.get(curr.location_id) - curr.quantity
|
||||
: -curr.quantity
|
||||
? inventoryLevelMap.get(curr.location_id) + curr.quantity * multiplier
|
||||
: curr.quantity * multiplier
|
||||
|
||||
inventoryLevelMap.set(curr.location_id, adjustment)
|
||||
acc.set(curr.inventory_item_id, inventoryLevelMap)
|
||||
|
||||
@@ -410,7 +410,9 @@ moduleIntegrationTestRunner({
|
||||
confirmed_by: "cx_agent_123",
|
||||
})
|
||||
|
||||
expect(service.confirmOrderChange(orderChange.id)).rejects.toThrowError(
|
||||
await expect(
|
||||
service.confirmOrderChange(orderChange.id)
|
||||
).rejects.toThrowError(
|
||||
`Order Change cannot be modified: ${orderChange.id}`
|
||||
)
|
||||
|
||||
@@ -579,9 +581,9 @@ moduleIntegrationTestRunner({
|
||||
canceled_by: "cx_agent_123",
|
||||
})
|
||||
|
||||
expect(service.cancelOrderChange(orderChange.id)).rejects.toThrowError(
|
||||
"Order Change cannot be modified"
|
||||
)
|
||||
await expect(
|
||||
service.cancelOrderChange(orderChange.id)
|
||||
).rejects.toThrowError("Order Change cannot be modified")
|
||||
|
||||
await service.declineOrderChange({
|
||||
id: orderChange2.id,
|
||||
@@ -589,7 +591,7 @@ moduleIntegrationTestRunner({
|
||||
declined_reason: "changed my mind",
|
||||
})
|
||||
|
||||
expect(
|
||||
await expect(
|
||||
service.declineOrderChange(orderChange2.id)
|
||||
).rejects.toThrowError("Order Change cannot be modified")
|
||||
|
||||
|
||||
@@ -2753,10 +2753,55 @@ export default class OrderModuleService<
|
||||
}
|
||||
|
||||
await this.orderService_.update(
|
||||
orderIds.map((id) => {
|
||||
return {
|
||||
id,
|
||||
status: OrderStatus.COMPLETED,
|
||||
}
|
||||
}),
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return Array.isArray(orderId) ? orders : orders[0]
|
||||
}
|
||||
|
||||
async cancel(
|
||||
orderId: string,
|
||||
sharedContext?: Context
|
||||
): Promise<OrderTypes.OrderDTO>
|
||||
async cancel(
|
||||
orderId: string[],
|
||||
sharedContext?: Context
|
||||
): Promise<OrderTypes.OrderDTO[]>
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async cancel(
|
||||
orderId: string | string[],
|
||||
sharedContext?: Context
|
||||
): Promise<OrderTypes.OrderDTO | OrderTypes.OrderDTO[]> {
|
||||
const orderIds = Array.isArray(orderId) ? orderId : [orderId]
|
||||
const orders = await this.list(
|
||||
{
|
||||
id: orderIds,
|
||||
status: OrderStatus.COMPLETED,
|
||||
},
|
||||
{},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const canceled_at = new Date()
|
||||
for (const order of orders) {
|
||||
order.status = OrderStatus.CANCELED
|
||||
order.canceled_at = canceled_at
|
||||
}
|
||||
|
||||
await this.orderService_.update(
|
||||
orderIds.map((id) => {
|
||||
return {
|
||||
id,
|
||||
status: OrderStatus.CANCELED,
|
||||
canceled_at,
|
||||
}
|
||||
}),
|
||||
sharedContext
|
||||
)
|
||||
|
||||
|
||||
+2
-2
@@ -299,7 +299,7 @@ moduleIntegrationTestRunner({
|
||||
{}
|
||||
)
|
||||
|
||||
expect(result).rejects.toThrow(
|
||||
await expect(result).rejects.toThrow(
|
||||
"Method calculatePrices requires currency_code in the pricing context"
|
||||
)
|
||||
|
||||
@@ -308,7 +308,7 @@ moduleIntegrationTestRunner({
|
||||
{ context: { region_id: "DE" } }
|
||||
)
|
||||
|
||||
expect(result).rejects.toThrow(
|
||||
await expect(result).rejects.toThrow(
|
||||
"Method calculatePrices requires currency_code in the pricing context"
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user