Skip to content

Commit

Permalink
feat(core-flows): custom price flag for order line items and shipping…
Browse files Browse the repository at this point in the history
… methods (#8969)

CLOSES: CC-402
  • Loading branch information
carlos-r-l-rodrigues authored Sep 4, 2024
1 parent 0fe1201 commit 2a055b7
Show file tree
Hide file tree
Showing 28 changed files with 2,303 additions and 2,095 deletions.
31 changes: 29 additions & 2 deletions integration-tests/http/__tests__/claims/claims.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,23 +594,50 @@ medusaIntegrationTestRunner({
adminHeaders
)

// shipping Options w/ custom price
const {
data: {
order_preview: { shipping_methods: outboundShippingMethods },
},
} = await api.post(
`/admin/claims/${claimId}/outbound/shipping-method`,
{ shipping_option_id: outboundShippingOption.id },
{
shipping_option_id: outboundShippingOption.id,
custom_amount: 12.5,
},
adminHeaders
)

const outboundShippingMethod = outboundShippingMethods.find(
(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
await api.delete(
`/admin/claims/${claimId}/outbound/shipping-method/${outboundShippingMethod.actions[0].id}`,
`/admin/claims/${claimId}/outbound/shipping-method/${outboundShippingMethodReset.actions[0].id}`,
adminHeaders
)

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/http/__tests__/returns/returns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ medusaIntegrationTestRunner({
result = await api.post(
`/admin/returns/${returnId}/shipping-method/${updateShippingActionId}`,
{
custom_price: 1002,
custom_amount: 1002,
internal_note: "cx agent note",
},
adminHeaders
Expand Down
1 change: 1 addition & 0 deletions integration-tests/modules/__tests__/order/order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ medusaIntegrationTestRunner({
value: "50",
precision: 20,
},
is_custom_price: false,
metadata: null,
created_at: expect.any(String),
updated_at: expect.any(String),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ medusaIntegrationTestRunner({
input: {
return_id: returnOrder.id,
shipping_option_id: shippingOptionId,
custom_price: 20,
custom_amount: 20,
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ export const ClaimCreateForm = ({
updateInboundShipping(
{
actionId,
custom_price: customPrice,
custom_amount: customPrice,
},
{
onError: (error) => {
Expand Down Expand Up @@ -916,7 +916,7 @@ export const ClaimCreateForm = ({
updateOutboundShipping(
{
actionId,
custom_price: customPrice,
custom_amount: customPrice,
},
{
onError: (error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export const ExchangeCreateForm = ({
updateInboundShipping(
{
actionId,
custom_price: customPrice,
custom_amount: customPrice,
},
{
onError: (error) => {
Expand Down Expand Up @@ -435,7 +435,7 @@ export const ExchangeCreateForm = ({
updateOutboundShipping(
{
actionId,
custom_price: customPrice,
custom_amount: customPrice,
},
{
onError: (error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ export const ReturnCreateForm = ({
if (actionId) {
updateReturnShipping({
actionId,
custom_price:
custom_amount:
typeof customShippingAmount === "string"
? null
: customShippingAmount,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
throwIfIsCancelled,
throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation"
import { prepareShippingMethod } from "../../utils/prepare-shipping-method"
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
import { updateOrderTaxLinesWorkflow } from "../update-tax-lines"

Expand Down Expand Up @@ -53,7 +54,7 @@ export const createClaimShippingMethodWorkflow = createWorkflow(
return_id?: string
claim_id?: string
shipping_option_id: string
custom_price?: BigNumberInput
custom_amount?: BigNumberInput | null
}): WorkflowResponse<OrderPreviewDTO> {
const orderClaim: OrderClaimDTO = useRemoteQueryStep({
entry_point: "order_claim",
Expand All @@ -65,7 +66,7 @@ export const createClaimShippingMethodWorkflow = createWorkflow(

const order: OrderDTO = useRemoteQueryStep({
entry_point: "orders",
fields: ["id", "status", "currency_code", "canceled_at"],
fields: ["id", "status", "region_id", "currency_code", "canceled_at"],
variables: { id: orderClaim.order_id },
list: false,
throw_if_key_not_found: true,
Expand Down Expand Up @@ -104,29 +105,13 @@ export const createClaimShippingMethodWorkflow = createWorkflow(

const shippingMethodInput = transform(
{
orderClaim,
relatedEntity: orderClaim,
shippingOptions,
customPrice: input.custom_price,
customPrice: input.custom_amount,
orderChange,
input,
},
(data) => {
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,
}
}
prepareShippingMethod("claim_id")
)

const createdMethods = createOrderShippingMethods({
Expand Down Expand Up @@ -155,7 +140,7 @@ export const createClaimShippingMethodWorkflow = createWorkflow(
orderClaim,
shippingOptions,
createdMethods,
customPrice: input.custom_price,
customPrice: input.custom_amount,
orderChange,
input,
},
Expand All @@ -170,6 +155,7 @@ export const createClaimShippingMethodWorkflow = createWorkflow(
}) => {
const shippingOption = shippingOptions[0]
const createdMethod = createdMethods[0]

const methodPrice =
customPrice ?? shippingOption.calculated_price.calculated_amount

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
createWorkflow,
parallelize,
transform,
when,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common"
import {
Expand All @@ -24,6 +25,7 @@ import {
throwIfIsCancelled,
throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation"
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"

/**
* This step validates that a claim's shipping method can be updated.
Expand Down Expand Up @@ -70,7 +72,13 @@ export const updateClaimShippingMethodWorkflow = createWorkflow(
): WorkflowResponse<OrderPreviewDTO> {
const orderClaim: OrderClaimDTO = useRemoteQueryStep({
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 },
list: false,
throw_if_key_not_found: true,
Expand All @@ -89,34 +97,54 @@ export const updateClaimShippingMethodWorkflow = createWorkflow(
list: false,
}).config({ name: "order-change-query" })

updateClaimShippingMethodValidationStep({ orderClaim, orderChange, input })
const shippingOptions = when({ input }, ({ input }) => {
return input.data?.custom_amount === null
}).then(() => {
const action = transform(
{ orderChange, input, orderClaim },
({ orderChange, input, orderClaim }) => {
const originalAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO

const updateData = transform(
{ orderChange, input },
({ input, orderChange }) => {
const originalAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
return {
shipping_method_id: originalAction.reference_id,
currency_code: (orderClaim as any).order.currency_code,
}
}
)

const data = input.data
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" })

const action = {
id: originalAction.id,
amount: data.custom_price,
internal_note: data.internal_note,
}
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" })
})

const shippingMethod = {
id: originalAction.reference_id,
amount: data.custom_price,
metadata: data.metadata,
}
updateClaimShippingMethodValidationStep({ orderClaim, orderChange, input })

return {
action,
shippingMethod,
}
}
const updateData = transform(
{ orderChange, input, shippingOptions },
prepareShippingMethodUpdate
)

parallelize(
Expand Down
Loading

0 comments on commit 2a055b7

Please sign in to comment.