chore(core-flows): order update item quantity (#8659)
This commit is contained in:
committed by
GitHub
parent
dd82a56ec5
commit
1be9373290
387
integration-tests/http/__tests__/order-edits/order-edits.spec.ts
Normal file
387
integration-tests/http/__tests__/order-edits/order-edits.spec.ts
Normal file
@@ -0,0 +1,387 @@
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
ModuleRegistrationName,
|
||||
Modules,
|
||||
RuleOperator,
|
||||
} from "@medusajs/utils"
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import {
|
||||
adminHeaders,
|
||||
createAdminUser,
|
||||
} from "../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||
let order
|
||||
let shippingOption
|
||||
let shippingProfile
|
||||
let fulfillmentSet
|
||||
let inventoryItem
|
||||
let inventoryItemExtra
|
||||
let location
|
||||
let productExtra
|
||||
const shippingProviderId = "manual_test-provider"
|
||||
|
||||
beforeEach(async () => {
|
||||
const container = getContainer()
|
||||
await createAdminUser(dbConnection, adminHeaders, container)
|
||||
|
||||
const region = (
|
||||
await api.post(
|
||||
"/admin/regions",
|
||||
{
|
||||
name: "test-region",
|
||||
currency_code: "usd",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.region
|
||||
|
||||
const customer = (
|
||||
await api.post(
|
||||
"/admin/customers",
|
||||
{
|
||||
first_name: "joe",
|
||||
email: "joe@admin.com",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.customer
|
||||
|
||||
const salesChannel = (
|
||||
await api.post(
|
||||
"/admin/sales-channels",
|
||||
{
|
||||
name: "Test channel",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.sales_channel
|
||||
|
||||
const product = (
|
||||
await api.post(
|
||||
"/admin/products",
|
||||
{
|
||||
title: "Test product",
|
||||
variants: [
|
||||
{
|
||||
title: "Test variant",
|
||||
sku: "test-variant",
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 10,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product
|
||||
|
||||
productExtra = (
|
||||
await api.post(
|
||||
"/admin/products",
|
||||
{
|
||||
title: "Extra product",
|
||||
variants: [
|
||||
{
|
||||
title: "my variant",
|
||||
sku: "variant-sku",
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 12,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product
|
||||
|
||||
const orderModule = container.resolve(ModuleRegistrationName.ORDER)
|
||||
|
||||
order = await orderModule.createOrders({
|
||||
region_id: region.id,
|
||||
email: "foo@bar.com",
|
||||
items: [
|
||||
{
|
||||
title: "Custom Item",
|
||||
variant_id: product.variants[0].id,
|
||||
quantity: 2,
|
||||
unit_price: 25,
|
||||
},
|
||||
],
|
||||
sales_channel_id: salesChannel.id,
|
||||
shipping_address: {
|
||||
first_name: "Test",
|
||||
last_name: "Test",
|
||||
address_1: "Test",
|
||||
city: "Test",
|
||||
country_code: "US",
|
||||
postal_code: "12345",
|
||||
phone: "12345",
|
||||
},
|
||||
billing_address: {
|
||||
first_name: "Test",
|
||||
last_name: "Test",
|
||||
address_1: "Test",
|
||||
city: "Test",
|
||||
country_code: "US",
|
||||
postal_code: "12345",
|
||||
},
|
||||
shipping_methods: [
|
||||
{
|
||||
name: "Test shipping method",
|
||||
amount: 10,
|
||||
},
|
||||
],
|
||||
currency_code: "usd",
|
||||
customer_id: customer.id,
|
||||
})
|
||||
|
||||
shippingProfile = (
|
||||
await api.post(
|
||||
`/admin/shipping-profiles`,
|
||||
{
|
||||
name: "Test",
|
||||
type: "default",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.shipping_profile
|
||||
|
||||
location = (
|
||||
await api.post(
|
||||
`/admin/stock-locations`,
|
||||
{
|
||||
name: "Test location",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.stock_location
|
||||
|
||||
location = (
|
||||
await api.post(
|
||||
`/admin/stock-locations/${location.id}/fulfillment-sets?fields=*fulfillment_sets`,
|
||||
{
|
||||
name: "Test",
|
||||
type: "test-type",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.stock_location
|
||||
|
||||
fulfillmentSet = (
|
||||
await api.post(
|
||||
`/admin/fulfillment-sets/${location.fulfillment_sets[0].id}/service-zones`,
|
||||
{
|
||||
name: "Test",
|
||||
geo_zones: [{ type: "country", country_code: "us" }],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.fulfillment_set
|
||||
|
||||
inventoryItem = (
|
||||
await api.post(
|
||||
`/admin/inventory-items`,
|
||||
{ sku: "inv-1234" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.inventory_item
|
||||
|
||||
await api.post(
|
||||
`/admin/inventory-items/${inventoryItem.id}/location-levels`,
|
||||
{
|
||||
location_id: location.id,
|
||||
stocked_quantity: 2,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
inventoryItemExtra = (
|
||||
await api.get(`/admin/inventory-items?sku=variant-sku`, adminHeaders)
|
||||
).data.inventory_items[0]
|
||||
|
||||
await api.post(
|
||||
`/admin/inventory-items/${inventoryItemExtra.id}/location-levels`,
|
||||
{
|
||||
location_id: location.id,
|
||||
stocked_quantity: 4,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const remoteLink = container.resolve(
|
||||
ContainerRegistrationKeys.REMOTE_LINK
|
||||
)
|
||||
|
||||
await remoteLink.create([
|
||||
{
|
||||
[Modules.STOCK_LOCATION]: {
|
||||
stock_location_id: location.id,
|
||||
},
|
||||
[Modules.FULFILLMENT]: {
|
||||
fulfillment_provider_id: shippingProviderId,
|
||||
},
|
||||
},
|
||||
{
|
||||
[Modules.STOCK_LOCATION]: {
|
||||
stock_location_id: location.id,
|
||||
},
|
||||
[Modules.FULFILLMENT]: {
|
||||
fulfillment_set_id: fulfillmentSet.id,
|
||||
},
|
||||
},
|
||||
{
|
||||
[Modules.SALES_CHANNEL]: {
|
||||
sales_channel_id: salesChannel.id,
|
||||
},
|
||||
[Modules.STOCK_LOCATION]: {
|
||||
stock_location_id: location.id,
|
||||
},
|
||||
},
|
||||
{
|
||||
[Modules.PRODUCT]: {
|
||||
variant_id: product.variants[0].id,
|
||||
},
|
||||
[Modules.INVENTORY]: {
|
||||
inventory_item_id: inventoryItem.id,
|
||||
},
|
||||
},
|
||||
{
|
||||
[Modules.PRODUCT]: {
|
||||
variant_id: productExtra.variants[0].id,
|
||||
},
|
||||
[Modules.INVENTORY]: {
|
||||
inventory_item_id: inventoryItemExtra.id,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const shippingOptionPayload = {
|
||||
name: "Shipping",
|
||||
service_zone_id: fulfillmentSet.service_zones[0].id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id: shippingProviderId,
|
||||
price_type: "flat",
|
||||
type: {
|
||||
label: "Test type",
|
||||
description: "Test description",
|
||||
code: "test-code",
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 10,
|
||||
},
|
||||
],
|
||||
rules: [
|
||||
{
|
||||
operator: RuleOperator.EQ,
|
||||
attribute: "is_return",
|
||||
value: "true",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
shippingOption = (
|
||||
await api.post(
|
||||
"/admin/shipping-options",
|
||||
shippingOptionPayload,
|
||||
adminHeaders
|
||||
)
|
||||
).data.shipping_option
|
||||
|
||||
const item = order.items[0]
|
||||
|
||||
await api.post(
|
||||
`/admin/orders/${order.id}/fulfillments`,
|
||||
{
|
||||
items: [
|
||||
{
|
||||
id: item.id,
|
||||
quantity: 2,
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
})
|
||||
|
||||
describe("Order Edits lifecycle", () => {
|
||||
it("Full flow test", async () => {
|
||||
let result = await api.post(
|
||||
"/admin/order-edits",
|
||||
{
|
||||
order_id: order.id,
|
||||
description: "Test",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const orderId = result.data.order_change.order_id
|
||||
|
||||
const item = order.items[0]
|
||||
|
||||
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
||||
.order
|
||||
|
||||
expect(result.summary.current_order_total).toEqual(60)
|
||||
expect(result.summary.original_order_total).toEqual(60)
|
||||
|
||||
// New Items ($12 each)
|
||||
result = (
|
||||
await api.post(
|
||||
`/admin/order-edits/${orderId}/items`,
|
||||
{
|
||||
items: [
|
||||
{
|
||||
variant_id: productExtra.variants[0].id,
|
||||
quantity: 2,
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.order_preview
|
||||
|
||||
expect(result.summary.current_order_total).toEqual(84)
|
||||
expect(result.summary.original_order_total).toEqual(60)
|
||||
|
||||
// Update item quantity
|
||||
result = (
|
||||
await api.post(
|
||||
`/admin/order-edits/${orderId}/items/item/${item.id}`,
|
||||
{
|
||||
quantity: 4,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.order_preview
|
||||
|
||||
expect(result.summary.current_order_total).toEqual(134)
|
||||
expect(result.summary.original_order_total).toEqual(60)
|
||||
|
||||
result = (
|
||||
await api.post(
|
||||
`/admin/order-edits/${orderId}/confirm`,
|
||||
{},
|
||||
adminHeaders
|
||||
)
|
||||
).data.order_preview
|
||||
|
||||
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
||||
.order
|
||||
|
||||
expect(result.total).toEqual(134)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -45,9 +45,11 @@ export * from "./order-edit/cancel-begin-order-edit"
|
||||
export * from "./order-edit/confirm-order-edit-request"
|
||||
export * from "./order-edit/create-order-edit-shipping-method"
|
||||
export * from "./order-edit/order-edit-add-new-item"
|
||||
export * from "./order-edit/order-edit-update-item-quantity"
|
||||
export * from "./order-edit/remove-order-edit-item-action"
|
||||
export * from "./order-edit/remove-order-edit-shipping-method"
|
||||
export * from "./order-edit/update-order-edit-add-item"
|
||||
export * from "./order-edit/update-order-edit-item-quantity"
|
||||
export * from "./order-edit/update-order-edit-shipping-method"
|
||||
export * from "./return/begin-receive-return"
|
||||
export * from "./return/begin-return"
|
||||
|
||||
@@ -102,51 +102,51 @@ export const confirmOrderEditRequestWorkflow = createWorkflow(
|
||||
"version",
|
||||
"canceled_at",
|
||||
"sales_channel_id",
|
||||
"items.quantity",
|
||||
"items.raw_quantity",
|
||||
"items.item.id",
|
||||
"items.item.variant.manage_inventory",
|
||||
"items.item.variant.allow_backorder",
|
||||
"items.item.variant.inventory_items.inventory_item_id",
|
||||
"items.item.variant.inventory_items.required_quantity",
|
||||
"items.item.variant.inventory_items.inventory.location_levels.stock_locations.id",
|
||||
"items.item.variant.inventory_items.inventory.location_levels.stock_locations.name",
|
||||
"items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
|
||||
"items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
|
||||
"items.*",
|
||||
"items.variant.manage_inventory",
|
||||
"items.variant.allow_backorder",
|
||||
"items.variant.inventory_items.inventory_item_id",
|
||||
"items.variant.inventory_items.required_quantity",
|
||||
"items.variant.inventory_items.inventory.location_levels.stock_locations.id",
|
||||
"items.variant.inventory_items.inventory.location_levels.stock_locations.name",
|
||||
"items.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
|
||||
"items.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
|
||||
],
|
||||
variables: { id: input.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "order-items-query" })
|
||||
|
||||
const { variants, items } = transform({ orderItems }, ({ orderItems }) => {
|
||||
const allItems: any[] = []
|
||||
const allVariants: any[] = []
|
||||
orderItems.items.forEach((ordItem) => {
|
||||
const itemAction = orderPreview.items?.find(
|
||||
(item) =>
|
||||
item.id === ordItem.id &&
|
||||
item.actions?.find((a) => a.action === ChangeActionType.ITEM_ADD)
|
||||
)
|
||||
const { variants, items } = transform(
|
||||
{ orderItems, orderPreview },
|
||||
({ orderItems, orderPreview }) => {
|
||||
const allItems: any[] = []
|
||||
const allVariants: any[] = []
|
||||
orderItems.items.forEach((ordItem) => {
|
||||
const itemAction = orderPreview.items?.find(
|
||||
(item) =>
|
||||
item.id === ordItem.id &&
|
||||
item.actions?.find((a) => a.action === ChangeActionType.ITEM_ADD)
|
||||
)
|
||||
|
||||
if (!itemAction) {
|
||||
return
|
||||
}
|
||||
if (!itemAction) {
|
||||
return
|
||||
}
|
||||
|
||||
const item = ordItem.item
|
||||
allItems.push({
|
||||
id: item.id,
|
||||
variant_id: item.variant_id,
|
||||
quantity: itemAction.raw_quantity ?? itemAction.quantity,
|
||||
allItems.push({
|
||||
id: ordItem.id,
|
||||
variant_id: ordItem.variant_id,
|
||||
quantity: itemAction.raw_quantity ?? itemAction.quantity,
|
||||
})
|
||||
allVariants.push(ordItem.variant)
|
||||
})
|
||||
allVariants.push(item.variant)
|
||||
})
|
||||
|
||||
return {
|
||||
variants: allVariants,
|
||||
items: allItems,
|
||||
return {
|
||||
variants: allVariants,
|
||||
items: allItems,
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
const formatedInventoryItems = transform(
|
||||
{
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import {
|
||||
OrderChangeDTO,
|
||||
OrderDTO,
|
||||
OrderPreviewDTO,
|
||||
OrderWorkflow,
|
||||
} from "@medusajs/types"
|
||||
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../../common"
|
||||
import { previewOrderChangeStep } from "../../steps/preview-order-change"
|
||||
import {
|
||||
throwIfIsCancelled,
|
||||
throwIfOrderChangeIsNotActive,
|
||||
} from "../../utils/order-validation"
|
||||
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
|
||||
|
||||
/**
|
||||
* This step validates that item quantity updated can be added to an order edit.
|
||||
*/
|
||||
export const orderEditUpdateItemQuantityValidationStep = createStep(
|
||||
"order-edit-update-item-quantity-validation",
|
||||
async function ({
|
||||
order,
|
||||
orderChange,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
orderChange: OrderChangeDTO
|
||||
}) {
|
||||
throwIfIsCancelled(order, "Order")
|
||||
throwIfOrderChangeIsNotActive({ orderChange })
|
||||
}
|
||||
)
|
||||
|
||||
export const orderEditUpdateItemQuantityWorkflowId =
|
||||
"order-edit-update-item-quantity"
|
||||
/**
|
||||
* This workflow update item's quantity of an order.
|
||||
*/
|
||||
export const orderEditUpdateItemQuantityWorkflow = createWorkflow(
|
||||
orderEditUpdateItemQuantityWorkflowId,
|
||||
function (
|
||||
input: WorkflowData<OrderWorkflow.OrderEditUpdateItemQuantityWorkflowInput>
|
||||
): WorkflowResponse<OrderPreviewDTO> {
|
||||
const order: OrderDTO = useRemoteQueryStep({
|
||||
entry_point: "orders",
|
||||
fields: ["id", "status", "canceled_at", "items.*"],
|
||||
variables: { id: input.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "order-query" })
|
||||
|
||||
const orderChange: OrderChangeDTO = useRemoteQueryStep({
|
||||
entry_point: "order_change",
|
||||
fields: ["id", "status"],
|
||||
variables: {
|
||||
filters: {
|
||||
order_id: input.order_id,
|
||||
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
|
||||
},
|
||||
},
|
||||
list: false,
|
||||
}).config({ name: "order-change-query" })
|
||||
|
||||
orderEditUpdateItemQuantityValidationStep({
|
||||
order,
|
||||
orderChange,
|
||||
})
|
||||
|
||||
const orderChangeActionInput = transform(
|
||||
{ order, orderChange, items: input.items },
|
||||
({ order, orderChange, items }) => {
|
||||
return items.map((item) => ({
|
||||
order_change_id: orderChange.id,
|
||||
order_id: order.id,
|
||||
version: orderChange.version,
|
||||
action: ChangeActionType.ITEM_UPDATE,
|
||||
internal_note: item.internal_note,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
quantity: item.quantity,
|
||||
},
|
||||
}))
|
||||
}
|
||||
)
|
||||
|
||||
createOrderChangeActionsWorkflow.runAsStep({
|
||||
input: orderChangeActionInput,
|
||||
})
|
||||
|
||||
return new WorkflowResponse(previewOrderChangeStep(input.order_id))
|
||||
}
|
||||
)
|
||||
@@ -24,7 +24,7 @@ import {
|
||||
} from "../../utils/order-validation"
|
||||
|
||||
/**
|
||||
* This step validates that a new item can be removed from an order edit.
|
||||
* This step validates that a new item can be updated from an order edit.
|
||||
*/
|
||||
export const updateOrderEditAddItemValidationStep = createStep(
|
||||
"update-order-edit-add-item-validation",
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import {
|
||||
OrderChangeActionDTO,
|
||||
OrderChangeDTO,
|
||||
OrderDTO,
|
||||
OrderPreviewDTO,
|
||||
OrderWorkflow,
|
||||
} from "@medusajs/types"
|
||||
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../../common"
|
||||
import {
|
||||
previewOrderChangeStep,
|
||||
updateOrderChangeActionsStep,
|
||||
} from "../../steps"
|
||||
import {
|
||||
throwIfIsCancelled,
|
||||
throwIfOrderChangeIsNotActive,
|
||||
} from "../../utils/order-validation"
|
||||
|
||||
/**
|
||||
* This step validates that an item can be updated from an order edit.
|
||||
*/
|
||||
export const updateOrderEditItemQuantityValidationStep = createStep(
|
||||
"update-order-edit-update-quantity-validation",
|
||||
async function (
|
||||
{
|
||||
order,
|
||||
orderChange,
|
||||
input,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
orderChange: OrderChangeDTO
|
||||
input: OrderWorkflow.UpdateOrderEditItemQuantityWorkflowInput
|
||||
},
|
||||
context
|
||||
) {
|
||||
throwIfIsCancelled(order, "Order")
|
||||
throwIfOrderChangeIsNotActive({ orderChange })
|
||||
|
||||
const associatedAction = (orderChange.actions ?? []).find(
|
||||
(a) => a.id === input.action_id
|
||||
) as OrderChangeActionDTO
|
||||
|
||||
if (!associatedAction) {
|
||||
throw new Error(
|
||||
`No request to update item quantity for order ${input.order_id} in order change ${orderChange.id}`
|
||||
)
|
||||
} else if (associatedAction.action !== ChangeActionType.ITEM_UPDATE) {
|
||||
throw new Error(`Action ${associatedAction.id} is not updating an item`)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const updateOrderEditItemQuantityWorkflowId =
|
||||
"update-order-edit-update-quantity"
|
||||
/**
|
||||
* This workflow updates a new item in the order edit.
|
||||
*/
|
||||
export const updateOrderEditItemQuantityWorkflow = createWorkflow(
|
||||
updateOrderEditItemQuantityWorkflowId,
|
||||
function (
|
||||
input: WorkflowData<OrderWorkflow.UpdateOrderEditItemQuantityWorkflowInput>
|
||||
): WorkflowResponse<OrderPreviewDTO> {
|
||||
const order: OrderDTO = useRemoteQueryStep({
|
||||
entry_point: "orders",
|
||||
fields: ["id", "status", "canceled_at", "items.*"],
|
||||
variables: { id: input.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "order-query" })
|
||||
|
||||
const orderChange: OrderChangeDTO = useRemoteQueryStep({
|
||||
entry_point: "order_change",
|
||||
fields: ["id", "status", "version", "actions.*"],
|
||||
variables: {
|
||||
filters: {
|
||||
order_id: input.order_id,
|
||||
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
|
||||
},
|
||||
},
|
||||
list: false,
|
||||
}).config({ name: "order-change-query" })
|
||||
|
||||
updateOrderEditItemQuantityValidationStep({
|
||||
order,
|
||||
input,
|
||||
orderChange,
|
||||
})
|
||||
|
||||
const updateData = transform(
|
||||
{ orderChange, input },
|
||||
({ input, orderChange }) => {
|
||||
const originalAction = (orderChange.actions ?? []).find(
|
||||
(a) => a.id === input.action_id
|
||||
) as OrderChangeActionDTO
|
||||
|
||||
const data = input.data
|
||||
return {
|
||||
id: input.action_id,
|
||||
details: {
|
||||
quantity: data.quantity ?? originalAction.details?.quantity,
|
||||
},
|
||||
internal_note: data.internal_note,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
updateOrderChangeActionsStep([updateData])
|
||||
|
||||
return new WorkflowResponse(previewOrderChangeStep(order.id))
|
||||
}
|
||||
)
|
||||
@@ -1,5 +1,9 @@
|
||||
import { OrderPreviewDTO } from "../../../order"
|
||||
import { OrderChangeDTO, OrderPreviewDTO } from "../../../order"
|
||||
|
||||
export interface AdminOrderEditPreviewResponse {
|
||||
order_preview: OrderPreviewDTO
|
||||
}
|
||||
|
||||
export interface AdminOrderEditResponse {
|
||||
order_change: OrderChangeDTO
|
||||
}
|
||||
|
||||
@@ -5,14 +5,14 @@ interface NewItem {
|
||||
variant_id: string
|
||||
quantity: BigNumberInput
|
||||
unit_price?: BigNumberInput
|
||||
internal_note?: string
|
||||
internal_note?: string | null
|
||||
metadata?: Record<string, any> | null
|
||||
}
|
||||
|
||||
interface ExistingItem {
|
||||
id: string
|
||||
quantity: BigNumberInput
|
||||
internal_note?: string
|
||||
internal_note?: string | null
|
||||
}
|
||||
|
||||
export interface OrderExchangeAddNewItemWorkflowInput {
|
||||
@@ -35,6 +35,11 @@ export interface OrderEditAddNewItemWorkflowInput {
|
||||
items: NewItem[]
|
||||
}
|
||||
|
||||
export interface OrderEditUpdateItemQuantityWorkflowInput {
|
||||
order_id: string
|
||||
items: ExistingItem[]
|
||||
}
|
||||
|
||||
export interface OrderAddLineItemWorkflowInput {
|
||||
order_id: string
|
||||
items: NewItem[]
|
||||
@@ -58,6 +63,9 @@ export interface UpdateOrderEditAddNewItemWorkflowInput {
|
||||
}
|
||||
}
|
||||
|
||||
export interface UpdateOrderEditItemQuantityWorkflowInput
|
||||
extends UpdateOrderEditAddNewItemWorkflowInput {}
|
||||
|
||||
export interface UpdateClaimAddNewItemWorkflowInput {
|
||||
claim_id: string
|
||||
action_id: string
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { orderEditUpdateItemQuantityWorkflow } from "@medusajs/core-flows"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { AdminPostOrderEditsUpdateItemQuantityReqSchemaType } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostOrderEditsUpdateItemQuantityReqSchemaType>,
|
||||
res: MedusaResponse<HttpTypes.AdminOrderEditPreviewResponse>
|
||||
) => {
|
||||
const { id, item_id } = req.params
|
||||
|
||||
const { result } = await orderEditUpdateItemQuantityWorkflow(req.scope).run({
|
||||
input: {
|
||||
...req.validatedBody,
|
||||
order_id: id,
|
||||
items: [
|
||||
{
|
||||
...req.validatedBody,
|
||||
id: item_id,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
res.json({
|
||||
order_preview: result,
|
||||
})
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
AdminPostOrderEditsReqSchema,
|
||||
AdminPostOrderEditsShippingActionReqSchema,
|
||||
AdminPostOrderEditsShippingReqSchema,
|
||||
AdminPostOrderEditsUpdateItemQuantityReqSchema,
|
||||
} from "./validators"
|
||||
|
||||
export const adminOrderEditRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
@@ -34,6 +35,13 @@ export const adminOrderEditRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
validateAndTransformBody(AdminPostOrderEditsItemsActionReqSchema),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/order-edits/:id/items/item/:item_id",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminPostOrderEditsUpdateItemQuantityReqSchema),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["DELETE"],
|
||||
matcher: "/admin/order-edits/:id/items/:action_id",
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import { beginOrderEditOrderWorkflow } from "@medusajs/core-flows"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -12,30 +8,16 @@ import { AdminPostOrderEditsReqSchemaType } from "./validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostOrderEditsReqSchemaType>,
|
||||
res: MedusaResponse<HttpTypes.AdminOrderResponse>
|
||||
res: MedusaResponse<HttpTypes.AdminOrderEditResponse>
|
||||
) => {
|
||||
const input = req.validatedBody as AdminPostOrderEditsReqSchemaType
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const workflow = beginOrderEditOrderWorkflow(req.scope)
|
||||
const { result } = await workflow.run({
|
||||
input,
|
||||
})
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "order",
|
||||
variables: {
|
||||
id: result.order_id,
|
||||
filters: {
|
||||
...req.filterableFields,
|
||||
},
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [order] = await remoteQuery(queryObject)
|
||||
|
||||
res.json({
|
||||
order,
|
||||
order_change: result,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ export const AdminPostOrderEditsAddItemsReqSchema = z.object({
|
||||
export type AdminPostOrderEditsAddItemsReqSchemaType = z.infer<
|
||||
typeof AdminPostOrderEditsAddItemsReqSchema
|
||||
>
|
||||
|
||||
export const AdminPostOrderEditsItemsActionReqSchema = z.object({
|
||||
quantity: z.number().optional(),
|
||||
internal_note: z.string().nullish().optional(),
|
||||
@@ -56,3 +57,12 @@ export const AdminPostOrderEditsItemsActionReqSchema = z.object({
|
||||
export type AdminPostOrderEditsItemsActionReqSchemaType = z.infer<
|
||||
typeof AdminPostOrderEditsItemsActionReqSchema
|
||||
>
|
||||
|
||||
export const AdminPostOrderEditsUpdateItemQuantityReqSchema = z.object({
|
||||
quantity: z.number(),
|
||||
internal_note: z.string().nullish().optional(),
|
||||
})
|
||||
|
||||
export type AdminPostOrderEditsUpdateItemQuantityReqSchemaType = z.infer<
|
||||
typeof AdminPostOrderEditsUpdateItemQuantityReqSchema
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user