diff --git a/controllers/featureFlags.ts b/controllers/featureFlags.ts index 96aa91819..009dce290 100644 --- a/controllers/featureFlags.ts +++ b/controllers/featureFlags.ts @@ -72,7 +72,6 @@ export const createFeatureFlag = async (req: CustomRequest, res: CustomResponse) export const updateFeatureFlag = async (req: CustomRequest, res: CustomResponse) => { try { const { flagId } = req.params; - console.log("Mehulllll", flagId) const updateData: UpdateFeatureFlagRequestBody = { Status: req.body.Status, UserId: req.body.UserId, diff --git a/middlewares/validators/featureFlag.ts b/middlewares/validators/featureFlag.ts index b0c113f91..fa3e9ceb9 100644 --- a/middlewares/validators/featureFlag.ts +++ b/middlewares/validators/featureFlag.ts @@ -25,4 +25,32 @@ export const validateUpdateFeatureFlag = async (req: Request, res: CustomRespons logger.error(`Error validating update feature flag payload: ${error.message}`); res.boom.badRequest(error.message); } -}; \ No newline at end of file +}; + +const createFeatureFlagSchema = Joi.object({ + Name: Joi.string() + .required() + .messages({ + 'any.required': 'Name is required' + }), + Description: Joi.string() + .required() + .messages({ + 'any.required': 'Description is required' + }), + UserId: Joi.string() + .required() + .messages({ + 'any.required': 'UserId is required' + }) +}); + +export const validateCreateFeatureFlag = async (req: Request, res: CustomResponse, next: NextFunction) => { + try { + await createFeatureFlagSchema.validateAsync(req.body); + next(); + } catch (error) { + logger.error(`Error validating create feature flag payload: ${error.message}`); + res.boom.badRequest(error.message); + } +}; \ No newline at end of file diff --git a/routes/featureFlag.ts b/routes/featureFlag.ts index 6a219b020..a5caabb85 100644 --- a/routes/featureFlag.ts +++ b/routes/featureFlag.ts @@ -2,13 +2,13 @@ import express from "express"; const router = express.Router(); import authenticate from "../middlewares/authenticate"; const authorizeRoles = require("../middlewares/authorizeRoles"); -import { createFeatureFlag, getAllFeatureFlags, getFeatureFlagById, updateFeatureFlag} from "../controllers/featureFlags"; +import { createFeatureFlag, getAllFeatureFlags, getFeatureFlagById, updateFeatureFlag } from "../controllers/featureFlags"; const { SUPERUSER } = require("../constants/roles"); -import { validateUpdateFeatureFlag } from '../middlewares/validators/featureFlag'; +import { validateUpdateFeatureFlag, validateCreateFeatureFlag } from '../middlewares/validators/featureFlag'; router.get("/getAllFeatureFlags", authenticate, getAllFeatureFlags); router.get("/getFeatureFlag/:flagId", authenticate, getFeatureFlagById); -router.post('/createFeatureFlag', authenticate, authorizeRoles([SUPERUSER]), createFeatureFlag); +router.post('/createFeatureFlag', authenticate, authorizeRoles([SUPERUSER]), validateCreateFeatureFlag, createFeatureFlag); router.patch('/updateFeatureFlag/:flagId', authenticate, authorizeRoles([SUPERUSER]), validateUpdateFeatureFlag, updateFeatureFlag); -module.exports = router; +module.exports = router; \ No newline at end of file diff --git a/services/featureFlagService.ts b/services/featureFlagService.ts index bedeaf3a4..748dd4dac 100644 --- a/services/featureFlagService.ts +++ b/services/featureFlagService.ts @@ -62,12 +62,12 @@ const updateFeatureFlag = async ( updateData: UpdateFeatureFlagRequestBody ): Promise => { try { - const response = await fetch(`http://127.0.0.1:3000/feature-flags/${flagId}`, { + const response = await fetch(`${FEATURE_FLAG_BASE_URL}/feature-flags/${flagId}`, { method: "PATCH", headers: generateHeaders(), body: JSON.stringify(updateData), }); - console.log("Mehulllll", response) + if (!response.ok) { const error = await response.json(); return {