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: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,6 @@ combine.log

package-lock.json

!example.env
!example.env

database/
5 changes: 5 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"watch": ["src"],
"ext": "ts",
"exec": "concurrently \"npx tsc --watch\" \"ts-node src/server.ts\""
}
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"name": "migration-v2-node-server",
"version": "1.0.0",
"description": "This is the TSO migration V2's node server",
"main": "index.js",
"exports": "./src/server.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npx tsc && NODE_ENV=production node dist/server.js",
"dev": "NODE_ENV=development ts-node-dev --respawn ./src/server.ts",
"build": "npx tsc",
"start": "NODE_ENV=production node dist/server.js",
"dev": "NODE_ENV=development tsx watch ./src/server.ts",
"precommit": "lint-staged --concurrent false"
},
"type": "commonjs",
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/contentstack-expert-services/migration-v2-node-server.git"
Expand All @@ -21,7 +21,6 @@
},
"homepage": "https://github.com/contentstack-expert-services/migration-v2-node-server#readme",
"dependencies": {
"@types/express": "^4.17.21",
"axios": "^1.6.5",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
Expand All @@ -30,12 +29,15 @@
"express-winston": "^4.2.0",
"helmet": "^7.1.0",
"jsonwebtoken": "^9.0.2",
"lowdb": "^7.0.1",
"mongoose": "^8.0.4",
"winston": "^3.11.0"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jsonwebtoken": "^9.0.5",
"@types/lodash": "^4.17.0",
"@types/node": "^20.10.4",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
Expand All @@ -44,10 +46,10 @@
"eslint-config-prettier": "^8.3.0",
"husky": "^4.3.8",
"lint-staged": "^15.2.2",
"lodash": "^4.17.21",
"nodemon": "^3.0.2",
"prettier": "^2.4.1",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"tsx": "^4.7.1",
"typescript": "^5.3.3",
"validate-branch-name": "^1.3.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dotenv from "dotenv";
import path from "path";
import { prodConfig } from "./prod.config";
import { devConfig } from "./dev.config";
import { prodConfig } from "./prod.config.js";
import { devConfig } from "./dev.config.js";

dotenv.config({
path: path.resolve(process.cwd(), `${process.env.NODE_ENV}.env`),
Expand Down
5 changes: 5 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export const HTTP_TEXTS = {
PROJECT_CREATION_FAILED: "Error occurred while creating project.",
NO_PROJECT: "resource not found with the given ID(s).",
AFFIX_UPDATED: "Project's Affix updated successfully",
AFFIX_CONFIRMATION_UPDATED:
"Project's Affix confirmation updated successfully",
FILEFORMAT_CONFIRMATION_UPDATED:
"Project's Fileformat confirmation updated successfully",
CMS_UPDATED: "Project's migration cms updated successfully",
FILE_FORMAT_UPDATED: "Project's migration file format updated successfully",
DESTINATION_STACK_UPDATED:
Expand Down Expand Up @@ -81,6 +85,7 @@ export const VALIDATION_ERRORS = {
EMAIL_LIMIT: "Email's max limit reached.",
LENGTH_LIMIT: "$'s max limit reached.",
STRING_REQUIRED: "Provided $ should be a string.",
BOOLEAN_REQUIRED: "Provided $ should be a boolean.",
INVALID_REGION: "Provided region doesn't exists.",
FIELD_REQUIRED: "Field '$' is required.",
INVALID_AFFIX: "Invalid affix format",
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import { authService } from "../services/auth.service";
import { authService } from "../services/auth.service.js";

const login = async (req: Request, res: Response) => {
const resp = await authService.login(req);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/org.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import { orgService } from "../services/org.service";
import { orgService } from "../services/org.service.js";

const getAllStacks = async (req: Request, res: Response) => {
const resp = await orgService.getAllStacks(req);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/projects.contentMapper.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import { contentMapperService } from "../services/contentMapper.service";
import { contentMapperService } from "../services/contentMapper.service.js";
const putTestData = async (req: Request, res: Response): Promise<void> => {
const resp = await contentMapperService.putTestData(req);
res.status(200).json(resp);
Expand Down
14 changes: 13 additions & 1 deletion src/controllers/projects.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import { projectService } from "../services/projects.service";
import { projectService } from "../services/projects.service.js";

const getAllProjects = async (req: Request, res: Response): Promise<void> => {
const allProjects = await projectService.getAllProjects(req);
Expand Down Expand Up @@ -36,11 +36,21 @@ const updateAffix = async (req: Request, res: Response) => {
res.status(resp.status).json(resp.data);
};

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

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

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

const updateDestinationStack = async (req: Request, res: Response) => {
const resp = await projectService.updateDestinationStack(req);
res.status(resp.status).json(resp.data);
Expand All @@ -63,7 +73,9 @@ export const projectController = {
updateProject,
updateLegacyCMS,
updateAffix,
affixConfirmation,
updateFileFormat,
fileformatConfirmation,
updateDestinationStack,
updateCurrentStep,
deleteProject,
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import { userService } from "../services/user.service";
import { userService } from "../services/user.service.js";

const getUserProfile = async (req: Request, res: Response) => {
const resp = await userService.getUserProfile(req);
Expand Down
20 changes: 12 additions & 8 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
// database.ts
import mongoose from "mongoose";
import { config } from "./config";
import logger from "./utils/logger";
import ProjectModel from "./models/project";
import AuthenticationModel from "./models/authentication";
import ContentTypesMapperModel from "./models/contentTypesMapper";
import FieldMapperModel from "./models/FieldMapper";
import { config } from "./config/index.js";
import logger from "./utils/logger.js";
import ProjectModel from "./models/project.js";
// import AuthenticationModel from "./models/authentication.js";
import ContentTypesMapperModel from "./models/contentTypesMapper.js";
import FieldMapperModel from "./models/FieldMapper.js";
import fs from "fs";

const connectToDatabase = async () => {
try {
await mongoose.connect(config.MONGODB_URI, {
...(config.APP_ENV === "production" ? { autoIndex: false } : {}),
});

logger.info("Connected to MongoDB");
//check if the database folder exists
if (!fs.existsSync("./database")) {
fs.mkdirSync("./database");
}

// Create the collection's if it doesn't exist
await ProjectModel.init();
await AuthenticationModel.init();
// const AuthenticationModel = await AuthenticationDb;
await ContentTypesMapperModel.init();
await FieldMapperModel.init();
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/auth.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// middleware/authentication.middleware.ts
import { Request, Response, NextFunction } from "express";
import jwt from "jsonwebtoken";
import { config } from "../config";
import { HTTP_CODES } from "../constants";
import { config } from "../config/index.js";
import { HTTP_CODES } from "../constants/index.js";

export const authenticateUser = (
req: Request,
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/auth.migration.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response, NextFunction } from "express";
import { HTTP_CODES } from "../constants";
import { config } from "../config";
import { HTTP_CODES } from "../constants/index.js";
import { config } from "../config/index.js";

export const authenticateMigrationService = (
req: Request,
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/auth.uploadService.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response, NextFunction } from "express";
import { HTTP_CODES } from "../constants";
import { config } from "../config";
import { HTTP_CODES } from "../constants/index.js";
import { config } from "../config/index.js";

export const authenticateUploadService = (
req: Request,
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/error.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response, NextFunction } from "express";
import { AppError } from "../utils/custom-errors.utils";
import logger from "../utils/logger";
import { AppError } from "../utils/custom-errors.utils.js";
import logger from "../utils/logger.js";

export const errorMiddleware = (
err: Error,
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/logger.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import expressWinston from "express-winston";
import logger from "../utils/logger";
import logger from "../utils/logger.js";

//Logger Middleware to log every request
const loggerMiddleware = expressWinston.logger({
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/unmatched-routes.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import { HTTP_CODES, HTTP_TEXTS } from "../constants";
import { HTTP_CODES, HTTP_TEXTS } from "../constants/index.js";

export const unmatchedRoutesMiddleware = (req: Request, res: Response) => {
const status = HTTP_CODES.NOT_FOUND;
Expand Down
37 changes: 19 additions & 18 deletions src/models/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
// src/models/Authentication.ts
import { Low } from "lowdb";
import { JSONFile } from "lowdb/node";
import lodash from "lodash";

import { Schema, model, Document } from "mongoose";
import { CS_REGIONS } from "../constants";
class LowWithLodash<T> extends Low<T> {
chain: lodash.ExpChain<this["data"]> = lodash.chain(this).get("data");
}

interface AuthenticationDocument extends Document {
user_id: string;
region: string;
authtoken: string;
interface AuthenticationDocument {
users: {
user_id: string;
region: string;
authtoken: string;
created_at: string;
updated_at: string;
}[];
}

const authenticationSchema = new Schema<AuthenticationDocument>(
{
user_id: { type: String, required: true },
region: { type: String, required: true, enum: CS_REGIONS },
authtoken: { type: String, required: true },
},
{ timestamps: { createdAt: "created_at", updatedAt: "updated_at" } }
);
const defaultData: AuthenticationDocument = { users: [] };

const AuthenticationModel = model<AuthenticationDocument>(
"Authentication",
authenticationSchema
const db = new LowWithLodash(
new JSONFile<AuthenticationDocument>("database/authentication.json"),
defaultData
);

export default AuthenticationModel;
export default db;
6 changes: 5 additions & 1 deletion src/models/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {
PREDEFINED_STEPS,
PROJECT_STATUS,
STEPPER_STEPS,
} from "../constants";
} from "../constants/index.js";

interface LegacyCMS {
cms: string;
affix: string;
affix_confirmation: boolean;
file_format: string;
file_format_confirmation: boolean;
file: {
id: string;
name: string;
Expand Down Expand Up @@ -70,7 +72,9 @@ const projectSchema = new Schema<ProjectDocument>(
legacy_cms: {
cms: { type: String },
affix: { type: String },
affix_confirmation: { type: Boolean },
file_format: { type: String },
file_format_confirmation: { type: Boolean },
file: {
id: { type: String },
name: { type: String },
Expand Down
6 changes: 3 additions & 3 deletions src/routes/auth.routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from "express";
import { authController } from "../controllers/auth.controller";
import { asyncRouter } from "../utils/async-router.utils";
import validator from "../validators";
import { authController } from "../controllers/auth.controller.js";
import { asyncRouter } from "../utils/async-router.utils.js";
import validator from "../validators/index.js";

const router = express.Router();

Expand Down
4 changes: 2 additions & 2 deletions src/routes/contentMapper.routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from "express";
import { contentMapperController } from "../controllers/projects.contentMapper.controller";
import { asyncRouter } from "../utils/async-router.utils";
import { contentMapperController } from "../controllers/projects.contentMapper.controller.js";
import { asyncRouter } from "../utils/async-router.utils.js";

const router = express.Router({ mergeParams: true });

Expand Down
4 changes: 2 additions & 2 deletions src/routes/migrationMidlleware.routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from "express";
import { projectController } from "../controllers/projects.controller";
import { asyncRouter } from "../utils/async-router.utils";
import { projectController } from "../controllers/projects.controller.js";
import { asyncRouter } from "../utils/async-router.utils.js";

const router = express.Router({ mergeParams: true });

Expand Down
6 changes: 3 additions & 3 deletions src/routes/org.routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from "express";
import { orgController } from "../controllers/org.controller";
import { asyncRouter } from "../utils/async-router.utils";
import validator from "../validators";
import { orgController } from "../controllers/org.controller.js";
import { asyncRouter } from "../utils/async-router.utils.js";
import validator from "../validators/index.js";

const router = express.Router({ mergeParams: true });

Expand Down
Loading