feat: Add HTTP endpoints and workflows for price preference management (#7960)

REF CORE-2376

Remaining pieces are adding UI to manage the flag, showing the flag in price editor, plugging it in cart calculations, and https://github.com/medusajs/medusa/pull/7827
This commit is contained in:
Stevche Radevski
2024-07-05 10:47:01 +02:00
committed by GitHub
parent 1162b1625d
commit 3e86cb6ac3
27 changed files with 702 additions and 10 deletions

View File

@@ -2,7 +2,7 @@ import { defineJoinerConfig, Modules } from "@medusajs/utils"
import { Price, PriceList, PricePreference, PriceSet } from "@models"
export const joinerConfig = defineJoinerConfig(Modules.PRICING, {
models: [PriceSet, PriceList, Price],
models: [PriceSet, PriceList, Price, PricePreference],
linkableKeys: {
price_set_id: PriceSet.name,
price_list_id: PriceList.name,

View File

@@ -734,7 +734,10 @@ export default class PricingModuleService
sharedContext
)
return await this.baseRepository_.serialize<any[]>(preferences)
const serialized = await this.baseRepository_.serialize<
PricePreferenceDTO[]
>(preferences)
return Array.isArray(data) ? serialized : serialized[0]
}
async upsertPricePreferences(
@@ -800,11 +803,38 @@ export default class PricingModuleService
data: PricingTypes.UpdatePricePreferenceDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<PricePreferenceDTO | PricePreferenceDTO[]> {
const preferences = await this.pricePreferenceService_.update(
data,
let normalizedInput: ServiceTypes.UpdatePricePreferenceInput[] = []
if (isString(idOrSelector)) {
// Check if the ID exists, it will throw if not.
await this.pricePreferenceService_.retrieve(
idOrSelector,
{},
sharedContext
)
normalizedInput = [{ id: idOrSelector, ...data }]
} else {
const pricePreferences = await this.pricePreferenceService_.list(
idOrSelector,
{},
sharedContext
)
normalizedInput = pricePreferences.map((pricePreference) => ({
id: pricePreference.id,
...data,
}))
}
const updateResult = await this.pricePreferenceService_.update(
normalizedInput,
sharedContext
)
return await this.baseRepository_.serialize<any[]>(preferences)
const pricePreferences = await this.baseRepository_.serialize<
PricePreferenceDTO[] | PricePreferenceDTO
>(updateResult)
return isString(idOrSelector) ? pricePreferences[0] : pricePreferences
}
@InjectTransactionManager("baseRepository_")