feat(medusa,types): GET admin promotion endpoint to fetch by code (#6697)
what: - GET admin promotion endpoint will search for a promotion either through ID or code
This commit is contained in:
1 parent
c3f8c30ba6
commit
1a661adf3e
4 files changed
+51
-34
No files matched your search
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
"@medusajs/types": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa,types): GET admin promotion endpoint to fetch by code
|
||||||
@@ -14,7 +14,7 @@ const adminHeaders = {
|
|||||||
medusaIntegrationTestRunner({
|
medusaIntegrationTestRunner({
|
||||||
env,
|
env,
|
||||||
testSuite: ({ dbConnection, getContainer, api }) => {
|
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||||
describe("GET /admin/promotions", () => {
|
describe("GET /admin/promotions/:id", () => {
|
||||||
let appContainer
|
let appContainer
|
||||||
let promotionModuleService: IPromotionModuleService
|
let promotionModuleService: IPromotionModuleService
|
||||||
|
|
||||||
@@ -36,11 +36,11 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
expect(response.status).toEqual(404)
|
expect(response.status).toEqual(404)
|
||||||
expect(response.data.message).toEqual(
|
expect(response.data.message).toEqual(
|
||||||
"Promotion with id: does-not-exist was not found"
|
"Promotion with id or code: does-not-exist was not found"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should get the requested promotion", async () => {
|
it("should get the requested promotion by id or codde", async () => {
|
||||||
const createdPromotion = await promotionModuleService.create({
|
const createdPromotion = await promotionModuleService.create({
|
||||||
code: "TEST",
|
code: "TEST",
|
||||||
type: PromotionType.STANDARD,
|
type: PromotionType.STANDARD,
|
||||||
@@ -51,7 +51,7 @@ medusaIntegrationTestRunner({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const response = await api.get(
|
let response = await api.get(
|
||||||
`/admin/promotions/${createdPromotion.id}`,
|
`/admin/promotions/${createdPromotion.id}`,
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
@@ -59,22 +59,19 @@ medusaIntegrationTestRunner({
|
|||||||
expect(response.status).toEqual(200)
|
expect(response.status).toEqual(200)
|
||||||
expect(response.data.promotion).toEqual(
|
expect(response.data.promotion).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: expect.any(String),
|
id: createdPromotion.id,
|
||||||
code: "TEST",
|
})
|
||||||
campaign: null,
|
)
|
||||||
is_automatic: false,
|
|
||||||
type: "standard",
|
response = await api.get(
|
||||||
created_at: expect.any(String),
|
`/admin/promotions/${createdPromotion.code}`,
|
||||||
updated_at: expect.any(String),
|
adminHeaders
|
||||||
deleted_at: null,
|
)
|
||||||
application_method: expect.objectContaining({
|
|
||||||
id: expect.any(String),
|
expect(response.status).toEqual(200)
|
||||||
promotion: expect.any(Object),
|
expect(response.data.promotion).toEqual(
|
||||||
value: 100,
|
expect.objectContaining({
|
||||||
type: "fixed",
|
id: createdPromotion.id,
|
||||||
target_type: "order",
|
|
||||||
allocation: null,
|
|
||||||
}),
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,27 +2,42 @@ import {
|
|||||||
deletePromotionsWorkflow,
|
deletePromotionsWorkflow,
|
||||||
updatePromotionsWorkflow,
|
updatePromotionsWorkflow,
|
||||||
} from "@medusajs/core-flows"
|
} from "@medusajs/core-flows"
|
||||||
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
import { IPromotionModuleService, UpdatePromotionDTO } from "@medusajs/types"
|
||||||
|
import { MedusaError } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../types/routing"
|
} from "../../../../types/routing"
|
||||||
|
import {
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
AdminGetPromotionsParams,
|
||||||
import { IPromotionModuleService, UpdatePromotionDTO } from "@medusajs/types"
|
AdminPostPromotionsPromotionReq,
|
||||||
import { AdminPostPromotionsPromotionReq } from "../validators"
|
} from "../validators"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminGetPromotionsParams>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
|
const idOrCode = req.params.id
|
||||||
const promotionModuleService: IPromotionModuleService = req.scope.resolve(
|
const promotionModuleService: IPromotionModuleService = req.scope.resolve(
|
||||||
ModuleRegistrationName.PROMOTION
|
ModuleRegistrationName.PROMOTION
|
||||||
)
|
)
|
||||||
|
|
||||||
const promotion = await promotionModuleService.retrieve(req.params.id, {
|
const [promotion] = await promotionModuleService.list(
|
||||||
|
{ $or: [{ id: idOrCode }, { code: idOrCode }] },
|
||||||
|
{
|
||||||
select: req.retrieveConfig.select,
|
select: req.retrieveConfig.select,
|
||||||
relations: req.retrieveConfig.relations,
|
relations: req.retrieveConfig.relations,
|
||||||
})
|
take: 1,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!promotion) {
|
||||||
|
throw new MedusaError(
|
||||||
|
MedusaError.Types.NOT_FOUND,
|
||||||
|
`Promotion with id or code: does-not-exist was not found`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
res.status(200).json({ promotion })
|
res.status(200).json({ promotion })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
|
import { BaseFilterable, OperatorMap } from "../../dal"
|
||||||
|
import { CreateCampaignDTO } from "../mutations"
|
||||||
import {
|
import {
|
||||||
ApplicationMethodDTO,
|
ApplicationMethodDTO,
|
||||||
CreateApplicationMethodDTO,
|
CreateApplicationMethodDTO,
|
||||||
UpdateApplicationMethodDTO,
|
UpdateApplicationMethodDTO,
|
||||||
} from "./application-method"
|
} from "./application-method"
|
||||||
import { CreatePromotionRuleDTO, PromotionRuleDTO } from "./promotion-rule"
|
|
||||||
|
|
||||||
import { BaseFilterable } from "../../dal"
|
|
||||||
import { CampaignDTO } from "./campaign"
|
import { CampaignDTO } from "./campaign"
|
||||||
import { CreateCampaignDTO } from "../mutations"
|
import { CreatePromotionRuleDTO, PromotionRuleDTO } from "./promotion-rule"
|
||||||
|
|
||||||
export type PromotionTypeValues = "standard" | "buyget"
|
export type PromotionTypeValues = "standard" | "buyget"
|
||||||
|
|
||||||
@@ -42,9 +41,9 @@ export interface UpdatePromotionDTO {
|
|||||||
|
|
||||||
export interface FilterablePromotionProps
|
export interface FilterablePromotionProps
|
||||||
extends BaseFilterable<FilterablePromotionProps> {
|
extends BaseFilterable<FilterablePromotionProps> {
|
||||||
id?: string[]
|
id?: string | string[]
|
||||||
code?: string[]
|
code?: string | string[] | OperatorMap<string>
|
||||||
|
budget_id?: string[] | OperatorMap<string>
|
||||||
is_automatic?: boolean
|
is_automatic?: boolean
|
||||||
type?: PromotionTypeValues[]
|
type?: PromotionTypeValues[]
|
||||||
budget_id?: string[]
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user