feat(core-flows,medusa,types,utils): rename psma to prices (#6796)
What: Renames pricesetmoneyamount to prices Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
co-authored by
Adrien de Peretti
parent
fbc369705d
commit
9073d7aba3
@@ -9,7 +9,6 @@ import middlewares, {
|
||||
import {
|
||||
defaultAdminProductFields,
|
||||
defaultAdminProductRelations,
|
||||
defaultAdminProductRemoteQueryObject,
|
||||
} from "../products"
|
||||
|
||||
import { FlagRouter } from "@medusajs/utils"
|
||||
@@ -87,56 +86,6 @@ export default (app, featureFlagRouter: FlagRouter) => {
|
||||
return app
|
||||
}
|
||||
|
||||
export const defaultAdminPriceListRemoteQueryObject = {
|
||||
fields: [
|
||||
"created_at",
|
||||
"deleted_at",
|
||||
"description",
|
||||
"ends_at",
|
||||
"id",
|
||||
"title",
|
||||
"starts_at",
|
||||
"status",
|
||||
"type",
|
||||
"updated_at",
|
||||
],
|
||||
price_list_rules: {
|
||||
price_list_rule_values: {
|
||||
fields: ["value"],
|
||||
},
|
||||
rule_type: {
|
||||
fields: ["rule_attribute"],
|
||||
},
|
||||
},
|
||||
price_set_money_amounts: {
|
||||
money_amount: {
|
||||
fields: [
|
||||
"id",
|
||||
"currency_code",
|
||||
"amount",
|
||||
"min_quantity",
|
||||
"max_quantity",
|
||||
"created_at",
|
||||
"deleted_at",
|
||||
"updated_at",
|
||||
],
|
||||
},
|
||||
price_rules: {
|
||||
fields: ["value"],
|
||||
rule_type: {
|
||||
fields: ["rule_attribute"],
|
||||
},
|
||||
},
|
||||
price_set: {
|
||||
variant_link: {
|
||||
variant: {
|
||||
fields: defaultAdminProductRemoteQueryObject.variants.fields,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
export const defaultAdminPriceListFields = [
|
||||
"id",
|
||||
"name",
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { PriceList } from "../../../../../models"
|
||||
import { listAndCountPriceListPricingModule } from "./list-and-count-price-lists"
|
||||
|
||||
export async function getPriceListPricingModule(
|
||||
id: string,
|
||||
{
|
||||
container,
|
||||
}: {
|
||||
container: MedusaContainer
|
||||
}
|
||||
): Promise<PriceList> {
|
||||
const [priceLists, count] = await listAndCountPriceListPricingModule({
|
||||
filters: { id: [id] },
|
||||
container,
|
||||
})
|
||||
|
||||
if (count === 0) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Price list with id: ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return priceLists[0]
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from "./get-price-list"
|
||||
export * from "./list-and-count-price-lists"
|
||||
-86
@@ -1,86 +0,0 @@
|
||||
import { LinkModuleUtils, ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { FilterablePriceListProps, MedusaContainer } from "@medusajs/types"
|
||||
import { CustomerGroup, MoneyAmount, PriceList } from "../../../../../models"
|
||||
import { FindConfig } from "../../../../../types/common"
|
||||
import { defaultAdminPriceListRemoteQueryObject } from "../index"
|
||||
|
||||
export async function listAndCountPriceListPricingModule({
|
||||
filters,
|
||||
listConfig = { skip: 0 },
|
||||
container,
|
||||
}: {
|
||||
container: MedusaContainer
|
||||
filters?: FilterablePriceListProps
|
||||
listConfig?: FindConfig<PriceList>
|
||||
}): Promise<[PriceList[], number]> {
|
||||
const remoteQuery = container.resolve(LinkModuleUtils.REMOTE_QUERY)
|
||||
const customerModule = container.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
|
||||
const query = {
|
||||
price_list: {
|
||||
__args: { filters, ...listConfig },
|
||||
...defaultAdminPriceListRemoteQueryObject,
|
||||
},
|
||||
}
|
||||
|
||||
const {
|
||||
rows: priceLists,
|
||||
metadata: { count },
|
||||
} = await remoteQuery(query)
|
||||
|
||||
if (!count) {
|
||||
return [[], 0]
|
||||
}
|
||||
|
||||
const customerGroupIds: string[] = priceLists
|
||||
.map((priceList) =>
|
||||
priceList.price_list_rules
|
||||
.filter((rule) => rule.rule_type.rule_attribute === "customer_group_id")
|
||||
.map((rule) =>
|
||||
rule.price_list_rule_values.map((rule_value) => rule_value.value)
|
||||
)
|
||||
)
|
||||
.flat(2)
|
||||
|
||||
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 || []
|
||||
const priceListRulesData = priceList.price_list_rules || []
|
||||
delete priceList.price_set_money_amounts
|
||||
delete priceList.price_list_rules
|
||||
|
||||
priceList.prices = priceSetMoneyAmounts.map((priceSetMoneyAmount) => {
|
||||
const productVariant = priceSetMoneyAmount.price_set.variant_link.variant
|
||||
|
||||
const rules = priceSetMoneyAmount.price_rules.reduce((acc, curr) => {
|
||||
acc[curr.rule_type.rule_attribute] = curr.value
|
||||
return acc
|
||||
}, {})
|
||||
|
||||
return {
|
||||
...(priceSetMoneyAmount.money_amount as MoneyAmount),
|
||||
price_list_id: priceList.id,
|
||||
variant_id: productVariant?.id ?? null,
|
||||
variant: productVariant ?? null,
|
||||
region_id: rules["region_id"] ?? null,
|
||||
rules,
|
||||
}
|
||||
})
|
||||
|
||||
priceList.name = priceList.title
|
||||
delete priceList.title
|
||||
|
||||
const customerGroupRule = priceListRulesData.find(
|
||||
(plr) => plr.rule_type.rule_attribute === "customer_group_id"
|
||||
)
|
||||
|
||||
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