feat(core-flows): custom price flag for order line items and shipping methods (#8969)

CLOSES: CC-402
This commit is contained in:
Carlos R. L. Rodrigues authored and GitHub committed 2024-09-04 20:30:59 +00:00
1 parent 0fe1201435
commit 2a055b71ef
28 files changed
+1986 -1778

No files matched your search

@@ -594,13 +594,17 @@ medusaIntegrationTestRunner({
adminHeaders adminHeaders
) )
// shipping Options w/ custom price
const { const {
data: { data: {
order_preview: { shipping_methods: outboundShippingMethods }, order_preview: { shipping_methods: outboundShippingMethods },
}, },
} = await api.post( } = await api.post(
`/admin/claims/${claimId}/outbound/shipping-method`, `/admin/claims/${claimId}/outbound/shipping-method`,
{ shipping_option_id: outboundShippingOption.id }, {
shipping_option_id: outboundShippingOption.id,
custom_amount: 12.5,
},
adminHeaders adminHeaders
) )
@@ -608,9 +612,32 @@ medusaIntegrationTestRunner({
(m) => m.shipping_option_id == outboundShippingOption.id (m) => m.shipping_option_id == outboundShippingOption.id
) )
expect(outboundShippingMethod.subtotal).toBe(12.5)
expect(outboundShippingMethod.is_custom_amount).toBe(true)
// Reset shipping custom price
const {
data: {
order_preview: { shipping_methods: outboundShippingMethods2 },
},
} = await api.post(
`/admin/claims/${claimId}/outbound/shipping-method/${outboundShippingMethod.actions[0].id}`,
{
custom_amount: null,
},
adminHeaders
)
const outboundShippingMethodReset = outboundShippingMethods2.find(
(m) => m.shipping_option_id == outboundShippingOption.id
)
expect(outboundShippingMethodReset.subtotal).toBe(20)
expect(outboundShippingMethodReset.is_custom_amount).toBe(false)
// Delete & recreate again to ensure it works for both delete and create // Delete & recreate again to ensure it works for both delete and create
await api.delete( await api.delete(
`/admin/claims/${claimId}/outbound/shipping-method/${outboundShippingMethod.actions[0].id}`, `/admin/claims/${claimId}/outbound/shipping-method/${outboundShippingMethodReset.actions[0].id}`,
adminHeaders adminHeaders
) )
@@ -665,7 +665,7 @@ medusaIntegrationTestRunner({
result = await api.post( result = await api.post(
`/admin/returns/${returnId}/shipping-method/${updateShippingActionId}`, `/admin/returns/${returnId}/shipping-method/${updateShippingActionId}`,
{ {
custom_price: 1002, custom_amount: 1002,
internal_note: "cx agent note", internal_note: "cx agent note",
}, },
adminHeaders adminHeaders
@@ -203,6 +203,7 @@ medusaIntegrationTestRunner({
value: "50", value: "50",
precision: 20, precision: 20,
}, },
is_custom_price: false,
metadata: null, metadata: null,
created_at: expect.any(String), created_at: expect.any(String),
updated_at: expect.any(String), updated_at: expect.any(String),
@@ -95,7 +95,7 @@ medusaIntegrationTestRunner({
input: { input: {
return_id: returnOrder.id, return_id: returnOrder.id,
shipping_option_id: shippingOptionId, shipping_option_id: shippingOptionId,
custom_price: 20, custom_amount: 20,
}, },
}) })
@@ -845,7 +845,7 @@ export const ClaimCreateForm = ({
updateInboundShipping( updateInboundShipping(
{ {
actionId, actionId,
custom_price: customPrice, custom_amount: customPrice,
}, },
{ {
onError: (error) => { onError: (error) => {
@@ -916,7 +916,7 @@ export const ClaimCreateForm = ({
updateOutboundShipping( updateOutboundShipping(
{ {
actionId, actionId,
custom_price: customPrice, custom_amount: customPrice,
}, },
{ {
onError: (error) => { onError: (error) => {
@@ -364,7 +364,7 @@ export const ExchangeCreateForm = ({
updateInboundShipping( updateInboundShipping(
{ {
actionId, actionId,
custom_price: customPrice, custom_amount: customPrice,
}, },
{ {
onError: (error) => { onError: (error) => {
@@ -435,7 +435,7 @@ export const ExchangeCreateForm = ({
updateOutboundShipping( updateOutboundShipping(
{ {
actionId, actionId,
custom_price: customPrice, custom_amount: customPrice,
}, },
{ {
onError: (error) => { onError: (error) => {
@@ -646,7 +646,7 @@ export const ReturnCreateForm = ({
if (actionId) { if (actionId) {
updateReturnShipping({ updateReturnShipping({
actionId, actionId,
custom_price: custom_amount:
typeof customShippingAmount === "string" typeof customShippingAmount === "string"
? null ? null
: customShippingAmount, : customShippingAmount,
@@ -0,0 +1,73 @@
import { OrderChangeActionDTO } from "@medusajs/types"
import { isDefined } from "@medusajs/utils"
export function prepareShippingMethod(relatedEntityField?: string) {
return function (data) {
const option = data.shippingOptions[0]
const orderChange = data.orderChange
const isCustomPrice = isDefined(data.customPrice)
const obj = {
shipping_option_id: option.id,
amount: isCustomPrice
? data.customPrice
: option.calculated_price.calculated_amount,
is_custom_amount: isCustomPrice,
is_tax_inclusive:
!!option.calculated_price.is_calculated_price_tax_inclusive,
data: option.data ?? {},
name: option.name,
version: orderChange.version,
order_id: data.relatedEntity.order_id,
} as any
if (relatedEntityField) {
obj.return_id = data.input.return_id
obj[relatedEntityField] = data.relatedEntity.id
if (relatedEntityField === "return_id") {
obj.claim_id = data.relatedEntity.claim_id
obj.exchange_id = data.relatedEntity.exchange_id
}
}
return obj
}
}
export function prepareShippingMethodUpdate({
input,
orderChange,
shippingOptions,
}) {
const originalAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
const data = input.data
const option = shippingOptions?.[0]
const isCustomPrice = !isDefined(shippingOptions)
const price = isCustomPrice
? data.custom_amount
: option.calculated_price.calculated_amount
const action = {
id: originalAction.id,
amount: price,
internal_note: data.internal_note,
}
const shippingMethod = {
id: originalAction.reference_id,
amount: price,
is_custom_amount: isCustomPrice,
metadata: data.metadata,
}
return {
action,
shippingMethod,
}
}
@@ -19,6 +19,7 @@ import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation" } from "../../utils/order-validation"
import { prepareShippingMethod } from "../../utils/prepare-shipping-method"
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions" import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
import { updateOrderTaxLinesWorkflow } from "../update-tax-lines" import { updateOrderTaxLinesWorkflow } from "../update-tax-lines"
@@ -53,7 +54,7 @@ export const createClaimShippingMethodWorkflow = createWorkflow(
return_id?: string return_id?: string
claim_id?: string claim_id?: string
shipping_option_id: string shipping_option_id: string
custom_price?: BigNumberInput custom_amount?: BigNumberInput | null
}): WorkflowResponse<OrderPreviewDTO> { }): WorkflowResponse<OrderPreviewDTO> {
const orderClaim: OrderClaimDTO = useRemoteQueryStep({ const orderClaim: OrderClaimDTO = useRemoteQueryStep({
entry_point: "order_claim", entry_point: "order_claim",
@@ -65,7 +66,7 @@ export const createClaimShippingMethodWorkflow = createWorkflow(
const order: OrderDTO = useRemoteQueryStep({ const order: OrderDTO = useRemoteQueryStep({
entry_point: "orders", entry_point: "orders",
fields: ["id", "status", "currency_code", "canceled_at"], fields: ["id", "status", "region_id", "currency_code", "canceled_at"],
variables: { id: orderClaim.order_id }, variables: { id: orderClaim.order_id },
list: false, list: false,
throw_if_key_not_found: true, throw_if_key_not_found: true,
@@ -104,29 +105,13 @@ export const createClaimShippingMethodWorkflow = createWorkflow(
const shippingMethodInput = transform( const shippingMethodInput = transform(
{ {
orderClaim, relatedEntity: orderClaim,
shippingOptions, shippingOptions,
customPrice: input.custom_price, customPrice: input.custom_amount,
orderChange, orderChange,
input, input,
}, },
(data) => { prepareShippingMethod("claim_id")
const option = data.shippingOptions[0]
const orderChange = data.orderChange
return {
shipping_option_id: option.id,
amount: data.customPrice ?? option.calculated_price.calculated_amount,
is_tax_inclusive:
!!option.calculated_price.is_calculated_price_tax_inclusive,
data: option.data ?? {},
name: option.name,
version: orderChange.version,
order_id: data.orderClaim.order_id,
return_id: input.return_id,
claim_id: data.orderClaim.id,
}
}
) )
const createdMethods = createOrderShippingMethods({ const createdMethods = createOrderShippingMethods({
@@ -155,7 +140,7 @@ export const createClaimShippingMethodWorkflow = createWorkflow(
orderClaim, orderClaim,
shippingOptions, shippingOptions,
createdMethods, createdMethods,
customPrice: input.custom_price, customPrice: input.custom_amount,
orderChange, orderChange,
input, input,
}, },
@@ -170,6 +155,7 @@ export const createClaimShippingMethodWorkflow = createWorkflow(
}) => { }) => {
const shippingOption = shippingOptions[0] const shippingOption = shippingOptions[0]
const createdMethod = createdMethods[0] const createdMethod = createdMethods[0]
const methodPrice = const methodPrice =
customPrice ?? shippingOption.calculated_price.calculated_amount customPrice ?? shippingOption.calculated_price.calculated_amount
@@ -13,6 +13,7 @@ import {
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
when,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common" import { useRemoteQueryStep } from "../../../common"
import { import {
@@ -24,6 +25,7 @@ import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation" } from "../../utils/order-validation"
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
/** /**
* This step validates that a claim's shipping method can be updated. * This step validates that a claim's shipping method can be updated.
@@ -70,7 +72,13 @@ export const updateClaimShippingMethodWorkflow = createWorkflow(
): WorkflowResponse<OrderPreviewDTO> { ): WorkflowResponse<OrderPreviewDTO> {
const orderClaim: OrderClaimDTO = useRemoteQueryStep({ const orderClaim: OrderClaimDTO = useRemoteQueryStep({
entry_point: "order_claim", entry_point: "order_claim",
fields: ["id", "status", "order_id", "canceled_at"], fields: [
"id",
"status",
"order_id",
"canceled_at",
"order.currency_code",
],
variables: { id: input.claim_id }, variables: { id: input.claim_id },
list: false, list: false,
throw_if_key_not_found: true, throw_if_key_not_found: true,
@@ -89,36 +97,56 @@ export const updateClaimShippingMethodWorkflow = createWorkflow(
list: false, list: false,
}).config({ name: "order-change-query" }) }).config({ name: "order-change-query" })
updateClaimShippingMethodValidationStep({ orderClaim, orderChange, input }) const shippingOptions = when({ input }, ({ input }) => {
return input.data?.custom_amount === null
const updateData = transform( }).then(() => {
{ orderChange, input }, const action = transform(
({ input, orderChange }) => { { orderChange, input, orderClaim },
({ orderChange, input, orderClaim }) => {
const originalAction = (orderChange.actions ?? []).find( const originalAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id (a) => a.id === input.action_id
) as OrderChangeActionDTO ) as OrderChangeActionDTO
const data = input.data
const action = {
id: originalAction.id,
amount: data.custom_price,
internal_note: data.internal_note,
}
const shippingMethod = {
id: originalAction.reference_id,
amount: data.custom_price,
metadata: data.metadata,
}
return { return {
action, shipping_method_id: originalAction.reference_id,
shippingMethod, currency_code: (orderClaim as any).order.currency_code,
} }
} }
) )
const shippingMethod = useRemoteQueryStep({
entry_point: "order_shipping_method",
fields: ["id", "shipping_option_id"],
variables: {
id: action.shipping_method_id,
},
list: false,
}).config({ name: "fetch-shipping-method" })
return useRemoteQueryStep({
entry_point: "shipping_option",
fields: [
"id",
"name",
"calculated_price.calculated_amount",
"calculated_price.is_calculated_price_tax_inclusive",
],
variables: {
id: shippingMethod.shipping_option_id,
calculated_price: {
context: { currency_code: action.currency_code },
},
},
}).config({ name: "fetch-shipping-option" })
})
updateClaimShippingMethodValidationStep({ orderClaim, orderChange, input })
const updateData = transform(
{ orderChange, input, shippingOptions },
prepareShippingMethodUpdate
)
parallelize( parallelize(
updateOrderChangeActionsStep([updateData.action]), updateOrderChangeActionsStep([updateData.action]),
updateOrderShippingMethodsStep([updateData.shippingMethod!]) updateOrderShippingMethodsStep([updateData.shippingMethod!])
@@ -19,6 +19,7 @@ import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation" } from "../../utils/order-validation"
import { prepareShippingMethod } from "../../utils/prepare-shipping-method"
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions" import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
import { updateOrderTaxLinesWorkflow } from "../update-tax-lines" import { updateOrderTaxLinesWorkflow } from "../update-tax-lines"
@@ -53,7 +54,7 @@ export const createExchangeShippingMethodWorkflow = createWorkflow(
return_id?: string return_id?: string
exchange_id?: string exchange_id?: string
shipping_option_id: string shipping_option_id: string
custom_price?: BigNumberInput custom_amount?: BigNumberInput | null
}): WorkflowResponse<OrderPreviewDTO> { }): WorkflowResponse<OrderPreviewDTO> {
const orderExchange: OrderExchangeDTO = useRemoteQueryStep({ const orderExchange: OrderExchangeDTO = useRemoteQueryStep({
entry_point: "order_exchange", entry_point: "order_exchange",
@@ -108,29 +109,13 @@ export const createExchangeShippingMethodWorkflow = createWorkflow(
const shippingMethodInput = transform( const shippingMethodInput = transform(
{ {
orderExchange, relatedEntity: orderExchange,
shippingOptions, shippingOptions,
customPrice: input.custom_price, customPrice: input.custom_amount,
orderChange, orderChange,
input, input,
}, },
(data) => { prepareShippingMethod("exchange_id")
const option = data.shippingOptions[0]
const orderChange = data.orderChange
return {
shipping_option_id: option.id,
amount: data.customPrice ?? option.calculated_price.calculated_amount,
is_tax_inclusive:
!!option.calculated_price.is_calculated_price_tax_inclusive,
data: option.data ?? {},
name: option.name,
version: orderChange.version,
order_id: data.orderExchange.order_id,
return_id: input.return_id,
exchange_id: data.orderExchange.id,
}
}
) )
const createdMethods = createOrderShippingMethods({ const createdMethods = createOrderShippingMethods({
@@ -159,7 +144,7 @@ export const createExchangeShippingMethodWorkflow = createWorkflow(
orderExchange, orderExchange,
shippingOptions, shippingOptions,
createdMethods, createdMethods,
customPrice: input.custom_price, customPrice: input.custom_amount,
orderChange, orderChange,
input, input,
}, },
@@ -13,6 +13,7 @@ import {
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
when,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common" import { useRemoteQueryStep } from "../../../common"
import { import {
@@ -24,6 +25,7 @@ import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation" } from "../../utils/order-validation"
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
/** /**
* This step validates that an exchange's shipping method can be updated. * This step validates that an exchange's shipping method can be updated.
@@ -70,7 +72,13 @@ export const updateExchangeShippingMethodWorkflow = createWorkflow(
): WorkflowResponse<OrderPreviewDTO> { ): WorkflowResponse<OrderPreviewDTO> {
const orderExchange: OrderExchangeDTO = useRemoteQueryStep({ const orderExchange: OrderExchangeDTO = useRemoteQueryStep({
entry_point: "order_exchange", entry_point: "order_exchange",
fields: ["id", "status", "order_id", "canceled_at"], fields: [
"id",
"status",
"order_id",
"canceled_at",
"order.currency_code",
],
variables: { id: input.exchange_id }, variables: { id: input.exchange_id },
list: false, list: false,
throw_if_key_not_found: true, throw_if_key_not_found: true,
@@ -89,6 +97,49 @@ export const updateExchangeShippingMethodWorkflow = createWorkflow(
list: false, list: false,
}).config({ name: "order-change-query" }) }).config({ name: "order-change-query" })
const shippingOptions = when({ input }, ({ input }) => {
return input.data?.custom_amount === null
}).then(() => {
const action = transform(
{ orderChange, input, orderExchange },
({ orderChange, input, orderExchange }) => {
const originalAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
return {
shipping_method_id: originalAction.reference_id,
currency_code: (orderExchange as any).order.currency_code,
}
}
)
const shippingMethod = useRemoteQueryStep({
entry_point: "order_shipping_method",
fields: ["id", "shipping_option_id"],
variables: {
id: action.shipping_method_id,
},
list: false,
}).config({ name: "fetch-shipping-method" })
return useRemoteQueryStep({
entry_point: "shipping_option",
fields: [
"id",
"name",
"calculated_price.calculated_amount",
"calculated_price.is_calculated_price_tax_inclusive",
],
variables: {
id: shippingMethod.shipping_option_id,
calculated_price: {
context: { currency_code: action.currency_code },
},
},
}).config({ name: "fetch-shipping-option" })
})
updateExchangeShippingMethodValidationStep({ updateExchangeShippingMethodValidationStep({
orderExchange, orderExchange,
orderChange, orderChange,
@@ -96,31 +147,8 @@ export const updateExchangeShippingMethodWorkflow = createWorkflow(
}) })
const updateData = transform( const updateData = transform(
{ orderChange, input }, { orderChange, input, shippingOptions },
({ input, orderChange }) => { prepareShippingMethodUpdate
const originalAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
const data = input.data
const action = {
id: originalAction.id,
amount: data.custom_price,
internal_note: data.internal_note,
}
const shippingMethod = {
id: originalAction.reference_id,
amount: data.custom_price,
metadata: data.metadata,
}
return {
action,
shippingMethod,
}
}
) )
parallelize( parallelize(
@@ -18,6 +18,7 @@ import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation" } from "../../utils/order-validation"
import { prepareShippingMethod } from "../../utils/prepare-shipping-method"
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions" import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
import { updateOrderTaxLinesWorkflow } from "../update-tax-lines" import { updateOrderTaxLinesWorkflow } from "../update-tax-lines"
@@ -48,7 +49,7 @@ export const createOrderEditShippingMethodWorkflow = createWorkflow(
function (input: { function (input: {
order_id: string order_id: string
shipping_option_id: string shipping_option_id: string
custom_price?: BigNumberInput custom_amount?: BigNumberInput | null
}): WorkflowResponse<OrderPreviewDTO> { }): WorkflowResponse<OrderPreviewDTO> {
const order: OrderDTO = useRemoteQueryStep({ const order: OrderDTO = useRemoteQueryStep({
entry_point: "orders", entry_point: "orders",
@@ -89,25 +90,11 @@ export const createOrderEditShippingMethodWorkflow = createWorkflow(
const shippingMethodInput = transform( const shippingMethodInput = transform(
{ {
shippingOptions, shippingOptions,
customPrice: input.custom_price, customPrice: input.custom_amount,
orderChange, orderChange,
input, input,
}, },
(data) => { prepareShippingMethod()
const option = data.shippingOptions[0]
const orderChange = data.orderChange
return {
shipping_option_id: option.id,
amount: data.customPrice ?? option.calculated_price.calculated_amount,
is_tax_inclusive:
!!option.calculated_price.is_calculated_price_tax_inclusive,
data: option.data ?? {},
name: option.name,
version: orderChange.version,
order_id: data.input.order_id,
}
}
) )
const createdMethods = createOrderShippingMethods({ const createdMethods = createOrderShippingMethods({
@@ -130,7 +117,7 @@ export const createOrderEditShippingMethodWorkflow = createWorkflow(
order, order,
shippingOptions, shippingOptions,
createdMethods, createdMethods,
customPrice: input.custom_price, customPrice: input.custom_amount,
orderChange, orderChange,
input, input,
}, },
@@ -1,6 +1,7 @@
import { import {
OrderChangeActionDTO, OrderChangeActionDTO,
OrderChangeDTO, OrderChangeDTO,
OrderDTO,
OrderPreviewDTO, OrderPreviewDTO,
OrderWorkflow, OrderWorkflow,
} from "@medusajs/types" } from "@medusajs/types"
@@ -12,6 +13,7 @@ import {
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
when,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common" import { useRemoteQueryStep } from "../../../common"
import { import {
@@ -20,6 +22,7 @@ import {
} from "../../steps" } from "../../steps"
import { previewOrderChangeStep } from "../../steps/preview-order-change" import { previewOrderChangeStep } from "../../steps/preview-order-change"
import { throwIfOrderChangeIsNotActive } from "../../utils/order-validation" import { throwIfOrderChangeIsNotActive } from "../../utils/order-validation"
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
/** /**
* This step validates that an order edit's shipping method can be updated. * This step validates that an order edit's shipping method can be updated.
@@ -61,6 +64,14 @@ export const updateOrderEditShippingMethodWorkflow = createWorkflow(
function ( function (
input: WorkflowData<OrderWorkflow.UpdateOrderEditShippingMethodWorkflowInput> input: WorkflowData<OrderWorkflow.UpdateOrderEditShippingMethodWorkflowInput>
): WorkflowResponse<OrderPreviewDTO> { ): WorkflowResponse<OrderPreviewDTO> {
const order: OrderDTO = useRemoteQueryStep({
entry_point: "order_claim",
fields: ["id", "currency_code"],
variables: { id: input.order_id },
list: false,
throw_if_key_not_found: true,
})
const orderChange: OrderChangeDTO = useRemoteQueryStep({ const orderChange: OrderChangeDTO = useRemoteQueryStep({
entry_point: "order_change", entry_point: "order_change",
fields: ["id", "status", "version", "actions.*"], fields: ["id", "status", "version", "actions.*"],
@@ -73,37 +84,57 @@ export const updateOrderEditShippingMethodWorkflow = createWorkflow(
list: false, list: false,
}).config({ name: "order-change-query" }) }).config({ name: "order-change-query" })
const shippingOptions = when({ input }, ({ input }) => {
return input.data?.custom_amount === null
}).then(() => {
const action = transform(
{ orderChange, input, order },
({ orderChange, input, order }) => {
const originalAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
return {
shipping_method_id: originalAction.reference_id,
currency_code: order.currency_code,
}
}
)
const shippingMethod = useRemoteQueryStep({
entry_point: "order_shipping_method",
fields: ["id", "shipping_option_id"],
variables: {
id: action.shipping_method_id,
},
list: false,
}).config({ name: "fetch-shipping-method" })
return useRemoteQueryStep({
entry_point: "shipping_option",
fields: [
"id",
"name",
"calculated_price.calculated_amount",
"calculated_price.is_calculated_price_tax_inclusive",
],
variables: {
id: shippingMethod.shipping_option_id,
calculated_price: {
context: { currency_code: action.currency_code },
},
},
}).config({ name: "fetch-shipping-option" })
})
updateOrderEditShippingMethodValidationStep({ updateOrderEditShippingMethodValidationStep({
orderChange, orderChange,
input, input,
}) })
const updateData = transform( const updateData = transform(
{ orderChange, input }, { orderChange, input, shippingOptions },
({ input, orderChange }) => { prepareShippingMethodUpdate
const originalAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
const data = input.data
const action = {
id: originalAction.id,
amount: data.custom_price,
internal_note: data.internal_note,
}
const shippingMethod = {
id: originalAction.reference_id,
amount: data.custom_price,
metadata: data.metadata,
}
return {
action,
shippingMethod,
}
}
) )
parallelize( parallelize(
@@ -19,6 +19,7 @@ import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation" } from "../../utils/order-validation"
import { prepareShippingMethod } from "../../utils/prepare-shipping-method"
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions" import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
import { updateOrderTaxLinesWorkflow } from "../update-tax-lines" import { updateOrderTaxLinesWorkflow } from "../update-tax-lines"
@@ -54,11 +55,18 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
claim_id?: string claim_id?: string
exchange_id?: string exchange_id?: string
shipping_option_id: string shipping_option_id: string
custom_price?: BigNumberInput custom_amount?: BigNumberInput | null
}): WorkflowResponse<OrderPreviewDTO> { }): WorkflowResponse<OrderPreviewDTO> {
const orderReturn: ReturnDTO = useRemoteQueryStep({ const orderReturn: ReturnDTO = useRemoteQueryStep({
entry_point: "return", entry_point: "return",
fields: ["id", "status", "order_id", "canceled_at"], fields: [
"id",
"status",
"order_id",
"claim_id",
"exchange_id",
"canceled_at",
],
variables: { id: input.return_id }, variables: { id: input.return_id },
list: false, list: false,
throw_if_key_not_found: true, throw_if_key_not_found: true,
@@ -109,30 +117,13 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
const shippingMethodInput = transform( const shippingMethodInput = transform(
{ {
orderReturn, relatedEntity: orderReturn,
shippingOptions, shippingOptions,
customPrice: input.custom_price, customPrice: input.custom_amount,
orderChange, orderChange,
input, input,
}, },
(data) => { prepareShippingMethod("return_id")
const option = data.shippingOptions[0]
const orderChange = data.orderChange
return {
shipping_option_id: option.id,
amount: data.customPrice ?? option.calculated_price.calculated_amount,
is_tax_inclusive:
!!option.calculated_price.is_calculated_price_tax_inclusive,
data: option.data ?? {},
name: option.name,
version: orderChange.version,
order_id: data.orderReturn.order_id,
return_id: data.orderReturn.id,
claim_id: data.input.claim_id,
exchange_id: data.input.exchange_id,
}
}
) )
const createdMethods = createOrderShippingMethods({ const createdMethods = createOrderShippingMethods({
@@ -157,7 +148,7 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
orderReturn, orderReturn,
shippingOptions, shippingOptions,
createdMethods, createdMethods,
customPrice: input.custom_price, customPrice: input.custom_amount,
orderChange, orderChange,
input, input,
}, },
@@ -13,6 +13,7 @@ import {
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
when,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common" import { useRemoteQueryStep } from "../../../common"
import { import {
@@ -24,6 +25,7 @@ import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation" } from "../../utils/order-validation"
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
/** /**
* This step validates that a return's shipping method can be updated. * This step validates that a return's shipping method can be updated.
@@ -70,7 +72,13 @@ export const updateReturnShippingMethodWorkflow = createWorkflow(
): WorkflowResponse<OrderPreviewDTO> { ): WorkflowResponse<OrderPreviewDTO> {
const orderReturn: ReturnDTO = useRemoteQueryStep({ const orderReturn: ReturnDTO = useRemoteQueryStep({
entry_point: "return", entry_point: "return",
fields: ["id", "status", "order_id", "canceled_at"], fields: [
"id",
"status",
"order_id",
"canceled_at",
"order.currency_code",
],
variables: { id: input.return_id }, variables: { id: input.return_id },
list: false, list: false,
throw_if_key_not_found: true, throw_if_key_not_found: true,
@@ -89,6 +97,49 @@ export const updateReturnShippingMethodWorkflow = createWorkflow(
list: false, list: false,
}).config({ name: "order-change-query" }) }).config({ name: "order-change-query" })
const shippingOptions = when({ input }, ({ input }) => {
return input.data?.custom_amount === null
}).then(() => {
const action = transform(
{ orderChange, input, orderReturn },
({ orderChange, input, orderReturn }) => {
const originalAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
return {
shipping_method_id: originalAction.reference_id,
currency_code: (orderReturn as any).order.currency_code,
}
}
)
const shippingMethod = useRemoteQueryStep({
entry_point: "order_shipping_method",
fields: ["id", "shipping_option_id"],
variables: {
id: action.shipping_method_id,
},
list: false,
}).config({ name: "fetch-shipping-method" })
return useRemoteQueryStep({
entry_point: "shipping_option",
fields: [
"id",
"name",
"calculated_price.calculated_amount",
"calculated_price.is_calculated_price_tax_inclusive",
],
variables: {
id: shippingMethod.shipping_option_id,
calculated_price: {
context: { currency_code: action.currency_code },
},
},
}).config({ name: "fetch-shipping-option" })
})
updateReturnShippingMethodValidationStep({ updateReturnShippingMethodValidationStep({
orderReturn, orderReturn,
orderChange, orderChange,
@@ -96,31 +147,8 @@ export const updateReturnShippingMethodWorkflow = createWorkflow(
}) })
const updateData = transform( const updateData = transform(
{ orderChange, input }, { orderChange, input, shippingOptions },
({ input, orderChange }) => { prepareShippingMethodUpdate
const originalAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
const data = input.data
const action = {
id: originalAction.id,
amount: data.custom_price,
internal_note: data.internal_note,
}
const shippingMethod = {
id: originalAction.reference_id,
amount: data.custom_price,
metadata: data.metadata,
}
return {
action,
shippingMethod,
}
}
) )
parallelize( parallelize(
@@ -24,14 +24,14 @@ interface AdminClaimUpdateItem {
interface AdminClaimAddShippingMethod { interface AdminClaimAddShippingMethod {
shipping_option_id: string shipping_option_id: string
custom_price?: number custom_amount?: number
description?: string description?: string
internal_note?: string internal_note?: string
metadata?: Record<string, unknown> | null metadata?: Record<string, unknown> | null
} }
interface AdminClaimUpdateShippingMethod { interface AdminClaimUpdateShippingMethod {
custom_price?: number | null custom_amount?: number | null
internal_note?: string internal_note?: string
metadata?: Record<string, unknown> | null metadata?: Record<string, unknown> | null
} }
@@ -24,14 +24,14 @@ interface AdminExchangeUpdateItem {
interface AdminExchangeAddShippingMethod { interface AdminExchangeAddShippingMethod {
shipping_option_id: string shipping_option_id: string
custom_price?: number custom_amount?: number
description?: string description?: string
internal_note?: string internal_note?: string
metadata?: Record<string, unknown> | null metadata?: Record<string, unknown> | null
} }
interface AdminExchangeUpdateShippingMethod { interface AdminExchangeUpdateShippingMethod {
custom_price?: number | null custom_amount?: number | null
internal_note?: string internal_note?: string
metadata?: Record<string, unknown> | null metadata?: Record<string, unknown> | null
} }
@@ -27,14 +27,14 @@ export interface AdminUpdateReturnItems {
export interface AdminAddReturnShipping { export interface AdminAddReturnShipping {
shipping_option_id: string shipping_option_id: string
custom_price?: number custom_amount?: number
description?: string description?: string
internal_note?: string internal_note?: string
metadata?: Record<string, unknown> metadata?: Record<string, unknown>
} }
export interface AdminUpdateReturnShipping { export interface AdminUpdateReturnShipping {
custom_price?: number custom_amount?: number
internal_note?: string internal_note?: string
metadata?: Record<string, unknown> metadata?: Record<string, unknown>
} }
@@ -4,7 +4,7 @@ export interface UpdateReturnShippingMethodWorkflowInput {
return_id: string return_id: string
action_id: string action_id: string
data: { data: {
custom_price?: BigNumberInput custom_amount?: BigNumberInput | null
internal_note?: string | null internal_note?: string | null
metadata?: Record<string, any> | null metadata?: Record<string, any> | null
} }
@@ -19,7 +19,7 @@ export interface UpdateClaimShippingMethodWorkflowInput {
claim_id: string claim_id: string
action_id: string action_id: string
data: { data: {
custom_price?: BigNumberInput custom_amount?: BigNumberInput | null
internal_note?: string | null internal_note?: string | null
metadata?: Record<string, any> | null metadata?: Record<string, any> | null
} }
@@ -34,7 +34,7 @@ export interface UpdateExchangeShippingMethodWorkflowInput {
exchange_id: string exchange_id: string
action_id: string action_id: string
data: { data: {
custom_price?: BigNumberInput custom_amount?: BigNumberInput | null
internal_note?: string | null internal_note?: string | null
metadata?: Record<string, any> | null metadata?: Record<string, any> | null
} }
@@ -44,7 +44,7 @@ export interface UpdateOrderEditShippingMethodWorkflowInput {
order_id: string order_id: string
action_id: string action_id: string
data: { data: {
custom_price?: BigNumberInput custom_amount?: BigNumberInput | null
internal_note?: string | null internal_note?: string | null
metadata?: Record<string, any> | null metadata?: Record<string, any> | null
} }
@@ -91,7 +91,7 @@ export type AdminPostCancelClaimReqSchemaType = z.infer<
export const AdminPostClaimsShippingReqSchema = z.object({ export const AdminPostClaimsShippingReqSchema = z.object({
shipping_option_id: z.string(), shipping_option_id: z.string(),
custom_price: z.number().optional(), custom_amount: z.number().optional(),
description: z.string().optional(), description: z.string().optional(),
internal_note: z.string().optional(), internal_note: z.string().optional(),
metadata: z.record(z.unknown()).optional(), metadata: z.record(z.unknown()).optional(),
@@ -102,7 +102,7 @@ export type AdminPostClaimsShippingReqSchemaType = z.infer<
> >
export const AdminPostClaimsShippingActionReqSchema = z.object({ export const AdminPostClaimsShippingActionReqSchema = z.object({
custom_price: z.number().optional(), custom_amount: z.number().nullish().optional(),
internal_note: z.string().nullish().optional(), internal_note: z.string().nullish().optional(),
metadata: z.record(z.unknown()).nullish().optional(), metadata: z.record(z.unknown()).nullish().optional(),
}) })
@@ -89,7 +89,7 @@ export type AdminPostExchangesRequestItemsReturnActionReqSchemaType = z.infer<
export const AdminPostExchangesShippingReqSchema = z.object({ export const AdminPostExchangesShippingReqSchema = z.object({
shipping_option_id: z.string(), shipping_option_id: z.string(),
custom_price: z.number().optional(), custom_amount: z.number().optional(),
description: z.string().optional(), description: z.string().optional(),
internal_note: z.string().optional(), internal_note: z.string().optional(),
metadata: z.record(z.unknown()).optional(), metadata: z.record(z.unknown()).optional(),
@@ -100,7 +100,7 @@ export type AdminPostExchangesShippingReqSchemaType = z.infer<
> >
export const AdminPostExchangesShippingActionReqSchema = z.object({ export const AdminPostExchangesShippingActionReqSchema = z.object({
custom_price: z.number().optional(), custom_amount: z.number().nullish().optional(),
internal_note: z.string().nullish().optional(), internal_note: z.string().nullish().optional(),
metadata: z.record(z.unknown()).nullish().optional(), metadata: z.record(z.unknown()).nullish().optional(),
}) })
@@ -12,7 +12,7 @@ export type AdminPostOrderEditsReqSchemaType = z.infer<
export const AdminPostOrderEditsShippingReqSchema = z.object({ export const AdminPostOrderEditsShippingReqSchema = z.object({
shipping_option_id: z.string(), shipping_option_id: z.string(),
custom_price: z.number().optional(), custom_amount: z.number().optional(),
description: z.string().optional(), description: z.string().optional(),
internal_note: z.string().optional(), internal_note: z.string().optional(),
metadata: z.record(z.unknown()).optional(), metadata: z.record(z.unknown()).optional(),
@@ -23,7 +23,7 @@ export type AdminPostOrderEditsShippingReqSchemaType = z.infer<
> >
export const AdminPostOrderEditsShippingActionReqSchema = z.object({ export const AdminPostOrderEditsShippingActionReqSchema = z.object({
custom_price: z.number().optional(), custom_amount: z.number().nullish().optional(),
internal_note: z.string().nullish().optional(), internal_note: z.string().nullish().optional(),
metadata: z.record(z.unknown()).nullish().optional(), metadata: z.record(z.unknown()).nullish().optional(),
}) })
@@ -100,7 +100,7 @@ export type AdminPostCancelReturnReqSchemaType = z.infer<
export const AdminPostReturnsShippingReqSchema = z.object({ export const AdminPostReturnsShippingReqSchema = z.object({
shipping_option_id: z.string(), shipping_option_id: z.string(),
custom_price: z.number().optional(), custom_amount: z.number().optional(),
description: z.string().optional(), description: z.string().optional(),
internal_note: z.string().optional(), internal_note: z.string().optional(),
metadata: z.record(z.unknown()).optional(), metadata: z.record(z.unknown()).optional(),
@@ -111,7 +111,7 @@ export type AdminPostReturnsShippingReqSchemaType = z.infer<
> >
export const AdminPostReturnsShippingActionReqSchema = z.object({ export const AdminPostReturnsShippingActionReqSchema = z.object({
custom_price: z.number().optional(), custom_amount: z.number().nullish().optional(),
internal_note: z.string().nullish().optional(), internal_note: z.string().nullish().optional(),
metadata: z.record(z.unknown()).nullish().optional(), metadata: z.record(z.unknown()).nullish().optional(),
}) })
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,17 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20240902195921 extends Migration {
async up(): Promise<void> {
this.addSql('alter table if exists "order_line_item" add column if not exists "is_custom_price" boolean not null default false;');
this.addSql('alter table if exists "order_shipping_method" add column if not exists "is_custom_amount" boolean not null default false;');
}
async down(): Promise<void> {
this.addSql('alter table if exists "order_line_item" drop column if exists "is_custom_price";');
this.addSql('alter table if exists "order_shipping_method" drop column if exists "is_custom_amount";');
}
}
@@ -128,6 +128,9 @@ export default class OrderLineItem {
@Property({ columnType: "jsonb" }) @Property({ columnType: "jsonb" })
raw_unit_price: BigNumberRawValue raw_unit_price: BigNumberRawValue
@Property({ columnType: "boolean", default: false })
is_custom_price: boolean = false
@OneToMany(() => OrderLineItemTaxLine, (taxLine) => taxLine.item, { @OneToMany(() => OrderLineItemTaxLine, (taxLine) => taxLine.item, {
cascade: [Cascade.PERSIST, "soft-remove" as Cascade], cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
}) })
@@ -54,6 +54,9 @@ export default class OrderShippingMethod {
@Property({ columnType: "boolean" }) @Property({ columnType: "boolean" })
is_tax_inclusive: boolean = false is_tax_inclusive: boolean = false
@Property({ columnType: "boolean", default: false })
is_custom_amount: boolean = false
@Property({ @Property({
columnType: "text", columnType: "text",
nullable: true, nullable: true,