feat(core-flows,medusa,types,utils): add rules to promotion endpoints + workflow (#6692)
* feat(core-flows,medusa,types,utils): add rules to promotion endpoints + workflow * chore: fix specs * chore: move input type to types package
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { PromotionType } from "@medusajs/utils"
|
||||
import { createAdminUser } from "../../../../helpers/create-admin-user"
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { createAdminUser } from "../../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
@@ -56,15 +56,13 @@ medusaIntegrationTestRunner({
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
rules: [],
|
||||
application_method: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: 100,
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
allocation: null,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
}),
|
||||
}),
|
||||
])
|
||||
@@ -84,7 +82,7 @@ medusaIntegrationTestRunner({
|
||||
])
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/promotions?fields=code,created_at,application_method.id`,
|
||||
`/admin/promotions?fields=code,created_at,application_method.id&expand=application_method`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,347 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { PromotionType } from "@medusajs/utils"
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { createAdminUser } from "../../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
env,
|
||||
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||
describe("Admin: Promotion Rules API", () => {
|
||||
let appContainer
|
||||
let standardPromotion
|
||||
let promotionModule: IPromotionModuleService
|
||||
const promotionRule = {
|
||||
operator: "eq",
|
||||
attribute: "old_attr",
|
||||
values: ["old value"],
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
promotionModule = appContainer.resolve(ModuleRegistrationName.PROMOTION)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
|
||||
standardPromotion = await promotionModule.create({
|
||||
code: "TEST_ACROSS",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
allocation: "across",
|
||||
target_type: "items",
|
||||
value: 100,
|
||||
target_rules: [promotionRule],
|
||||
},
|
||||
rules: [promotionRule],
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/promotions/:id/rules", () => {
|
||||
it("should throw error when required params are missing", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/${standardPromotion.id}/rules`,
|
||||
{
|
||||
rules: [
|
||||
{
|
||||
operator: "eq",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data).toEqual({
|
||||
type: "invalid_data",
|
||||
message:
|
||||
"attribute must be a string, attribute should not be empty",
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw error when promotion does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/does-not-exist/rules`,
|
||||
{
|
||||
rules: [
|
||||
{
|
||||
attribute: "new_attr",
|
||||
operator: "eq",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data).toEqual({
|
||||
type: "not_found",
|
||||
message: "Promotion with id: does-not-exist was not found",
|
||||
})
|
||||
})
|
||||
|
||||
it("should add rules to a promotion successfully", async () => {
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${standardPromotion.id}/rules`,
|
||||
{
|
||||
rules: [
|
||||
{
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: standardPromotion.id,
|
||||
rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "old_attr",
|
||||
values: [expect.objectContaining({ value: "old value" })],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: [expect.objectContaining({ value: "new value" })],
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/promotions/:id/target-rules", () => {
|
||||
it("should throw error when required params are missing", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/${standardPromotion.id}/target-rules`,
|
||||
{
|
||||
rules: [
|
||||
{
|
||||
operator: "eq",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data).toEqual({
|
||||
type: "invalid_data",
|
||||
message:
|
||||
"attribute must be a string, attribute should not be empty",
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw error when promotion does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/does-not-exist/target-rules`,
|
||||
{
|
||||
rules: [
|
||||
{
|
||||
attribute: "new_attr",
|
||||
operator: "eq",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data).toEqual({
|
||||
type: "not_found",
|
||||
message: "Promotion with id: does-not-exist was not found",
|
||||
})
|
||||
})
|
||||
|
||||
it("should add target rules to a promotion successfully", async () => {
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${standardPromotion.id}/target-rules`,
|
||||
{
|
||||
rules: [
|
||||
{
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: standardPromotion.id,
|
||||
application_method: expect.objectContaining({
|
||||
target_rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "old_attr",
|
||||
values: [expect.objectContaining({ value: "old value" })],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: [expect.objectContaining({ value: "new value" })],
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/promotions/:id/buy-rules", () => {
|
||||
it("should throw error when required params are missing", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/${standardPromotion.id}/buy-rules`,
|
||||
{
|
||||
rules: [
|
||||
{
|
||||
operator: "eq",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data).toEqual({
|
||||
type: "invalid_data",
|
||||
message:
|
||||
"attribute must be a string, attribute should not be empty",
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw error when promotion does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/does-not-exist/buy-rules`,
|
||||
{
|
||||
rules: [
|
||||
{
|
||||
attribute: "new_attr",
|
||||
operator: "eq",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data).toEqual({
|
||||
type: "not_found",
|
||||
message: "Promotion with id: does-not-exist was not found",
|
||||
})
|
||||
})
|
||||
|
||||
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`,
|
||||
{
|
||||
rules: [
|
||||
{
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data).toEqual({
|
||||
type: "invalid_data",
|
||||
message: "Can't add buy rules to a standard promotion",
|
||||
})
|
||||
})
|
||||
|
||||
it("should add buy rules to a buyget promotion successfully", async () => {
|
||||
const buyGetPromotion = await promotionModule.create({
|
||||
code: "TEST_BUYGET",
|
||||
type: PromotionType.BUYGET,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "items",
|
||||
allocation: "across",
|
||||
value: 100,
|
||||
apply_to_quantity: 1,
|
||||
buy_rules_min_quantity: 1,
|
||||
buy_rules: [promotionRule],
|
||||
target_rules: [promotionRule],
|
||||
},
|
||||
rules: [promotionRule],
|
||||
})
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${buyGetPromotion.id}/buy-rules`,
|
||||
{
|
||||
rules: [
|
||||
{
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: buyGetPromotion.id,
|
||||
application_method: expect.objectContaining({
|
||||
buy_rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "old_attr",
|
||||
values: [expect.objectContaining({ value: "old value" })],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: [expect.objectContaining({ value: "new value" })],
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -1,8 +1,8 @@
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { PromotionType } from "@medusajs/utils"
|
||||
import { createAdminUser } from "../../../../helpers/create-admin-user"
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { createAdminUser } from "../../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
@@ -73,11 +73,7 @@ medusaIntegrationTestRunner({
|
||||
value: 100,
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
max_quantity: 0,
|
||||
allocation: null,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
}),
|
||||
})
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user