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:
6
.changeset/empty-cherries-battle.md
Normal file
6
.changeset/empty-cherries-battle.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
"@medusajs/types": patch
|
||||
---
|
||||
|
||||
feat(medusa,utils): added campaign get endpoints
|
||||
@@ -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",
|
||||
})
|
||||
})
|
||||
})
|
||||
19
packages/medusa/src/api-v2/admin/campaigns/[id]/route.ts
Normal file
19
packages/medusa/src/api-v2/admin/campaigns/[id]/route.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const promotionModuleService: IPromotionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const campaign = await promotionModuleService.retrieveCampaign(
|
||||
req.params.id,
|
||||
{
|
||||
select: req.retrieveConfig.select,
|
||||
relations: req.retrieveConfig.relations,
|
||||
}
|
||||
)
|
||||
|
||||
res.status(200).json({ campaign })
|
||||
}
|
||||
35
packages/medusa/src/api-v2/admin/campaigns/middlewares.ts
Normal file
35
packages/medusa/src/api-v2/admin/campaigns/middlewares.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { MedusaV2Flag } from "@medusajs/utils"
|
||||
import { isFeatureFlagEnabled, transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
AdminGetCampaignsCampaignParams,
|
||||
AdminGetCampaignsParams,
|
||||
} from "./validators"
|
||||
|
||||
export const adminCampaignRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
matcher: "/admin/campaigns*",
|
||||
middlewares: [isFeatureFlagEnabled(MedusaV2Flag.key)],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/campaigns",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetCampaignsParams,
|
||||
QueryConfig.listTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/campaigns/:id",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetCampaignsCampaignParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
28
packages/medusa/src/api-v2/admin/campaigns/query-config.ts
Normal file
28
packages/medusa/src/api-v2/admin/campaigns/query-config.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
export const defaultAdminCampaignRelations = ["budget"]
|
||||
export const allowedAdminCampaignRelations = [
|
||||
...defaultAdminCampaignRelations,
|
||||
"promotions",
|
||||
]
|
||||
export const defaultAdminCampaignFields = [
|
||||
"name",
|
||||
"description",
|
||||
"currency",
|
||||
"campaign_identifier",
|
||||
"starts_at",
|
||||
"ends_at",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultAdminCampaignFields,
|
||||
defaultRelations: defaultAdminCampaignRelations,
|
||||
allowedRelations: allowedAdminCampaignRelations,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listTransformQueryConfig = {
|
||||
...retrieveTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
23
packages/medusa/src/api-v2/admin/campaigns/route.ts
Normal file
23
packages/medusa/src/api-v2/admin/campaigns/route.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const promotionModuleService: IPromotionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const [campaigns, count] = await promotionModuleService.listAndCountCampaigns(
|
||||
req.filterableFields,
|
||||
req.listConfig
|
||||
)
|
||||
|
||||
const { limit, offset } = req.validatedQuery
|
||||
|
||||
res.json({
|
||||
count,
|
||||
campaigns,
|
||||
offset,
|
||||
limit,
|
||||
})
|
||||
}
|
||||
17
packages/medusa/src/api-v2/admin/campaigns/validators.ts
Normal file
17
packages/medusa/src/api-v2/admin/campaigns/validators.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { IsOptional, IsString } from "class-validator"
|
||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
||||
|
||||
export class AdminGetCampaignsCampaignParams extends FindParams {}
|
||||
|
||||
export class AdminGetCampaignsParams extends extendedFindParamsMixin({
|
||||
limit: 100,
|
||||
offset: 0,
|
||||
}) {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
campaign_identifier?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
currency?: string
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
import { MiddlewaresConfig } from "../loaders/helpers/routing/types"
|
||||
import { adminCampaignRoutesMiddlewares } from "./admin/campaigns/middlewares"
|
||||
import { adminPromotionRoutesMiddlewares } from "./admin/promotions/middlewares"
|
||||
|
||||
export const config: MiddlewaresConfig = {
|
||||
routes: [...adminPromotionRoutesMiddlewares],
|
||||
routes: [
|
||||
...adminPromotionRoutesMiddlewares,
|
||||
...adminCampaignRoutesMiddlewares,
|
||||
],
|
||||
}
|
||||
|
||||
@@ -27,6 +27,71 @@ describe("Promotion Module Service: Campaigns", () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("listAndCountCampaigns", () => {
|
||||
beforeEach(async () => {
|
||||
await createCampaigns(repositoryManager)
|
||||
})
|
||||
|
||||
it("should return all campaigns and its count", async () => {
|
||||
const [campaigns, count] = await service.listAndCountCampaigns()
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(campaigns).toEqual([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-1",
|
||||
starts_at: expect.any(Date),
|
||||
ends_at: expect.any(Date),
|
||||
budget: expect.any(String),
|
||||
created_at: expect.any(Date),
|
||||
updated_at: expect.any(Date),
|
||||
deleted_at: null,
|
||||
},
|
||||
{
|
||||
id: "campaign-id-2",
|
||||
name: "campaign 1",
|
||||
description: "test description",
|
||||
currency: "USD",
|
||||
campaign_identifier: "test-2",
|
||||
starts_at: expect.any(Date),
|
||||
ends_at: expect.any(Date),
|
||||
budget: expect.any(String),
|
||||
created_at: expect.any(Date),
|
||||
updated_at: expect.any(Date),
|
||||
deleted_at: null,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should return all campaigns based on config select and relations param", async () => {
|
||||
const [campaigns, count] = await service.listAndCountCampaigns(
|
||||
{
|
||||
id: ["campaign-id-1"],
|
||||
},
|
||||
{
|
||||
relations: ["budget"],
|
||||
select: ["name", "budget.limit"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(campaigns).toEqual([
|
||||
{
|
||||
id: "campaign-id-1",
|
||||
name: "campaign 1",
|
||||
budget: {
|
||||
id: expect.any(String),
|
||||
campaign: expect.any(Object),
|
||||
limit: 1000,
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("createCampaigns", () => {
|
||||
it("should throw an error when required params are not passed", async () => {
|
||||
const error = await service
|
||||
|
||||
@@ -938,6 +938,27 @@ export default class PromotionModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listAndCountCampaigns(
|
||||
filters: PromotionTypes.FilterableCampaignProps = {},
|
||||
config: FindConfig<PromotionTypes.CampaignDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[PromotionTypes.CampaignDTO[], number]> {
|
||||
const [campaigns, count] = await this.campaignService_.listAndCount(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return [
|
||||
await this.baseRepository_.serialize<PromotionTypes.CampaignDTO[]>(
|
||||
campaigns,
|
||||
{ populate: true }
|
||||
),
|
||||
count,
|
||||
]
|
||||
}
|
||||
|
||||
async createCampaigns(
|
||||
data: PromotionTypes.CreateCampaignDTO,
|
||||
sharedContext?: Context
|
||||
|
||||
@@ -116,6 +116,12 @@ export interface IPromotionModuleService extends IModuleService {
|
||||
sharedContext?: Context
|
||||
): Promise<CampaignDTO[]>
|
||||
|
||||
listAndCountCampaigns(
|
||||
filters?: FilterableCampaignProps,
|
||||
config?: FindConfig<CampaignDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<[CampaignDTO[], number]>
|
||||
|
||||
retrieveCampaign(
|
||||
id: string,
|
||||
config?: FindConfig<CampaignDTO>,
|
||||
|
||||
Reference in New Issue
Block a user