feat(order): fulfillment workflow (#7385)
FIXES: CORE-2162 CORE-2167 CORE-2041
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { FulfillmentTypes, IFulfillmentModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const createReturnFulfillmentStepId = "create-return-fulfillment"
|
||||
export const createReturnFulfillmentStep = createStep(
|
||||
createReturnFulfillmentStepId,
|
||||
async (data: FulfillmentTypes.CreateFulfillmentDTO, { container }) => {
|
||||
const service = container.resolve<IFulfillmentModuleService>(
|
||||
ModuleRegistrationName.FULFILLMENT
|
||||
)
|
||||
|
||||
const fulfillment = await service.createReturnFulfillment(data)
|
||||
|
||||
return new StepResponse(fulfillment, fulfillment.id)
|
||||
},
|
||||
async (id, { container }) => {
|
||||
if (!id) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IFulfillmentModuleService>(
|
||||
ModuleRegistrationName.FULFILLMENT
|
||||
)
|
||||
|
||||
// await service.cancelReturnFulfillment(id) // TODO: Implement cancelReturnFulfillment
|
||||
await service.cancelFulfillment(id)
|
||||
}
|
||||
)
|
||||
@@ -1,14 +1,15 @@
|
||||
export * from "./create-shipping-option-rules"
|
||||
export * from "./add-shipping-options-prices"
|
||||
export * from "./cancel-fulfillment"
|
||||
export * from "./create-fulfillment"
|
||||
export * from "./create-fulfillment-set"
|
||||
export * from "./create-return-fulfillment"
|
||||
export * from "./create-service-zones"
|
||||
export * from "./create-shipping-option-rules"
|
||||
export * from "./create-shipping-profiles"
|
||||
export * from "./delete-fulfillment-sets"
|
||||
export * from "./delete-service-zones"
|
||||
export * from "./delete-shipping-options"
|
||||
export * from "./delete-shipping-option-rules"
|
||||
export * from "./delete-shipping-options"
|
||||
export * from "./set-shipping-options-prices"
|
||||
export * from "./update-fulfillment"
|
||||
export * from "./upsert-shipping-options"
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
import { FulfillmentDTO, FulfillmentWorkflow } from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createFulfillmentStep } from "../steps"
|
||||
import { Modules } from "@medusajs/utils"
|
||||
import { createLinkStep } from "../../common"
|
||||
|
||||
export const createFulfillmentWorkflowId = "create-fulfillment-workflow"
|
||||
export const createFulfillmentWorkflow = createWorkflow(
|
||||
@@ -16,20 +10,6 @@ export const createFulfillmentWorkflow = createWorkflow(
|
||||
): WorkflowData<FulfillmentDTO> => {
|
||||
const fulfillment = createFulfillmentStep(input)
|
||||
|
||||
const link = transform(
|
||||
{ order_id: input.order_id, fulfillment },
|
||||
(data) => {
|
||||
return [
|
||||
{
|
||||
[Modules.ORDER]: { order_id: data.order_id },
|
||||
[Modules.FULFILLMENT]: { fulfillment_id: data.fulfillment.id },
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
createLinkStep(link)
|
||||
|
||||
return fulfillment
|
||||
}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { FulfillmentDTO, FulfillmentWorkflow } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createReturnFulfillmentStep } from "../steps"
|
||||
|
||||
export const createReturnFulfillmentWorkflowId =
|
||||
"create-return-fulfillment-workflow"
|
||||
export const createReturnFulfillmentWorkflow = createWorkflow(
|
||||
createReturnFulfillmentWorkflowId,
|
||||
(
|
||||
input: WorkflowData<FulfillmentWorkflow.CreateFulfillmentWorkflowInput>
|
||||
): WorkflowData<FulfillmentDTO> => {
|
||||
const fulfillment = createReturnFulfillmentStep(input)
|
||||
|
||||
return fulfillment
|
||||
}
|
||||
)
|
||||
@@ -1,6 +1,7 @@
|
||||
export * from "./batch-shipping-option-rules"
|
||||
export * from "./cancel-fulfillment"
|
||||
export * from "./create-fulfillment"
|
||||
export * from "./create-return-fulfillment"
|
||||
export * from "./create-service-zones"
|
||||
export * from "./create-shipment"
|
||||
export * from "./create-shipping-options"
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { IInventoryServiceNext, InventoryNext } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
|
||||
export const adjustInventoryLevelsStepId = "adjust-inventory-levels-step"
|
||||
export const adjustInventoryLevelsStep = createStep(
|
||||
adjustInventoryLevelsStepId,
|
||||
async (
|
||||
input: InventoryNext.BulkAdjustInventoryLevelInput[],
|
||||
{ container }
|
||||
) => {
|
||||
const inventoryService: IInventoryServiceNext = container.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
const adjustedLevels: InventoryNext.InventoryLevelDTO[] =
|
||||
await inventoryService.adjustInventory(
|
||||
input.map((item) => {
|
||||
return {
|
||||
inventoryItemId: item.inventory_item_id,
|
||||
locationId: item.location_id,
|
||||
adjustment: item.adjustment,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return new StepResponse(
|
||||
adjustedLevels,
|
||||
input.map((item) => {
|
||||
return {
|
||||
...item,
|
||||
adjustment: item.adjustment * -1,
|
||||
}
|
||||
})
|
||||
)
|
||||
},
|
||||
async (adjustedLevels, { container }) => {
|
||||
if (!adjustedLevels) {
|
||||
return
|
||||
}
|
||||
|
||||
const inventoryService = container.resolve(ModuleRegistrationName.INVENTORY)
|
||||
|
||||
await inventoryService.adjustInventory(adjustedLevels)
|
||||
}
|
||||
)
|
||||
@@ -1,10 +1,11 @@
|
||||
export * from "./delete-inventory-items"
|
||||
export * from "./adjust-inventory-levels"
|
||||
export * from "./attach-inventory-items"
|
||||
export * from "./create-inventory-items"
|
||||
export * from "./validate-singular-inventory-items-for-tags"
|
||||
export * from "./create-inventory-levels"
|
||||
export * from "./validate-inventory-locations"
|
||||
export * from "./update-inventory-items"
|
||||
export * from "./delete-inventory-items"
|
||||
export * from "./delete-inventory-levels"
|
||||
export * from "./update-inventory-levels"
|
||||
export * from "./delete-levels-by-item-and-location"
|
||||
export * from "./update-inventory-items"
|
||||
export * from "./update-inventory-levels"
|
||||
export * from "./validate-inventory-locations"
|
||||
export * from "./validate-singular-inventory-items-for-tags"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { CreateOrderReturnDTO, IOrderModuleService } from "@medusajs/types"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type CreateReturnStepInput = CreateOrderReturnDTO
|
||||
|
||||
@@ -12,11 +12,11 @@ export const createReturnStep = createStep(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
const created = await service.createReturn(data)
|
||||
return new StepResponse(created, created)
|
||||
await service.createReturn(data)
|
||||
return new StepResponse(void 0, data.order_id)
|
||||
},
|
||||
async (createdId, { container }) => {
|
||||
if (!createdId) {
|
||||
async (orderId, { container }) => {
|
||||
if (!orderId) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -24,6 +24,6 @@ export const createReturnStep = createStep(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
// TODO: delete return
|
||||
await service.revertLastVersion(orderId)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -3,5 +3,7 @@ export * from "./complete-orders"
|
||||
export * from "./create-orders"
|
||||
export * from "./get-item-tax-lines"
|
||||
export * from "./link-order-payment-collection"
|
||||
export * from "./register-fulfillment"
|
||||
export * from "./register-shipment"
|
||||
export * from "./set-tax-lines-for-items"
|
||||
export * from "./update-tax-lines"
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
IOrderModuleService,
|
||||
RegisterOrderFulfillmentDTO,
|
||||
} from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type RegisterOrderFulfillmentStepInput = RegisterOrderFulfillmentDTO
|
||||
|
||||
export const registerOrderFulfillmentStepId = "register-order-fullfillment"
|
||||
export const registerOrderFulfillmentStep = createStep(
|
||||
registerOrderFulfillmentStepId,
|
||||
async (data: RegisterOrderFulfillmentStepInput, { container }) => {
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
await service.registerFulfillment(data)
|
||||
return new StepResponse(void 0, data.order_id)
|
||||
},
|
||||
async (orderId, { container }) => {
|
||||
if (!orderId) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
await service.revertLastVersion(orderId)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IOrderModuleService, RegisterOrderShipmentDTO } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type RegisterOrderShipmentStepInput = RegisterOrderShipmentDTO
|
||||
|
||||
export const registerOrderShipmentStepId = "register-order-shipment"
|
||||
export const registerOrderShipmentStep = createStep(
|
||||
registerOrderShipmentStepId,
|
||||
async (data: RegisterOrderShipmentStepInput, { container }) => {
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
await service.registerShipment(data)
|
||||
return new StepResponse(void 0, data.order_id)
|
||||
},
|
||||
async (orderId, { container }) => {
|
||||
if (!orderId) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
await service.revertLastVersion(orderId)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,32 @@
|
||||
import { OrderDTO, OrderWorkflow } from "@medusajs/types"
|
||||
import { MedusaError, OrderStatus, arrayDifference } from "@medusajs/utils"
|
||||
|
||||
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.`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export function throwIfItemsDoesNotExistsInOrder({
|
||||
order,
|
||||
inputItems,
|
||||
}: {
|
||||
order: Pick<OrderDTO, "id" | "items">
|
||||
inputItems: OrderWorkflow.CreateOrderFulfillmentWorkflowInput["items"]
|
||||
}) {
|
||||
const orderItemIds = order.items?.map((i) => i.id) ?? []
|
||||
const inputItemIds = inputItems.map((i) => i.id)
|
||||
const diff = arrayDifference(inputItemIds, orderItemIds)
|
||||
|
||||
if (diff.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Items with ids ${diff.join(", ")} does not exist in order with id ${
|
||||
order.id
|
||||
}.`
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
FulfillmentDTO,
|
||||
FulfillmentWorkflow,
|
||||
OrderDTO,
|
||||
OrderWorkflow,
|
||||
} from "@medusajs/types"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createLinkStep, useRemoteQueryStep } from "../../common"
|
||||
import { createFulfillmentWorkflow } from "../../fulfillment"
|
||||
import { adjustInventoryLevelsStep } from "../../inventory"
|
||||
import {
|
||||
deleteReservationsStep,
|
||||
updateReservationsStep,
|
||||
} from "../../reservation"
|
||||
import { registerOrderFulfillmentStep } from "../steps"
|
||||
import {
|
||||
throwIfItemsDoesNotExistsInOrder,
|
||||
throwIfOrderIsCancelled,
|
||||
} from "../utils/order-validation"
|
||||
|
||||
const validateOrder = createStep(
|
||||
"validate-order",
|
||||
(
|
||||
{
|
||||
order,
|
||||
inputItems,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
inputItems: OrderWorkflow.CreateOrderFulfillmentWorkflowInput["items"]
|
||||
},
|
||||
context
|
||||
) => {
|
||||
throwIfOrderIsCancelled({ order })
|
||||
throwIfItemsDoesNotExistsInOrder({ order, inputItems })
|
||||
}
|
||||
)
|
||||
|
||||
function prepareRegisterOrderFulfillmentData({
|
||||
order,
|
||||
fulfillment,
|
||||
input,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
fulfillment: FulfillmentDTO
|
||||
input: OrderWorkflow.CreateOrderFulfillmentWorkflowInput
|
||||
}) {
|
||||
return {
|
||||
order_id: order.id,
|
||||
reference: Modules.FULFILLMENT,
|
||||
reference_id: fulfillment.id,
|
||||
created_by: input.created_by,
|
||||
items: order.items!.map((i) => {
|
||||
return {
|
||||
id: i.id,
|
||||
quantity: i.quantity,
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
function prepareFulfillmentData({
|
||||
order,
|
||||
input,
|
||||
shippingOption,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
input: OrderWorkflow.CreateOrderFulfillmentWorkflowInput
|
||||
shippingOption: {
|
||||
id: string
|
||||
provider_id: string
|
||||
service_zone: { fulfillment_set: { location?: { id: string } } }
|
||||
}
|
||||
}) {
|
||||
const inputItems = input.items
|
||||
const orderItemsMap = new Map<string, Required<OrderDTO>["items"][0]>(
|
||||
order.items!.map((i) => [i.id, i])
|
||||
)
|
||||
const fulfillmentItems = inputItems.map((i) => {
|
||||
const orderItem = orderItemsMap.get(i.id)!
|
||||
return {
|
||||
line_item_id: i.id,
|
||||
quantity: i.quantity,
|
||||
title: orderItem.variant_title ?? orderItem.title,
|
||||
sku: orderItem.variant_sku || "",
|
||||
barcode: orderItem.variant_barcode || "",
|
||||
} as FulfillmentWorkflow.CreateFulfillmentItemWorkflowDTO
|
||||
})
|
||||
|
||||
let locationId: string | undefined = input.location_id
|
||||
if (!locationId) {
|
||||
locationId = shippingOption.service_zone.fulfillment_set.location?.id
|
||||
}
|
||||
|
||||
if (!locationId) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Cannot create fulfillment without stock location, either provide a location or you should link the shipping option ${shippingOption.id} to a stock location.`
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
input: {
|
||||
location_id: locationId,
|
||||
provider_id: shippingOption.provider_id,
|
||||
shipping_option_id: shippingOption.id,
|
||||
items: fulfillmentItems,
|
||||
labels: [] as FulfillmentWorkflow.CreateFulfillmentLabelWorkflowDTO[], // TODO: shipping labels
|
||||
delivery_address: order.shipping_address ?? ({} as any),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function prepareInventoryReservations({ reservations, order, input }) {
|
||||
if (!reservations || !reservations.length) {
|
||||
throw new Error(
|
||||
`No stock reservation found for items ${input.items.map((i) => i.id)}`
|
||||
)
|
||||
}
|
||||
|
||||
const reservationMap = reservations.reduce((acc, reservation) => {
|
||||
acc[reservation.line_item_id as string] = reservation
|
||||
return acc
|
||||
}, {})
|
||||
|
||||
const inputItemsMap = input.items.reduce((acc, item) => {
|
||||
acc[item.id] = item
|
||||
return acc
|
||||
}, {})
|
||||
|
||||
const toDelete: string[] = []
|
||||
const toUpdate: {
|
||||
id: string
|
||||
quantity: number // TODO: BigNumberInput
|
||||
location_id: string
|
||||
}[] = []
|
||||
const inventoryAdjustment: {
|
||||
inventory_item_id: string
|
||||
location_id: string
|
||||
adjustment: number // TODO: BigNumberInput
|
||||
}[] = []
|
||||
|
||||
for (const item of order.items) {
|
||||
const reservation = reservationMap[item.id]
|
||||
const inputQuantity = inputItemsMap[item.id]?.quantity ?? item.quantity
|
||||
|
||||
const quantity = reservation.quantity - inputQuantity
|
||||
|
||||
inventoryAdjustment.push({
|
||||
inventory_item_id: reservation.inventory_item_id,
|
||||
location_id: input.location_id ?? reservation.location_id,
|
||||
adjustment: -item.quantity, // TODO: MathBN.mul(-1, item.quantity)
|
||||
})
|
||||
|
||||
if (quantity === 0) {
|
||||
toDelete.push(reservation.id)
|
||||
} else {
|
||||
toUpdate.push({
|
||||
id: reservation.id,
|
||||
quantity: quantity,
|
||||
location_id: input.location_id ?? reservation.location_id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
toDelete,
|
||||
toUpdate,
|
||||
inventoryAdjustment,
|
||||
}
|
||||
}
|
||||
|
||||
export const createOrderFulfillmentWorkflowId = "create-order-fulfillment"
|
||||
export const createOrderFulfillmentWorkflow = createWorkflow(
|
||||
createOrderFulfillmentWorkflowId,
|
||||
(
|
||||
input: WorkflowData<OrderWorkflow.CreateOrderFulfillmentWorkflowInput>
|
||||
): WorkflowData<FulfillmentDTO> => {
|
||||
const order: OrderDTO = useRemoteQueryStep({
|
||||
entry_point: "orders",
|
||||
fields: [
|
||||
"id",
|
||||
"status",
|
||||
"region_id",
|
||||
"currency_code",
|
||||
"items.*",
|
||||
"shipping_address.*",
|
||||
"shipping_methods.shipping_option_id", // TODO: which shipping method to use when multiple?
|
||||
],
|
||||
variables: { id: input.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
})
|
||||
|
||||
validateOrder({ order, inputItems: input.items })
|
||||
|
||||
const shippingOptionId = transform(order, (data) => {
|
||||
return data.shipping_methods?.[0]?.shipping_option_id
|
||||
})
|
||||
|
||||
const shippingOption = useRemoteQueryStep({
|
||||
entry_point: "shipping_options",
|
||||
fields: ["id", "provider_id", "service_zone.fulfillment_set.location.id"],
|
||||
variables: {
|
||||
id: shippingOptionId,
|
||||
},
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "get-shipping-option" })
|
||||
|
||||
const fulfillmentData = transform(
|
||||
{ order, input, shippingOption },
|
||||
prepareFulfillmentData
|
||||
)
|
||||
|
||||
const fulfillment = createFulfillmentWorkflow.runAsStep(fulfillmentData)
|
||||
|
||||
const registerOrderFulfillmentData = transform(
|
||||
{ order, fulfillment, input },
|
||||
prepareRegisterOrderFulfillmentData
|
||||
)
|
||||
|
||||
registerOrderFulfillmentStep(registerOrderFulfillmentData)
|
||||
|
||||
const link = transform(
|
||||
{ order_id: input.order_id, fulfillment },
|
||||
(data) => {
|
||||
return [
|
||||
{
|
||||
[Modules.ORDER]: { order_id: data.order_id },
|
||||
[Modules.FULFILLMENT]: { fulfillment_id: data.fulfillment.id },
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
createLinkStep(link)
|
||||
|
||||
const lineItemIds = transform({ order }, ({ order }) => {
|
||||
return order.items?.map((i) => i.id)
|
||||
})
|
||||
const reservations = useRemoteQueryStep({
|
||||
entry_point: "reservations",
|
||||
fields: [
|
||||
"id",
|
||||
"line_item_id",
|
||||
"quantity",
|
||||
"inventory_item_id",
|
||||
"location_id",
|
||||
],
|
||||
variables: {
|
||||
filter: {
|
||||
line_item_id: lineItemIds,
|
||||
},
|
||||
},
|
||||
}).config({ name: "get-reservations" })
|
||||
|
||||
const { toDelete, toUpdate, inventoryAdjustment } = transform(
|
||||
{ order, reservations, input },
|
||||
prepareInventoryReservations
|
||||
)
|
||||
|
||||
parallelize(
|
||||
updateReservationsStep(toUpdate),
|
||||
deleteReservationsStep(toDelete),
|
||||
adjustInventoryLevelsStep(inventoryAdjustment)
|
||||
)
|
||||
|
||||
// trigger event OrderModuleService.Events.FULFILLMENT_CREATED
|
||||
return fulfillment
|
||||
}
|
||||
)
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
CreateOrderShippingMethodDTO,
|
||||
FulfillmentWorkflow,
|
||||
@@ -7,55 +8,27 @@ import {
|
||||
WithCalculatedPrice,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MathBN,
|
||||
MedusaError,
|
||||
arrayDifference,
|
||||
isDefined,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
transform,
|
||||
WorkflowData,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createLinkStep, useRemoteQueryStep } from "../../common"
|
||||
import {
|
||||
arrayDifference,
|
||||
ContainerRegistrationKeys,
|
||||
isDefined,
|
||||
MathBN,
|
||||
MedusaError,
|
||||
Modules,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { createReturnFulfillmentWorkflow } from "../../fulfillment"
|
||||
import { updateOrderTaxLinesStep } from "../steps"
|
||||
import { createReturnStep } from "../steps/create-return"
|
||||
import { createFulfillmentWorkflow } from "../../fulfillment"
|
||||
|
||||
function throwIfOrderIsCancelled({ order }: { order: OrderDTO }) {
|
||||
// TODO: need work, check canceled
|
||||
if (false /*order.canceled_at*/) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Order with id ${order.id} has been cancelled.`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function throwIfItemsDoesNotExistsInOrder({
|
||||
order,
|
||||
inputItems,
|
||||
}: {
|
||||
order: Pick<OrderDTO, "id" | "items">
|
||||
inputItems: OrderWorkflow.CreateOrderReturnWorkflowInput["items"]
|
||||
}) {
|
||||
const orderItemIds = order.items?.map((i) => i.id) ?? []
|
||||
const inputItemIds = inputItems.map((i) => i.id)
|
||||
const diff = arrayDifference(inputItemIds, orderItemIds)
|
||||
|
||||
if (diff.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Items with ids ${diff.join(", ")} does not exist in order with id ${
|
||||
order.id
|
||||
}.`
|
||||
)
|
||||
}
|
||||
}
|
||||
import {
|
||||
throwIfItemsDoesNotExistsInOrder,
|
||||
throwIfOrderIsCancelled,
|
||||
} from "../utils/order-validation"
|
||||
|
||||
async function validateReturnReasons(
|
||||
{
|
||||
@@ -214,8 +187,7 @@ function prepareFulfillmentData({
|
||||
items: fulfillmentItems,
|
||||
labels: [] as FulfillmentWorkflow.CreateFulfillmentLabelWorkflowDTO[],
|
||||
delivery_address: order.shipping_address ?? ({} as any), // TODO: should it be the stock location address?
|
||||
order: {} as FulfillmentWorkflow.CreateFulfillmentOrderWorkflowDTO, // TODO see what todo here, is that even necessary?
|
||||
order_id: input.order_id,
|
||||
order: order,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -280,6 +252,7 @@ export const createReturnOrderWorkflow = createWorkflow(
|
||||
entry_point: "orders",
|
||||
fields: [
|
||||
"id",
|
||||
"status",
|
||||
"region_id",
|
||||
"currency_code",
|
||||
"total",
|
||||
@@ -339,8 +312,20 @@ export const createReturnOrderWorkflow = createWorkflow(
|
||||
prepareFulfillmentData
|
||||
)
|
||||
|
||||
createFulfillmentWorkflow.runAsStep(fulfillmentData)
|
||||
const returnFulfillment =
|
||||
createReturnFulfillmentWorkflow.runAsStep(fulfillmentData)
|
||||
|
||||
// TODO call the createReturn from the fulfillment provider
|
||||
const link = transform(
|
||||
{ order_id: input.order_id, fulfillment: returnFulfillment },
|
||||
(data) => {
|
||||
return [
|
||||
{
|
||||
[Modules.ORDER]: { order_id: data.order_id },
|
||||
[Modules.FULFILLMENT]: { fulfillment_id: data.fulfillment.id },
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
createLinkStep(link)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import { FulfillmentDTO, OrderDTO, OrderWorkflow } from "@medusajs/types"
|
||||
import { Modules } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
import { createShipmentWorkflow } from "../../fulfillment"
|
||||
import { registerOrderShipmentStep } from "../steps"
|
||||
import {
|
||||
throwIfItemsDoesNotExistsInOrder,
|
||||
throwIfOrderIsCancelled,
|
||||
} from "../utils/order-validation"
|
||||
|
||||
const validateOrder = createStep(
|
||||
"validate-order",
|
||||
({
|
||||
order,
|
||||
input,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
input: OrderWorkflow.CreateOrderShipmentWorkflowInput
|
||||
}) => {
|
||||
const inputItems = input.items
|
||||
|
||||
throwIfOrderIsCancelled({ order })
|
||||
throwIfItemsDoesNotExistsInOrder({ order, inputItems })
|
||||
|
||||
const order_ = order as OrderDTO & { fulfillments: FulfillmentDTO[] }
|
||||
const fulfillment = order_.fulfillments.find(
|
||||
(f) => f.id === input.fulfillment_id
|
||||
)
|
||||
if (!fulfillment) {
|
||||
throw new Error(
|
||||
`Fulfillment with id ${input.fulfillment_id} not found in the order`
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
function prepareRegisterShipmentData({
|
||||
order,
|
||||
input,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
input: OrderWorkflow.CreateOrderShipmentWorkflowInput
|
||||
}) {
|
||||
const fulfillId = input.fulfillment_id
|
||||
const order_ = order as OrderDTO & { fulfillments: FulfillmentDTO[] }
|
||||
const fulfillment = order_.fulfillments.find((f) => f.id === fulfillId)!
|
||||
|
||||
return {
|
||||
order_id: order.id,
|
||||
reference: Modules.FULFILLMENT,
|
||||
reference_id: fulfillment.id,
|
||||
created_by: input.created_by,
|
||||
items: order.items!.map((i) => {
|
||||
return {
|
||||
id: i.id,
|
||||
quantity: i.quantity,
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
export const createOrderShipmentWorkflowId = "create-order-shipment"
|
||||
export const createOrderShipmentWorkflow = createWorkflow(
|
||||
createOrderShipmentWorkflowId,
|
||||
(
|
||||
input: WorkflowData<OrderWorkflow.CreateOrderShipmentWorkflowInput>
|
||||
): WorkflowData<void> => {
|
||||
const order: OrderDTO = useRemoteQueryStep({
|
||||
entry_point: "orders",
|
||||
fields: [
|
||||
"id",
|
||||
"status",
|
||||
"region_id",
|
||||
"currency_code",
|
||||
"items.*",
|
||||
"fulfillments.*",
|
||||
],
|
||||
variables: { id: input.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
})
|
||||
|
||||
validateOrder({ order, input })
|
||||
|
||||
const fulfillmentData = transform({ input }, ({ input }) => {
|
||||
return {
|
||||
id: input.fulfillment_id,
|
||||
labels: input.labels,
|
||||
}
|
||||
})
|
||||
|
||||
createShipmentWorkflow.runAsStep({
|
||||
input: fulfillmentData,
|
||||
})
|
||||
|
||||
const shipmentData = transform(
|
||||
{ order, input },
|
||||
prepareRegisterShipmentData
|
||||
)
|
||||
|
||||
registerOrderShipmentStep(shipmentData)
|
||||
}
|
||||
)
|
||||
@@ -1,5 +1,7 @@
|
||||
export * from "./archive-orders"
|
||||
export * from "./complete-orders"
|
||||
export * from "./create-fulfillment"
|
||||
export * from "./create-orders"
|
||||
export * from "./create-return"
|
||||
export * from "./create-shipment"
|
||||
export * from "./update-tax-lines"
|
||||
|
||||
Reference in New Issue
Block a user