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
59 changes: 1 addition & 58 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,4 @@
export type HttpErrorCodes = {
OK: number;
FORBIDDEN: number;
BAD_REQUEST: number;
NOT_FOUND: number;
UNAUTHORIZED: number;
TOO_MANY_REQS: number;
SOMETHING_WRONG: number;
MOVED_PERMANENTLY: number;
SUPPORT_DOC: number;
SERVER_ERROR: number;
UNPROCESSABLE_CONTENT: number;
};

export type ValidationErrors = {
INVALID_EMAIL: string;
EMAIL_LIMIT: string;
LENGTH_LIMIT: string;
STRING_REQUIRED: string;
INVALID_REGION: string;
};

export type ConstantType = {
CS_REGIONS: Array<string>;
AXIOS_TIMEOUT: number;
HTTP_CODES: HttpErrorCodes;
HTTP_TEXTS: HttpErrorTexts;
HTTP_RESPONSE_HEADERS: HttpResponseHeaders;
METHODS_TO_INCLUDE_DATA_IN_AXIOS: Array<string>;
VALIDATION_ERRORS: ValidationErrors;
};

export type HttpErrorTexts = {
UNAUTHORIZED: string;
INTERNAL_ERROR: string;
SOMETHING_WENT_WRONG: string;
NO_CS_USER: string;
SUCCESS_LOGIN: string;
TOKEN_ERROR: string;
LOGIN_ERROR: string;
ROUTE_ERROR: string;
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;
};

export type HttpResponseHeaders = {
"Access-Control-Allow-Origin": string;
"Content-Type": string;
Connection: string;
};

