fix(pricing, types): update calculatePrices return type to match actual type (#5709)

* initial type fix

* add changeset

* update changeset

* update tsdocs

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
authored and GitHub committed 2023-11-24 17:12:34 +00:00
1 parent ffb0c7b4cd
commit 0df1c7d427
3 files changed
+109 -100

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
---
"@medusajs/pricing": patch
"@medusajs/types": patch
---
fix(pricing, types): update pricingModule.calculateprices return type to match actual type
+48 -45
View File
@@ -14,12 +14,12 @@ import {
RuleTypeDTO, RuleTypeDTO,
} from "@medusajs/types" } from "@medusajs/types"
import { import {
groupBy,
InjectManager, InjectManager,
InjectTransactionManager, InjectTransactionManager,
MedusaContext, MedusaContext,
MedusaError, MedusaError,
PriceListType, PriceListType,
groupBy,
removeNullish, removeNullish,
} from "@medusajs/utils" } from "@medusajs/utils"
@@ -140,7 +140,7 @@ export default class PricingModuleService<
pricingFilters: PricingFilters, pricingFilters: PricingFilters,
pricingContext: PricingContext = { context: {} }, pricingContext: PricingContext = { context: {} },
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.CalculatedPriceSetDTO> { ): Promise<PricingTypes.CalculatedPriceSet[]> {
const results = await this.pricingRepository_.calculatePrices( const results = await this.pricingRepository_.calculatePrices(
pricingFilters, pricingFilters,
pricingContext, pricingContext,
@@ -149,55 +149,58 @@ export default class PricingModuleService<
const pricesSetPricesMap = groupBy(results, "price_set_id") const pricesSetPricesMap = groupBy(results, "price_set_id")
const calculatedPrices = pricingFilters.id.map( const calculatedPrices: PricingTypes.CalculatedPriceSet[] =
(priceSetId: string): PricingTypes.CalculatedPriceSet => { pricingFilters.id.map(
// This is where we select prices, for now we just do a first match based on the database results (priceSetId: string): PricingTypes.CalculatedPriceSet => {
// which is prioritized by number_rules first for exact match and then deafult_priority of the rule_type // This is where we select prices, for now we just do a first match based on the database results
// inject custom price selection here // which is prioritized by number_rules first for exact match and then deafult_priority of the rule_type
const prices = pricesSetPricesMap.get(priceSetId) || [] // inject custom price selection here
const priceListPrice = prices.find((p) => p.price_list_id) const prices = pricesSetPricesMap.get(priceSetId) || []
const priceListPrice = prices.find((p) => p.price_list_id)
const defaultPrice = prices?.find((p) => !p.price_list_id) const defaultPrice = prices?.find((p) => !p.price_list_id)
let calculatedPrice: PricingTypes.CalculatedPriceSetDTO = defaultPrice let calculatedPrice: PricingTypes.CalculatedPriceSetDTO = defaultPrice
let originalPrice: PricingTypes.CalculatedPriceSetDTO = defaultPrice let originalPrice: PricingTypes.CalculatedPriceSetDTO = defaultPrice
if (priceListPrice) { if (priceListPrice) {
calculatedPrice = priceListPrice calculatedPrice = priceListPrice
if (priceListPrice.price_list_type === PriceListType.OVERRIDE) { if (priceListPrice.price_list_type === PriceListType.OVERRIDE) {
originalPrice = priceListPrice originalPrice = priceListPrice
}
}
return {
id: priceSetId,
is_calculated_price_price_list: !!calculatedPrice?.price_list_id,
calculated_amount: parseInt(calculatedPrice?.amount || "") || null,
is_original_price_price_list: !!originalPrice?.price_list_id,
original_amount: parseInt(originalPrice?.amount || "") || null,
currency_code: calculatedPrice?.currency_code || null,
calculated_price: {
money_amount_id: calculatedPrice?.id || null,
price_list_id: calculatedPrice?.price_list_id || null,
price_list_type: calculatedPrice?.price_list_type || null,
min_quantity:
parseInt(calculatedPrice?.min_quantity || "") || null,
max_quantity:
parseInt(calculatedPrice?.max_quantity || "") || null,
},
original_price: {
money_amount_id: originalPrice?.id || null,
price_list_id: originalPrice?.price_list_id || null,
price_list_type: originalPrice?.price_list_type || null,
min_quantity: parseInt(originalPrice?.min_quantity || "") || null,
max_quantity: parseInt(originalPrice?.max_quantity || "") || null,
},
} }
} }
)
return {
id: priceSetId,
is_calculated_price_price_list: !!calculatedPrice?.price_list_id,
calculated_amount: parseInt(calculatedPrice?.amount || "") || null,
is_original_price_price_list: !!originalPrice?.price_list_id,
original_amount: parseInt(originalPrice?.amount || "") || null,
currency_code: calculatedPrice?.currency_code || null,
calculated_price: {
money_amount_id: calculatedPrice?.id || null,
price_list_id: calculatedPrice?.price_list_id || null,
price_list_type: calculatedPrice?.price_list_type || null,
min_quantity: parseInt(calculatedPrice?.min_quantity || "") || null,
max_quantity: parseInt(calculatedPrice?.max_quantity || "") || null,
},
original_price: {
money_amount_id: originalPrice?.id || null,
price_list_id: originalPrice?.price_list_id || null,
price_list_type: originalPrice?.price_list_type || null,
min_quantity: parseInt(originalPrice?.min_quantity || "") || null,
max_quantity: parseInt(originalPrice?.max_quantity || "") || null,
},
}
}
)
return JSON.parse(JSON.stringify(calculatedPrices)) return JSON.parse(JSON.stringify(calculatedPrices))
} }
+55 -55
View File
@@ -2,7 +2,7 @@ import {
AddPriceListPricesDTO, AddPriceListPricesDTO,
AddPricesDTO, AddPricesDTO,
AddRulesDTO, AddRulesDTO,
CalculatedPriceSetDTO, CalculatedPriceSet,
CreateCurrencyDTO, CreateCurrencyDTO,
CreateMoneyAmountDTO, CreateMoneyAmountDTO,
CreatePriceListDTO, CreatePriceListDTO,
@@ -62,7 +62,7 @@ export interface IPricingModuleService {
* The context used to select the prices. For example, you can specify the region ID in this context, and only prices having the same value * The context used to select the prices. For example, you can specify the region ID in this context, and only prices having the same value
* will be retrieved. * will be retrieved.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<CalculatedPriceSetDTO>} The calculated price matching the context and filters provided. * @returns {Promise<CalculatedPriceSet[]>} The calculated prices matching the context and filters provided.
* *
* @example * @example
* When you calculate prices, you must at least specify the currency code: * When you calculate prices, you must at least specify the currency code:
@@ -137,7 +137,7 @@ export interface IPricingModuleService {
filters: PricingFilters, filters: PricingFilters,
context?: PricingContext, context?: PricingContext,
sharedContext?: Context sharedContext?: Context
): Promise<CalculatedPriceSetDTO> ): Promise<CalculatedPriceSet[]>
/** /**
* This method is used to retrieve a price set by its ID. * This method is used to retrieve a price set by its ID.
@@ -3170,16 +3170,16 @@ export interface IPricingModuleService {
/** /**
* This method is used to create price lists. * This method is used to create price lists.
* *
* @param {CreatePriceListDTO[]} data - The details of each price list to be created. * @param {CreatePriceListDTO[]} data - The details of each price list to be created.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PriceListDTO[]>} The created price lists. * @returns {Promise<PriceListDTO[]>} The created price lists.
* *
* @example * @example
* import { * import {
* initialize as initializePricingModule, * initialize as initializePricingModule,
* } from "@medusajs/pricing" * } from "@medusajs/pricing"
* *
* async function createPriceList (items: { * async function createPriceList (items: {
* title: string * title: string
* description: string * description: string
@@ -3187,9 +3187,9 @@ export interface IPricingModuleService {
* ends_at?: string * ends_at?: string
* }[]) { * }[]) {
* const pricingService = await initializePricingModule() * const pricingService = await initializePricingModule()
* *
* const priceList = await pricingService.createPriceLists(items) * const priceList = await pricingService.createPriceLists(items)
* *
* // do something with the price lists or return them * // do something with the price lists or return them
* } * }
*/ */
@@ -3200,16 +3200,16 @@ export interface IPricingModuleService {
/** /**
* This method is used to update price lists. * This method is used to update price lists.
* *
* @param {UpdatePriceListDTO[]} data - The attributes to update in each price list. * @param {UpdatePriceListDTO[]} data - The attributes to update in each price list.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PriceListDTO[]>} The updated price lists. * @returns {Promise<PriceListDTO[]>} The updated price lists.
* *
* @example * @example
* import { * import {
* initialize as initializePricingModule, * initialize as initializePricingModule,
* } from "@medusajs/pricing" * } from "@medusajs/pricing"
* *
* async function updatePriceLists (items: { * async function updatePriceLists (items: {
* id: string * id: string
* title: string * title: string
@@ -3218,9 +3218,9 @@ export interface IPricingModuleService {
* ends_at?: string * ends_at?: string
* }[]) { * }[]) {
* const pricingService = await initializePricingModule() * const pricingService = await initializePricingModule()
* *
* const priceList = await pricingService.updatePriceLists(items) * const priceList = await pricingService.updatePriceLists(items)
* *
* // do something with the price lists or return them * // do something with the price lists or return them
* } * }
*/ */
@@ -3231,19 +3231,19 @@ export interface IPricingModuleService {
/** /**
* This method is used to delete price lists. * This method is used to delete price lists.
* *
* @param {string[]} priceListIds - The IDs of the price lists to delete. * @param {string[]} priceListIds - The IDs of the price lists to delete.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<void>} Resolves when the price lists are deleted successfully. * @returns {Promise<void>} Resolves when the price lists are deleted successfully.
* *
* @example * @example
* import { * import {
* initialize as initializePricingModule, * initialize as initializePricingModule,
* } from "@medusajs/pricing" * } from "@medusajs/pricing"
* *
* async function deletePriceLists (ids: string[]) { * async function deletePriceLists (ids: string[]) {
* const pricingService = await initializePricingModule() * const pricingService = await initializePricingModule()
* *
* await pricingService.deletePriceLists(ids) * await pricingService.deletePriceLists(ids)
* } * }
*/ */
@@ -3269,14 +3269,14 @@ export interface IPricingModuleService {
* import { * import {
* initialize as initializePricingModule, * initialize as initializePricingModule,
* } from "@medusajs/pricing" * } from "@medusajs/pricing"
* *
* async function retrievePriceListRule (priceListRuleId: string) { * async function retrievePriceListRule (priceListRuleId: string) {
* const pricingService = await initializePricingModule() * const pricingService = await initializePricingModule()
* *
* const priceListRule = await pricingService.retrievePriceListRule( * const priceListRule = await pricingService.retrievePriceListRule(
* priceListRuleId * priceListRuleId
* ) * )
* *
* // do something with the price list rule or return it * // do something with the price list rule or return it
* } * }
* ``` * ```
@@ -3287,17 +3287,17 @@ export interface IPricingModuleService {
* import { * import {
* initialize as initializePricingModule, * initialize as initializePricingModule,
* } from "@medusajs/pricing" * } from "@medusajs/pricing"
* *
* async function retrievePriceListRule (priceListRuleId: string) { * async function retrievePriceListRule (priceListRuleId: string) {
* const pricingService = await initializePricingModule() * const pricingService = await initializePricingModule()
* *
* const priceListRule = await pricingService.retrievePriceListRule( * const priceListRule = await pricingService.retrievePriceListRule(
* priceListRuleId, * priceListRuleId,
* { * {
* relations: ["price_list"] * relations: ["price_list"]
* } * }
* ) * )
* *
* // do something with the price list rule or return it * // do something with the price list rule or return it
* } * }
* ``` * ```
@@ -3546,24 +3546,24 @@ export interface IPricingModuleService {
/** /**
* This method is used to create price list rules. * This method is used to create price list rules.
* *
* @param {CreatePriceListRuleDTO[]} data - The price list rules to create. * @param {CreatePriceListRuleDTO[]} data - The price list rules to create.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PriceListRuleDTO[]>} The created price list rules. * @returns {Promise<PriceListRuleDTO[]>} The created price list rules.
* *
* @example * @example
* import { * import {
* initialize as initializePricingModule, * initialize as initializePricingModule,
* } from "@medusajs/pricing" * } from "@medusajs/pricing"
* *
* async function createPriceListRules (items: { * async function createPriceListRules (items: {
* rule_type_id: string * rule_type_id: string
* price_list_id: string * price_list_id: string
* }[]) { * }[]) {
* const pricingService = await initializePricingModule() * const pricingService = await initializePricingModule()
* *
* const priceListRules = await pricingService.createPriceListRules(items) * const priceListRules = await pricingService.createPriceListRules(items)
* *
* // do something with the price list rule or return them * // do something with the price list rule or return them
* } * }
*/ */
@@ -3574,25 +3574,25 @@ export interface IPricingModuleService {
/** /**
* This method is used to update price list rules. * This method is used to update price list rules.
* *
* @param {UpdatePriceListRuleDTO[]} data - The attributes to update for each price list rule. * @param {UpdatePriceListRuleDTO[]} data - The attributes to update for each price list rule.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PriceListRuleDTO[]>} The updated price list rules. * @returns {Promise<PriceListRuleDTO[]>} The updated price list rules.
* *
* @example * @example
* import { * import {
* initialize as initializePricingModule, * initialize as initializePricingModule,
* } from "@medusajs/pricing" * } from "@medusajs/pricing"
* *
* async function updatePriceListRules (items: { * async function updatePriceListRules (items: {
* id: string * id: string
* rule_type_id?: string * rule_type_id?: string
* price_list_id?: string * price_list_id?: string
* }[]) { * }[]) {
* const pricingService = await initializePricingModule() * const pricingService = await initializePricingModule()
* *
* const priceListRules = await pricingService.updatePriceListRules(items) * const priceListRules = await pricingService.updatePriceListRules(items)
* *
* // do something with the price list rule or return them * // do something with the price list rule or return them
* } * }
*/ */
@@ -3603,19 +3603,19 @@ export interface IPricingModuleService {
/** /**
* This method is used to delete price list rules. * This method is used to delete price list rules.
* *
* @param {string[]} priceListRuleIds - The IDs of the price list rules to delete. * @param {string[]} priceListRuleIds - The IDs of the price list rules to delete.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<void>} Resolves successfully when the price list rules are deleted. * @returns {Promise<void>} Resolves successfully when the price list rules are deleted.
* *
* @example * @example
* import { * import {
* initialize as initializePricingModule, * initialize as initializePricingModule,
* } from "@medusajs/pricing" * } from "@medusajs/pricing"
* *
* async function deletePriceListRules (priceListRuleIds: string[]) { * async function deletePriceListRules (priceListRuleIds: string[]) {
* const pricingService = await initializePricingModule() * const pricingService = await initializePricingModule()
* *
* await pricingService.deletePriceListRules(priceListRuleIds) * await pricingService.deletePriceListRules(priceListRuleIds)
* } * }
*/ */
@@ -3626,16 +3626,16 @@ export interface IPricingModuleService {
/** /**
* This method is used to add prices to price lists. * This method is used to add prices to price lists.
* *
* @param {AddPriceListPricesDTO[]} data - The prices to add for each price list. * @param {AddPriceListPricesDTO[]} data - The prices to add for each price list.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PriceListDTO[]>} The updated price lists. * @returns {Promise<PriceListDTO[]>} The updated price lists.
* *
* @example * @example
* import { * import {
* initialize as initializePricingModule, * initialize as initializePricingModule,
* } from "@medusajs/pricing" * } from "@medusajs/pricing"
* *
* async function addPriceListPrices (items: { * async function addPriceListPrices (items: {
* priceListId: string, * priceListId: string,
* prices: { * prices: {
@@ -3645,9 +3645,9 @@ export interface IPricingModuleService {
* }[] * }[]
* }[]) { * }[]) {
* const pricingService = await initializePricingModule() * const pricingService = await initializePricingModule()
* *
* const priceLists = await pricingService.addPriceListPrices(items) * const priceLists = await pricingService.addPriceListPrices(items)
* *
* // do something with the price lists or return them * // do something with the price lists or return them
* } * }
*/ */
@@ -3658,26 +3658,26 @@ export interface IPricingModuleService {
/** /**
* This method is used to set the rules of a price list. * This method is used to set the rules of a price list.
* *
* @param {SetPriceListRulesDTO} data - The rules to set for a price list. * @param {SetPriceListRulesDTO} data - The rules to set for a price list.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PriceListDTO>} The updated price lists. * @returns {Promise<PriceListDTO>} The updated price lists.
* *
* @example * @example
* import { * import {
* initialize as initializePricingModule, * initialize as initializePricingModule,
* } from "@medusajs/pricing" * } from "@medusajs/pricing"
* *
* async function setPriceListRules (priceListId: string) { * async function setPriceListRules (priceListId: string) {
* const pricingService = await initializePricingModule() * const pricingService = await initializePricingModule()
* *
* const priceList = await pricingService.setPriceListRules({ * const priceList = await pricingService.setPriceListRules({
* priceListId, * priceListId,
* rules: { * rules: {
* region_id: "US" * region_id: "US"
* } * }
* }) * })
* *
* // do something with the price list or return it * // do something with the price list or return it
* } * }
*/ */
@@ -3688,24 +3688,24 @@ export interface IPricingModuleService {
/** /**
* This method is used to remove rules from a price list. * This method is used to remove rules from a price list.
* *
* @param {RemovePriceListRulesDTO} data - The rules to remove from a price list. * @param {RemovePriceListRulesDTO} data - The rules to remove from a price list.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PriceListDTO>} The updated price lists. * @returns {Promise<PriceListDTO>} The updated price lists.
* *
* @example * @example
* import { * import {
* initialize as initializePricingModule, * initialize as initializePricingModule,
* } from "@medusajs/pricing" * } from "@medusajs/pricing"
* *
* async function setPriceListRules (priceListId: string) { * async function setPriceListRules (priceListId: string) {
* const pricingService = await initializePricingModule() * const pricingService = await initializePricingModule()
* *
* const priceList = await pricingService.removePriceListRules({ * const priceList = await pricingService.removePriceListRules({
* priceListId, * priceListId,
* rules: ["region_id"] * rules: ["region_id"]
* }) * })
* *
* // do something with the price list or return it * // do something with the price list or return it
* } * }
*/ */