Skip to content

Commit

Permalink
fix: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
fPolic committed Dec 10, 2024
1 parent 2e2de63 commit 174d2e2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import {
} from "@medusajs/framework/utils"
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"

type OptionsInput = (
| FulfillmentWorkflow.CreateShippingOptionsWorkflowInput
| FulfillmentWorkflow.UpdateShippingOptionsWorkflowInput
)[]

export const validateShippingOptionPricesStepId =
"validate-shipping-option-prices"

Expand All @@ -17,13 +22,7 @@ export const validateShippingOptionPricesStepId =
*/
export const validateShippingOptionPricesStep = createStep(
validateShippingOptionPricesStepId,
async (
options: (
| FulfillmentWorkflow.CreateShippingOptionsWorkflowInput
| FulfillmentWorkflow.UpdateShippingOptionsWorkflowInput
)[],
{ container }
) => {
async (options: OptionsInput, { container }) => {
const fulfillmentModuleService = container.resolve(Modules.FULFILLMENT)

const optionIds = options.map(
Expand Down Expand Up @@ -61,17 +60,18 @@ export const validateShippingOptionPricesStep = createStep(
})
}

const flatRatePrices = options.flatMap((option) =>
option.price_type === ShippingOptionPriceType.FLAT
? ((option.prices ?? []) as { region_id: string; amount: number }[])
: []
)
const flatRatePrices: FulfillmentWorkflow.UpdateShippingOptionPriceRecord[] =
[]
const calculatedOptions: OptionsInput = []

const calculatedOptions = options
.filter(
(option) => option.price_type === ShippingOptionPriceType.CALCULATED
)
.flatMap((option) => option ?? [])
options.forEach((option) => {
if (option.price_type === ShippingOptionPriceType.FLAT) {
flatRatePrices.push(...(option.prices ?? []))
}
if (option.price_type === ShippingOptionPriceType.CALCULATED) {
calculatedOptions.push(option)
}
})

await fulfillmentModuleService.validateShippingOptionsForPriceCalculation(
calculatedOptions as FulfillmentWorkflow.CreateShippingOptionsWorkflowInput[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ export const updateShippingOptionsWorkflow = createWorkflow(

const data = transform(input, (data) => {
const shippingOptionsIndexToPrices = data.map((option, index) => {
const prices = (option as any).prices
delete (option as any).prices
const prices = (
option as FulfillmentWorkflow.UpdateFlatRateShippingOptionInput
).prices

delete (option as FulfillmentWorkflow.UpdateFlatRateShippingOptionInput)
.prices

/**
* When we are updating an option to be calculated, remove the prices.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { ShippingOptionDTO } from "../../fulfillment"
import { RuleOperatorType } from "../../common"

export type CreateShippingOptionsWorkflowInput = {
type CreateFlatRateShippingOptionPriceRecord =
| {
currency_code: string
amount: number
}
| {
region_id: string
amount: number
}

type CreateFlatShippingOptionInputBase = {
name: string
service_zone_id: string
shipping_profile_id: string
Expand All @@ -17,21 +27,19 @@ export type CreateShippingOptionsWorkflowInput = {
operator: RuleOperatorType
value: string | string[]
}[]
} & (
| { price_type: "calculated" }
| {
price_type: "flat"
prices: (
| {
currency_code: string
amount: number
}
| {
region_id: string
amount: number
}
)[]
}
)
}

type CreateFlatRateShippingOptionInput = CreateFlatShippingOptionInputBase & {
price_type: "flat"
prices: CreateFlatRateShippingOptionPriceRecord[]
}

type CreateCalculatedShippingOptionInput = CreateFlatShippingOptionInputBase & {
price_type: "calculated"
}

export type CreateShippingOptionsWorkflowInput =
| CreateFlatRateShippingOptionInput
| CreateCalculatedShippingOptionInput

export type CreateShippingOptionsWorkflowOutput = ShippingOptionDTO[]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RuleOperatorType } from "../../common"
import { PriceRule } from "../../pricing"

export type UpdateShippingOptionsWorkflowInput = {
type UpdateFlatShippingOptionInputBase = {
id: string
name?: string
service_zone_id?: string
Expand All @@ -18,13 +18,7 @@ export type UpdateShippingOptionsWorkflowInput = {
operator: RuleOperatorType
value: string | string[]
}[]
} & (
| { price_type?: "calculated" }
| {
price_type?: "flat"
prices?: UpdateShippingOptionPriceRecord[]
}
)
}

export type UpdateShippingOptionPriceRecord =
| {
Expand All @@ -40,6 +34,21 @@ export type UpdateShippingOptionPriceRecord =
rules?: PriceRule[]
}

export type UpdateCalculatedShippingOptionInput =
UpdateFlatShippingOptionInputBase & {
price_type?: "calculated"
}

export type UpdateFlatRateShippingOptionInput =
UpdateFlatShippingOptionInputBase & {
price_type?: "flat"
prices?: UpdateShippingOptionPriceRecord[]
}

export type UpdateShippingOptionsWorkflowInput =
| UpdateFlatRateShippingOptionInput
| UpdateCalculatedShippingOptionInput

export type UpdateShippingOptionsWorkflowOutput = {
id: string
}[]

0 comments on commit 174d2e2

Please sign in to comment.