export const constants: ConstantType = {
export const constants = {
CS_REGIONS: ["US", "EU", "AZURE_NA", "AZURE_EU"],
AXIOS_TIMEOUT: 60 * 1000,
HTTP_CODES: {
Expand Down
1 change: 1 addition & 0 deletions src/middlewares/error.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const errorMiddleware = (
logger.error(err.stack);

if (err instanceof AppError) {
logger.error(`Error in method: ${err.srcFunc}`);
res
.status(err.statusCode)
.json({ error: { code: err.statusCode, message: err.message } });
Expand Down
36 changes: 16 additions & 20 deletions src/models/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ interface Modules {
}

interface Migration {
_id: Schema.Types.ObjectId;
name: string;
description: string;
created_at: Date;
Expand All @@ -38,7 +37,7 @@ interface ProjectDocument extends Document {
name: string;
description: string;
status: boolean;
migration: [Migration];
migration: Migration;
execution_log: ExecutionLog;
}

Expand All @@ -51,26 +50,23 @@ const projectSchema = new Schema<ProjectDocument>(
name: { type: String, required: true },
description: { type: String, required: true },
status: { type: Boolean, default: true },
migration: [
{
_id: Schema.Types.ObjectId,
name: { type: String },
description: { type: String },
created_at: { type: Date },
updated_at: { type: Date },
modules: {
legacy_cms: {
cms: { type: String },
file_format: { type: String },
import_data: { type: String },
},
destination_cms: {
stack_id: { type: String },
org_id: { type: String },
},
migration: {
name: { type: String },
description: { type: String },
created_at: { type: Date },
updated_at: { type: Date },
modules: {
legacy_cms: {
cms: { type: String },
file_format: { type: String },
import_data: { type: String },
},
destination_cms: {
stack_id: { type: String },
org_id: { type: String },
},
},
],
},
execution_log: {
log_url: { type: String },
},
Expand Down
1 change: 0 additions & 1 deletion src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export interface LoginServiceType {

export interface MigrationQueryType {
_id: string;
"migration._id"?: string;
org_id: string;
region: string;
owner: string;
Expand Down
11 changes: 4 additions & 7 deletions src/routes/projects.migrations.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,26 @@ router.post(

// Update project's migration route
router.put(
"/:migrationId",
"/",
validator("project"),
asyncRouter(migrationController.updateMigration)
);

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

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

// Delete project's migration route
router.delete(
"/:migrationId",
asyncRouter(migrationController.deleteMigration)
);
router.delete("/", asyncRouter(migrationController.deleteMigration));

export default router;
7 changes: 5 additions & 2 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const login = async (req: Request): Promise<LoginServiceType> => {
};

if (!res?.data?.user)
throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER);
throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER, "login");

const appTokenPayload: AppTokenPayload = {
region: userData?.region,
Expand Down Expand Up @@ -106,7 +106,10 @@ const requestSms = async (req: Request): Promise<LoginServiceType> => {
status: res.status,
};
} catch (error) {
throw new InternalServerError();
throw new InternalServerError(
constants.HTTP_TEXTS.INTERNAL_ERROR,
"requestSms"
);
}
};

Expand Down
85 changes: 29 additions & 56 deletions src/services/projects.migrations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@
import { Request } from "express";
import { constants } from "../constants";
import ProjectModel from "../models/project";
import { isValidObjectId, getMongooseID } from "../utils";
import { isValidObjectId } from "../utils";
import { NotFoundError, BadRequestError } from "../utils/custom-errors.utils";
import { MigrationQueryType } from "../models/types";

const _getProject = async (projectId: string, query: MigrationQueryType) => {
if (!isValidObjectId(projectId))
throw new BadRequestError(
constants.HTTP_TEXTS.INVALID_ID.replace("$", "project")
constants.HTTP_TEXTS.INVALID_ID.replace("$", "project"),
"_getProject"
);

const project = await ProjectModel.findOne(query);

if (!project) throw new NotFoundError(constants.HTTP_TEXTS.NO_PROJECT);
if (!project)
throw new NotFoundError(constants.HTTP_TEXTS.NO_PROJECT, "_getProject");

return project;
};

const _getCondensedMigration = (projectId: string, data: any) => {
return {
id: data._id,
name: data.name,
description: data.description,
created_at: data.created_at,
Expand All @@ -46,11 +47,9 @@ const getMigration = async (req: Request) => {
return {
status: constants.HTTP_CODES.OK,
data: {
migration: [
project.migration?.[0]?._id
? _getCondensedMigration(projectId, project.migration[0])
: {},
],
migration: project?.migration?.name
? _getCondensedMigration(projectId, project.migration)
: {},
},
};
};
Expand All @@ -67,11 +66,13 @@ const createMigration = async (req: Request) => {
owner: token_payload?.user_id,
});

if (project.migration?.length)
throw new BadRequestError(constants.HTTP_TEXTS.MIGRATION_EXISTS);
if (project.migration?.name)
throw new BadRequestError(
constants.HTTP_TEXTS.MIGRATION_EXISTS,
"createMigration"
);

project.migration.push({
_id: getMongooseID(),
project.migration = {
created_at: new Date(),
updated_at: new Date(),
...req?.body,
Expand All @@ -86,75 +87,58 @@ const createMigration = async (req: Request) => {
org_id: "",
},
},
});
};

const updatedProject = await project.save();

return {
status: constants.HTTP_CODES.OK,
data: {
message: constants.HTTP_TEXTS.MIGRATION_CREATED,
migration: [
_getCondensedMigration(projectId, updatedProject.migration[0]),
],
migration: _getCondensedMigration(projectId, updatedProject.migration),
},
};
};

const updateMigration = async (req: Request) => {
const orgId = req?.params?.orgId;
const projectId = req?.params?.projectId;
const migrationId = req?.params?.migrationId;
const { token_payload, name, description } = 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].name = name;
project.migration[0].description = description;
project.migration[0].updated_at = new Date();
project.migration.name = name;
project.migration.description = description;
project.migration.updated_at = new Date();

const updatedProject = await project.save();

return {
status: constants.HTTP_CODES.OK,
data: {
message: constants.HTTP_TEXTS.MIGRATION_UPDATED,
migration: [
_getCondensedMigration(projectId, updatedProject.migration[0]),
],
migration: _getCondensedMigration(projectId, updatedProject.migration),
},
};
};

const updateMigrationLegacyCMS = async (req: Request) => {
const { orgId, projectId, migrationId } = req.params;
const { orgId, projectId } = 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;
project.migration.modules.legacy_cms.cms = legacy_cms;

await project.save();

Expand All @@ -167,23 +151,17 @@ const updateMigrationLegacyCMS = async (req: Request) => {
};

const updateMigrationFileFormat = async (req: Request) => {
const { orgId, projectId, migrationId } = req.params;
const { orgId, projectId } = 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;
project.migration.modules.legacy_cms.file_format = file_format;

await project.save();

Expand All @@ -198,26 +176,21 @@ const updateMigrationFileFormat = async (req: Request) => {
const deleteMigration = async (req: Request) => {
const orgId = req?.params?.orgId;
const projectId = req?.params?.projectId;
const migrationId = req?.params?.migrationId;
const { token_payload } = req.body;

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

const filter = {
_id: projectId,
"migration._id": migrationId,
org_id: orgId,
region: token_payload?.region,
owner: token_payload?.user_id,
};

const project = await _getProject(projectId, filter);
await _getProject(projectId, filter);

project.migration.shift();
await project.save();
await ProjectModel.updateOne(
{ _id: projectId },
{ $set: { migration: null } }
);

return {
status: constants.HTTP_CODES.OK,
Expand Down
Loading