feat(medusa,types,workflows,utils,product): PricingModule Integration of PriceLists into Core (#5536)
This commit is contained in:
@@ -1,12 +1,20 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import { Currency, MoneyAmount, PriceSet } from "@models"
|
||||
import {
|
||||
Currency,
|
||||
MoneyAmount,
|
||||
PriceList,
|
||||
PriceSet,
|
||||
PriceSetMoneyAmount,
|
||||
} from "@models"
|
||||
|
||||
export const LinkableKeys = {
|
||||
money_amount_id: MoneyAmount.name,
|
||||
currency_code: Currency.name,
|
||||
price_set_id: PriceSet.name,
|
||||
price_list_id: PriceList.name,
|
||||
price_set_money_amount_id: PriceSetMoneyAmount.name,
|
||||
}
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
@@ -16,11 +24,12 @@ Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.PRICING,
|
||||
primaryKeys: ["id", "currency_code"],
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: [
|
||||
{
|
||||
@@ -53,5 +62,17 @@ export const joinerConfig: ModuleJoinerConfig = {
|
||||
methodSuffix: "Currencies",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "price_list",
|
||||
args: {
|
||||
methodSuffix: "PriceLists",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "price_lists",
|
||||
args: {
|
||||
methodSuffix: "PriceLists",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
@@ -71,6 +71,9 @@ export class PricingRepository
|
||||
price_list_id: "psma1.price_list_id",
|
||||
pl_number_rules: "pl.number_rules",
|
||||
pl_type: "pl.type",
|
||||
has_price_list: knex.raw(
|
||||
"case when psma1.price_list_id IS NULL then False else True end"
|
||||
),
|
||||
})
|
||||
.leftJoin("price_set_money_amount as psma1", "psma1.id", "psma1.id")
|
||||
.leftJoin("price_rule as pr", "pr.price_set_money_amount_id", "psma1.id")
|
||||
@@ -154,15 +157,23 @@ export class PricingRepository
|
||||
.leftJoin("rule_type as rt", "rt.id", "pr.rule_type_id")
|
||||
.whereIn("ps.id", pricingFilters.id)
|
||||
.andWhere("ma.currency_code", "=", currencyCode)
|
||||
|
||||
.orderBy([
|
||||
{ column: "price_list_id", order: "asc" },
|
||||
{ column: "psma.has_price_list", order: "asc" },
|
||||
{ column: "number_rules", order: "desc" },
|
||||
{ column: "default_priority", order: "desc" },
|
||||
{ column: "amount", order: "asc" },
|
||||
])
|
||||
|
||||
if (quantity) {
|
||||
priceSetQueryKnex.where("ma.min_quantity", "<=", quantity)
|
||||
priceSetQueryKnex.andWhere("ma.max_quantity", ">=", quantity)
|
||||
} else {
|
||||
priceSetQueryKnex.andWhere(function () {
|
||||
this.andWhere("ma.min_quantity", "<=", "1").orWhereNull(
|
||||
"ma.min_quantity"
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
return await priceSetQueryKnex
|
||||
|
||||
@@ -13,7 +13,15 @@ import {
|
||||
PricingTypes,
|
||||
RuleTypeDTO,
|
||||
} from "@medusajs/types"
|
||||
import { PriceListType } from "@medusajs/utils"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
PriceListType,
|
||||
groupBy,
|
||||
removeNullish,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import {
|
||||
Currency,
|
||||
@@ -42,15 +50,6 @@ import {
|
||||
PriceSetService,
|
||||
RuleTypeService,
|
||||
} from "@services"
|
||||
|
||||
import {
|
||||
groupBy,
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
removeNullish,
|
||||
} from "@medusajs/utils"
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
import { CreatePriceListRuleValueDTO, PricingRepositoryService } from "../types"
|
||||
|
||||
@@ -147,6 +146,7 @@ export default class PricingModuleService<
|
||||
pricingContext,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const pricesSetPricesMap = groupBy(results, "price_set_id")
|
||||
|
||||
const calculatedPrices = pricingFilters.id.map(
|
||||
@@ -155,8 +155,9 @@ export default class PricingModuleService<
|
||||
// which is prioritized by number_rules first for exact match and then deafult_priority of the rule_type
|
||||
// inject custom price selection here
|
||||
const prices = pricesSetPricesMap.get(priceSetId) || []
|
||||
const priceListPrice = prices?.find((p) => !!p.price_list_id)
|
||||
const defaultPrice = prices?.find((p) => !!!p.price_list_id)
|
||||
const priceListPrice = prices.find((p) => p.price_list_id)
|
||||
|
||||
const defaultPrice = prices?.find((p) => !p.price_list_id)
|
||||
|
||||
let calculatedPrice: PricingTypes.CalculatedPriceSetDTO = defaultPrice
|
||||
let originalPrice: PricingTypes.CalculatedPriceSetDTO = defaultPrice
|
||||
|
||||
Reference in New Issue
Block a user