chore(medusa): use batch/add and batch/remove endpoints for promotion rules (#6701)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore(medusa): use batch/add and batch/remove endpoints for promotion rules
|
||||||
@@ -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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/rules`,
|
`/admin/promotions/${standardPromotion.id}/rules/batch/add`,
|
||||||
{
|
{
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
@@ -72,7 +72,7 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/does-not-exist/rules`,
|
`/admin/promotions/does-not-exist/rules/batch/add`,
|
||||||
{
|
{
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
@@ -95,7 +95,7 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("should add rules to a promotion successfully", async () => {
|
it("should add rules to a promotion successfully", async () => {
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/rules`,
|
`/admin/promotions/${standardPromotion.id}/rules/batch/add`,
|
||||||
{
|
{
|
||||||
rules: [
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/target-rules`,
|
`/admin/promotions/${standardPromotion.id}/target-rules/batch/add`,
|
||||||
{
|
{
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
@@ -157,7 +157,7 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/does-not-exist/target-rules`,
|
`/admin/promotions/does-not-exist/target-rules/batch/add`,
|
||||||
{
|
{
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
@@ -180,7 +180,7 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("should add target rules to a promotion successfully", async () => {
|
it("should add target rules to a promotion successfully", async () => {
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/target-rules`,
|
`/admin/promotions/${standardPromotion.id}/target-rules/batch/add`,
|
||||||
{
|
{
|
||||||
rules: [
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/buy-rules`,
|
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/add`,
|
||||||
{
|
{
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
@@ -244,7 +244,7 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/does-not-exist/buy-rules`,
|
`/admin/promotions/does-not-exist/buy-rules/batch/add`,
|
||||||
{
|
{
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
@@ -268,7 +268,7 @@ medusaIntegrationTestRunner({
|
|||||||
it("should throw an error when trying to add buy rules to a standard promotion", async () => {
|
it("should throw an error when trying to add buy rules to a standard promotion", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.post(
|
.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/buy-rules`,
|
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/add`,
|
||||||
{
|
{
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
@@ -307,7 +307,7 @@ medusaIntegrationTestRunner({
|
|||||||
})
|
})
|
||||||
|
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
`/admin/promotions/${buyGetPromotion.id}/buy-rules`,
|
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch/add`,
|
||||||
{
|
{
|
||||||
rules: [
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.delete(`/admin/promotions/${standardPromotion.id}/rules`, {
|
.post(
|
||||||
...adminHeaders,
|
`/admin/promotions/${standardPromotion.id}/rules/batch/remove`,
|
||||||
data: {},
|
{},
|
||||||
})
|
adminHeaders
|
||||||
|
)
|
||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
|
|
||||||
expect(response.status).toEqual(400)
|
expect(response.status).toEqual(400)
|
||||||
@@ -362,10 +363,11 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.delete(`/admin/promotions/does-not-exist/rules`, {
|
.post(
|
||||||
...adminHeaders,
|
`/admin/promotions/does-not-exist/rules/batch/remove`,
|
||||||
data: { rule_ids: ["test-rule-id"] },
|
{ rule_ids: ["test-rule-id"] },
|
||||||
})
|
adminHeaders
|
||||||
|
)
|
||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
|
|
||||||
expect(response.status).toEqual(404)
|
expect(response.status).toEqual(404)
|
||||||
@@ -376,14 +378,11 @@ medusaIntegrationTestRunner({
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should remove rules from a promotion successfully", async () => {
|
it("should remove rules from a promotion successfully", async () => {
|
||||||
const response = await api.delete(
|
const response = await api.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/rules`,
|
`/admin/promotions/${standardPromotion.id}/rules/batch/remove`,
|
||||||
{
|
{ rule_ids: [standardPromotion.rules[0].id] },
|
||||||
...adminHeaders,
|
adminHeaders
|
||||||
data: { rule_ids: [standardPromotion.rules[0].id] },
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
expect(response.status).toEqual(200)
|
||||||
expect(response.data).toEqual({
|
expect(response.data).toEqual({
|
||||||
ids: [standardPromotion.rules[0].id],
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.delete(`/admin/promotions/${standardPromotion.id}/target-rules`, {
|
.post(
|
||||||
...adminHeaders,
|
`/admin/promotions/${standardPromotion.id}/target-rules/batch/remove`,
|
||||||
data: {},
|
{},
|
||||||
})
|
adminHeaders
|
||||||
|
)
|
||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
|
|
||||||
expect(response.status).toEqual(400)
|
expect(response.status).toEqual(400)
|
||||||
@@ -419,10 +419,11 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.delete(`/admin/promotions/does-not-exist/target-rules`, {
|
.post(
|
||||||
...adminHeaders,
|
`/admin/promotions/does-not-exist/target-rules/batch/remove`,
|
||||||
data: { rule_ids: ["test-rule-id"] },
|
{ rule_ids: ["test-rule-id"] },
|
||||||
})
|
adminHeaders
|
||||||
|
)
|
||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
|
|
||||||
expect(response.status).toEqual(404)
|
expect(response.status).toEqual(404)
|
||||||
@@ -434,12 +435,10 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("should remove target rules from a promotion successfully", async () => {
|
it("should remove target rules from a promotion successfully", async () => {
|
||||||
const ruleId = standardPromotion.application_method.target_rules[0].id
|
const ruleId = standardPromotion.application_method.target_rules[0].id
|
||||||
const response = await api.delete(
|
const response = await api.post(
|
||||||
`/admin/promotions/${standardPromotion.id}/target-rules`,
|
`/admin/promotions/${standardPromotion.id}/target-rules/batch/remove`,
|
||||||
{
|
{ rule_ids: [ruleId] },
|
||||||
...adminHeaders,
|
adminHeaders
|
||||||
data: { rule_ids: [ruleId] },
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
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 () => {
|
it("should throw error when required params are missing", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.delete(`/admin/promotions/${standardPromotion.id}/buy-rules`, {
|
.post(
|
||||||
...adminHeaders,
|
`/admin/promotions/${standardPromotion.id}/buy-rules/batch/remove`,
|
||||||
data: {},
|
{},
|
||||||
})
|
adminHeaders
|
||||||
|
)
|
||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
|
|
||||||
expect(response.status).toEqual(400)
|
expect(response.status).toEqual(400)
|
||||||
@@ -477,10 +477,11 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("should throw error when promotion does not exist", async () => {
|
it("should throw error when promotion does not exist", async () => {
|
||||||
const { response } = await api
|
const { response } = await api
|
||||||
.delete(`/admin/promotions/does-not-exist/buy-rules`, {
|
.post(
|
||||||
...adminHeaders,
|
`/admin/promotions/does-not-exist/buy-rules/batch/remove`,
|
||||||
data: { rule_ids: ["test-rule-id"] },
|
{ rule_ids: ["test-rule-id"] },
|
||||||
})
|
adminHeaders
|
||||||
|
)
|
||||||
.catch((e) => e)
|
.catch((e) => e)
|
||||||
|
|
||||||
expect(response.status).toEqual(404)
|
expect(response.status).toEqual(404)
|
||||||
@@ -508,12 +509,10 @@ medusaIntegrationTestRunner({
|
|||||||
})
|
})
|
||||||
|
|
||||||
const ruleId = buyGetPromotion!.application_method!.buy_rules![0].id
|
const ruleId = buyGetPromotion!.application_method!.buy_rules![0].id
|
||||||
const response = await api.delete(
|
const response = await api.post(
|
||||||
`/admin/promotions/${buyGetPromotion.id}/buy-rules`,
|
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch/remove`,
|
||||||
{
|
{ rule_ids: [ruleId] },
|
||||||
...adminHeaders,
|
adminHeaders
|
||||||
data: { rule_ids: [ruleId] },
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
expect(response.status).toEqual(200)
|
||||||
|
|||||||
+6
-45
@@ -1,25 +1,19 @@
|
|||||||
import {
|
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
|
||||||
addRulesToPromotionsWorkflow,
|
|
||||||
removeRulesFromPromotionsWorkflow,
|
|
||||||
} from "@medusajs/core-flows"
|
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
import { IPromotionModuleService } from "@medusajs/types"
|
import { IPromotionModuleService } from "@medusajs/types"
|
||||||
import { RuleType } from "@medusajs/utils"
|
import { RuleType } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../types/routing"
|
} from "../../../../../../../types/routing"
|
||||||
import {
|
import {
|
||||||
defaultAdminPromotionFields,
|
defaultAdminPromotionFields,
|
||||||
defaultAdminPromotionRelations,
|
defaultAdminPromotionRelations,
|
||||||
} from "../../query-config"
|
} from "../../../../query-config"
|
||||||
import {
|
import { AdminPostPromotionsPromotionRulesBatchAddReq } from "../../../../validators"
|
||||||
AdminDeletePromotionsPromotionRulesReq,
|
|
||||||
AdminPostPromotionsPromotionRulesReq,
|
|
||||||
} from "../../validators"
|
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
|
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchAddReq>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const id = req.params.id
|
const id = req.params.id
|
||||||
@@ -27,10 +21,7 @@ export const POST = async (
|
|||||||
const { errors } = await workflow.run({
|
const { errors } = await workflow.run({
|
||||||
input: {
|
input: {
|
||||||
rule_type: RuleType.BUY_RULES,
|
rule_type: RuleType.BUY_RULES,
|
||||||
data: {
|
data: { id, ...req.validatedBody },
|
||||||
id,
|
|
||||||
...req.validatedBody,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
throwOnError: false,
|
throwOnError: false,
|
||||||
})
|
})
|
||||||
@@ -50,33 +41,3 @@ export const POST = async (
|
|||||||
|
|
||||||
res.status(200).json({ promotion })
|
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -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,
|
||||||
|
})
|
||||||
|
}
|
||||||
+6
-45
@@ -1,25 +1,19 @@
|
|||||||
import {
|
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
|
||||||
addRulesToPromotionsWorkflow,
|
|
||||||
removeRulesFromPromotionsWorkflow,
|
|
||||||
} from "@medusajs/core-flows"
|
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
import { IPromotionModuleService } from "@medusajs/types"
|
import { IPromotionModuleService } from "@medusajs/types"
|
||||||
import { RuleType } from "@medusajs/utils"
|
import { RuleType } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../types/routing"
|
} from "../../../../../../../types/routing"
|
||||||
import {
|
import {
|
||||||
defaultAdminPromotionFields,
|
defaultAdminPromotionFields,
|
||||||
defaultAdminPromotionRelations,
|
defaultAdminPromotionRelations,
|
||||||
} from "../../query-config"
|
} from "../../../../query-config"
|
||||||
import {
|
import { AdminPostPromotionsPromotionRulesBatchAddReq } from "../../../../validators"
|
||||||
AdminDeletePromotionsPromotionRulesReq,
|
|
||||||
AdminPostPromotionsPromotionRulesReq,
|
|
||||||
} from "../../validators"
|
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
|
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchAddReq>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const id = req.params.id
|
const id = req.params.id
|
||||||
@@ -28,10 +22,7 @@ export const POST = async (
|
|||||||
const { errors } = await workflow.run({
|
const { errors } = await workflow.run({
|
||||||
input: {
|
input: {
|
||||||
rule_type: RuleType.RULES,
|
rule_type: RuleType.RULES,
|
||||||
data: {
|
data: { id, ...req.validatedBody },
|
||||||
id,
|
|
||||||
...req.validatedBody,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
throwOnError: false,
|
throwOnError: false,
|
||||||
})
|
})
|
||||||
@@ -51,33 +42,3 @@ export const POST = async (
|
|||||||
|
|
||||||
res.status(200).json({ promotion })
|
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -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,
|
||||||
|
})
|
||||||
|
}
|
||||||
+6
-45
@@ -1,25 +1,19 @@
|
|||||||
import {
|
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
|
||||||
addRulesToPromotionsWorkflow,
|
|
||||||
removeRulesFromPromotionsWorkflow,
|
|
||||||
} from "@medusajs/core-flows"
|
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
import { IPromotionModuleService } from "@medusajs/types"
|
import { IPromotionModuleService } from "@medusajs/types"
|
||||||
import { RuleType } from "@medusajs/utils"
|
import { RuleType } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../types/routing"
|
} from "../../../../../../../types/routing"
|
||||||
import {
|
import {
|
||||||
defaultAdminPromotionFields,
|
defaultAdminPromotionFields,
|
||||||
defaultAdminPromotionRelations,
|
defaultAdminPromotionRelations,
|
||||||
} from "../../query-config"
|
} from "../../../../query-config"
|
||||||
import {
|
import { AdminPostPromotionsPromotionRulesBatchAddReq } from "../../../../validators"
|
||||||
AdminDeletePromotionsPromotionRulesReq,
|
|
||||||
AdminPostPromotionsPromotionRulesReq,
|
|
||||||
} from "../../validators"
|
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
|
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchAddReq>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const id = req.params.id
|
const id = req.params.id
|
||||||
@@ -27,10 +21,7 @@ export const POST = async (
|
|||||||
const { errors } = await workflow.run({
|
const { errors } = await workflow.run({
|
||||||
input: {
|
input: {
|
||||||
rule_type: RuleType.TARGET_RULES,
|
rule_type: RuleType.TARGET_RULES,
|
||||||
data: {
|
data: { id, ...req.validatedBody },
|
||||||
id,
|
|
||||||
...req.validatedBody,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
throwOnError: false,
|
throwOnError: false,
|
||||||
})
|
})
|
||||||
@@ -50,33 +41,3 @@ export const POST = async (
|
|||||||
|
|
||||||
res.status(200).json({ promotion })
|
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -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,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -2,11 +2,11 @@ import * as QueryConfig from "./query-config"
|
|||||||
|
|
||||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||||
import {
|
import {
|
||||||
AdminDeletePromotionsPromotionRulesReq,
|
|
||||||
AdminGetPromotionsParams,
|
AdminGetPromotionsParams,
|
||||||
AdminGetPromotionsPromotionParams,
|
AdminGetPromotionsPromotionParams,
|
||||||
AdminPostPromotionsPromotionReq,
|
AdminPostPromotionsPromotionReq,
|
||||||
AdminPostPromotionsPromotionRulesReq,
|
AdminPostPromotionsPromotionRulesBatchAddReq,
|
||||||
|
AdminPostPromotionsPromotionRulesBatchRemoveReq,
|
||||||
AdminPostPromotionsReq,
|
AdminPostPromotionsReq,
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
|
|
||||||
@@ -50,32 +50,38 @@ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/promotions/:id/rules",
|
matcher: "/admin/promotions/:id/rules/batch/add",
|
||||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
|
middlewares: [transformBody(AdminPostPromotionsPromotionRulesBatchAddReq)],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/promotions/:id/target-rules",
|
matcher: "/admin/promotions/:id/target-rules/batch/add",
|
||||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
|
middlewares: [transformBody(AdminPostPromotionsPromotionRulesBatchAddReq)],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/promotions/:id/buy-rules",
|
matcher: "/admin/promotions/:id/buy-rules/batch/add",
|
||||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
|
middlewares: [transformBody(AdminPostPromotionsPromotionRulesBatchAddReq)],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["DELETE"],
|
method: ["POST"],
|
||||||
matcher: "/admin/promotions/:id/rules",
|
matcher: "/admin/promotions/:id/rules/batch/remove",
|
||||||
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
|
middlewares: [
|
||||||
|
transformBody(AdminPostPromotionsPromotionRulesBatchRemoveReq),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["DELETE"],
|
method: ["POST"],
|
||||||
matcher: "/admin/promotions/:id/target-rules",
|
matcher: "/admin/promotions/:id/target-rules/batch/remove",
|
||||||
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
|
middlewares: [
|
||||||
|
transformBody(AdminPostPromotionsPromotionRulesBatchRemoveReq),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["DELETE"],
|
method: ["POST"],
|
||||||
matcher: "/admin/promotions/:id/buy-rules",
|
matcher: "/admin/promotions/:id/buy-rules/batch/remove",
|
||||||
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
|
middlewares: [
|
||||||
|
transformBody(AdminPostPromotionsPromotionRulesBatchRemoveReq),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -216,14 +216,14 @@ export class AdminPostPromotionsPromotionReq {
|
|||||||
rules?: PromotionRule[]
|
rules?: PromotionRule[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AdminPostPromotionsPromotionRulesReq {
|
export class AdminPostPromotionsPromotionRulesBatchAddReq {
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => PromotionRule)
|
@Type(() => PromotionRule)
|
||||||
rules: PromotionRule[]
|
rules: PromotionRule[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AdminDeletePromotionsPromotionRulesReq {
|
export class AdminPostPromotionsPromotionRulesBatchRemoveReq {
|
||||||
@ArrayNotEmpty()
|
@ArrayNotEmpty()
|
||||||
@IsString({ each: true })
|
@IsString({ each: true })
|
||||||
rule_ids: string[]
|
rule_ids: string[]
|
||||||
|
|||||||
Reference in New Issue
Block a user