refactor: migrate pricing entities to DML models (#10335)
Fixes: FRMW-2810 ## Breaking changes There is only one breaking change - The `min_quantity` and `max_quantity` properties are now consistently typed as numbers. Earlier, it was [typed as numbers](https://github.com/medusajs/medusa/blob/develop/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts#L68-L69) in some API responses and as [string in others](https://github.com/medusajs/medusa/blob/develop/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts#L186-L187). I did not go to the bottom of this inconsistency, but the tests reveals them. Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
co-authored by
Adrien de Peretti
parent
b4d6a4b3f0
commit
913cf15e2b
@@ -7,6 +7,7 @@ import {
|
||||
CreatePriceSetDTO,
|
||||
DAL,
|
||||
FindConfig,
|
||||
InferEntityType,
|
||||
InternalModuleDeclaration,
|
||||
ModuleJoinerConfig,
|
||||
ModulesSdkTypes,
|
||||
@@ -52,6 +53,7 @@ import {
|
||||
import { ServiceTypes } from "@types"
|
||||
import { eventBuilders, validatePriceListDates } from "@utils"
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
import { Collection } from "@mikro-orm/core"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
@@ -91,12 +93,24 @@ export default class PricingModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly pricingRepository_: PricingRepositoryService
|
||||
protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService<PriceSet>
|
||||
protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceRule>
|
||||
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<Price>
|
||||
protected readonly priceListService_: ModulesSdkTypes.IMedusaInternalService<PriceList>
|
||||
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceListRule>
|
||||
protected readonly pricePreferenceService_: ModulesSdkTypes.IMedusaInternalService<PricePreference>
|
||||
protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
InferEntityType<typeof PriceSet>
|
||||
>
|
||||
protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
InferEntityType<typeof PriceRule>
|
||||
>
|
||||
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
InferEntityType<typeof Price>
|
||||
>
|
||||
protected readonly priceListService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
InferEntityType<typeof PriceList>
|
||||
>
|
||||
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
InferEntityType<typeof PriceListRule>
|
||||
>
|
||||
protected readonly pricePreferenceService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
InferEntityType<typeof PricePreference>
|
||||
>
|
||||
|
||||
constructor(
|
||||
{
|
||||
@@ -446,7 +460,7 @@ export default class PricingModuleService
|
||||
(priceSet): priceSet is CreatePriceSetDTO => !priceSet.id
|
||||
)
|
||||
|
||||
const operations: Promise<PriceSet[]>[] = []
|
||||
const operations: Promise<InferEntityType<typeof PriceSet>[]>[] = []
|
||||
|
||||
if (forCreate.length) {
|
||||
operations.push(this.createPriceSets_(forCreate, sharedContext))
|
||||
@@ -512,7 +526,7 @@ export default class PricingModuleService
|
||||
protected async updatePriceSets_(
|
||||
data: ServiceTypes.UpdatePriceSetInput[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PriceSet[]> {
|
||||
): Promise<InferEntityType<typeof PriceSet>[]> {
|
||||
// TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries.
|
||||
// We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this
|
||||
const normalizedData = await this.normalizeUpdateData(data)
|
||||
@@ -828,7 +842,7 @@ export default class PricingModuleService
|
||||
!pricePreference.id
|
||||
)
|
||||
|
||||
const operations: Promise<PricePreference[]>[] = []
|
||||
const operations: Promise<InferEntityType<typeof PricePreference>[]>[] = []
|
||||
|
||||
if (forCreate.length) {
|
||||
operations.push(this.createPricePreferences_(forCreate, sharedContext))
|
||||
@@ -1249,7 +1263,7 @@ export default class PricingModuleService
|
||||
protected async updatePriceListPrices_(
|
||||
data: PricingTypes.UpdatePriceListPricesDTO[],
|
||||
sharedContext: Context = {}
|
||||
): Promise<Price[]> {
|
||||
): Promise<InferEntityType<typeof Price>[]> {
|
||||
const priceLists = await this.listPriceLists(
|
||||
{ id: data.map((p) => p.price_list_id) },
|
||||
{ relations: ["prices", "prices.price_rules"] },
|
||||
@@ -1305,7 +1319,7 @@ export default class PricingModuleService
|
||||
protected async addPriceListPrices_(
|
||||
data: PricingTypes.AddPriceListPricesDTO[],
|
||||
sharedContext: Context = {}
|
||||
): Promise<Price[]> {
|
||||
): Promise<InferEntityType<typeof Price>[]> {
|
||||
const priceLists = await this.listPriceLists(
|
||||
{ id: data.map((p) => p.price_list_id) },
|
||||
{ relations: ["prices", "prices.price_rules"] },
|
||||
@@ -1379,7 +1393,7 @@ export default class PricingModuleService
|
||||
protected async setPriceListRules_(
|
||||
data: PricingTypes.SetPriceListRulesDTO[],
|
||||
sharedContext: Context = {}
|
||||
): Promise<PriceList[]> {
|
||||
): Promise<InferEntityType<typeof PriceList>[]> {
|
||||
// TODO: re think this method
|
||||
const priceLists = await this.priceListService_.list(
|
||||
{ id: data.map((d) => d.price_list_id) },
|
||||
@@ -1402,10 +1416,12 @@ export default class PricingModuleService
|
||||
|
||||
const priceListsUpsert = priceLists
|
||||
.map((priceList) => {
|
||||
const priceListRules =
|
||||
priceList.price_list_rules as unknown as Collection<
|
||||
InferEntityType<typeof PriceListRule>
|
||||
>
|
||||
const allRules = new Map(
|
||||
priceList.price_list_rules
|
||||
.toArray()
|
||||
.map((r) => [r.attribute, r.value])
|
||||
priceListRules.toArray().map((r) => [r.attribute, r.value])
|
||||
)
|
||||
|
||||
const rules = rulesMap.get(priceList.id)
|
||||
@@ -1441,7 +1457,7 @@ export default class PricingModuleService
|
||||
protected async removePriceListRules_(
|
||||
data: PricingTypes.RemovePriceListRulesDTO[],
|
||||
sharedContext: Context = {}
|
||||
): Promise<PriceList[]> {
|
||||
): Promise<InferEntityType<typeof PriceList>[]> {
|
||||
// TODO: re think this method
|
||||
const priceLists = await this.priceListService_.list(
|
||||
{ id: data.map((d) => d.price_list_id) },
|
||||
@@ -1464,10 +1480,13 @@ export default class PricingModuleService
|
||||
|
||||
const priceListsUpsert = priceLists
|
||||
.map((priceList) => {
|
||||
const priceListRules =
|
||||
priceList.price_list_rules as unknown as Collection<
|
||||
InferEntityType<typeof PriceListRule>
|
||||
>
|
||||
|
||||
const allRules = new Map(
|
||||
priceList.price_list_rules
|
||||
.toArray()
|
||||
.map((r) => [r.attribute, r.value])
|
||||
priceListRules.toArray().map((r) => [r.attribute, r.value])
|
||||
)
|
||||
|
||||
const rules = rulesMap.get(priceList.id)
|
||||
@@ -1536,8 +1555,8 @@ export default class PricingModuleService
|
||||
}
|
||||
|
||||
const isTaxInclusive = (
|
||||
priceRules: PriceRule[],
|
||||
preferences: PricePreference[],
|
||||
priceRules: InferEntityType<typeof PriceRule>[],
|
||||
preferences: InferEntityType<typeof PricePreference>[],
|
||||
currencyCode: string,
|
||||
regionId?: string
|
||||
) => {
|
||||
|
||||
Reference in New Issue
Block a user