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:
@@ -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 })
|
||||
}
|
||||
@@ -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
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -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,
|
||||
}
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
@@ -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,
|
||||
],
|
||||
}
|
||||
|
||||
+65
@@ -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