feat(medusa,core-flows,types): adds update promotion rule endpoint + workflow (#6702)
what: - adds endpoint + workflow to update promotion rule - adds method in promotion to update promotion rules
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { updatePromotionRulesWorkflow } from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import {
|
||||
defaultAdminPromotionFields,
|
||||
defaultAdminPromotionRelations,
|
||||
} from "../../../../query-config"
|
||||
import { AdminPostPromotionsPromotionRulesBatchUpdateReq } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchUpdateReq>,
|
||||
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 promotionModuleService: IPromotionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const promotion = await promotionModuleService.retrieve(id, {
|
||||
select: defaultAdminPromotionFields,
|
||||
relations: defaultAdminPromotionRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
AdminPostPromotionsPromotionReq,
|
||||
AdminPostPromotionsPromotionRulesBatchAddReq,
|
||||
AdminPostPromotionsPromotionRulesBatchRemoveReq,
|
||||
AdminPostPromotionsPromotionRulesBatchUpdateReq,
|
||||
AdminPostPromotionsReq,
|
||||
} from "./validators"
|
||||
|
||||
@@ -63,6 +64,13 @@ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
matcher: "/admin/promotions/:id/buy-rules/batch/add",
|
||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesBatchAddReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/rules/batch/update",
|
||||
middlewares: [
|
||||
transformBody(AdminPostPromotionsPromotionRulesBatchUpdateReq),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/rules/batch/remove",
|
||||
|
||||
@@ -228,3 +228,34 @@ export class AdminPostPromotionsPromotionRulesBatchRemoveReq {
|
||||
@IsString({ each: true })
|
||||
rule_ids: string[]
|
||||
}
|
||||
|
||||
export class AdminPostPromotionsPromotionRulesBatchUpdateReq {
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => UpdatePromotionRule)
|
||||
rules: UpdatePromotionRule[]
|
||||
}
|
||||
|
||||
export class UpdatePromotionRule {
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
id: string
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(PromotionRuleOperator)
|
||||
operator?: PromotionRuleOperator
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
description?: string | null
|
||||
|
||||
@IsOptional()
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
attribute: string
|
||||
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@Type(() => String)
|
||||
values: string[]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user