-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
765 additions
and
60 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
integration-tests/plugins/__tests__/promotion/admin/list-promotions.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { IPromotionModuleService } from "@medusajs/types" | ||
import { PromotionType } 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" | ||
|
||
const env = { MEDUSA_FF_MEDUSA_V2: true } | ||
const adminHeaders = { | ||
headers: { "x-medusa-access-token": "test_token" }, | ||
} | ||
|
||
describe("GET /admin/promotions", () => { | ||
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() | ||
}) | ||
|
||
it("should get all promotions and its count", async () => { | ||
await promotionModuleService.create([ | ||
{ | ||
code: "TEST", | ||
type: PromotionType.STANDARD, | ||
application_method: { | ||
type: "fixed", | ||
target_type: "order", | ||
value: "100", | ||
}, | ||
}, | ||
]) | ||
|
||
const api = useApi() as any | ||
const response = await api.get(`/admin/promotions`, adminHeaders) | ||
|
||
expect(response.status).toEqual(200) | ||
expect(response.data.count).toEqual(1) | ||
expect(response.data.promotions).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), | ||
value: 100, | ||
type: "fixed", | ||
target_type: "order", | ||
allocation: null, | ||
created_at: expect.any(String), | ||
updated_at: expect.any(String), | ||
deleted_at: null, | ||
}), | ||
}), | ||
]) | ||
}) | ||
|
||
it("should get all promotions and its count filtered", async () => { | ||
const [createdPromotion] = await promotionModuleService.create([ | ||
{ | ||
code: "TEST", | ||
type: PromotionType.STANDARD, | ||
application_method: { | ||
type: "fixed", | ||
target_type: "order", | ||
value: "100", | ||
}, | ||
}, | ||
]) | ||
|
||
const api = useApi() as any | ||
const response = await api.get( | ||
`/admin/promotions?fields=code,created_at,application_method.id`, | ||
adminHeaders | ||
) | ||
|
||
expect(response.status).toEqual(200) | ||
expect(response.data.count).toEqual(1) | ||
expect(response.data.promotions).toEqual([ | ||
{ | ||
id: expect.any(String), | ||
code: "TEST", | ||
created_at: expect.any(String), | ||
application_method: { | ||
id: expect.any(String), | ||
promotion: expect.any(Object), | ||
}, | ||
}, | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { PromotionDTO } from "@medusajs/types" | ||
import { MedusaV2Flag, wrapHandler } from "@medusajs/utils" | ||
import { Express, Router } from "express" | ||
import { PaginatedResponse } from "../../../../types/common" | ||
import { transformQuery } from "../../../middlewares" | ||
import { isFeatureFlagEnabled } from "../../../middlewares/feature-flag-enabled" | ||
import listPromotions, { AdminGetPromotionsParams } from "./list-promotions" | ||
|
||
const route = Router() | ||
|
||
export default (app: Express) => { | ||
const retrieveTransformQueryConfig = { | ||
defaultFields: defaultPromotionFields, | ||
defaultRelations: defaultAdminPromotionRelations, | ||
allowedRelations: allowedAdminPromotionRelations, | ||
isList: false, | ||
} | ||
|
||
const listTransformQueryConfig = { | ||
...retrieveTransformQueryConfig, | ||
isList: true, | ||
} | ||
|
||
app.use("/promotions", isFeatureFlagEnabled(MedusaV2Flag.key), route) | ||
|
||
route.get( | ||
"/", | ||
transformQuery(AdminGetPromotionsParams, listTransformQueryConfig), | ||
wrapHandler(listPromotions) | ||
) | ||
|
||
return app | ||
} | ||
|
||
export const defaultAdminPromotionRelations = ["campaign", "application_method"] | ||
export const allowedAdminPromotionRelations = [ | ||
...defaultAdminPromotionRelations, | ||
] | ||
export const defaultPromotionFields = [ | ||
"id", | ||
"code", | ||
"campaign", | ||
"is_automatic", | ||
"type", | ||
"created_at", | ||
"updated_at", | ||
"deleted_at", | ||
] | ||
|
||
export type AdminPromotionsListRes = PaginatedResponse & { | ||
promotions: PromotionDTO[] | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/medusa/src/api/routes/admin/promotions/list-promotions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { IPromotionModuleService } from "@medusajs/types" | ||
import { IsOptional, IsString } from "class-validator" | ||
import { Request, Response } from "express" | ||
import { extendedFindParamsMixin } from "../../../../types/common" | ||
|
||
export default async (req: Request, res: Response) => { | ||
const promotionModuleService: IPromotionModuleService = req.scope.resolve( | ||
"promotionModuleService" | ||
) | ||
|
||
const [promotions, count] = await promotionModuleService.listAndCount( | ||
req.filterableFields, | ||
req.listConfig | ||
) | ||
console.log("running here - ", promotions) | ||
const { limit, offset } = req.validatedQuery | ||
|
||
res.json({ | ||
count, | ||
promotions, | ||
offset, | ||
limit, | ||
}) | ||
} | ||
|
||
export class AdminGetPromotionsParams extends extendedFindParamsMixin({ | ||
limit: 100, | ||
offset: 0, | ||
}) { | ||
@IsString() | ||
@IsOptional() | ||
code?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.