fix: Standardize remaining batch methods and update FE calls (#7122)
This commit is contained in:
1 parent
ea14a3bd68
commit
9e3556686b
49 files changed
+851
-611
No files matched your search
@@ -1,35 +0,0 @@
|
||||
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { refetchPromotion } from "../../../../helpers"
|
||||
import { AdminCreateBatchRulesType } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminCreateBatchRulesType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = addRulesToPromotionsWorkflow(req.scope)
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.BUY_RULES,
|
||||
data: { id, ...req.validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const promotion = await refetchPromotion(
|
||||
id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { AdminRemoveBatchRulesType } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminRemoveBatchRulesType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
|
||||
const validatedBody = req.validatedBody
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.BUY_RULES,
|
||||
data: { id, ...validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
ids: validatedBody.rule_ids,
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { batchPromotionRulesWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../types/routing"
|
||||
import { BatchMethodRequest } from "@medusajs/types"
|
||||
import {
|
||||
AdminCreatePromotionRuleType,
|
||||
AdminUpdatePromotionRuleType,
|
||||
} from "../../../validators"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import { refetchBatchRules } from "../../../helpers"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<
|
||||
BatchMethodRequest<
|
||||
AdminCreatePromotionRuleType,
|
||||
AdminUpdatePromotionRuleType
|
||||
>
|
||||
>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const { result, errors } = await batchPromotionRulesWorkflow(req.scope).run({
|
||||
input: {
|
||||
id,
|
||||
rule_type: RuleType.BUY_RULES,
|
||||
create: req.validatedBody.create,
|
||||
update: req.validatedBody.update,
|
||||
delete: req.validatedBody.delete,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const batchResults = await refetchBatchRules(
|
||||
result,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json(batchResults)
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { refetchPromotion } from "../../../../helpers"
|
||||
import { AdminCreateBatchRulesType } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminCreateBatchRulesType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = addRulesToPromotionsWorkflow(req.scope)
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.RULES,
|
||||
data: { id, ...req.validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const promotion = await refetchPromotion(
|
||||
id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
|
||||
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import { AdminRemoveBatchRulesType } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminRemoveBatchRulesType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
|
||||
const validatedBody = req.validatedBody
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.RULES,
|
||||
data: { id, ...validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
ids: validatedBody.rule_ids,
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { batchPromotionRulesWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../types/routing"
|
||||
import { BatchMethodRequest } from "@medusajs/types"
|
||||
import {
|
||||
AdminCreatePromotionRuleType,
|
||||
AdminUpdatePromotionRuleType,
|
||||
} from "../../../validators"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import { refetchBatchRules } from "../../../helpers"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<
|
||||
BatchMethodRequest<
|
||||
AdminCreatePromotionRuleType,
|
||||
AdminUpdatePromotionRuleType
|
||||
>
|
||||
>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const { result, errors } = await batchPromotionRulesWorkflow(req.scope).run({
|
||||
input: {
|
||||
id,
|
||||
rule_type: RuleType.RULES,
|
||||
create: req.validatedBody.create,
|
||||
update: req.validatedBody.update,
|
||||
delete: req.validatedBody.delete,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const batchResults = await refetchBatchRules(
|
||||
result,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json(batchResults)
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
import { updatePromotionRulesWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { AdminUpdateBatchRulesType } from "../../../../validators"
|
||||
import { refetchPromotion } from "../../../../helpers"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminUpdateBatchRulesType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = updatePromotionRulesWorkflow(req.scope)
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: { data: req.validatedBody.rules },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const promotion = await refetchPromotion(
|
||||
id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
if (!promotion) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Promotion with id: ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { refetchPromotion } from "../../../../helpers"
|
||||
import { AdminCreateBatchRulesType } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminCreateBatchRulesType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = addRulesToPromotionsWorkflow(req.scope)
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.TARGET_RULES,
|
||||
data: { id, ...req.validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const promotion = await refetchPromotion(
|
||||
id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { AdminRemoveBatchRulesType } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminRemoveBatchRulesType>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
|
||||
const validatedBody = req.validatedBody
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.TARGET_RULES,
|
||||
data: { id, ...validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
ids: validatedBody.rule_ids,
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { batchPromotionRulesWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../types/routing"
|
||||
import { BatchMethodRequest } from "@medusajs/types"
|
||||
import {
|
||||
AdminCreatePromotionRuleType,
|
||||
AdminUpdatePromotionRuleType,
|
||||
} from "../../../validators"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import { refetchBatchRules } from "../../../helpers"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<
|
||||
BatchMethodRequest<
|
||||
AdminCreatePromotionRuleType,
|
||||
AdminUpdatePromotionRuleType
|
||||
>
|
||||
>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const { result, errors } = await batchPromotionRulesWorkflow(req.scope).run({
|
||||
input: {
|
||||
id,
|
||||
rule_type: RuleType.TARGET_RULES,
|
||||
create: req.validatedBody.create,
|
||||
update: req.validatedBody.update,
|
||||
delete: req.validatedBody.delete,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const batchResults = await refetchBatchRules(
|
||||
result,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json(batchResults)
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import { BatchMethodResponse } from "@medusajs/types"
|
||||
import { PromotionRuleDTO, MedusaContainer } from "@medusajs/types"
|
||||
import {
|
||||
promiseAll,
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
@@ -21,3 +23,44 @@ export const refetchPromotion = async (
|
||||
const promotions = await remoteQuery(queryObject)
|
||||
return promotions[0]
|
||||
}
|
||||
|
||||
export const refetchBatchRules = async (
|
||||
batchResult: BatchMethodResponse<PromotionRuleDTO>,
|
||||
scope: MedusaContainer,
|
||||
fields: string[]
|
||||
) => {
|
||||
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
let created = Promise.resolve<PromotionRuleDTO[]>([])
|
||||
let updated = Promise.resolve<PromotionRuleDTO[]>([])
|
||||
|
||||
if (batchResult.created.length) {
|
||||
const createdQuery = remoteQueryObjectFromString({
|
||||
entryPoint: "promotion_rule",
|
||||
variables: {
|
||||
filters: { id: batchResult.created.map((p) => p.id) },
|
||||
},
|
||||
fields: fields,
|
||||
})
|
||||
|
||||
created = remoteQuery(createdQuery)
|
||||
}
|
||||
|
||||
if (batchResult.updated.length) {
|
||||
const updatedQuery = remoteQueryObjectFromString({
|
||||
entryPoint: "promotion_rule",
|
||||
variables: {
|
||||
filters: { id: batchResult.updated.map((p) => p.id) },
|
||||
},
|
||||
fields: fields,
|
||||
})
|
||||
|
||||
updated = remoteQuery(updatedQuery)
|
||||
}
|
||||
|
||||
const [createdRes, updatedRes] = await promiseAll([created, updated])
|
||||
return {
|
||||
created: createdRes,
|
||||
updated: updatedRes,
|
||||
deleted: batchResult.deleted,
|
||||
}
|
||||
}
|
||||
@@ -3,17 +3,18 @@ import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||
import {
|
||||
AdminCreateBatchRules,
|
||||
AdminCreatePromotion,
|
||||
AdminCreatePromotionRule,
|
||||
AdminGetPromotionParams,
|
||||
AdminGetPromotionRuleParams,
|
||||
AdminGetPromotionRuleTypeParams,
|
||||
AdminGetPromotionsParams,
|
||||
AdminGetPromotionsRuleValueParams,
|
||||
AdminRemoveBatchRules,
|
||||
AdminUpdateBatchRules,
|
||||
AdminUpdatePromotion,
|
||||
AdminUpdatePromotionRule,
|
||||
} from "./validators"
|
||||
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||
import { createBatchBody } from "../../utils/validators"
|
||||
|
||||
export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
@@ -74,78 +75,40 @@ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/rules/batch/add",
|
||||
matcher: "/admin/promotions/:id/rules/batch",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminCreateBatchRules),
|
||||
validateAndTransformBody(
|
||||
createBatchBody(AdminCreatePromotionRule, AdminUpdatePromotionRule)
|
||||
),
|
||||
validateAndTransformQuery(
|
||||
AdminGetPromotionParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
AdminGetPromotionRuleParams,
|
||||
QueryConfig.retrieveRuleTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/target-rules/batch/add",
|
||||
matcher: "/admin/promotions/:id/target-rules/batch",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminCreateBatchRules),
|
||||
validateAndTransformBody(
|
||||
createBatchBody(AdminCreatePromotionRule, AdminUpdatePromotionRule)
|
||||
),
|
||||
validateAndTransformQuery(
|
||||
AdminGetPromotionParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
AdminGetPromotionRuleParams,
|
||||
QueryConfig.retrieveRuleTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/buy-rules/batch/add",
|
||||
matcher: "/admin/promotions/:id/buy-rules/batch",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminCreateBatchRules),
|
||||
validateAndTransformQuery(
|
||||
AdminGetPromotionParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
validateAndTransformBody(
|
||||
createBatchBody(AdminCreatePromotionRule, AdminUpdatePromotionRule)
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/rules/batch/update",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminUpdateBatchRules),
|
||||
validateAndTransformQuery(
|
||||
AdminGetPromotionParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/rules/batch/remove",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminRemoveBatchRules),
|
||||
validateAndTransformQuery(
|
||||
AdminGetPromotionParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/target-rules/batch/remove",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminRemoveBatchRules),
|
||||
validateAndTransformQuery(
|
||||
AdminGetPromotionParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/buy-rules/batch/remove",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminRemoveBatchRules),
|
||||
validateAndTransformQuery(
|
||||
AdminGetPromotionParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
AdminGetPromotionRuleParams,
|
||||
QueryConfig.retrieveRuleTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
|
||||
@@ -19,6 +19,14 @@ export const defaultAdminPromotionFields = [
|
||||
"rules.values.value",
|
||||
]
|
||||
|
||||
export const defaultAdminPromotionRuleFields = [
|
||||
"id",
|
||||
"description",
|
||||
"attribute",
|
||||
"operator",
|
||||
"values.value",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaults: defaultAdminPromotionFields,
|
||||
isList: false,
|
||||
@@ -29,6 +37,16 @@ export const listTransformQueryConfig = {
|
||||
isList: true,
|
||||
}
|
||||
|
||||
export const retrieveRuleTransformQueryConfig = {
|
||||
defaults: defaultAdminPromotionRuleFields,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listRuleTransformQueryConfig = {
|
||||
...retrieveRuleTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
|
||||
export const listRuleValueTransformQueryConfig = {
|
||||
defaults: [],
|
||||
allowed: [],
|
||||
|
||||
@@ -36,6 +36,11 @@ export const AdminGetPromotionsParams = createFindParams({
|
||||
})
|
||||
)
|
||||
|
||||
export type AdminGetPromotionRuleParamsType = z.infer<
|
||||
typeof AdminGetPromotionRuleParams
|
||||
>
|
||||
export const AdminGetPromotionRuleParams = createSelectParams()
|
||||
|
||||
export type AdminGetPromotionRuleTypeParamsType = z.infer<
|
||||
typeof AdminGetPromotionRuleTypeParams
|
||||
>
|
||||
@@ -185,24 +190,3 @@ export const AdminUpdatePromotion = z
|
||||
message:
|
||||
"Buyget promotions require at least one buy rule and quantities to be defined",
|
||||
})
|
||||
|
||||
export type AdminCreateBatchRulesType = z.infer<typeof AdminCreateBatchRules>
|
||||
export const AdminCreateBatchRules = z
|
||||
.object({
|
||||
rules: z.array(AdminCreatePromotionRule).min(1),
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type AdminUpdateBatchRulesType = z.infer<typeof AdminUpdateBatchRules>
|
||||
export const AdminUpdateBatchRules = z
|
||||
.object({
|
||||
rules: z.array(AdminUpdatePromotionRule).min(1),
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type AdminRemoveBatchRulesType = z.infer<typeof AdminRemoveBatchRules>
|
||||
export const AdminRemoveBatchRules = z
|
||||
.object({
|
||||
rule_ids: z.array(z.string()).min(1),
|
||||
})
|
||||
.strict()
|
||||
@@ -1,37 +0,0 @@
|
||||
import { addRulesToFulfillmentShippingOptionWorkflow } from "@medusajs/core-flows"
|
||||
import { AdminShippingOptionRetrieveResponse } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { AdminShippingOptionRulesBatchAddType } from "../../../../validators"
|
||||
import { refetchShippingOption } from "../../../../helpers"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminShippingOptionRulesBatchAddType>,
|
||||
res: MedusaResponse<AdminShippingOptionRetrieveResponse>
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = addRulesToFulfillmentShippingOptionWorkflow(req.scope)
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
data: req.validatedBody.rules.map((rule) => ({
|
||||
...rule,
|
||||
shipping_option_id: id,
|
||||
})),
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const shippingOption = await refetchShippingOption(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
res.status(200).json({ shipping_option: shippingOption })
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import { removeRulesFromFulfillmentShippingOptionWorkflow } from "@medusajs/core-flows"
|
||||
import { AdminShippingOptionRetrieveResponse } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { AdminShippingOptionRulesBatchRemoveType } from "../../../../validators"
|
||||
import { refetchShippingOption } from "../../../../helpers"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminShippingOptionRulesBatchRemoveType>,
|
||||
res: MedusaResponse<AdminShippingOptionRetrieveResponse>
|
||||
) => {
|
||||
const workflow = removeRulesFromFulfillmentShippingOptionWorkflow(req.scope)
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: { ids: req.validatedBody.rule_ids },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const shippingOption = await refetchShippingOption(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
res.status(200).json({ shipping_option: shippingOption })
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../types/routing"
|
||||
import { BatchMethodRequest } from "@medusajs/types"
|
||||
import {
|
||||
AdminCreateShippingOptionRuleType,
|
||||
AdminUpdateShippingOptionRuleType,
|
||||
} from "../../../validators"
|
||||
import { refetchBatchRules } from "../../../helpers"
|
||||
import { batchShippingOptionRulesWorkflow } from "@medusajs/core-flows"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<
|
||||
BatchMethodRequest<
|
||||
AdminCreateShippingOptionRuleType,
|
||||
AdminUpdateShippingOptionRuleType
|
||||
>
|
||||
>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const { result, errors } = await batchShippingOptionRulesWorkflow(
|
||||
req.scope
|
||||
).run({
|
||||
input: {
|
||||
create: req.validatedBody.create?.map((c) => ({
|
||||
...c,
|
||||
shipping_option_id: id,
|
||||
})),
|
||||
update: req.validatedBody.update,
|
||||
delete: req.validatedBody.delete,
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const batchResults = await refetchBatchRules(
|
||||
result,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json(batchResults)
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import {
|
||||
BatchMethodResponse,
|
||||
MedusaContainer,
|
||||
ShippingOptionRuleDTO,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
promiseAll,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
@@ -21,3 +26,44 @@ export const refetchShippingOption = async (
|
||||
const shippingOptions = await remoteQuery(queryObject)
|
||||
return shippingOptions[0]
|
||||
}
|
||||
|
||||
export const refetchBatchRules = async (
|
||||
batchResult: BatchMethodResponse<ShippingOptionRuleDTO>,
|
||||
scope: MedusaContainer,
|
||||
fields: string[]
|
||||
) => {
|
||||
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
let created = Promise.resolve<ShippingOptionRuleDTO[]>([])
|
||||
let updated = Promise.resolve<ShippingOptionRuleDTO[]>([])
|
||||
|
||||
if (batchResult.created.length) {
|
||||
const createdQuery = remoteQueryObjectFromString({
|
||||
entryPoint: "shipping_option_rule",
|
||||
variables: {
|
||||
filters: { id: batchResult.created.map((p) => p.id) },
|
||||
},
|
||||
fields: fields,
|
||||
})
|
||||
|
||||
created = remoteQuery(createdQuery)
|
||||
}
|
||||
|
||||
if (batchResult.updated.length) {
|
||||
const updatedQuery = remoteQueryObjectFromString({
|
||||
entryPoint: "shipping_option_rule",
|
||||
variables: {
|
||||
filters: { id: batchResult.updated.map((p) => p.id) },
|
||||
},
|
||||
fields: fields,
|
||||
})
|
||||
|
||||
updated = remoteQuery(updatedQuery)
|
||||
}
|
||||
|
||||
const [createdRes, updatedRes] = await promiseAll([created, updated])
|
||||
return {
|
||||
created: createdRes,
|
||||
updated: updatedRes,
|
||||
deleted: batchResult.deleted,
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,21 @@ import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import {
|
||||
AdminCreateShippingOption,
|
||||
AdminCreateShippingOptionRule,
|
||||
AdminGetShippingOptionParams,
|
||||
AdminGetShippingOptionRuleParams,
|
||||
AdminGetShippingOptionsParams,
|
||||
AdminShippingOptionRulesBatchAdd,
|
||||
AdminShippingOptionRulesBatchRemove,
|
||||
AdminUpdateShippingOption,
|
||||
AdminUpdateShippingOptionRule,
|
||||
} from "./validators"
|
||||
import {
|
||||
listTransformQueryConfig,
|
||||
retrieveRuleTransformQueryConfig,
|
||||
retrieveTransformQueryConfig,
|
||||
} from "./query-config"
|
||||
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||
import { createBatchBody } from "../../utils/validators"
|
||||
|
||||
export const adminShippingOptionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
@@ -58,23 +61,17 @@ export const adminShippingOptionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/shipping-options/:id/rules/batch/add",
|
||||
matcher: "/admin/shipping-options/:id/rules/batch",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminShippingOptionRulesBatchAdd),
|
||||
validateAndTransformQuery(
|
||||
AdminGetShippingOptionParams,
|
||||
retrieveTransformQueryConfig
|
||||
validateAndTransformBody(
|
||||
createBatchBody(
|
||||
AdminCreateShippingOptionRule,
|
||||
AdminUpdateShippingOptionRule
|
||||
)
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/shipping-options/:id/rules/batch/remove",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminShippingOptionRulesBatchRemove),
|
||||
validateAndTransformQuery(
|
||||
AdminGetShippingOptionParams,
|
||||
retrieveTransformQueryConfig
|
||||
AdminGetShippingOptionRuleParams,
|
||||
retrieveRuleTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
|
||||
@@ -23,3 +23,21 @@ export const listTransformQueryConfig = {
|
||||
...retrieveTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
|
||||
export const defaultAdminShippingOptionRuleFields = [
|
||||
"id",
|
||||
"description",
|
||||
"attribute",
|
||||
"operator",
|
||||
"values.value",
|
||||
]
|
||||
|
||||
export const retrieveRuleTransformQueryConfig = {
|
||||
defaults: defaultAdminShippingOptionRuleFields,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listRuleTransformQueryConfig = {
|
||||
...retrieveRuleTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
@@ -21,6 +21,10 @@ export const AdminGetShippingOptionsParams = createFindParams({
|
||||
/**
|
||||
* SHIPPING OPTIONS RULES
|
||||
*/
|
||||
export type AdminGetShippingOptionRuleParamsType = z.infer<
|
||||
typeof AdminGetShippingOptionRuleParams
|
||||
>
|
||||
export const AdminGetShippingOptionRuleParams = createSelectParams()
|
||||
|
||||
export type AdminCreateShippingOptionRuleType = z.infer<
|
||||
typeof AdminCreateShippingOptionRule
|
||||
@@ -33,21 +37,15 @@ export const AdminCreateShippingOptionRule = z
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type AdminShippingOptionRulesBatchAddType = z.infer<
|
||||
typeof AdminShippingOptionRulesBatchAdd
|
||||
export type AdminUpdateShippingOptionRuleType = z.infer<
|
||||
typeof AdminUpdateShippingOptionRule
|
||||
>
|
||||
export const AdminShippingOptionRulesBatchAdd = z
|
||||
export const AdminUpdateShippingOptionRule = z
|
||||
.object({
|
||||
rules: AdminCreateShippingOptionRule.array(),
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type AdminShippingOptionRulesBatchRemoveType = z.infer<
|
||||
typeof AdminShippingOptionRulesBatchRemove
|
||||
>
|
||||
export const AdminShippingOptionRulesBatchRemove = z
|
||||
.object({
|
||||
rule_ids: z.array(z.string()),
|
||||
id: z.string(),
|
||||
operator: z.nativeEnum(RuleOperator),
|
||||
attribute: z.string(),
|
||||
value: z.string().or(z.array(z.string())),
|
||||
})
|
||||
.strict()
|
||||
|
||||
|
||||
Reference in new issue
Block a user