feat(core-flows,types,pricing,medusa): Products API can create prices with rules (#7796)
* chore: Products API can create prices with rules * chore: fix tests * chore: cleanup * chore: address comments
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../types/routing"
|
||||
import {
|
||||
deleteProductVariantsWorkflow,
|
||||
updateProductVariantsWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../types/routing"
|
||||
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { refetchEntity } from "../../../../../utils/refetch-entity"
|
||||
import {
|
||||
remapKeysForProduct,
|
||||
remapKeysForVariant,
|
||||
remapProductResponse,
|
||||
remapVariantResponse,
|
||||
} from "../../../helpers"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { refetchEntity } from "../../../../../utils/refetch-entity"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
@@ -54,6 +54,7 @@ export const POST = async (
|
||||
req.scope,
|
||||
remapKeysForProduct(req.remoteQueryConfig.fields ?? [])
|
||||
)
|
||||
|
||||
res.status(200).json({ product: remapProductResponse(product) })
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
BatchMethodResponse,
|
||||
HttpTypes,
|
||||
MedusaContainer,
|
||||
PriceDTO,
|
||||
ProductDTO,
|
||||
ProductVariantDTO,
|
||||
} from "@medusajs/types"
|
||||
@@ -25,6 +26,7 @@ export const remapKeysForProduct = (selectFields: string[]) => {
|
||||
const productFields = selectFields.filter(
|
||||
(fieldName: string) => !isPricing(fieldName)
|
||||
)
|
||||
|
||||
const pricingFields = selectFields
|
||||
.filter((fieldName: string) => isPricing(fieldName))
|
||||
.map((fieldName: string) =>
|
||||
@@ -38,6 +40,7 @@ export const remapKeysForVariant = (selectFields: string[]) => {
|
||||
const variantFields = selectFields.filter(
|
||||
(fieldName: string) => !isPricing(fieldName)
|
||||
)
|
||||
|
||||
const pricingFields = selectFields
|
||||
.filter((fieldName: string) => isPricing(fieldName))
|
||||
.map((fieldName: string) =>
|
||||
@@ -75,14 +78,30 @@ export const remapVariantResponse = (
|
||||
variant_id: variant.id,
|
||||
created_at: price.created_at,
|
||||
updated_at: price.updated_at,
|
||||
rules: buildRules(price),
|
||||
})),
|
||||
}
|
||||
|
||||
delete (resp as any).price_set
|
||||
|
||||
// TODO: Remove any once all typings are cleaned up
|
||||
return resp as any
|
||||
}
|
||||
|
||||
export const buildRules = (price: PriceDTO) => {
|
||||
const rules: Record<string, string> = {}
|
||||
|
||||
for (const priceRule of price.price_rules || []) {
|
||||
const ruleAttribute = priceRule.rule_type?.rule_attribute
|
||||
|
||||
if (ruleAttribute) {
|
||||
rules[ruleAttribute] = priceRule.value
|
||||
}
|
||||
}
|
||||
|
||||
return rules
|
||||
}
|
||||
|
||||
export const refetchVariant = async (
|
||||
variantId: string,
|
||||
scope: MedusaContainer,
|
||||
|
||||
@@ -22,6 +22,8 @@ export const defaultAdminProductsVariantFields = [
|
||||
"upc",
|
||||
"barcode",
|
||||
"*prices",
|
||||
"prices.price_rules.value",
|
||||
"prices.price_rules.rule_type.rule_attribute",
|
||||
"*options",
|
||||
]
|
||||
|
||||
@@ -82,6 +84,8 @@ export const defaultAdminProductFields = [
|
||||
"*images",
|
||||
"*variants",
|
||||
"*variants.prices",
|
||||
"variants.prices.price_rules.value",
|
||||
"variants.prices.price_rules.rule_type.rule_attribute",
|
||||
"*variants.options",
|
||||
"*sales_channels",
|
||||
]
|
||||
|
||||
@@ -106,6 +106,7 @@ export const AdminCreateVariantPrice = z.object({
|
||||
amount: z.number(),
|
||||
min_quantity: z.number().nullish(),
|
||||
max_quantity: z.number().nullish(),
|
||||
rules: z.record(z.string(), z.string()).optional(),
|
||||
})
|
||||
|
||||
// TODO: Add support for rules
|
||||
@@ -118,6 +119,7 @@ export const AdminUpdateVariantPrice = z.object({
|
||||
amount: z.number().optional(),
|
||||
min_quantity: z.number().nullish(),
|
||||
max_quantity: z.number().nullish(),
|
||||
rules: z.record(z.string(), z.string()).optional(),
|
||||
})
|
||||
|
||||
export type AdminCreateProductTypeType = z.infer<typeof AdminCreateProductType>
|
||||
|
||||
Reference in New Issue
Block a user