@@ -265,3 +290,89 @@ export function EditShippingOptionsPricingForm({
)
}
+
+const findRuleValue = (
+ rules: HttpTypes.AdminShippingOptionPriceRule[],
+ operator: string
+) => {
+ const fallbackValue = ["eq", "gt", "lt"].includes(operator) ? undefined : null
+
+ return (
+ rules?.find(
+ (r) => r.attribute === ITEM_TOTAL_ATTRIBUTE && r.operator === operator
+ )?.value || fallbackValue
+ )
+}
+
+const mapToConditionalPrice = (
+ price: HttpTypes.AdminShippingOptionPrice
+): UpdateConditionalPrice => {
+ const rules = price.price_rules || []
+
+ return {
+ id: price.id,
+ amount: price.amount,
+ gte: findRuleValue(rules, "gte"),
+ lte: findRuleValue(rules, "lte"),
+ gt: findRuleValue(rules, "gt") as undefined | null,
+ lt: findRuleValue(rules, "lt") as undefined | null,
+ eq: findRuleValue(rules, "eq") as undefined | null,
+ }
+}
+
+const getDefaultValues = (prices: HttpTypes.AdminShippingOptionPrice[]) => {
+ const hasAttributes = (
+ price: HttpTypes.AdminShippingOptionPrice,
+ required: string[],
+ forbidden: string[] = []
+ ) => {
+ const attributes = price.price_rules?.map((r) => r.attribute) || []
+ return (
+ required.every((attr) => attributes.includes(attr)) &&
+ !forbidden.some((attr) => attributes.includes(attr))
+ )
+ }
+
+ const currency_prices: Record = {}
+ const conditional_currency_prices: Record =
+ {}
+ const region_prices: Record = {}
+ const conditional_region_prices: Record = {}
+
+ prices.forEach((price) => {
+ if (!price.price_rules?.length) {
+ currency_prices[price.currency_code!] = price.amount
+ return
+ }
+
+ if (hasAttributes(price, [ITEM_TOTAL_ATTRIBUTE], [REGION_ID_ATTRIBUTE])) {
+ const code = price.currency_code!
+ if (!conditional_currency_prices[code]) {
+ conditional_currency_prices[code] = []
+ }
+ conditional_currency_prices[code].push(mapToConditionalPrice(price))
+ return
+ }
+
+ if (hasAttributes(price, [REGION_ID_ATTRIBUTE], [ITEM_TOTAL_ATTRIBUTE])) {
+ const regionId = price.price_rules[0].value
+ region_prices[regionId] = price.amount
+ return
+ }
+
+ if (hasAttributes(price, [REGION_ID_ATTRIBUTE, ITEM_TOTAL_ATTRIBUTE])) {
+ const regionId = price.price_rules[0].value
+ if (!conditional_region_prices[regionId]) {
+ conditional_region_prices[regionId] = []
+ }
+ conditional_region_prices[regionId].push(mapToConditionalPrice(price))
+ }
+ })
+
+ return {
+ currency_prices,
+ conditional_currency_prices,
+ region_prices,
+ conditional_region_prices,
+ }
+}
diff --git a/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-pricing/location-service-zone-shipping-option-pricing.tsx b/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-pricing/location-service-zone-shipping-option-pricing.tsx
index 9cd94ca5687cd..066940c15f7cb 100644
--- a/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-pricing/location-service-zone-shipping-option-pricing.tsx
+++ b/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-pricing/location-service-zone-shipping-option-pricing.tsx
@@ -14,10 +14,13 @@ export function LocationServiceZoneShippingOptionPricing() {
})
}
- const { shipping_option: shippingOption, isError, error } =
- useShippingOption(so_id, {
- fields: "*prices,*prices.price_rules",
- })
+ const {
+ shipping_option: shippingOption,
+ isError,
+ error,
+ } = useShippingOption(so_id, {
+ fields: "*prices,*prices.price_rules",
+ })
if (isError) {
throw error
diff --git a/packages/core/types/src/http/shipping-option/admin/entities.ts b/packages/core/types/src/http/shipping-option/admin/entities.ts
index d22eac5c52e0e..cab9ad9429c27 100644
--- a/packages/core/types/src/http/shipping-option/admin/entities.ts
+++ b/packages/core/types/src/http/shipping-option/admin/entities.ts
@@ -30,7 +30,14 @@ export interface AdminShippingOptionRule {
// TODO: This type is complete, but it's not clear what the `rules` field is supposed to return in all cases.
export interface AdminShippingOptionPriceRule {
id: string
- value: string
+ value: string | number
+ operator: RuleOperatorType
+ attribute: string
+ price_id: string
+ priority: number
+ created_at: string
+ updated_at: string
+ deleted_at: string | null
}
export interface AdminShippingOptionPrice extends AdminPrice {
diff --git a/packages/core/types/src/http/shipping-option/admin/payloads.ts b/packages/core/types/src/http/shipping-option/admin/payloads.ts
index c0297dde6b794..514226f795494 100644
--- a/packages/core/types/src/http/shipping-option/admin/payloads.ts
+++ b/packages/core/types/src/http/shipping-option/admin/payloads.ts
@@ -13,12 +13,24 @@ export interface AdminCreateShippingOptionType {
code: string
}
-export interface AdminCreateShippingOptionPriceWithCurrency {
+interface AdminShippingOptionPriceRulePayload {
+ operator: string
+ attribute: string
+ value: string | string[] | number
+}
+
+interface AdminShippingOptionPriceWithRules {
+ rules?: AdminShippingOptionPriceRulePayload[]
+}
+
+export interface AdminCreateShippingOptionPriceWithCurrency
+ extends AdminShippingOptionPriceWithRules {
currency_code: string
amount: number
}
-export interface AdminCreateShippingOptionPriceWithRegion {
+export interface AdminCreateShippingOptionPriceWithRegion
+ extends AdminShippingOptionPriceWithRules {
region_id: string
amount: number
}
@@ -43,13 +55,15 @@ export interface AdminUpdateShippingOptionRule
id: string
}
-export interface AdminUpdateShippingOptionPriceWithCurrency {
+export interface AdminUpdateShippingOptionPriceWithCurrency
+ extends AdminShippingOptionPriceWithRules {
id?: string
currency_code?: string
amount?: number
}
-export interface AdminUpdateShippingOptionPriceWithRegion {
+export interface AdminUpdateShippingOptionPriceWithRegion
+ extends AdminShippingOptionPriceWithRules {
id?: string
region_id?: string
amount?: number