feat(medusa,utils): added campaign get endpoints (#6125)

what:

adds endpoints for the following:

- list endpoint (RESOLVES CORE-1682)
- retrieve endpoint (RESOLVES CORE-1683)
This commit is contained in:
Riqwan Thamir
2024-01-19 14:54:40 +00:00
committed by GitHub
parent 5e655dd59b
commit af7af73745
12 changed files with 520 additions and 1 deletions
@@ -0,0 +1,165 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { CampaignBudgetType } from "@medusajs/utils"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
export const campaignsData = [
{
id: "campaign-id-1",
name: "campaign 1",
description: "test description",
currency: "USD",
campaign_identifier: "test-1",
starts_at: new Date("01/01/2023"),
ends_at: new Date("01/01/2024"),
budget: {
type: CampaignBudgetType.SPEND,
limit: 1000,
used: 0,
},
},
{
id: "campaign-id-2",
name: "campaign 2",
description: "test description",
currency: "USD",
campaign_identifier: "test-2",
starts_at: new Date("01/01/2023"),
ends_at: new Date("01/01/2024"),
budget: {
type: CampaignBudgetType.USAGE,
limit: 1000,
used: 0,
},
},
]
const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("GET /admin/campaigns", () => {
let dbConnection
let appContainer
let shutdownServer
let promotionModuleService: IPromotionModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
promotionModuleService = appContainer.resolve(
ModuleRegistrationName.PROMOTION
)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
beforeEach(async () => {
await adminSeeder(dbConnection)
await promotionModuleService.createCampaigns(campaignsData)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should get all campaigns and its count", async () => {
const api = useApi() as any
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",
currency: "USD",
campaign_identifier: "test-1",
starts_at: expect.any(String),
ends_at: expect.any(String),
budget: {
id: expect.any(String),
campaign: expect.any(Object),
type: "spend",
limit: 1000,
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,
}),
expect.objectContaining({
id: expect.any(String),
name: "campaign 2",
description: "test description",
currency: "USD",
campaign_identifier: "test-2",
starts_at: expect.any(String),
ends_at: expect.any(String),
budget: {
id: expect.any(String),
campaign: expect.any(Object),
type: "usage",
limit: 1000,
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 all campaigns and its count filtered", async () => {
const api = useApi() as any
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([
{
id: expect.any(String),
name: "campaign 1",
created_at: expect.any(String),
budget: {
id: expect.any(String),
campaign: expect.any(Object),
},
},
{
id: expect.any(String),
name: "campaign 2",
created_at: expect.any(String),
budget: {
id: expect.any(String),
campaign: expect.any(Object),
},
},
])
})
})
@@ -0,0 +1,130 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { CampaignBudgetType } from "@medusajs/utils"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
export const campaignData = {
name: "campaign 1",
description: "test description",
currency: "USD",
campaign_identifier: "test-1",
starts_at: new Date("01/01/2023"),
ends_at: new Date("01/01/2024"),
budget: {
type: CampaignBudgetType.SPEND,
limit: 1000,
used: 0,
},
}
const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("GET /admin/campaigns", () => {
let dbConnection
let appContainer
let shutdownServer
let promotionModuleService: IPromotionModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
promotionModuleService = appContainer.resolve(
ModuleRegistrationName.PROMOTION
)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
beforeEach(async () => {
await adminSeeder(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
let campaigns
beforeEach(async () => {})
it("should throw an error if id does not exist", async () => {
const api = useApi() as any
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 api = useApi() as any
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",
currency: "USD",
campaign_identifier: "test-1",
starts_at: expect.any(String),
ends_at: expect.any(String),
budget: {
id: expect.any(String),
campaign: expect.any(Object),
type: "spend",
limit: 1000,
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 api = useApi() as any
const response = await api.get(
`/admin/campaigns/${createdCampaign.id}?fields=name&expand=`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.campaign).toEqual({
id: expect.any(String),
name: "campaign 1",
})
})
})