From 1a661adf3ef4991aa6e237dd894b6a5c47cd4aca Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 14 Mar 2024 16:13:53 +0100 Subject: [PATCH] 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 --- .changeset/cold-phones-sneeze.md | 6 +++ .../admin/retrieve-promotion.spec.ts | 37 +++++++++---------- .../src/api-v2/admin/promotions/[id]/route.ts | 33 ++++++++++++----- .../types/src/promotion/common/promotion.ts | 13 +++---- 4 files changed, 53 insertions(+), 36 deletions(-) create mode 100644 .changeset/cold-phones-sneeze.md diff --git a/.changeset/cold-phones-sneeze.md b/.changeset/cold-phones-sneeze.md new file mode 100644 index 0000000000..2dd75c69b1 --- /dev/null +++ b/.changeset/cold-phones-sneeze.md @@ -0,0 +1,6 @@ +--- +"@medusajs/medusa": patch +"@medusajs/types": patch +--- + +feat(medusa,types): GET admin promotion endpoint to fetch by code diff --git a/integration-tests/modules/__tests__/promotion/admin/retrieve-promotion.spec.ts b/integration-tests/modules/__tests__/promotion/admin/retrieve-promotion.spec.ts index ff516111c1..f1a5c272ad 100644 --- a/integration-tests/modules/__tests__/promotion/admin/retrieve-promotion.spec.ts +++ b/integration-tests/modules/__tests__/promotion/admin/retrieve-promotion.spec.ts @@ -14,7 +14,7 @@ const adminHeaders = { medusaIntegrationTestRunner({ env, testSuite: ({ dbConnection, getContainer, api }) => { - describe("GET /admin/promotions", () => { + describe("GET /admin/promotions/:id", () => { let appContainer let promotionModuleService: IPromotionModuleService @@ -36,11 +36,11 @@ medusaIntegrationTestRunner({ expect(response.status).toEqual(404) 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({ code: "TEST", type: PromotionType.STANDARD, @@ -51,7 +51,7 @@ medusaIntegrationTestRunner({ }, }) - const response = await api.get( + let response = await api.get( `/admin/promotions/${createdPromotion.id}`, adminHeaders ) @@ -59,22 +59,19 @@ medusaIntegrationTestRunner({ expect(response.status).toEqual(200) expect(response.data.promotion).toEqual( expect.objectContaining({ - id: expect.any(String), - code: "TEST", - campaign: null, - is_automatic: false, - type: "standard", - created_at: expect.any(String), - updated_at: expect.any(String), - deleted_at: null, - application_method: expect.objectContaining({ - id: expect.any(String), - promotion: expect.any(Object), - value: 100, - type: "fixed", - target_type: "order", - allocation: null, - }), + 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, }) ) }) diff --git a/packages/medusa/src/api-v2/admin/promotions/[id]/route.ts b/packages/medusa/src/api-v2/admin/promotions/[id]/route.ts index 442158f956..79e6af946c 100644 --- a/packages/medusa/src/api-v2/admin/promotions/[id]/route.ts +++ b/packages/medusa/src/api-v2/admin/promotions/[id]/route.ts @@ -2,27 +2,42 @@ import { deletePromotionsWorkflow, updatePromotionsWorkflow, } from "@medusajs/core-flows" +import { ModuleRegistrationName } from "@medusajs/modules-sdk" +import { IPromotionModuleService, UpdatePromotionDTO } from "@medusajs/types" +import { MedusaError } from "@medusajs/utils" import { AuthenticatedMedusaRequest, MedusaResponse, } from "../../../../types/routing" - -import { ModuleRegistrationName } from "@medusajs/modules-sdk" -import { IPromotionModuleService, UpdatePromotionDTO } from "@medusajs/types" -import { AdminPostPromotionsPromotionReq } from "../validators" +import { + AdminGetPromotionsParams, + AdminPostPromotionsPromotionReq, +} from "../validators" export const GET = async ( - req: AuthenticatedMedusaRequest, + req: AuthenticatedMedusaRequest, res: MedusaResponse ) => { + const idOrCode = req.params.id const promotionModuleService: IPromotionModuleService = req.scope.resolve( ModuleRegistrationName.PROMOTION ) - const promotion = await promotionModuleService.retrieve(req.params.id, { - select: req.retrieveConfig.select, - relations: req.retrieveConfig.relations, - }) + const [promotion] = await promotionModuleService.list( + { $or: [{ id: idOrCode }, { code: idOrCode }] }, + { + select: req.retrieveConfig.select, + 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 }) } diff --git a/packages/types/src/promotion/common/promotion.ts b/packages/types/src/promotion/common/promotion.ts index 739c523666..8d248dab14 100644 --- a/packages/types/src/promotion/common/promotion.ts +++ b/packages/types/src/promotion/common/promotion.ts @@ -1,13 +1,12 @@ +import { BaseFilterable, OperatorMap } from "../../dal" +import { CreateCampaignDTO } from "../mutations" import { ApplicationMethodDTO, CreateApplicationMethodDTO, UpdateApplicationMethodDTO, } from "./application-method" -import { CreatePromotionRuleDTO, PromotionRuleDTO } from "./promotion-rule" - -import { BaseFilterable } from "../../dal" import { CampaignDTO } from "./campaign" -import { CreateCampaignDTO } from "../mutations" +import { CreatePromotionRuleDTO, PromotionRuleDTO } from "./promotion-rule" export type PromotionTypeValues = "standard" | "buyget" @@ -42,9 +41,9 @@ export interface UpdatePromotionDTO { export interface FilterablePromotionProps extends BaseFilterable { - id?: string[] - code?: string[] + id?: string | string[] + code?: string | string[] | OperatorMap + budget_id?: string[] | OperatorMap is_automatic?: boolean type?: PromotionTypeValues[] - budget_id?: string[] }