Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
chore: Removed migrationId and project schema.
  • Loading branch information
hanoak20 committed Jan 29, 2024
commit 914e04bdf1e37c3cf497c21f0fe77348aa9ac744
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;
78 changes: 21 additions & 57 deletions src/services/projects.migrations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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";

Expand All @@ -23,7 +23,6 @@ const _getProject = async (projectId: string, query: MigrationQueryType) => {

const _getCondensedMigration = (projectId: string, data: any) => {
return {
id: data._id,
name: data.name,
description: data.description,
created_at: data.created_at,
Expand All @@ -48,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 @@ -69,14 +66,13 @@ const createMigration = async (req: Request) => {
owner: token_payload?.user_id,
});

if (project.migration?.length)
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 @@ -91,77 +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"),
"updateMigration"
);

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"),
"updateMigrationLegacyCMS"
);

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 @@ -174,24 +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"),
"updateMigrationFileFormat"
);

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 @@ -206,27 +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"),
"deleteMigration"
);

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