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
4 changes: 4 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export type HttpErrorTexts = {
NO_PROJECT: string;
MIGRATION_CREATED: string;
MIGRATION_UPDATED: string;
CMS_UPDATED: string;
FILE_FORMAT_UPDATED: string;
MIGRATION_DELETED: string;
INVALID_ID: string;
MIGRATION_EXISTS: string;
Expand Down Expand Up @@ -82,6 +84,8 @@ export const constants: ConstantType = {
NO_PROJECT: "resource not found with the given ID(s).",
MIGRATION_CREATED: "Project's migration created successfully.",
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",
MIGRATION_DELETED: "Project's migration deleted successfully.",
INVALID_ID: "Provided $ ID is invalid.",
MIGRATION_EXISTS: "Project's migration already exists.",
Expand Down
12 changes: 12 additions & 0 deletions src/controllers/projects.migrations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ const updateMigration = async (req: Request, res: Response) => {
res.status(resp.status).json(resp.data);
};

const updateMigrationLegacyCMS = async (req: Request, res: Response) => {
const resp = await migrationService.updateMigrationLegacyCMS(req);
res.status(resp.status).json(resp.data);
};

const updateMigrationFileFormat = async (req: Request, res: Response) => {
const resp = await migrationService.updateMigrationFileFormat(req);
res.status(resp.status).json(resp.data);
};

const deleteMigration = async (req: Request, res: Response) => {
const resp = await migrationService.deleteMigration(req);
res.status(resp.status).json(resp.data);
Expand All @@ -25,4 +35,6 @@ export const migrationController = {
createMigration,
updateMigration,
deleteMigration,
updateMigrationLegacyCMS,
updateMigrationFileFormat,
};
14 changes: 14 additions & 0 deletions src/routes/projects.migrations.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ router.put(
asyncRouter(migrationController.updateMigration)
);

// Update project's legacy-cms
router.put(
"/:migrationId/legacy-cms",
validator("cms"),
asyncRouter(migrationController.updateMigrationLegacyCMS)
);

// Update project's file format
router.put(
"/:migrationId/file-format",
validator("file_format"),
asyncRouter(migrationController.updateMigrationFileFormat)
);

// Delete project's migration route
router.delete(
"/:migrationId",
Expand Down
71 changes: 71 additions & 0 deletions src/services/projects.migrations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ const createMigration = async (req: Request) => {
created_at: new Date(),
updated_at: new Date(),
...req?.body,
modules: {
legacy_cms: {
cms: "",
file_format: "",
import_data: "",
},
destination_cms: {
stack_id: "",
org_id: "",
},
},
});

const updatedProject = await project.save();
Expand Down Expand Up @@ -126,6 +137,64 @@ const updateMigration = async (req: Request) => {
};
};

const updateMigrationLegacyCMS = async (req: Request) => {
const { orgId, projectId, migrationId } = req.params;
const { token_payload, legacy_cms } = req.body;

if (!isValidObjectId(migrationId))
throw new BadRequestError(
constants.HTTP_TEXTS.INVALID_ID.replace("$", "migration")
);

const project = await _getProject(projectId, {
_id: projectId,
"migration._id": migrationId,
org_id: orgId,
region: token_payload?.region,
owner: token_payload?.user_id,
});

project.migration[0].modules.legacy_cms.cms = legacy_cms;

await project.save();

return {
status: constants.HTTP_CODES.OK,
data: {
message: constants.HTTP_TEXTS.CMS_UPDATED,
},
};
};

const updateMigrationFileFormat = async (req: Request) => {
const { orgId, projectId, migrationId } = req.params;
const { token_payload, file_format } = req.body;

if (!isValidObjectId(migrationId))
throw new BadRequestError(
constants.HTTP_TEXTS.INVALID_ID.replace("$", "migration")
);

const project = await _getProject(projectId, {
_id: projectId,
"migration._id": migrationId,
org_id: orgId,
region: token_payload?.region,
owner: token_payload?.user_id,
});

project.migration[0].modules.legacy_cms.file_format = file_format;

await project.save();

return {
status: constants.HTTP_CODES.OK,
data: {
message: constants.HTTP_TEXTS.FILE_FORMAT_UPDATED,
},
};
};

const deleteMigration = async (req: Request) => {
const orgId = req?.params?.orgId;
const projectId = req?.params?.projectId;
Expand Down Expand Up @@ -164,4 +233,6 @@ export const migrationService = {
createMigration,
updateMigration,
deleteMigration,
updateMigrationLegacyCMS,
updateMigrationFileFormat,
};
27 changes: 27 additions & 0 deletions src/validators/cms.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { checkSchema } from "express-validator";
import { constants } from "../constants";

export default checkSchema({
legacy_cms: {
in: "body",
isString: {
errorMessage: constants.VALIDATION_ERRORS.STRING_REQUIRED.replace(
"$",
"legacy_cms"
),
bail: true,
},
trim: true,
isLength: {
errorMessage: constants.VALIDATION_ERRORS.LENGTH_LIMIT.replace(
"$",
"legacy_cms"
),
options: {
min: 1,
max: 200,
},
bail: true,
},
},
});
27 changes: 27 additions & 0 deletions src/validators/file-format.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { checkSchema } from "express-validator";
import { constants } from "../constants";

export default checkSchema({
file_format: {
in: "body",
isString: {
errorMessage: constants.VALIDATION_ERRORS.STRING_REQUIRED.replace(
"$",
"file_format"
),
bail: true,
},
trim: true,
isLength: {
errorMessage: constants.VALIDATION_ERRORS.LENGTH_LIMIT.replace(
"$",
"file_format"
),
options: {
min: 1,
max: 200,
},
bail: true,
},
},
});
4 changes: 4 additions & 0 deletions src/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { ValidationError } from "../utils/custom-errors.utils";
import { asyncRouter } from "../utils/async-router.utils";
import authValidator from "./auth.validator";
import projectValidator from "./project.validator";
import cmsValidator from "./cms.validator";
import fileFormatValidator from "./file-format.validator";

export default (route: string = "") =>
asyncRouter(async (req: Request, res: Response, next: NextFunction) => {
const appValidators = {
auth: authValidator,
project: projectValidator,
cms: cmsValidator,
file_format: fileFormatValidator,
};

const validator = appValidators[route as keyof typeof appValidators];
Expand Down