feat(core-flows,types,medusa): ability to update/create custom shipping prices (#10368)
This commit is contained in:
@@ -1,19 +1,23 @@
|
||||
import {
|
||||
CreatePriceSetDTO,
|
||||
CreatePriceSetPriceRules,
|
||||
IPricingModuleService,
|
||||
IRegionModuleService,
|
||||
PriceRule,
|
||||
} from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
|
||||
import { isString, Modules } from "@medusajs/framework/utils"
|
||||
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
|
||||
|
||||
export interface ShippingOptionsPriceCurrencyCode {
|
||||
currency_code: string
|
||||
amount: number
|
||||
rules?: PriceRule[]
|
||||
}
|
||||
|
||||
interface ShippingOptionsPriceRegionId {
|
||||
region_id: string
|
||||
amount: number
|
||||
rules?: PriceRule[]
|
||||
}
|
||||
|
||||
export type CreateShippingOptionsPriceSetsStepInput = {
|
||||
@@ -21,15 +25,36 @@ export type CreateShippingOptionsPriceSetsStepInput = {
|
||||
prices: (ShippingOptionsPriceCurrencyCode | ShippingOptionsPriceRegionId)[]
|
||||
}[]
|
||||
|
||||
function buildPriceSet(
|
||||
export function buildPriceSet(
|
||||
prices: CreateShippingOptionsPriceSetsStepInput[0]["prices"],
|
||||
regionToCurrencyMap: Map<string, string>
|
||||
): CreatePriceSetDTO {
|
||||
const shippingOptionPrices = prices.map((price) => {
|
||||
const { rules = [] } = price
|
||||
const additionalRules: CreatePriceSetPriceRules = {}
|
||||
|
||||
for (const rule of rules) {
|
||||
let existingPriceRules = additionalRules[rule.attribute]
|
||||
|
||||
if (isString(existingPriceRules)) {
|
||||
continue
|
||||
}
|
||||
|
||||
existingPriceRules ||= []
|
||||
|
||||
existingPriceRules.push({
|
||||
operator: rule.operator,
|
||||
value: rule.value,
|
||||
})
|
||||
|
||||
additionalRules[rule.attribute] = existingPriceRules
|
||||
}
|
||||
|
||||
if ("currency_code" in price) {
|
||||
return {
|
||||
currency_code: price.currency_code,
|
||||
amount: price.amount,
|
||||
rules: additionalRules,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +63,7 @@ function buildPriceSet(
|
||||
amount: price.amount,
|
||||
rules: {
|
||||
region_id: price.region_id,
|
||||
...additionalRules,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
CreatePriceDTO,
|
||||
CreatePriceSetPriceRules,
|
||||
CreatePricesDTO,
|
||||
FulfillmentWorkflow,
|
||||
IPricingModuleService,
|
||||
@@ -13,6 +14,7 @@ import {
|
||||
LINKS,
|
||||
Modules,
|
||||
isDefined,
|
||||
isString,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
|
||||
|
||||
@@ -64,6 +66,26 @@ function buildPrices(
|
||||
}
|
||||
|
||||
const shippingOptionPrices = prices.map((price) => {
|
||||
const { rules = [] } = price
|
||||
const additionalRules: CreatePriceSetPriceRules = {}
|
||||
|
||||
for (const rule of rules) {
|
||||
let existingPriceRules = additionalRules[rule.attribute]
|
||||
|
||||
if (isString(existingPriceRules)) {
|
||||
continue
|
||||
}
|
||||
|
||||
existingPriceRules ||= []
|
||||
|
||||
existingPriceRules.push({
|
||||
operator: rule.operator,
|
||||
value: rule.value,
|
||||
})
|
||||
|
||||
additionalRules[rule.attribute] = existingPriceRules
|
||||
}
|
||||
|
||||
if ("region_id" in price) {
|
||||
const currency_code = regionToCurrencyMap.get(price.region_id!)!
|
||||
const regionId = price.region_id
|
||||
@@ -74,6 +96,17 @@ function buildPrices(
|
||||
amount: price.amount,
|
||||
rules: {
|
||||
region_id: regionId,
|
||||
...additionalRules,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if ("currency_code" in price) {
|
||||
return {
|
||||
...price,
|
||||
amount: price.amount,
|
||||
rules: {
|
||||
...additionalRules,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -142,8 +175,10 @@ export const setShippingOptionsPricesStep = createStep(
|
||||
price_set_id: currentShippingOptionDataItem.price_set_id,
|
||||
}
|
||||
})
|
||||
|
||||
const buildPricesData =
|
||||
pricesData && buildPrices(pricesData, regionToCurrencyMap)
|
||||
|
||||
return [
|
||||
currentShippingOptionDataItem.shipping_option_id,
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user