feat(core-flows,medusa,pricing,types,utils): added price list workflows + endpoints (#6648)
Apologies for the giant PR in advance, but most of these are removing of files and migrations from old workflows and routes to new. What: - Adds CRUD endpoints for price lists - Migrate workflows from old sdk to new - Added missing updatePriceListPrices method to pricing module
This commit is contained in:
@@ -3,6 +3,9 @@ import { isObject } from "./is-object"
|
||||
|
||||
export function getSelectsAndRelationsFromObjectArray(
|
||||
dataArray: object[],
|
||||
options: { objectFields: string[] } = {
|
||||
objectFields: [],
|
||||
},
|
||||
prefix?: string
|
||||
): {
|
||||
selects: string[]
|
||||
@@ -13,10 +16,11 @@ export function getSelectsAndRelationsFromObjectArray(
|
||||
|
||||
for (const data of dataArray) {
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
if (isObject(value)) {
|
||||
if (isObject(value) && !options.objectFields.includes(key)) {
|
||||
relations.push(setKey(key, prefix))
|
||||
const res = getSelectsAndRelationsFromObjectArray(
|
||||
[value],
|
||||
options,
|
||||
setKey(key, prefix)
|
||||
)
|
||||
selects.push(...res.selects)
|
||||
@@ -25,6 +29,7 @@ export function getSelectsAndRelationsFromObjectArray(
|
||||
relations.push(setKey(key, prefix))
|
||||
const res = getSelectsAndRelationsFromObjectArray(
|
||||
value,
|
||||
options,
|
||||
setKey(key, prefix)
|
||||
)
|
||||
selects.push(...res.selects)
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
PriceListRuleDTO,
|
||||
PriceRuleDTO,
|
||||
PriceSetMoneyAmountDTO,
|
||||
ProductVariantDTO,
|
||||
UpdatePriceListPriceDTO,
|
||||
} from "@medusajs/types"
|
||||
|
||||
export function buildPriceListRules(
|
||||
priceListRules: PriceListRuleDTO[]
|
||||
): Record<string, string[]> {
|
||||
return priceListRules.reduce((acc, curr) => {
|
||||
const ruleAttribute = curr.rule_type.rule_attribute
|
||||
const ruleValues = curr.price_list_rule_values || []
|
||||
|
||||
acc[ruleAttribute] = ruleValues.map((ruleValue) => ruleValue.value)
|
||||
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
export function buildPriceSetRules(
|
||||
priceRules: PriceRuleDTO[]
|
||||
): Record<string, string> {
|
||||
return priceRules.reduce((acc, curr) => {
|
||||
const ruleAttribute = curr.rule_type.rule_attribute
|
||||
const ruleValue = curr.value
|
||||
|
||||
acc[ruleAttribute] = ruleValue
|
||||
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
export function buildPriceSetPricesForCore(
|
||||
priceSetMoneyAmounts: (PriceSetMoneyAmountDTO & {
|
||||
price_set: PriceSetMoneyAmountDTO["price_set"] & {
|
||||
variant?: ProductVariantDTO
|
||||
}
|
||||
})[]
|
||||
): Record<string, any>[] {
|
||||
return priceSetMoneyAmounts.map((priceSetMoneyAmount) => {
|
||||
const productVariant = (priceSetMoneyAmount.price_set as any).variant
|
||||
const rules: Record<string, string> = priceSetMoneyAmount.price_rules
|
||||
? buildPriceSetRules(priceSetMoneyAmount.price_rules)
|
||||
: {}
|
||||
|
||||
return {
|
||||
...priceSetMoneyAmount.money_amount,
|
||||
variant_id: productVariant?.id ?? null,
|
||||
rules,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function buildPriceSetPricesForModule(
|
||||
priceSetMoneyAmounts: PriceSetMoneyAmountDTO[]
|
||||
): UpdatePriceListPriceDTO[] {
|
||||
return priceSetMoneyAmounts.map((priceSetMoneyAmount) => {
|
||||
const rules: Record<string, string> = priceSetMoneyAmount.price_rules
|
||||
? buildPriceSetRules(priceSetMoneyAmount.price_rules)
|
||||
: {}
|
||||
|
||||
return {
|
||||
...priceSetMoneyAmount.money_amount!,
|
||||
price_set_id: priceSetMoneyAmount.price_set!?.id!,
|
||||
rules,
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from "./builders"
|
||||
export * from "./price-list"
|
||||
export * from "./rule-type"
|
||||
|
||||
Reference in New Issue
Block a user