chore: buyget templates add default target + buy rules (#7500)
* chore: buyget templates add default target + buy rules * chore: reposition * chore: address comments * chore: added fixes * chore: fix typo * chore: fix strictness checks
This commit is contained in:
@@ -10,8 +10,8 @@ import {
|
||||
MedusaResponse,
|
||||
} from "../../../../../types/routing"
|
||||
import {
|
||||
getRuleAttributesMap,
|
||||
operatorsMap,
|
||||
ruleAttributesMap,
|
||||
ruleQueryConfigurations,
|
||||
validateRuleType,
|
||||
} from "../../utils"
|
||||
@@ -33,7 +33,9 @@ export const GET = async (
|
||||
})
|
||||
|
||||
const [promotion] = await remoteQuery(queryObject)
|
||||
const ruleAttributes = ruleAttributesMap[ruleType]
|
||||
const ruleAttributes = getRuleAttributesMap(
|
||||
promotion?.type || req.query.promotion_type
|
||||
)[ruleType]
|
||||
const promotionRules: any[] = []
|
||||
|
||||
if (dasherizedRuleType === RuleType.RULES) {
|
||||
@@ -49,8 +51,19 @@ export const GET = async (
|
||||
const requiredRules = ruleAttributes.filter((attr) => !!attr.required)
|
||||
|
||||
for (const disguisedRule of disguisedRules) {
|
||||
const value = promotion?.application_method?.[disguisedRule.id]
|
||||
const values = value ? [{ label: value, value }] : []
|
||||
const getValues = () => {
|
||||
const value = promotion?.application_method?.[disguisedRule.id]
|
||||
|
||||
if (disguisedRule.field_type === "number") {
|
||||
return value
|
||||
}
|
||||
|
||||
if (value) {
|
||||
return [{ label: value, value }]
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
transformedRules.push({
|
||||
id: undefined,
|
||||
@@ -60,7 +73,7 @@ export const GET = async (
|
||||
hydrate: disguisedRule.hydrate || false,
|
||||
operator: RuleOperator.EQ,
|
||||
operator_label: operatorsMap[RuleOperator.EQ].label,
|
||||
values,
|
||||
values: getValues(),
|
||||
disguised: true,
|
||||
required: true,
|
||||
})
|
||||
@@ -90,7 +103,7 @@ export const GET = async (
|
||||
entryPoint: queryConfig.entryPoint,
|
||||
variables: {
|
||||
filters: {
|
||||
[queryConfig.valueAttr]: promotionRule.values.map((v) => v.value),
|
||||
[queryConfig.valueAttr]: promotionRule.values?.map((v) => v.value),
|
||||
},
|
||||
},
|
||||
fields: [queryConfig.labelAttr, queryConfig.valueAttr],
|
||||
@@ -104,10 +117,11 @@ export const GET = async (
|
||||
])
|
||||
)
|
||||
|
||||
promotionRule.values = promotionRule.values.map((value) => ({
|
||||
value: value.value,
|
||||
label: valueLabelMap.get(value.value) || value.value,
|
||||
}))
|
||||
promotionRule.values =
|
||||
promotionRule.values?.map((value) => ({
|
||||
value: value.value,
|
||||
label: valueLabelMap.get(value.value) || value.value,
|
||||
})) || promotionRule.values
|
||||
|
||||
if (!currentRuleAttribute.hydrate) {
|
||||
transformedRules.push({
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { unlessPath } from "../../utils/unless-path"
|
||||
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||
import { createBatchBody } from "../../utils/validators"
|
||||
@@ -62,9 +63,12 @@ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
method: ["GET"],
|
||||
matcher: "/admin/promotions/:id/:rule_type",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
AdminGetPromotionRuleTypeParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
unlessPath(
|
||||
/.*\/promotions\/rule-attribute-options/,
|
||||
validateAndTransformQuery(
|
||||
AdminGetPromotionRuleTypeParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
)
|
||||
),
|
||||
],
|
||||
},
|
||||
@@ -118,4 +122,14 @@ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/promotions/rule-attribute-options/:rule_type",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
AdminGetPromotionRuleParams,
|
||||
QueryConfig.listRuleTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
+5
-3
@@ -2,17 +2,19 @@ import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../types/routing"
|
||||
import { ruleAttributesMap, validateRuleType } from "../../utils"
|
||||
import { getRuleAttributesMap, validateRuleType } from "../../utils"
|
||||
import { AdminGetPromotionRuleParamsType } from "../../validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
req: AuthenticatedMedusaRequest<AdminGetPromotionRuleParamsType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { rule_type: ruleType } = req.params
|
||||
|
||||
validateRuleType(ruleType)
|
||||
|
||||
const attributes = ruleAttributesMap[ruleType] || []
|
||||
const attributes =
|
||||
getRuleAttributesMap(req.query.promotion_type as string)[ruleType] || []
|
||||
|
||||
res.json({
|
||||
attributes,
|
||||
|
||||
+8
-3
@@ -11,12 +11,17 @@ import {
|
||||
validateRuleAttribute,
|
||||
validateRuleType,
|
||||
} from "../../../utils"
|
||||
import { AdminGetPromotionRuleParamsType } from "../../../validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
req: AuthenticatedMedusaRequest<AdminGetPromotionRuleParamsType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { rule_type: ruleType, rule_attribute_id: ruleAttributeId } = req.params
|
||||
const {
|
||||
rule_type: ruleType,
|
||||
rule_attribute_id: ruleAttributeId,
|
||||
promotion_type: promotionType,
|
||||
} = req.params
|
||||
const queryConfig = ruleQueryConfigurations[ruleAttributeId]
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
const filterableFields = req.filterableFields
|
||||
@@ -28,7 +33,7 @@ export const GET = async (
|
||||
}
|
||||
|
||||
validateRuleType(ruleType)
|
||||
validateRuleAttribute(ruleType, ruleAttributeId)
|
||||
validateRuleAttribute(promotionType, ruleType, ruleAttributeId)
|
||||
|
||||
const { rows } = await remoteQuery(
|
||||
remoteQueryObjectFromString({
|
||||
|
||||
@@ -1,21 +1,11 @@
|
||||
import { PromotionType } from "@medusajs/utils"
|
||||
|
||||
export enum DisguisedRule {
|
||||
APPLY_TO_QUANTITY = "apply_to_quantity",
|
||||
BUY_RULES_MIN_QUANTITY = "buy_rules_min_quantity",
|
||||
CURRENCY_CODE = "currency_code",
|
||||
}
|
||||
|
||||
export const disguisedRulesMap = {
|
||||
[DisguisedRule.APPLY_TO_QUANTITY]: {
|
||||
relation: "application_method",
|
||||
},
|
||||
[DisguisedRule.BUY_RULES_MIN_QUANTITY]: {
|
||||
relation: "application_method",
|
||||
},
|
||||
[DisguisedRule.CURRENCY_CODE]: {
|
||||
relation: "application_method",
|
||||
},
|
||||
}
|
||||
|
||||
const ruleAttributes = [
|
||||
{
|
||||
id: DisguisedRule.CURRENCY_CODE,
|
||||
@@ -94,7 +84,7 @@ const commonAttributes = [
|
||||
},
|
||||
]
|
||||
|
||||
const buyRuleAttributes = [
|
||||
const buyGetBuyRules = [
|
||||
{
|
||||
id: DisguisedRule.BUY_RULES_MIN_QUANTITY,
|
||||
value: DisguisedRule.BUY_RULES_MIN_QUANTITY,
|
||||
@@ -103,10 +93,9 @@ const buyRuleAttributes = [
|
||||
required: true,
|
||||
disguised: true,
|
||||
},
|
||||
...commonAttributes,
|
||||
]
|
||||
|
||||
const targetRuleAttributes = [
|
||||
const buyGetTargetRules = [
|
||||
{
|
||||
id: DisguisedRule.APPLY_TO_QUANTITY,
|
||||
value: DisguisedRule.APPLY_TO_QUANTITY,
|
||||
@@ -115,11 +104,19 @@ const targetRuleAttributes = [
|
||||
required: true,
|
||||
disguised: true,
|
||||
},
|
||||
...commonAttributes,
|
||||
]
|
||||
|
||||
export const ruleAttributesMap = {
|
||||
rules: ruleAttributes,
|
||||
"target-rules": targetRuleAttributes,
|
||||
"buy-rules": buyRuleAttributes,
|
||||
export const getRuleAttributesMap = (promotionType?: string) => {
|
||||
const map = {
|
||||
rules: [...ruleAttributes],
|
||||
"target-rules": [...commonAttributes],
|
||||
"buy-rules": [...commonAttributes],
|
||||
}
|
||||
|
||||
if (promotionType === PromotionType.BUYGET) {
|
||||
map["buy-rules"].push(...buyGetBuyRules)
|
||||
map["target-rules"].push(...buyGetTargetRules)
|
||||
}
|
||||
|
||||
return map
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import { ruleAttributesMap } from "./rule-attributes-map"
|
||||
import { getRuleAttributesMap } from "./rule-attributes-map"
|
||||
|
||||
export function validateRuleAttribute(
|
||||
promotionType: string | undefined,
|
||||
ruleType: string,
|
||||
ruleAttributeId: string
|
||||
) {
|
||||
const ruleAttributes = ruleAttributesMap[ruleType] || []
|
||||
const ruleAttributes = getRuleAttributesMap(promotionType)[ruleType] || []
|
||||
const ruleAttribute = ruleAttributes.find((obj) => obj.id === ruleAttributeId)
|
||||
|
||||
if (!ruleAttribute) {
|
||||
|
||||
@@ -47,12 +47,18 @@ export const AdminGetPromotionsParams = createFindParams({
|
||||
export type AdminGetPromotionRuleParamsType = z.infer<
|
||||
typeof AdminGetPromotionRuleParams
|
||||
>
|
||||
export const AdminGetPromotionRuleParams = createSelectParams()
|
||||
export const AdminGetPromotionRuleParams = z.object({
|
||||
promotion_type: z.string().optional(),
|
||||
})
|
||||
|
||||
export type AdminGetPromotionRuleTypeParamsType = z.infer<
|
||||
typeof AdminGetPromotionRuleTypeParams
|
||||
>
|
||||
export const AdminGetPromotionRuleTypeParams = createSelectParams()
|
||||
export const AdminGetPromotionRuleTypeParams = createSelectParams().merge(
|
||||
z.object({
|
||||
promotion_type: z.string().optional(),
|
||||
})
|
||||
)
|
||||
|
||||
export type AdminGetPromotionsRuleValueParamsType = z.infer<
|
||||
typeof AdminGetPromotionsRuleValueParams
|
||||
|
||||
Reference in New Issue
Block a user