feat(medusa,types): add promotion list/get endpoint (#6110)
what: - adds get promotion endpoint (RESOLVES CORE-1677) - adds list promotions endpoint (RESOLVES CORE-1676) - uses new API routes
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
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 promotion = await promotionModuleService.retrieve(req.params.id, {
|
||||
select: req.retrieveConfig.select,
|
||||
relations: req.retrieveConfig.relations,
|
||||
})
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
@@ -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 {
|
||||
AdminGetPromotionsParams,
|
||||
AdminGetPromotionsPromotionParams,
|
||||
} from "./validators"
|
||||
|
||||
export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
matcher: "/admin/promotions*",
|
||||
middlewares: [isFeatureFlagEnabled(MedusaV2Flag.key)],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/promotions",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetPromotionsParams,
|
||||
QueryConfig.listTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/promotions/:id",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetPromotionsPromotionParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,26 @@
|
||||
export const defaultAdminPromotionRelations = ["campaign", "application_method"]
|
||||
export const allowedAdminPromotionRelations = [
|
||||
...defaultAdminPromotionRelations,
|
||||
]
|
||||
export const defaultAdminPromotionFields = [
|
||||
"id",
|
||||
"code",
|
||||
"campaign",
|
||||
"is_automatic",
|
||||
"type",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultAdminPromotionFields,
|
||||
defaultRelations: defaultAdminPromotionRelations,
|
||||
allowedRelations: allowedAdminPromotionRelations,
|
||||
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 [promotions, count] = await promotionModuleService.listAndCount(
|
||||
req.filterableFields,
|
||||
req.listConfig
|
||||
)
|
||||
|
||||
const { limit, offset } = req.validatedQuery
|
||||
|
||||
res.json({
|
||||
count,
|
||||
promotions,
|
||||
offset,
|
||||
limit,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { IsOptional, IsString } from "class-validator"
|
||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
||||
|
||||
export class AdminGetPromotionsPromotionParams extends FindParams {}
|
||||
|
||||
export class AdminGetPromotionsParams extends extendedFindParamsMixin({
|
||||
limit: 100,
|
||||
offset: 0,
|
||||
}) {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
code?: string
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { MiddlewaresConfig } from "../loaders/helpers/routing/types"
|
||||
import { adminPromotionRoutesMiddlewares } from "./admin/promotions/middlewares"
|
||||
|
||||
export const config: MiddlewaresConfig = {
|
||||
routes: [...adminPromotionRoutesMiddlewares],
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { AwilixContainer } from "awilix"
|
||||
import bodyParser from "body-parser"
|
||||
import { Express } from "express"
|
||||
import qs from "qs"
|
||||
import bodyParser from "body-parser"
|
||||
import routes from "../api"
|
||||
import { AwilixContainer } from "awilix"
|
||||
import { ConfigModule } from "../types/global"
|
||||
|
||||
type Options = {
|
||||
|
||||
@@ -13,6 +13,7 @@ import { asValue } from "awilix"
|
||||
import { createMedusaContainer } from "medusa-core-utils"
|
||||
import { track } from "medusa-telemetry"
|
||||
import { EOL } from "os"
|
||||
import path from "path"
|
||||
import requestIp from "request-ip"
|
||||
import { Connection } from "typeorm"
|
||||
import { MedusaContainer } from "../types/global"
|
||||
@@ -21,6 +22,7 @@ import loadConfig from "./config"
|
||||
import defaultsLoader from "./defaults"
|
||||
import expressLoader from "./express"
|
||||
import featureFlagsLoader from "./feature-flags"
|
||||
import { RoutesLoader } from "./helpers/routing"
|
||||
import Logger from "./logger"
|
||||
import loadMedusaApp, { mergeDefaultModules } from "./medusa-app"
|
||||
import modelsLoader from "./models"
|
||||
@@ -195,6 +197,22 @@ export default async ({
|
||||
next()
|
||||
})
|
||||
|
||||
// TODO: Figure out why this is causing issues with test when placed inside ./api.ts
|
||||
// Adding this here temporarily
|
||||
// Test: (packages/medusa/src/api/routes/admin/currencies/update-currency.ts)
|
||||
try {
|
||||
/**
|
||||
* Register the Medusa CORE API routes using the file based routing.
|
||||
*/
|
||||
await new RoutesLoader({
|
||||
app: expressApp,
|
||||
rootDir: path.join(__dirname, "../api-v2"),
|
||||
configModule,
|
||||
}).load()
|
||||
} catch (err) {
|
||||
throw Error("An error occurred while registering Medusa Core API Routes")
|
||||
}
|
||||
|
||||
const pluginsActivity = Logger.activity(`Initializing plugins${EOL}`)
|
||||
track("PLUGINS_INIT_STARTED")
|
||||
await pluginsLoader({
|
||||
|
||||
Reference in New Issue
Block a user