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:
Riqwan Thamir
2024-03-14 20:04:53 +00:00
committed by GitHub
parent 7be0a2cf6d
commit e5945479e0
13 changed files with 366 additions and 2 deletions
@@ -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[]
}