feat(pricing,medusa,utils): added list + get endpoints for price lists (#6592)
what: - brings back price list GET endpoints to v2 endpoints
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
import { NextFunction, Request, Response } from "express"
|
||||
import { ClassConstructor } from "../../types/global"
|
||||
import { validator } from "../../utils/validator"
|
||||
import { buildSelects, objectToStringPath } from "@medusajs/utils"
|
||||
import { ValidatorOptions } from "class-validator"
|
||||
import { default as normalizeQuery } from "./normalized-query"
|
||||
import { NextFunction, Request, Response } from "express"
|
||||
import { omit } from "lodash"
|
||||
import { BaseEntity } from "../../interfaces"
|
||||
import { FindConfig, QueryConfig, RequestQueryFields } from "../../types/common"
|
||||
import { ClassConstructor } from "../../types/global"
|
||||
import { removeUndefinedProperties } from "../../utils"
|
||||
import {
|
||||
prepareListQuery,
|
||||
prepareRetrieveQuery,
|
||||
} from "../../utils/get-query-config"
|
||||
import { BaseEntity } from "../../interfaces"
|
||||
import { FindConfig, QueryConfig, RequestQueryFields } from "../../types/common"
|
||||
import { omit } from "lodash"
|
||||
import { removeUndefinedProperties } from "../../utils"
|
||||
import { buildSelects, objectToStringPath } from "@medusajs/utils"
|
||||
import { validator } from "../../utils/validator"
|
||||
import { default as normalizeQuery } from "./normalized-query"
|
||||
|
||||
/**
|
||||
* Middleware that transform the query input for the admin end points
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import { FlagRouter, MedusaV2Flag } from "@medusajs/utils"
|
||||
import { defaultAdminPriceListFields, defaultAdminPriceListRelations } from "."
|
||||
import { PriceList } from "../../../.."
|
||||
import PriceListService from "../../../../services/price-list"
|
||||
import { getPriceListPricingModule } from "./modules-queries"
|
||||
|
||||
/**
|
||||
* @oas [get] /admin/price-lists/{id}
|
||||
@@ -87,22 +84,13 @@ import { getPriceListPricingModule } from "./modules-queries"
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const featureFlagRouter: FlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
|
||||
let priceList
|
||||
|
||||
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
|
||||
priceList = await getPriceListPricingModule(id, {
|
||||
container: req.scope as MedusaContainer,
|
||||
})
|
||||
} else {
|
||||
priceList = await priceListService.retrieve(id, {
|
||||
select: defaultAdminPriceListFields as (keyof PriceList)[],
|
||||
relations: defaultAdminPriceListRelations,
|
||||
})
|
||||
}
|
||||
const priceList = await priceListService.retrieve(id, {
|
||||
select: defaultAdminPriceListFields as (keyof PriceList)[],
|
||||
relations: defaultAdminPriceListRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ price_list: priceList })
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { FlagRouter, MedusaV2Flag } from "@medusajs/utils"
|
||||
import { Type } from "class-transformer"
|
||||
import { IsNumber, IsOptional, IsString } from "class-validator"
|
||||
import { Request } from "express"
|
||||
import PriceListService from "../../../../services/price-list"
|
||||
import { FilterablePriceListProps } from "../../../../types/price-list"
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import { listAndCountPriceListPricingModule } from "./modules-queries"
|
||||
|
||||
/**
|
||||
* @oas [get] /admin/price-lists
|
||||
@@ -190,27 +187,14 @@ import { listAndCountPriceListPricingModule } from "./modules-queries"
|
||||
* $ref: "#/components/responses/500_error"
|
||||
*/
|
||||
export default async (req: Request, res) => {
|
||||
const featureFlagRouter: FlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
|
||||
const validated = req.validatedQuery
|
||||
let priceLists
|
||||
let count
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
|
||||
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
|
||||
[priceLists, count] = await listAndCountPriceListPricingModule({
|
||||
filters: req.filterableFields,
|
||||
listConfig: req.listConfig,
|
||||
container: req.scope as MedusaContainer,
|
||||
})
|
||||
} else {
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
|
||||
;[priceLists, count] = await priceListService.listAndCount(
|
||||
req.filterableFields,
|
||||
req.listConfig
|
||||
)
|
||||
}
|
||||
const [priceLists, count] = await priceListService.listAndCount(
|
||||
req.filterableFields,
|
||||
req.listConfig
|
||||
)
|
||||
|
||||
res.json({
|
||||
price_lists: priceLists,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import { PriceList } from "../../../../../models"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { PriceList } from "../../../../../models"
|
||||
import { listAndCountPriceListPricingModule } from "./list-and-count-price-lists"
|
||||
|
||||
export async function getPriceListPricingModule(
|
||||
@@ -12,9 +12,7 @@ export async function getPriceListPricingModule(
|
||||
}
|
||||
): Promise<PriceList> {
|
||||
const [priceLists, count] = await listAndCountPriceListPricingModule({
|
||||
filters: {
|
||||
id: [id],
|
||||
},
|
||||
filters: { id: [id] },
|
||||
container,
|
||||
})
|
||||
|
||||
|
||||
+11
-35
@@ -1,7 +1,7 @@
|
||||
import { LinkModuleUtils, ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { FilterablePriceListProps, MedusaContainer } from "@medusajs/types"
|
||||
import { FindConfig } from "../../../../../types/common"
|
||||
import { CustomerGroup, MoneyAmount, PriceList } from "../../../../../models"
|
||||
import { CustomerGroupService } from "../../../../../services"
|
||||
import { FindConfig } from "../../../../../types/common"
|
||||
import { defaultAdminPriceListRemoteQueryObject } from "../index"
|
||||
|
||||
export async function listAndCountPriceListPricingModule({
|
||||
@@ -13,10 +13,8 @@ export async function listAndCountPriceListPricingModule({
|
||||
filters?: FilterablePriceListProps
|
||||
listConfig?: FindConfig<PriceList>
|
||||
}): Promise<[PriceList[], number]> {
|
||||
const remoteQuery = container.resolve("remoteQuery")
|
||||
const customerGroupService: CustomerGroupService = container.resolve(
|
||||
"customerGroupService"
|
||||
)
|
||||
const remoteQuery = container.resolve(LinkModuleUtils.REMOTE_QUERY)
|
||||
const customerModule = container.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
|
||||
const query = {
|
||||
price_list: {
|
||||
@@ -44,17 +42,8 @@ export async function listAndCountPriceListPricingModule({
|
||||
)
|
||||
.flat(2)
|
||||
|
||||
const priceListCustomerGroups = await customerGroupService.list(
|
||||
{ id: customerGroupIds },
|
||||
{}
|
||||
)
|
||||
|
||||
const customerGroupIdCustomerGroupMap = new Map(
|
||||
priceListCustomerGroups.map((customerGroup) => [
|
||||
customerGroup.id,
|
||||
customerGroup,
|
||||
])
|
||||
)
|
||||
const customerGroups = await customerModule.list({ id: customerGroupIds }, {})
|
||||
const customerGroupIdMap = new Map(customerGroups.map((cg) => [cg.id, cg]))
|
||||
|
||||
for (const priceList of priceLists) {
|
||||
const priceSetMoneyAmounts = priceList.price_set_money_amounts || []
|
||||
@@ -83,27 +72,14 @@ export async function listAndCountPriceListPricingModule({
|
||||
priceList.name = priceList.title
|
||||
delete priceList.title
|
||||
|
||||
const customerGroupPriceListRule = priceListRulesData.find(
|
||||
const customerGroupRule = priceListRulesData.find(
|
||||
(plr) => plr.rule_type.rule_attribute === "customer_group_id"
|
||||
)
|
||||
|
||||
if (
|
||||
customerGroupPriceListRule &&
|
||||
customerGroupPriceListRule?.price_list_rule_values
|
||||
) {
|
||||
priceList.customer_groups =
|
||||
customerGroupPriceListRule?.price_list_rule_values
|
||||
.map((customerGroupRule) =>
|
||||
customerGroupIdCustomerGroupMap.get(customerGroupRule.value)
|
||||
)
|
||||
.filter(
|
||||
(
|
||||
customerGroup: CustomerGroup | undefined
|
||||
): customerGroup is CustomerGroup => !!customerGroup
|
||||
)
|
||||
} else {
|
||||
priceList.customer_groups = []
|
||||
}
|
||||
priceList.customer_groups =
|
||||
customerGroupRule?.price_list_rule_values
|
||||
.map((cgr) => customerGroupIdMap.get(cgr.value))
|
||||
.filter((cg): cg is CustomerGroup => !!cg) || []
|
||||
}
|
||||
|
||||
return [priceLists, count]
|
||||
|
||||
Reference in New Issue
Block a user