Skip to content

Commit

Permalink
chore: use new api route
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed Jan 17, 2024
1 parent 7d77187 commit eb644e7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 61 deletions.
40 changes: 40 additions & 0 deletions packages/medusa/src/api-v2/admin/promotions/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { MedusaV2Flag } from "@medusajs/utils"
import { isFeatureFlagEnabled, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { AdminGetPromotionsParams } from "./route"

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",
]

const retrieveTransformQueryConfig = {
defaultFields: defaultPromotionFields,
defaultRelations: defaultAdminPromotionRelations,
allowedRelations: allowedAdminPromotionRelations,
isList: false,
}

const listTransformQueryConfig = {
...retrieveTransformQueryConfig,
isList: true,
}

export const promotionsRouteMiddleware: MiddlewareRoute = {
method: ["GET"],
matcher: "/admin/promotions*",
middlewares: [
isFeatureFlagEnabled(MedusaV2Flag.key),
transformQuery(AdminGetPromotionsParams, listTransformQueryConfig),
],
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { IsOptional, IsString } from "class-validator"
import { Request, Response } from "express"
import { extendedFindParamsMixin } from "../../../../types/common"
import { extendedFindParamsMixin } from "../../../types/common"
import { MedusaRequest, MedusaResponse } from "../../../types/routing"

export default async (req: Request, res: Response) => {
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const promotionModuleService: IPromotionModuleService = req.scope.resolve(
"promotionModuleService"
ModuleRegistrationName.PROMOTION
)

const [promotions, count] = await promotionModuleService.listAndCount(
req.filterableFields,
req.listConfig
req.filterableFields || {},
req.listConfig || {}
)

const { limit, offset } = req.validatedQuery
const { limit, offset } = req.validatedQuery || {}

res.json({
count,
Expand Down
6 changes: 6 additions & 0 deletions packages/medusa/src/api-v2/middlewares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MiddlewaresConfig } from "../loaders/helpers/routing/types"
import { promotionsRouteMiddleware } from "./admin/promotions/middleware"

export const config: MiddlewaresConfig = {
routes: [promotionsRouteMiddleware],
}
3 changes: 2 additions & 1 deletion packages/medusa/src/api/middlewares/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { default as requireCustomerAuthentication } from "./require-customer-aut

export { default as authenticate } from "./authenticate"
export { default as authenticateCustomer } from "./authenticate-customer"
export { default as errorHandler } from "./error-handler"
export { default as wrapHandler } from "./await-middleware"
export { canAccessBatchJob } from "./batch-job/can-access-batch-job"
export { getRequestedBatchJob } from "./batch-job/get-requested-batch-job"
export { doesConditionBelongToDiscount } from "./discount/does-condition-belong-to-discount"
export { default as errorHandler } from "./error-handler"
export { isFeatureFlagEnabled } from "./feature-flag-enabled"
export { default as normalizeQuery } from "./normalized-query"
export { default as requireCustomerAuthentication } from "./require-customer-authentication"
export { transformBody } from "./transform-body"
Expand Down
52 changes: 0 additions & 52 deletions packages/medusa/src/api/routes/admin/promotions/index.ts

This file was deleted.

10 changes: 9 additions & 1 deletion packages/medusa/src/loaders/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
promiseAll,
upperCaseFirst,
} from "@medusajs/utils"
import { aliasTo, asFunction, asValue, Lifetime } from "awilix"
import { Lifetime, aliasTo, asFunction, asValue } from "awilix"
import { Express } from "express"
import fs from "fs"
import { sync as existsSync } from "fs-exists-cached"
Expand Down Expand Up @@ -83,6 +83,14 @@ export default async ({
)
)

// Adds core api routes
await new RoutesLoader({
app,
rootDir: path.join(__dirname, "../api-v2"),
activityId: activityId,
configModule,
}).load()

await promiseAll(
resolved.map(async (pluginDetails) => {
registerRepositories(pluginDetails, container)
Expand Down

0 comments on commit eb644e7

Please sign in to comment.