diff --git a/src/constants/index.ts b/src/constants/index.ts index d8e47b4c5..2132d3edb 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -30,8 +30,11 @@ export const constants = { MIGRATION_UPDATED: "Project's migration updated successfully.", CMS_UPDATED: "Project's migration cms updated successfully", FILE_FORMAT_UPDATED: "Project's migration file format updated successfully", - DESTINATION_CMS_UPDATED: - "Project's migration destination cms updated successfully", + DESTINATION_STACK_UPDATED: + "Project's migration destination stack updated successfully", + DESTINATION_STACK_NOT_FOUND: "Destination stack does not exist", + DESTINATION_STACK_ERROR: + "Error occurred during verifying destination stack", MIGRATION_DELETED: "Project's migration deleted successfully.", INVALID_ID: "Provided $ ID is invalid.", MIGRATION_EXISTS: "Project's migration already exists.", diff --git a/src/controllers/projects.migrations.controller.ts b/src/controllers/projects.migrations.controller.ts index 2479dcaab..3a6806d42 100644 --- a/src/controllers/projects.migrations.controller.ts +++ b/src/controllers/projects.migrations.controller.ts @@ -25,8 +25,8 @@ const updateMigrationFileFormat = async (req: Request, res: Response) => { res.status(resp.status).json(resp.data); }; -const updateMigrationDestinationCMS = async (req: Request, res: Response) => { - const resp = await migrationService.updateMigrationDestinationCMS(req); +const updateMigrationDestinationStack = async (req: Request, res: Response) => { + const resp = await migrationService.updateMigrationDestinationStack(req); res.status(resp.status).json(resp.data); }; @@ -42,5 +42,5 @@ export const migrationController = { deleteMigration, updateMigrationLegacyCMS, updateMigrationFileFormat, - updateMigrationDestinationCMS, + updateMigrationDestinationStack, }; diff --git a/src/routes/projects.migrations.routes.ts b/src/routes/projects.migrations.routes.ts index 9f792d0b4..5b504fb4b 100644 --- a/src/routes/projects.migrations.routes.ts +++ b/src/routes/projects.migrations.routes.ts @@ -38,9 +38,9 @@ router.put( // Update project's destination-cms router.put( - "/destination-cms", - validator("destination_cms"), - asyncRouter(migrationController.updateMigrationDestinationCMS) + "/destination-stack", + validator("destination_stack"), + asyncRouter(migrationController.updateMigrationDestinationStack) ); // Delete project's migration route diff --git a/src/services/projects.migrations.service.ts b/src/services/projects.migrations.service.ts index 88af8439c..a21eaf3c8 100644 --- a/src/services/projects.migrations.service.ts +++ b/src/services/projects.migrations.service.ts @@ -5,6 +5,10 @@ import ProjectModel from "../models/project"; import { isValidObjectId } from "../utils"; import { NotFoundError, BadRequestError } from "../utils/custom-errors.utils"; import { MigrationQueryType } from "../models/types"; +import { safePromise } from "../utils/index"; +import { config } from "../config"; +import getAuthtoken from "../utils/auth.utils"; +import https from "../utils/https.utils"; const _getProject = async (projectId: string, query: MigrationQueryType) => { if (!isValidObjectId(projectId)) @@ -173,7 +177,7 @@ const updateMigrationFileFormat = async (req: Request) => { }; }; -const updateMigrationDestinationCMS = async (req: Request) => { +const updateMigrationDestinationStack = async (req: Request) => { const { orgId, projectId } = req.params; const { token_payload, stack_api_key } = req.body; @@ -184,6 +188,38 @@ const updateMigrationDestinationCMS = async (req: Request) => { owner: token_payload?.user_id, }); + const authtoken = await getAuthtoken( + token_payload?.region, + token_payload?.user_id + ); + + const [err, res] = await safePromise( + https({ + method: "GET", + url: `${config.CS_API[ + token_payload?.region as keyof typeof config.CS_API + ]!}/stacks`, + headers: { + organization_uid: orgId, + authtoken, + }, + }) + ); + + if (err) + return { + data: { + message: constants.HTTP_TEXTS.DESTINATION_STACK_ERROR, + }, + status: err.response.status, + }; + + if (!res.data.stacks.find((stack: any) => stack.api_key === stack_api_key)) + throw new BadRequestError( + constants.HTTP_TEXTS.DESTINATION_STACK_NOT_FOUND, + "updateMigrationDestinationStack" + ); + project.migration.modules.destination_cms.stack_id = stack_api_key; project.migration.modules.destination_cms.org_id = orgId; @@ -192,7 +228,7 @@ const updateMigrationDestinationCMS = async (req: Request) => { return { status: constants.HTTP_CODES.OK, data: { - message: constants.HTTP_TEXTS.DESTINATION_CMS_UPDATED, + message: constants.HTTP_TEXTS.DESTINATION_STACK_UPDATED, }, }; }; @@ -232,5 +268,5 @@ export const migrationService = { deleteMigration, updateMigrationLegacyCMS, updateMigrationFileFormat, - updateMigrationDestinationCMS, + updateMigrationDestinationStack, }; diff --git a/src/validators/destination-cms.validator.ts b/src/validators/destination-stack.validator.ts similarity index 96% rename from src/validators/destination-cms.validator.ts rename to src/validators/destination-stack.validator.ts index 9e5ec498f..318afcfaf 100644 --- a/src/validators/destination-cms.validator.ts +++ b/src/validators/destination-stack.validator.ts @@ -19,7 +19,7 @@ export default checkSchema({ ), options: { min: 1, - max: 400, + max: 100, }, bail: true, }, diff --git a/src/validators/index.ts b/src/validators/index.ts index 76aca1747..5b6f166f3 100644 --- a/src/validators/index.ts +++ b/src/validators/index.ts @@ -5,7 +5,7 @@ import authValidator from "./auth.validator"; import projectValidator from "./project.validator"; import cmsValidator from "./cms.validator"; import fileFormatValidator from "./file-format.validator"; -import destinationCmsValidator from "./destination-cms.validator"; +import destinationStackValidator from "./destination-stack.validator"; export default (route: string = "") => asyncRouter(async (req: Request, res: Response, next: NextFunction) => { @@ -14,7 +14,7 @@ export default (route: string = "") => project: projectValidator, cms: cmsValidator, file_format: fileFormatValidator, - destination_cms: destinationCmsValidator, + destination_stack: destinationStackValidator, }; const validator = appValidators[route as keyof typeof appValidators];