chore: remove promotions in campaign validators + move tests to http (#8965)
This commit is contained in:
@@ -1,627 +0,0 @@
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import {
|
||||
CampaignBudgetType,
|
||||
ModuleRegistrationName,
|
||||
PromotionType,
|
||||
} from "@medusajs/utils"
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { createAdminUser } from "../../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
export const campaignData = {
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: new Date("01/01/2023").toISOString(),
|
||||
ends_at: new Date("01/01/2024").toISOString(),
|
||||
budget: {
|
||||
type: CampaignBudgetType.SPEND,
|
||||
limit: 1000,
|
||||
currency_code: "USD",
|
||||
},
|
||||
}
|
||||
|
||||
export const campaignsData = [
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: new Date("01/01/2023"),
|
||||
ends_at: new Date("01/01/2024"),
|
||||
budget: {
|
||||
type: CampaignBudgetType.SPEND,
|
||||
limit: 1000,
|
||||
currency_code: "USD",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "campaign-id-2",
|
||||
name: "campaign 2",
|
||||
description: "test description",
|
||||
campaign_identifier: "test-2",
|
||||
starts_at: new Date("01/01/2023"),
|
||||
ends_at: new Date("01/01/2024"),
|
||||
budget: {
|
||||
type: CampaignBudgetType.USAGE,
|
||||
limit: 1000,
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
const promotionData = {
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
is_automatic: true,
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
currency_code: "USD",
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
target_rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
env,
|
||||
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||
describe("Admin Campaigns API", () => {
|
||||
let appContainer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
})
|
||||
|
||||
const generatePromotionData = () => {
|
||||
const code = Math.random().toString(36).substring(7)
|
||||
|
||||
return {
|
||||
code,
|
||||
type: PromotionType.STANDARD,
|
||||
is_automatic: true,
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
target_rules: [],
|
||||
currency_code: "USD",
|
||||
},
|
||||
rules: [],
|
||||
}
|
||||
}
|
||||
|
||||
describe("GET /admin/campaigns", () => {
|
||||
beforeEach(async () => {
|
||||
await promotionModuleService.createCampaigns(campaignsData)
|
||||
})
|
||||
|
||||
it("should get all campaigns and its count", async () => {
|
||||
const response = await api.get(`/admin/campaigns`, adminHeaders)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(2)
|
||||
expect(response.data.campaigns).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: expect.any(String),
|
||||
ends_at: expect.any(String),
|
||||
budget: {
|
||||
id: expect.any(String),
|
||||
type: "spend",
|
||||
currency_code: "USD",
|
||||
limit: 1000,
|
||||
used: 0,
|
||||
raw_limit: {
|
||||
precision: 20,
|
||||
value: "1000",
|
||||
},
|
||||
raw_used: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
},
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "campaign 2",
|
||||
description: "test description",
|
||||
campaign_identifier: "test-2",
|
||||
starts_at: expect.any(String),
|
||||
ends_at: expect.any(String),
|
||||
budget: {
|
||||
id: expect.any(String),
|
||||
type: "usage",
|
||||
limit: 1000,
|
||||
used: 0,
|
||||
currency_code: null,
|
||||
raw_limit: {
|
||||
precision: 20,
|
||||
value: "1000",
|
||||
},
|
||||
raw_used: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
},
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("should support search on campaigns", async () => {
|
||||
const response = await api.get(
|
||||
`/admin/campaigns?q=ign%202`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.campaigns).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "campaign 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should get all campaigns and its count filtered", async () => {
|
||||
const response = await api.get(
|
||||
`/admin/campaigns?fields=name,created_at,budget.id`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(2)
|
||||
expect(response.data.campaigns).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
id: expect.any(String),
|
||||
name: "campaign 1",
|
||||
created_at: expect.any(String),
|
||||
budget: {
|
||||
id: expect.any(String),
|
||||
},
|
||||
},
|
||||
{
|
||||
id: expect.any(String),
|
||||
name: "campaign 2",
|
||||
created_at: expect.any(String),
|
||||
budget: {
|
||||
id: expect.any(String),
|
||||
},
|
||||
},
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/campaigns/:id", () => {
|
||||
it("should throw an error if id does not exist", async () => {
|
||||
const { response } = await api
|
||||
.get(`/admin/campaigns/does-not-exist`, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data.message).toEqual(
|
||||
"Campaign with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should get the requested campaign", async () => {
|
||||
const createdCampaign = await promotionModuleService.createCampaigns(
|
||||
campaignData
|
||||
)
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/campaigns/${createdCampaign.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.campaign).toEqual({
|
||||
id: expect.any(String),
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: expect.any(String),
|
||||
ends_at: expect.any(String),
|
||||
budget: {
|
||||
id: expect.any(String),
|
||||
type: "spend",
|
||||
limit: 1000,
|
||||
currency_code: "USD",
|
||||
raw_limit: {
|
||||
precision: 20,
|
||||
value: "1000",
|
||||
},
|
||||
raw_used: {
|
||||
precision: 20,
|
||||
value: "0",
|
||||
},
|
||||
used: 0,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
},
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
})
|
||||
})
|
||||
|
||||
it("should get the requested campaign with filtered fields and relations", async () => {
|
||||
const createdCampaign = await promotionModuleService.createCampaigns(
|
||||
campaignData
|
||||
)
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/campaigns/${createdCampaign.id}?fields=name`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.campaign).toEqual({
|
||||
id: expect.any(String),
|
||||
name: "campaign 1",
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/campaigns", () => {
|
||||
it("should throw an error if required params are not passed", async () => {
|
||||
const { response } = await api
|
||||
.post(`/admin/campaigns`, {}, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
// expect(response.data.message).toEqual(
|
||||
// "name must be a string, name should not be empty"
|
||||
// )
|
||||
})
|
||||
|
||||
it("should create a campaign successfully", async () => {
|
||||
const response = await api.post(
|
||||
`/admin/campaigns?fields=*promotions`,
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024").toISOString(),
|
||||
ends_at: new Date("01/01/2029").toISOString(),
|
||||
budget: {
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: expect.any(String),
|
||||
ends_at: expect.any(String),
|
||||
budget: expect.objectContaining({
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create 3 campaigns in parallel and have the context passed as argument when calling createCampaigns with different transactionId", async () => {
|
||||
await api.post(
|
||||
`/admin/promotions`,
|
||||
{ ...promotionData, code: "PARALLEL" },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const spyCreateCampaigns = jest.spyOn(
|
||||
promotionModuleService.constructor.prototype,
|
||||
"createCampaigns"
|
||||
)
|
||||
|
||||
const a = async () => {
|
||||
return await api.post(
|
||||
`/admin/campaigns`,
|
||||
{
|
||||
name: "camp_1",
|
||||
campaign_identifier: "camp_1",
|
||||
starts_at: new Date("01/01/2024").toISOString(),
|
||||
ends_at: new Date("01/02/2024").toISOString(),
|
||||
budget: {
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
}
|
||||
|
||||
const b = async () => {
|
||||
return await api.post(
|
||||
`/admin/campaigns`,
|
||||
{
|
||||
name: "camp_2",
|
||||
campaign_identifier: "camp_2",
|
||||
starts_at: new Date("01/02/2024").toISOString(),
|
||||
ends_at: new Date("01/03/2029").toISOString(),
|
||||
budget: {
|
||||
limit: 500,
|
||||
type: "usage",
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
}
|
||||
|
||||
const c = async () => {
|
||||
return await api.post(
|
||||
`/admin/campaigns`,
|
||||
{
|
||||
name: "camp_3",
|
||||
campaign_identifier: "camp_3",
|
||||
starts_at: new Date("01/03/2024").toISOString(),
|
||||
ends_at: new Date("01/04/2029").toISOString(),
|
||||
budget: {
|
||||
limit: 250,
|
||||
type: "usage",
|
||||
},
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
...adminHeaders.headers,
|
||||
"x-request-id": "my-custom-request-id",
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
await Promise.all([a(), b(), c()])
|
||||
|
||||
expect(spyCreateCampaigns).toHaveBeenCalledTimes(3)
|
||||
expect(spyCreateCampaigns.mock.calls[0][1].__type).toBe(
|
||||
"MedusaContext"
|
||||
)
|
||||
|
||||
const distinctTransactionId = [
|
||||
...new Set(
|
||||
spyCreateCampaigns.mock.calls.map((call) => call[1].transactionId)
|
||||
),
|
||||
]
|
||||
expect(distinctTransactionId).toHaveLength(3)
|
||||
|
||||
const distinctRequestId = [
|
||||
...new Set(
|
||||
spyCreateCampaigns.mock.calls.map((call) => call[1].requestId)
|
||||
),
|
||||
]
|
||||
|
||||
expect(distinctRequestId).toHaveLength(3)
|
||||
expect(distinctRequestId).toContain("my-custom-request-id")
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/campaigns/:id", () => {
|
||||
it("should throw an error if id does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(`/admin/campaigns/does-not-exist`, {}, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data.message).toEqual(
|
||||
`Campaign with id "does-not-exist" not found`
|
||||
)
|
||||
})
|
||||
|
||||
it("should update a campaign successfully", async () => {
|
||||
const createdPromotion = (
|
||||
await api.post(`/admin/promotions`, promotionData, adminHeaders)
|
||||
).data.promotion
|
||||
|
||||
const createdCampaign = (
|
||||
await api.post(
|
||||
`/admin/campaigns`,
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024").toISOString(),
|
||||
ends_at: new Date("01/01/2029").toISOString(),
|
||||
budget: {
|
||||
limit: 1000,
|
||||
type: "usage",
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.campaign
|
||||
|
||||
await promotionModuleService.addPromotionsToCampaign({
|
||||
id: createdCampaign.id,
|
||||
promotion_ids: [createdPromotion.id],
|
||||
})
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/campaigns/${createdCampaign.id}?fields=*promotions`,
|
||||
{
|
||||
name: "test-2",
|
||||
campaign_identifier: "test-2",
|
||||
budget: {
|
||||
limit: 2000,
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "test-2",
|
||||
campaign_identifier: "test-2",
|
||||
budget: expect.objectContaining({
|
||||
limit: 2000,
|
||||
type: "usage",
|
||||
}),
|
||||
promotions: [
|
||||
expect.objectContaining({
|
||||
id: createdPromotion.id,
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("DELETE /admin/campaigns/:id", () => {
|
||||
it("should delete campaign successfully", async () => {
|
||||
const [createdCampaign] =
|
||||
await promotionModuleService.createCampaigns([
|
||||
{
|
||||
name: "test",
|
||||
campaign_identifier: "test",
|
||||
starts_at: new Date("01/01/2024"),
|
||||
ends_at: new Date("01/01/2025"),
|
||||
},
|
||||
])
|
||||
|
||||
const deleteRes = await api.delete(
|
||||
`/admin/campaigns/${createdCampaign.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(deleteRes.status).toEqual(200)
|
||||
|
||||
const campaigns = await promotionModuleService.listCampaigns({
|
||||
id: [createdCampaign.id],
|
||||
})
|
||||
|
||||
expect(campaigns.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/campaigns/:id/promotions", () => {
|
||||
it("should add or remove promotions from campaign", async () => {
|
||||
const campaign = (
|
||||
await api.post(`/admin/campaigns`, campaignData, adminHeaders)
|
||||
).data.campaign
|
||||
|
||||
const promotion1 = (
|
||||
await api.post(
|
||||
`/admin/promotions`,
|
||||
generatePromotionData(),
|
||||
adminHeaders
|
||||
)
|
||||
).data.promotion
|
||||
|
||||
const promotion2 = (
|
||||
await api.post(
|
||||
`/admin/promotions`,
|
||||
generatePromotionData(),
|
||||
adminHeaders
|
||||
)
|
||||
).data.promotion
|
||||
|
||||
let response = await api.post(
|
||||
`/admin/campaigns/${campaign.id}/promotions`,
|
||||
{ add: [promotion1.id, promotion2.id] },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.campaign).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
})
|
||||
)
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions?campaign_id=${campaign.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotions).toHaveLength(2)
|
||||
expect(response.data.promotions).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: promotion1.id,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: promotion2.id,
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
await api.post(
|
||||
`/admin/campaigns/${campaign.id}/promotions`,
|
||||
{ remove: [promotion1.id] },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions?campaign_id=${campaign.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotions).toHaveLength(1)
|
||||
expect(response.data.promotions).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: promotion2.id,
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -1,321 +0,0 @@
|
||||
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("POST /admin/promotions", () => {
|
||||
let appContainer
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
})
|
||||
|
||||
it("should throw an error if required params are not passed", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions`,
|
||||
{
|
||||
type: PromotionType.STANDARD,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
// expect(response.data.message).toEqual(
|
||||
// "code must be a string, code should not be empty, application_method should not be empty"
|
||||
// )
|
||||
})
|
||||
|
||||
it("should create a standard promotion successfully", async () => {
|
||||
const response = await api.post(
|
||||
`/admin/promotions`,
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
is_automatic: true,
|
||||
campaign: {
|
||||
name: "test",
|
||||
campaign_identifier: "test-1",
|
||||
budget: {
|
||||
type: "usage",
|
||||
limit: 100,
|
||||
},
|
||||
},
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
currency_code: "USD",
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
target_rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
code: "TEST",
|
||||
type: "standard",
|
||||
is_automatic: true,
|
||||
campaign: expect.objectContaining({
|
||||
name: "test",
|
||||
campaign_identifier: "test-1",
|
||||
budget: expect.objectContaining({
|
||||
currency_code: null,
|
||||
type: "usage",
|
||||
limit: 100,
|
||||
}),
|
||||
}),
|
||||
application_method: expect.objectContaining({
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
type: "fixed",
|
||||
target_type: "items",
|
||||
allocation: "each",
|
||||
target_rules: [
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "test.test",
|
||||
values: expect.arrayContaining([
|
||||
expect.objectContaining({ value: "test1" }),
|
||||
expect.objectContaining({ value: "test2" }),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
rules: [
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "test.test",
|
||||
values: expect.arrayContaining([
|
||||
expect.objectContaining({ value: "test1" }),
|
||||
expect.objectContaining({ value: "test2" }),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error if buy_rules params are not passed", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions`,
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.BUYGET,
|
||||
is_automatic: true,
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
currency_code: "USD",
|
||||
target_rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
// expect(response.data.message).toEqual(
|
||||
// "Buy rules are required for buyget promotion type"
|
||||
// )
|
||||
})
|
||||
|
||||
it("should throw an error if buy_rules params are not passed", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions`,
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.BUYGET,
|
||||
is_automatic: true,
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
currency_code: "USD",
|
||||
buy_rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
// expect(response.data.message).toEqual(
|
||||
// "Target rules are required for buyget promotion type"
|
||||
// )
|
||||
})
|
||||
|
||||
it("should create a buyget promotion successfully", async () => {
|
||||
const response = await api.post(
|
||||
`/admin/promotions`,
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.BUYGET,
|
||||
is_automatic: true,
|
||||
campaign: {
|
||||
name: "test",
|
||||
campaign_identifier: "test-1",
|
||||
budget: {
|
||||
type: "usage",
|
||||
limit: 100,
|
||||
},
|
||||
},
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
apply_to_quantity: 1,
|
||||
buy_rules_min_quantity: 1,
|
||||
currency_code: "USD",
|
||||
target_rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
buy_rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
rules: [
|
||||
{
|
||||
attribute: "test.test",
|
||||
operator: "eq",
|
||||
values: ["test1", "test2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
code: "TEST",
|
||||
type: "buyget",
|
||||
is_automatic: true,
|
||||
campaign: expect.objectContaining({
|
||||
name: "test",
|
||||
campaign_identifier: "test-1",
|
||||
budget: expect.objectContaining({
|
||||
type: "usage",
|
||||
limit: 100,
|
||||
}),
|
||||
}),
|
||||
application_method: expect.objectContaining({
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
type: "fixed",
|
||||
target_type: "items",
|
||||
allocation: "each",
|
||||
apply_to_quantity: 1,
|
||||
buy_rules_min_quantity: 1,
|
||||
target_rules: [
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "test.test",
|
||||
values: expect.arrayContaining([
|
||||
expect.objectContaining({ value: "test1" }),
|
||||
expect.objectContaining({ value: "test2" }),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
buy_rules: [
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "test.test",
|
||||
values: expect.arrayContaining([
|
||||
expect.objectContaining({ value: "test1" }),
|
||||
expect.objectContaining({ value: "test2" }),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
rules: [
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "test.test",
|
||||
values: expect.arrayContaining([
|
||||
expect.objectContaining({ value: "test1" }),
|
||||
expect.objectContaining({ value: "test2" }),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -1,58 +0,0 @@
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } 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("DELETE /admin/promotions/:id", () => {
|
||||
let appContainer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
})
|
||||
|
||||
it("should delete promotion successfully", async () => {
|
||||
const createdPromotion = await promotionModuleService.createPromotions({
|
||||
code: "TEST",
|
||||
type: "standard",
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: "100",
|
||||
currency_code: "USD",
|
||||
},
|
||||
})
|
||||
|
||||
const deleteRes = await api.delete(
|
||||
`/admin/promotions/${createdPromotion.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(deleteRes.status).toEqual(200)
|
||||
|
||||
const promotions = await promotionModuleService.listPromotions({
|
||||
id: [createdPromotion.id],
|
||||
})
|
||||
|
||||
expect(promotions.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -1,138 +0,0 @@
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName, 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("GET /admin/promotions", () => {
|
||||
let appContainer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
})
|
||||
|
||||
it("should get all promotions and its count", async () => {
|
||||
await promotionModuleService.createPromotions([
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: 100,
|
||||
currency_code: "USD",
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const response = await api.get(`/admin/promotions`, adminHeaders)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
expect(response.data.promotions).toEqual([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
code: "TEST",
|
||||
is_automatic: false,
|
||||
type: "standard",
|
||||
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,
|
||||
}),
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should support search of promotions", async () => {
|
||||
await promotionModuleService.createPromotions([
|
||||
{
|
||||
code: "first",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: 100,
|
||||
currency_code: "USD",
|
||||
},
|
||||
},
|
||||
{
|
||||
code: "second",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: 100,
|
||||
currency_code: "USD",
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const response = await api.get(`/admin/promotions?q=fir`, adminHeaders)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotions).toEqual([
|
||||
expect.objectContaining({
|
||||
code: "first",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should get all promotions and its count filtered", async () => {
|
||||
await promotionModuleService.createPromotions([
|
||||
{
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: 100,
|
||||
currency_code: "USD",
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/promotions?fields=code,created_at,application_method.id`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
expect(response.data.promotions).toEqual([
|
||||
{
|
||||
id: expect.any(String),
|
||||
code: "TEST",
|
||||
created_at: expect.any(String),
|
||||
application_method: {
|
||||
id: expect.any(String),
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -1,915 +0,0 @@
|
||||
import {
|
||||
ICustomerModuleService,
|
||||
IProductModuleService,
|
||||
IPromotionModuleService,
|
||||
IRegionModuleService,
|
||||
ISalesChannelModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { ModuleRegistrationName, 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.skip("Admin: Promotion Rules API", () => {
|
||||
let appContainer
|
||||
let standardPromotion
|
||||
let promotionModule: IPromotionModuleService
|
||||
let regionService: IRegionModuleService
|
||||
let productService: IProductModuleService
|
||||
let customerService: ICustomerModuleService
|
||||
let salesChannelService: ISalesChannelModuleService
|
||||
|
||||
const promotionRule = {
|
||||
operator: "eq",
|
||||
attribute: "old_attr",
|
||||
values: ["old value"],
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
promotionModule = appContainer.resolve(ModuleRegistrationName.PROMOTION)
|
||||
regionService = appContainer.resolve(ModuleRegistrationName.REGION)
|
||||
productService = appContainer.resolve(ModuleRegistrationName.PRODUCT)
|
||||
customerService = appContainer.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
salesChannelService = appContainer.resolve(
|
||||
ModuleRegistrationName.SALES_CHANNEL
|
||||
)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
|
||||
standardPromotion = await promotionModule.createPromotions({
|
||||
code: "TEST_ACROSS",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
allocation: "across",
|
||||
target_type: "items",
|
||||
value: 100,
|
||||
target_rules: [promotionRule],
|
||||
currency_code: "USD",
|
||||
},
|
||||
rules: [promotionRule],
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/promotions/:id/rules/batch", () => {
|
||||
it("should throw error when required params are missing", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/${standardPromotion.id}/rules/batch`,
|
||||
{
|
||||
create: [
|
||||
{
|
||||
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/batch`,
|
||||
{
|
||||
create: [
|
||||
{
|
||||
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/batch`,
|
||||
{
|
||||
create: [
|
||||
{
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const promotion = (
|
||||
await api.get(
|
||||
`/admin/promotions/${standardPromotion.id}?fields=*rules`,
|
||||
adminHeaders
|
||||
)
|
||||
).data.promotion
|
||||
expect(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/batch", () => {
|
||||
it("should throw error when required params are missing", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/${standardPromotion.id}/target-rules/batch`,
|
||||
{
|
||||
create: [
|
||||
{
|
||||
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/batch`,
|
||||
{
|
||||
create: [
|
||||
{
|
||||
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/batch`,
|
||||
{
|
||||
create: [
|
||||
{
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const promotion = (
|
||||
await api.get(
|
||||
`/admin/promotions/${standardPromotion.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
).data.promotion
|
||||
expect(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/batch", () => {
|
||||
it("should throw error when required params are missing", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/${standardPromotion.id}/buy-rules/batch`,
|
||||
{
|
||||
create: [
|
||||
{
|
||||
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/batch`,
|
||||
{
|
||||
create: [
|
||||
{
|
||||
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/batch`,
|
||||
{
|
||||
create: [
|
||||
{
|
||||
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.createPromotions({
|
||||
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],
|
||||
currency_code: "USD",
|
||||
},
|
||||
rules: [promotionRule],
|
||||
})
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch`,
|
||||
{
|
||||
create: [
|
||||
{
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const promotion = (
|
||||
await api.get(
|
||||
`/admin/promotions/${buyGetPromotion.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
).data.promotion
|
||||
|
||||
expect(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" })],
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/promotions/:id/rules/batch", () => {
|
||||
it("should throw error when promotion does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/does-not-exist/rules/batch`,
|
||||
{ delete: ["test-rule-id"] },
|
||||
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 remove rules from a promotion successfully", async () => {
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${standardPromotion.id}/rules/batch`,
|
||||
{ delete: [standardPromotion.rules[0].id] },
|
||||
adminHeaders
|
||||
)
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.deleted).toEqual({
|
||||
ids: [standardPromotion.rules[0].id],
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
|
||||
const promotion = await promotionModule.retrievePromotion(
|
||||
standardPromotion.id,
|
||||
{ relations: ["rules"] }
|
||||
)
|
||||
|
||||
expect(promotion.rules!.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/promotions/:id/target-rules/batch", () => {
|
||||
it("should throw error when promotion does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/does-not-exist/target-rules/batch`,
|
||||
{ delete: ["test-rule-id"] },
|
||||
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 remove target rules from a promotion successfully", async () => {
|
||||
const ruleId = standardPromotion.application_method.target_rules[0].id
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${standardPromotion.id}/target-rules/batch`,
|
||||
{ delete: [ruleId] },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.deleted).toEqual({
|
||||
ids: [ruleId],
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
|
||||
const promotion = await promotionModule.retrievePromotion(
|
||||
standardPromotion.id,
|
||||
{ relations: ["application_method.target_rules"] }
|
||||
)
|
||||
|
||||
expect(promotion.application_method!.target_rules!.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/promotions/:id/buy-rules/batch", () => {
|
||||
it("should throw error when promotion does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/does-not-exist/buy-rules/batch`,
|
||||
{ delete: ["test-rule-id"] },
|
||||
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 remove buy rules from a promotion successfully", async () => {
|
||||
const buyGetPromotion = await promotionModule.createPromotions({
|
||||
code: "TEST_BUYGET",
|
||||
type: PromotionType.BUYGET,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
currency_code: "USD",
|
||||
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 ruleId = buyGetPromotion!.application_method!.buy_rules![0].id
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${buyGetPromotion.id}/buy-rules/batch`,
|
||||
{ delete: [ruleId] },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.deleted).toEqual({
|
||||
ids: [ruleId],
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
|
||||
const promotion = await promotionModule.retrievePromotion(
|
||||
buyGetPromotion.id,
|
||||
{
|
||||
relations: ["application_method.buy_rules"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(promotion.application_method!.buy_rules!.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/promotions/:id/rules/batch", () => {
|
||||
it("should throw error when promotion does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/does-not-exist/rules/batch`,
|
||||
{
|
||||
update: [
|
||||
{
|
||||
id: standardPromotion.rules[0].id,
|
||||
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 error when promotion rule id does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/${standardPromotion.id}/rules/batch`,
|
||||
{
|
||||
update: [
|
||||
{
|
||||
id: "does-not-exist",
|
||||
attribute: "new_attr",
|
||||
operator: "eq",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data).toEqual({
|
||||
type: "invalid_data",
|
||||
message: "Promotion rules with id - does-not-exist not found",
|
||||
})
|
||||
})
|
||||
|
||||
it("should add rules to a promotion successfully", async () => {
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${standardPromotion.id}/rules/batch`,
|
||||
{
|
||||
update: [
|
||||
{
|
||||
id: standardPromotion.rules[0].id,
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: ["new value"],
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const promotion = (
|
||||
await api.get(
|
||||
`/admin/promotions/${standardPromotion.id}?fields=*rules`,
|
||||
adminHeaders
|
||||
)
|
||||
).data.promotion
|
||||
expect(promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: standardPromotion.id,
|
||||
rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
operator: "eq",
|
||||
attribute: "new_attr",
|
||||
values: [expect.objectContaining({ value: "new value" })],
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/promotions/rule-attribute-options/:ruleType", () => {
|
||||
it("should throw error when ruleType is invalid", async () => {
|
||||
const { response } = await api
|
||||
.get(
|
||||
`/admin/promotions/rule-attribute-options/does-not-exist`,
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data).toEqual({
|
||||
type: "invalid_data",
|
||||
message: "Invalid param rule_type (does-not-exist)",
|
||||
})
|
||||
})
|
||||
|
||||
it("return all rule attributes for a valid ruleType", async () => {
|
||||
const response = await api.get(
|
||||
`/admin/promotions/rule-attribute-options/rules`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.attributes).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: "currency_code",
|
||||
label: "Currency Code",
|
||||
required: true,
|
||||
value: "currency_code",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "customer_group",
|
||||
label: "Customer Group",
|
||||
required: false,
|
||||
value: "customer_group.id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "region",
|
||||
label: "Region",
|
||||
required: false,
|
||||
value: "region.id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "country",
|
||||
label: "Country",
|
||||
required: false,
|
||||
value: "shipping_address.country_code",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "sales_channel",
|
||||
label: "Sales Channel",
|
||||
required: false,
|
||||
value: "sales_channel.id",
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/promotions/rule-operator-options", () => {
|
||||
it("return all rule operators", async () => {
|
||||
const response = await api.get(
|
||||
`/admin/promotions/rule-operator-options`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.operators).toEqual([
|
||||
{
|
||||
id: "in",
|
||||
label: "In",
|
||||
value: "in",
|
||||
},
|
||||
{
|
||||
id: "eq",
|
||||
label: "Equals",
|
||||
value: "eq",
|
||||
},
|
||||
{
|
||||
id: "ne",
|
||||
label: "Not In",
|
||||
value: "ne",
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/promotions/rule-value-options/:ruleType/:ruleAttributeId", () => {
|
||||
it("should throw error when ruleType is invalid", async () => {
|
||||
const { response } = await api
|
||||
.get(
|
||||
`/admin/promotions/rule-value-options/does-not-exist/region`,
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data).toEqual({
|
||||
type: "invalid_data",
|
||||
message: "Invalid param rule_type (does-not-exist)",
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw error when ruleAttributeId is invalid", async () => {
|
||||
const { response } = await api
|
||||
.get(
|
||||
`/admin/promotions/rule-value-options/rules/does-not-exist`,
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data).toEqual({
|
||||
type: "invalid_data",
|
||||
message: "Invalid rule attribute - does-not-exist",
|
||||
})
|
||||
})
|
||||
|
||||
it("should return all values based on rule types", async () => {
|
||||
const [region1, region2] = await regionService.createRegions([
|
||||
{ name: "North America", currency_code: "usd" },
|
||||
{ name: "Europe", currency_code: "eur" },
|
||||
])
|
||||
|
||||
let response = await api.get(
|
||||
`/admin/promotions/rule-value-options/rules/region`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.values.length).toEqual(2)
|
||||
expect(response.data.values).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
label: "North America",
|
||||
value: region1.id,
|
||||
},
|
||||
{
|
||||
label: "Europe",
|
||||
value: region2.id,
|
||||
},
|
||||
])
|
||||
)
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions/rule-value-options/rules/currency_code?limit=2&order=name`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.values.length).toEqual(2)
|
||||
expect(response.data.values).toEqual(
|
||||
expect.arrayContaining([
|
||||
{ label: "Afghan Afghani", value: "afn" },
|
||||
{ label: "Albanian Lek", value: "all" },
|
||||
])
|
||||
)
|
||||
|
||||
const group = await customerService.createCustomerGroups({
|
||||
name: "VIP",
|
||||
})
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions/rule-value-options/rules/customer_group`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.values).toEqual([
|
||||
{
|
||||
label: "VIP",
|
||||
value: group.id,
|
||||
},
|
||||
])
|
||||
|
||||
const salesChannel = await salesChannelService.createSalesChannels({
|
||||
name: "Instagram",
|
||||
})
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions/rule-value-options/rules/sales_channel`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
// TODO: This is returning a default sales channel, but very flakily
|
||||
// Figure out why this happens and fix
|
||||
// expect(response.data.values.length).toEqual(1)
|
||||
expect(response.data.values).toEqual(
|
||||
expect.arrayContaining([
|
||||
{ label: "Instagram", value: salesChannel.id },
|
||||
])
|
||||
)
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions/rule-value-options/rules/country?limit=2`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.values.length).toEqual(2)
|
||||
expect(response.data.values).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
label: "Andorra",
|
||||
value: "ad",
|
||||
},
|
||||
{
|
||||
label: "United Arab Emirates",
|
||||
value: "ae",
|
||||
},
|
||||
])
|
||||
)
|
||||
|
||||
const [product1, product2] = await productService.createProducts([
|
||||
{ title: "test product 1" },
|
||||
{ title: "test product 2" },
|
||||
])
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions/rule-value-options/target-rules/product`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.values.length).toEqual(2)
|
||||
expect(response.data.values).toEqual(
|
||||
expect.arrayContaining([
|
||||
{ label: "test product 1", value: product1.id },
|
||||
{ label: "test product 2", value: product2.id },
|
||||
])
|
||||
)
|
||||
|
||||
const category = await productService.createProductCategories({
|
||||
name: "test category 1",
|
||||
parent_category_id: null,
|
||||
})
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions/rule-value-options/target-rules/product_category`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.values).toEqual([
|
||||
{ label: "test category 1", value: category.id },
|
||||
])
|
||||
|
||||
const collection = await productService.createProductCollections({
|
||||
title: "test collection 1",
|
||||
})
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions/rule-value-options/target-rules/product_collection`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.values).toEqual([
|
||||
{ label: "test collection 1", value: collection.id },
|
||||
])
|
||||
|
||||
const type = await productService.createProductTypes({
|
||||
value: "test type",
|
||||
})
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions/rule-value-options/target-rules/product_type`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.values).toEqual([
|
||||
{ label: "test type", value: type.id },
|
||||
])
|
||||
|
||||
const [tag1, tag2] = await productService.createProductTags([
|
||||
{ value: "test tag 1" },
|
||||
{ value: "test tag 2" },
|
||||
])
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions/rule-value-options/target-rules/product_tag`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.values.length).toEqual(2)
|
||||
expect(response.data.values).toEqual(
|
||||
expect.arrayContaining([
|
||||
{ label: "test tag 1", value: tag1.id },
|
||||
{ label: "test tag 2", value: tag2.id },
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -1,104 +0,0 @@
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName, 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("GET /admin/promotions/:id", () => {
|
||||
let appContainer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
})
|
||||
|
||||
it("should throw an error if id does not exist", async () => {
|
||||
const { response } = await api
|
||||
.get(`/admin/promotions/does-not-exist`, adminHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data.message).toEqual(
|
||||
"Promotion with id or code: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should get the requested promotion by id or codde", async () => {
|
||||
const createdPromotion = await promotionModuleService.createPromotions({
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: "100",
|
||||
currency_code: "USD",
|
||||
},
|
||||
})
|
||||
|
||||
let response = await api.get(
|
||||
`/admin/promotions/${createdPromotion.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: createdPromotion.id,
|
||||
})
|
||||
)
|
||||
|
||||
response = await api.get(
|
||||
`/admin/promotions/${createdPromotion.code}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: createdPromotion.id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should get the requested promotion with filtered fields and relations", async () => {
|
||||
const createdPromotion = await promotionModuleService.createPromotions({
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "order",
|
||||
value: "100",
|
||||
currency_code: "USD",
|
||||
},
|
||||
})
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/promotions/${createdPromotion.id}?fields=id,code`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual({
|
||||
id: expect.any(String),
|
||||
code: "TEST",
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -1,173 +0,0 @@
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName, 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("POST /admin/promotions/:id", () => {
|
||||
let appContainer
|
||||
let promotionModuleService: IPromotionModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
promotionModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
})
|
||||
|
||||
it("should throw an error if id does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/does-not-exist`,
|
||||
{ type: PromotionType.STANDARD },
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data.message).toEqual(
|
||||
`Promotion with id "does-not-exist" not found`
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when both campaign and campaign_id params are passed", async () => {
|
||||
const createdPromotion = await promotionModuleService.createPromotions({
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
is_automatic: true,
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
currency_code: "USD",
|
||||
},
|
||||
})
|
||||
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/promotions/${createdPromotion.id}`,
|
||||
{
|
||||
campaign: {
|
||||
name: "test campaign",
|
||||
},
|
||||
campaign_id: "test",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
// expect(response.data.message).toContain(
|
||||
// `Failed XOR relation between "campaign_id" and "campaign"`
|
||||
// )
|
||||
})
|
||||
|
||||
it("should update a promotion successfully", async () => {
|
||||
const createdPromotion = await promotionModuleService.createPromotions({
|
||||
code: "TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
is_automatic: true,
|
||||
application_method: {
|
||||
target_type: "items",
|
||||
type: "fixed",
|
||||
allocation: "each",
|
||||
value: 100,
|
||||
max_quantity: 100,
|
||||
currency_code: "USD",
|
||||
},
|
||||
})
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${createdPromotion.id}`,
|
||||
{
|
||||
code: "TEST_TWO",
|
||||
application_method: {
|
||||
value: 200,
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
code: "TEST_TWO",
|
||||
application_method: expect.objectContaining({
|
||||
value: 200,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should update a buyget promotion successfully", async () => {
|
||||
const createdPromotion = await promotionModuleService.createPromotions({
|
||||
code: "PROMOTION_TEST",
|
||||
type: PromotionType.BUYGET,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "items",
|
||||
allocation: "across",
|
||||
value: 100,
|
||||
apply_to_quantity: 1,
|
||||
buy_rules_min_quantity: 1,
|
||||
currency_code: "USD",
|
||||
buy_rules: [
|
||||
{
|
||||
attribute: "product_collection.id",
|
||||
operator: "eq",
|
||||
values: ["pcol_towel"],
|
||||
},
|
||||
],
|
||||
target_rules: [
|
||||
{
|
||||
attribute: "product.id",
|
||||
operator: "eq",
|
||||
values: "prod_mat",
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/promotions/${createdPromotion.id}`,
|
||||
{
|
||||
code: "TEST_TWO",
|
||||
application_method: {
|
||||
value: 200,
|
||||
buy_rules_min_quantity: 6,
|
||||
},
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.promotion).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
code: "TEST_TWO",
|
||||
application_method: expect.objectContaining({
|
||||
value: 200,
|
||||
buy_rules_min_quantity: 6,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user