chore(medusa): use batch/add and batch/remove endpoints for promotion rules (#6701)

This commit is contained in:
Riqwan Thamir
2024-03-14 19:07:48 +01:00
committed by GitHub
parent c384ede3b1
commit 7be0a2cf6d
10 changed files with 208 additions and 213 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
chore(medusa): use batch/add and batch/remove endpoints for promotion rules

View File

@@ -44,11 +44,11 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/promotions/:id/rules", () => {
describe("POST /admin/promotions/:id/rules/batch/add", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/rules`,
`/admin/promotions/${standardPromotion.id}/rules/batch/add`,
{
rules: [
{
@@ -72,7 +72,7 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.post(
`/admin/promotions/does-not-exist/rules`,
`/admin/promotions/does-not-exist/rules/batch/add`,
{
rules: [
{
@@ -95,7 +95,7 @@ medusaIntegrationTestRunner({
it("should add rules to a promotion successfully", async () => {
const response = await api.post(
`/admin/promotions/${standardPromotion.id}/rules`,
`/admin/promotions/${standardPromotion.id}/rules/batch/add`,
{
rules: [
{
@@ -129,11 +129,11 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/promotions/:id/target-rules", () => {
describe("POST /admin/promotions/:id/target-rules/batch/add", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/target-rules`,
`/admin/promotions/${standardPromotion.id}/target-rules/batch/add`,
{
rules: [
{
@@ -157,7 +157,7 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.post(
`/admin/promotions/does-not-exist/target-rules`,
`/admin/promotions/does-not-exist/target-rules/batch/add`,
{
rules: [
{
@@ -180,7 +180,7 @@ medusaIntegrationTestRunner({
it("should add target rules to a promotion successfully", async () => {
const response = await api.post(
`/admin/promotions/${standardPromotion.id}/target-rules`,
`/admin/promotions/${standardPromotion.id}/target-rules/batch/add`,
{
rules: [
{
@@ -216,11 +216,11 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/promotions/:id/buy-rules", () => {
describe("POST /admin/promotions/:id/buy-rules/batch/add", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/buy-rules`,
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/add`,
{
rules: [
{
@@ -244,7 +244,7 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.post(
`/admin/promotions/does-not-exist/buy-rules`,
`/admin/promotions/does-not-exist/buy-rules/batch/add`,
{
rules: [
{
@@ -268,7 +268,7 @@ medusaIntegrationTestRunner({
it("should throw an error when trying to add buy rules to a standard promotion", async () => {
const { response } = await api
.post(
`/admin/promotions/${standardPromotion.id}/buy-rules`,
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/add`,
{
rules: [
{
@@ -307,7 +307,7 @@ medusaIntegrationTestRunner({
})
const response = await api.post(
`/admin/promotions/${buyGetPromotion.id}/buy-rules`,
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch/add`,
{
rules: [
{
@@ -343,13 +343,14 @@ medusaIntegrationTestRunner({
})
})
describe("DELETE /admin/promotions/:id/rules", () => {
describe("POST /admin/promotions/:id/rules/batch/remove", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.delete(`/admin/promotions/${standardPromotion.id}/rules`, {
...adminHeaders,
data: {},
})
.post(
`/admin/promotions/${standardPromotion.id}/rules/batch/remove`,
{},
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(400)
@@ -362,10 +363,11 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.delete(`/admin/promotions/does-not-exist/rules`, {
...adminHeaders,
data: { rule_ids: ["test-rule-id"] },
})
.post(
`/admin/promotions/does-not-exist/rules/batch/remove`,
{ rule_ids: ["test-rule-id"] },
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(404)
@@ -376,14 +378,11 @@ medusaIntegrationTestRunner({
})
it("should remove rules from a promotion successfully", async () => {
const response = await api.delete(
`/admin/promotions/${standardPromotion.id}/rules`,
{
...adminHeaders,
data: { rule_ids: [standardPromotion.rules[0].id] },
}
const response = await api.post(
`/admin/promotions/${standardPromotion.id}/rules/batch/remove`,
{ rule_ids: [standardPromotion.rules[0].id] },
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data).toEqual({
ids: [standardPromotion.rules[0].id],
@@ -400,13 +399,14 @@ medusaIntegrationTestRunner({
})
})
describe("DELETE /admin/promotions/:id/target-rules", () => {
describe("POST /admin/promotions/:id/target-rules/batch/remove", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.delete(`/admin/promotions/${standardPromotion.id}/target-rules`, {
...adminHeaders,
data: {},
})
.post(
`/admin/promotions/${standardPromotion.id}/target-rules/batch/remove`,
{},
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(400)
@@ -419,10 +419,11 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.delete(`/admin/promotions/does-not-exist/target-rules`, {
...adminHeaders,
data: { rule_ids: ["test-rule-id"] },
})
.post(
`/admin/promotions/does-not-exist/target-rules/batch/remove`,
{ rule_ids: ["test-rule-id"] },
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(404)
@@ -434,12 +435,10 @@ medusaIntegrationTestRunner({
it("should remove target rules from a promotion successfully", async () => {
const ruleId = standardPromotion.application_method.target_rules[0].id
const response = await api.delete(
`/admin/promotions/${standardPromotion.id}/target-rules`,
{
...adminHeaders,
data: { rule_ids: [ruleId] },
}
const response = await api.post(
`/admin/promotions/${standardPromotion.id}/target-rules/batch/remove`,
{ rule_ids: [ruleId] },
adminHeaders
)
expect(response.status).toEqual(200)
@@ -458,13 +457,14 @@ medusaIntegrationTestRunner({
})
})
describe("DELETE /admin/promotions/:id/buy-rules", () => {
describe("POST /admin/promotions/:id/buy-rules/batch/remove", () => {
it("should throw error when required params are missing", async () => {
const { response } = await api
.delete(`/admin/promotions/${standardPromotion.id}/buy-rules`, {
...adminHeaders,
data: {},
})
.post(
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/remove`,
{},
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(400)
@@ -477,10 +477,11 @@ medusaIntegrationTestRunner({
it("should throw error when promotion does not exist", async () => {
const { response } = await api
.delete(`/admin/promotions/does-not-exist/buy-rules`, {
...adminHeaders,
data: { rule_ids: ["test-rule-id"] },
})
.post(
`/admin/promotions/does-not-exist/buy-rules/batch/remove`,
{ rule_ids: ["test-rule-id"] },
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(404)
@@ -508,12 +509,10 @@ medusaIntegrationTestRunner({
})
const ruleId = buyGetPromotion!.application_method!.buy_rules![0].id
const response = await api.delete(
`/admin/promotions/${buyGetPromotion.id}/buy-rules`,
{
...adminHeaders,
data: { rule_ids: [ruleId] },
}
const response = await api.post(
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch/remove`,
{ rule_ids: [ruleId] },
adminHeaders
)
expect(response.status).toEqual(200)

View File

@@ -1,25 +1,19 @@
import {
addRulesToPromotionsWorkflow,
removeRulesFromPromotionsWorkflow,
} from "@medusajs/core-flows"
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { RuleType } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
} from "../../../../../../../types/routing"
import {
defaultAdminPromotionFields,
defaultAdminPromotionRelations,
} from "../../query-config"
import {
AdminDeletePromotionsPromotionRulesReq,
AdminPostPromotionsPromotionRulesReq,
} from "../../validators"
} from "../../../../query-config"
import { AdminPostPromotionsPromotionRulesBatchAddReq } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchAddReq>,
res: MedusaResponse
) => {
const id = req.params.id
@@ -27,10 +21,7 @@ export const POST = async (
const { errors } = await workflow.run({
input: {
rule_type: RuleType.BUY_RULES,
data: {
id,
...req.validatedBody,
},
data: { id, ...req.validatedBody },
},
throwOnError: false,
})
@@ -50,33 +41,3 @@ export const POST = async (
res.status(200).json({ promotion })
}
export const DELETE = async (
req: AuthenticatedMedusaRequest<AdminDeletePromotionsPromotionRulesReq>,
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,
})
}

View File

@@ -0,0 +1,34 @@
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
import { RuleType } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { AdminPostPromotionsPromotionRulesBatchRemoveReq } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchRemoveReq>,
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,
})
}

View File

@@ -1,25 +1,19 @@
import {
addRulesToPromotionsWorkflow,
removeRulesFromPromotionsWorkflow,
} from "@medusajs/core-flows"
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { RuleType } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
} from "../../../../../../../types/routing"
import {
defaultAdminPromotionFields,
defaultAdminPromotionRelations,
} from "../../query-config"
import {
AdminDeletePromotionsPromotionRulesReq,
AdminPostPromotionsPromotionRulesReq,
} from "../../validators"
} from "../../../../query-config"
import { AdminPostPromotionsPromotionRulesBatchAddReq } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchAddReq>,
res: MedusaResponse
) => {
const id = req.params.id
@@ -28,10 +22,7 @@ export const POST = async (
const { errors } = await workflow.run({
input: {
rule_type: RuleType.RULES,
data: {
id,
...req.validatedBody,
},
data: { id, ...req.validatedBody },
},
throwOnError: false,
})
@@ -51,33 +42,3 @@ export const POST = async (
res.status(200).json({ promotion })
}
export const DELETE = async (
req: AuthenticatedMedusaRequest<AdminDeletePromotionsPromotionRulesReq>,
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,
})
}

View File

@@ -0,0 +1,34 @@
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
import { RuleType } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { AdminPostPromotionsPromotionRulesBatchRemoveReq } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchRemoveReq>,
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,
})
}

View File

@@ -1,25 +1,19 @@
import {
addRulesToPromotionsWorkflow,
removeRulesFromPromotionsWorkflow,
} from "@medusajs/core-flows"
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { RuleType } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
} from "../../../../../../../types/routing"
import {
defaultAdminPromotionFields,
defaultAdminPromotionRelations,
} from "../../query-config"
import {
AdminDeletePromotionsPromotionRulesReq,
AdminPostPromotionsPromotionRulesReq,
} from "../../validators"
} from "../../../../query-config"
import { AdminPostPromotionsPromotionRulesBatchAddReq } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchAddReq>,
res: MedusaResponse
) => {
const id = req.params.id
@@ -27,10 +21,7 @@ export const POST = async (
const { errors } = await workflow.run({
input: {
rule_type: RuleType.TARGET_RULES,
data: {
id,
...req.validatedBody,
},
data: { id, ...req.validatedBody },
},
throwOnError: false,
})
@@ -50,33 +41,3 @@ export const POST = async (
res.status(200).json({ promotion })
}
export const DELETE = async (
req: AuthenticatedMedusaRequest<AdminDeletePromotionsPromotionRulesReq>,
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,
})
}

View File

@@ -0,0 +1,34 @@
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
import { RuleType } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { AdminPostPromotionsPromotionRulesBatchRemoveReq } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchRemoveReq>,
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,
})
}

View File

@@ -2,11 +2,11 @@ import * as QueryConfig from "./query-config"
import { transformBody, transformQuery } from "../../../api/middlewares"
import {
AdminDeletePromotionsPromotionRulesReq,
AdminGetPromotionsParams,
AdminGetPromotionsPromotionParams,
AdminPostPromotionsPromotionReq,
AdminPostPromotionsPromotionRulesReq,
AdminPostPromotionsPromotionRulesBatchAddReq,
AdminPostPromotionsPromotionRulesBatchRemoveReq,
AdminPostPromotionsReq,
} from "./validators"
@@ -50,32 +50,38 @@ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
},
{
method: ["POST"],
matcher: "/admin/promotions/:id/rules",
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
matcher: "/admin/promotions/:id/rules/batch/add",
middlewares: [transformBody(AdminPostPromotionsPromotionRulesBatchAddReq)],
},
{
method: ["POST"],
matcher: "/admin/promotions/:id/target-rules",
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
matcher: "/admin/promotions/:id/target-rules/batch/add",
middlewares: [transformBody(AdminPostPromotionsPromotionRulesBatchAddReq)],
},
{
method: ["POST"],
matcher: "/admin/promotions/:id/buy-rules",
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
matcher: "/admin/promotions/:id/buy-rules/batch/add",
middlewares: [transformBody(AdminPostPromotionsPromotionRulesBatchAddReq)],
},
{
method: ["DELETE"],
matcher: "/admin/promotions/:id/rules",
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
method: ["POST"],
matcher: "/admin/promotions/:id/rules/batch/remove",
middlewares: [
transformBody(AdminPostPromotionsPromotionRulesBatchRemoveReq),
],
},
{
method: ["DELETE"],
matcher: "/admin/promotions/:id/target-rules",
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
method: ["POST"],
matcher: "/admin/promotions/:id/target-rules/batch/remove",
middlewares: [
transformBody(AdminPostPromotionsPromotionRulesBatchRemoveReq),
],
},
{
method: ["DELETE"],
matcher: "/admin/promotions/:id/buy-rules",
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
method: ["POST"],
matcher: "/admin/promotions/:id/buy-rules/batch/remove",
middlewares: [
transformBody(AdminPostPromotionsPromotionRulesBatchRemoveReq),
],
},
]

View File

@@ -216,14 +216,14 @@ export class AdminPostPromotionsPromotionReq {
rules?: PromotionRule[]
}
export class AdminPostPromotionsPromotionRulesReq {
export class AdminPostPromotionsPromotionRulesBatchAddReq {
@IsArray()
@ValidateNested({ each: true })
@Type(() => PromotionRule)
rules: PromotionRule[]
}
export class AdminDeletePromotionsPromotionRulesReq {
export class AdminPostPromotionsPromotionRulesBatchRemoveReq {
@ArrayNotEmpty()
@IsString({ each: true })
rule_ids: string[]