diff --git a/src/config/index.ts b/src/config/index.ts index cf405bcd..93b1575f 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -11,6 +11,7 @@ export type ConfigType = { APP_TOKEN_EXP: string; APP_TOKEN_KEY: string; PORT: string; + APP_ENV: string; MONGODB_URI: string; CS_API: { US: string; @@ -23,6 +24,7 @@ export type ConfigType = { export const config: ConfigType = { APP_TOKEN_EXP: "1d", PORT: process.env.PORT!, + APP_ENV: process.env.NODE_ENV!, APP_TOKEN_KEY: process.env.APP_TOKEN_KEY!, MONGODB_URI: process.env.MONGODB_URI!, ...(process.env.NODE_ENV === "production" ? prodConfig : devConfig), diff --git a/src/constants/index.ts b/src/constants/index.ts index 66d27df4..9a936cd2 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -11,6 +11,7 @@ export type HttpErrorCodes = { SERVER_ERROR: number; }; export type ConstantType = { + CS_REGIONS: Array; AXIOS_TIMEOUT: number; HTTP_CODES: HttpErrorCodes; HTTP_TEXTS: HttpErrorTexts; @@ -35,6 +36,7 @@ export type HttpResponseHeaders = { }; export const constants: ConstantType = { + CS_REGIONS: ["US", "EU", "AZURE_NA", "AZURE_EU"], AXIOS_TIMEOUT: 60 * 1000, HTTP_CODES: { OK: 200, diff --git a/src/database.ts b/src/database.ts index 1c8bc829..a30d7997 100644 --- a/src/database.ts +++ b/src/database.ts @@ -2,18 +2,20 @@ import mongoose from "mongoose"; import { config } from "./config"; import logger from "./utils/logger"; -import MigrationModel from "./models/migration"; -import AuthenticationModel from "./models/authenticationLog"; +import ProjectModel from "./models/project"; +import AuthenticationModel from "./models/authentication"; import AuditLogModel from "./models/auditLog"; const connectToDatabase = async () => { try { - await mongoose.connect(config.MONGODB_URI); + await mongoose.connect(config.MONGODB_URI, { + ...(config.APP_ENV === "production" ? { autoIndex: false } : {}), + }); logger.info("Connected to MongoDB"); // Create the collection's if it doesn't exist - await MigrationModel.init(); + await ProjectModel.init(); await AuthenticationModel.init(); await AuditLogModel.init(); } catch (error) { diff --git a/src/models/authentication.ts b/src/models/authentication.ts new file mode 100644 index 00000000..59a50b91 --- /dev/null +++ b/src/models/authentication.ts @@ -0,0 +1,26 @@ +// src/models/Authentication.ts + +import { Schema, model, Document } from "mongoose"; +import { constants } from "../constants"; + +interface AuthenticationDocument extends Document { + user_id: string; + region: string; + authtoken: string; +} + +const authenticationSchema = new Schema( + { + user_id: { type: String, required: true }, + region: { type: String, required: true, enum: constants.CS_REGIONS }, + authtoken: { type: String, required: true }, + }, + { timestamps: { createdAt: "created_at", updatedAt: "updated_at" } } +); + +const AuthenticationModel = model( + "Authentication", + authenticationSchema +); + +export default AuthenticationModel; diff --git a/src/models/authenticationLog.ts b/src/models/authenticationLog.ts deleted file mode 100644 index 638b5674..00000000 --- a/src/models/authenticationLog.ts +++ /dev/null @@ -1,36 +0,0 @@ -// src/models/Authentication.ts - -import { Schema, model, Document } from "mongoose"; - -// Disabling this error until API's being implemented -// eslint-disable-next-line @typescript-eslint/no-unused-vars -interface Authentication { - user_id: string; - region: string; - authtoken: string; - created_at: string; - modified_at: string; -} - -interface AuthenticationDocument extends Document { - user_id: string; - region: string; - authtoken: string; - created_at: string; - modified_at: string; -} - -const authenticationSchema = new Schema({ - user_id: { type: String, required: true }, - region: { type: String, required: true }, - authtoken: { type: String, required: true }, - created_at: { type: String, required: true }, - modified_at: { type: String, required: true }, -}); - -const AuthenticationModel = model( - "Authentication", - authenticationSchema -); - -export default AuthenticationModel; diff --git a/src/models/migration.ts b/src/models/migration.ts deleted file mode 100644 index 09a79748..00000000 --- a/src/models/migration.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { Schema, model, Document } from "mongoose"; - -interface LegacyCMS { - cms: string; - file_format: string; - import_data: string; -} - -interface DestinationCMS { - stack_id: string; - org_id: string; -} - -interface Modules { - legacy_cms: LegacyCMS; - destination_cms: DestinationCMS; -} - -interface Migration { - name: string; - description: string; - modified_at: string; - modules: Modules; -} - -interface ExecutionLog { - log_url: string; -} - -interface MigrationDocument extends Document { - id: string; - region: string; - org_id: string; - owner: string; - created_by: string; - name: string; - description: string; - created_at: string; - modified_at: string; - status: boolean; - migration: Migration; - execution_log: ExecutionLog; -} - -const migrationSchema = new Schema({ - id: { type: String, required: true }, - region: { type: String, required: true }, - org_id: { type: String, required: true }, - owner: { type: String, required: true }, - created_by: { type: String, required: true }, - name: { type: String, required: true }, - description: { type: String, required: true }, - created_at: { type: String, required: true }, - modified_at: { type: String, required: true }, - status: { type: Boolean, required: true }, - migration: { - name: { type: String, required: true }, - description: { type: String, required: true }, - modified_at: { type: String, required: true }, - modules: { - legacy_cms: { - cms: { type: String, required: true }, - file_format: { type: String, required: true }, - import_data: { type: String, required: true }, - }, - destination_cms: { - stack_id: { type: String, required: true }, - org_id: { type: String, required: true }, - }, - }, - }, - execution_log: { - log_url: { type: String, required: true }, - }, -}); - -const MigrationModel = model("Migration", migrationSchema); - -export default MigrationModel; diff --git a/src/models/project.ts b/src/models/project.ts new file mode 100644 index 00000000..60738430 --- /dev/null +++ b/src/models/project.ts @@ -0,0 +1,75 @@ +import { Schema, model, Document } from "mongoose"; +import { constants } from "../constants"; + +interface LegacyCMS { + cms: string; + file_format: string; + import_data: string; +} + +interface DestinationCMS { + stack_id: string; + org_id: string; +} + +interface Modules { + legacy_cms: LegacyCMS; + destination_cms: DestinationCMS; +} + +interface Migration { + name: string; + description: string; + modules: Modules; +} + +interface ExecutionLog { + log_url: string; +} + +interface ProjectDocument extends Document { + region: string; + org_id: string; + owner: string; + created_by: string; + name: string; + description: string; + status: boolean; + migration: Migration; + execution_log: ExecutionLog; +} + +const projectSchema = new Schema( + { + region: { type: String, required: true, enum: constants.CS_REGIONS }, + org_id: { type: String, required: true }, + owner: { type: String, required: true }, + created_by: { type: String, required: true }, + name: { type: String, required: true }, + description: { type: String, required: true }, + status: { type: Boolean, default: true }, + migration: { + name: { type: String }, + description: { type: String }, + 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 }, + }, + }, + { timestamps: { createdAt: "created_at", updatedAt: "updated_at" } } +); + +const ProjectModel = model("Project", projectSchema); + +export default ProjectModel;