Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/projects.migrations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand All @@ -42,5 +42,5 @@ export const migrationController = {
deleteMigration,
updateMigrationLegacyCMS,
updateMigrationFileFormat,
updateMigrationDestinationCMS,
updateMigrationDestinationStack,
};
6 changes: 3 additions & 3 deletions src/routes/projects.migrations.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 39 additions & 3 deletions src/services/projects.migrations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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,
},
};
};
Expand Down Expand Up @@ -232,5 +268,5 @@ export const migrationService = {
deleteMigration,
updateMigrationLegacyCMS,
updateMigrationFileFormat,
updateMigrationDestinationCMS,
updateMigrationDestinationStack,
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default checkSchema({
),
options: {
min: 1,
max: 400,
max: 100,
},
bail: true,
},
Expand Down
4 changes: 2 additions & 2 deletions src/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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];
Expand Down