feat(core-flows,pricing,medusa,pricing,types,utils): Price List Prices can have their own rules (#5752)
**What** - Add price-rules for prices in price-lists - make rules object optional when creating prices **Why** - more price granularity Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
This commit is contained in:
@@ -16,6 +16,7 @@ export async function prepareCreatePriceLists({
|
||||
})[]
|
||||
}>): Promise<Result | void> {
|
||||
const remoteQuery = container.resolve("remoteQuery")
|
||||
const regionService = container.resolve("regionService")
|
||||
|
||||
const { price_lists } = data
|
||||
|
||||
@@ -59,6 +60,17 @@ export async function prepareCreatePriceLists({
|
||||
)
|
||||
}
|
||||
|
||||
const regionIds = price_lists
|
||||
.map(({ prices }) => prices?.map((price) => price.region_id) ?? [])
|
||||
.flat(2)
|
||||
const regions = await regionService.list({ id: regionIds }, {})
|
||||
const regionIdCurrencyCodeMap: Map<string, string> = new Map(
|
||||
regions.map((region: { id: string; currency_code: string }) => [
|
||||
region.id,
|
||||
region.currency_code,
|
||||
])
|
||||
)
|
||||
|
||||
return price_lists.map((priceListDTO) => {
|
||||
priceListDTO.title ??= priceListDTO.name
|
||||
const { _associationTag, name, prices, ...rest } = priceListDTO
|
||||
@@ -70,12 +82,20 @@ export async function prepareCreatePriceLists({
|
||||
prices?.map((price) => {
|
||||
const price_set_id = variantIdPriceSetIdMap.get(price.variant_id)!
|
||||
|
||||
const rules: Record<string, string> = {}
|
||||
if (price.region_id) {
|
||||
rules.region_id = price.region_id
|
||||
}
|
||||
|
||||
return {
|
||||
currency_code: price.currency_code,
|
||||
currency_code:
|
||||
regionIdCurrencyCodeMap.get(price.region_id as string) ??
|
||||
(price.currency_code as string),
|
||||
amount: price.amount,
|
||||
min_quantity: price.min_quantity,
|
||||
max_quantity: price.max_quantity,
|
||||
price_set_id,
|
||||
rules,
|
||||
}
|
||||
}) ?? []
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ export async function prepareUpdatePriceLists({
|
||||
}>): Promise<Result> {
|
||||
const { price_lists: priceListsData } = data
|
||||
const remoteQuery = container.resolve("remoteQuery")
|
||||
const regionService = container.resolve("regionService")
|
||||
|
||||
const variantPriceSetMap = new Map<string, string>()
|
||||
const priceListPricesMap = new Map<string, PriceListPriceDTO[]>()
|
||||
@@ -44,6 +45,17 @@ export async function prepareUpdatePriceLists({
|
||||
variantPriceSetMap.set(variant_id, price_set_id)
|
||||
}
|
||||
|
||||
const regionIds = priceListsData
|
||||
.map(({ prices }) => prices?.map((price) => price.region_id) ?? [])
|
||||
.flat(2)
|
||||
const regions = await regionService.list({ id: regionIds })
|
||||
const regionsMap: Map<string, string> = new Map(
|
||||
regions.map((region: { id: string; currency_code: string }) => [
|
||||
region.id,
|
||||
region.currency_code,
|
||||
])
|
||||
)
|
||||
|
||||
const priceLists = priceListsData.map((priceListData) => {
|
||||
const priceListPrices: PriceListPriceDTO[] = []
|
||||
|
||||
@@ -53,13 +65,21 @@ export async function prepareUpdatePriceLists({
|
||||
return
|
||||
}
|
||||
|
||||
const rules: Record<string, string> = {}
|
||||
if (price.region_id) {
|
||||
rules.region_id = price.region_id
|
||||
}
|
||||
|
||||
priceListPrices.push({
|
||||
id: priceData.id,
|
||||
price_set_id: variantPriceSetMap.get(variant_id) as string,
|
||||
currency_code: priceData.currency_code as string,
|
||||
currency_code:
|
||||
regionsMap.get(priceData.region_id as string) ??
|
||||
(priceData.currency_code as string),
|
||||
amount: priceData.amount,
|
||||
min_quantity: priceData.min_quantity,
|
||||
max_quantity: priceData.max_quantity,
|
||||
rules,
|
||||
})
|
||||
|
||||
return
|
||||
|
||||
@@ -65,7 +65,6 @@ export async function updateProductsVariantsPrices({
|
||||
const obj = {
|
||||
amount: price.amount,
|
||||
currency_code: price.currency_code,
|
||||
rules: {},
|
||||
}
|
||||
|
||||
if (price.region_id) {
|
||||
|
||||
Reference in New Issue
Block a user