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 @@ -356,4 +356,6 @@ MigrationBackup/
error.log
combine.log

package-lock.json
package-lock.json

!example.env
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# migration-v2-node-server

This is the TSO migration V2's node server.

## To start the sever

1. Run `npm i`
2. Install & start MongoDB
3. To run dev server, create `./development.env` file and add env variables as per `./example.env`
4. To run prod server, create `./production.env` file and add env variables as per `./example.env`
3 changes: 3 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PORT=
APP_TOKEN_KEY=
MONGODB_URI=
2 changes: 1 addition & 1 deletion src/config/dev.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const devConfig = {
CS_API: {
US: "https://stag-api.csnonprod.com/v3",
NA: "https://stag-api.csnonprod.com/v3",
EU: "https://stag-eu-api.csnonprod.com/v3",
AZURE_NA: "https://stag-azure-na-api.csnonprod.com/v3",
},
Expand Down
2 changes: 1 addition & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type ConfigType = {
APP_ENV: string;
MONGODB_URI: string;
CS_API: {
US: string;
NA: string;
EU: string;
AZURE_NA: string;
AZURE_EU?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/config/prod.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const prodConfig = {
CS_API: {
US: "https://api.contentstack.io/v3",
NA: "https://api.contentstack.io/v3",
EU: "https://eu-api.contentstack.com/v3",
AZURE_NA: "https://azure-na-api.contentstack.com/v3",
AZURE_EU: "https://azure-eu-api.contentstack.com/v3",
Expand Down
134 changes: 68 additions & 66 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,71 @@
export const constants = {
CS_REGIONS: ["US", "EU", "AZURE_NA", "AZURE_EU"],
MODULES: [
"Project",
"Migration",
"Content Mapping",
"Legacy CMS",
"Destination Stack",
],
MODULES_ACTIONS: ["Create", "Update", "Delete"],
AXIOS_TIMEOUT: 60 * 1000,
HTTP_CODES: {
OK: 200,
FORBIDDEN: 403,
BAD_REQUEST: 400,
NOT_FOUND: 404,
UNAUTHORIZED: 401,
TOO_MANY_REQS: 429,
SOMETHING_WRONG: 501,
MOVED_PERMANENTLY: 301,
SUPPORT_DOC: 294,
SERVER_ERROR: 500,
UNPROCESSABLE_CONTENT: 422,
},
HTTP_TEXTS: {
UNAUTHORIZED: "You're unauthorized to access this resource.",
INTERNAL_ERROR: "Internal server error, please try again later.",
SOMETHING_WENT_WRONG:
"Something went wrong while processing your request, please try again.",
NO_CS_USER: "No user found with the credentials",
SUCCESS_LOGIN: "Login Successful.",
TOKEN_ERROR: "Error occurred during token generation.",
LOGIN_ERROR: "Error occurred during login",
ROUTE_ERROR: "Sorry, the requested resource is not available.",
PROJECT_NOT_FOUND: "Sorry, the requested project does not exists.",
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",
DESTINATION_STACK_UPDATED:
"Project's migration destination stack updated successfully",
DESTINATION_STACK_NOT_FOUND: "Destination stack does not exist",
DESTINATION_STACK_ERROR:
"Error occurred during verifying destination stack",
MIGRATION_DELETED: "Project's migration deleted successfully.",
INVALID_ID: "Provided $ ID is invalid.",
MIGRATION_EXISTS: "Project's migration already exists.",
CONTENT_TYPE_NOT_FOUND: "ContentType does not exist",
INVALID_CONTENT_TYPE: "Provide valid ContentType data",
RESET_CONTENT_MAPPING:
"ContentType has been successfully restored to its initial mapping",
},
HTTP_RESPONSE_HEADERS: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
Connection: "close",
},
METHODS_TO_INCLUDE_DATA_IN_AXIOS: ["PUT", "POST", "DELETE", "PATCH"],
VALIDATION_ERRORS: {
INVALID_EMAIL: "Given email ID is invalid.",
EMAIL_LIMIT: "Email's max limit reached.",
LENGTH_LIMIT: "$'s max limit reached.",
STRING_REQUIRED: "Provided $ should be a string.",
INVALID_REGION: "Provided region doesn't exists.",
},
export const CS_REGIONS = ["NA", "EU", "AZURE_NA", "AZURE_EU"];
export const MODULES = [
"Project",
"Migration",
"Content Mapping",
"Legacy CMS",
"Destination Stack",
];
export const MODULES_ACTIONS = ["Create", "Update", "Delete"];
export const AXIOS_TIMEOUT = 60 * 1000;
export const HTTP_CODES = {
OK: 200,
FORBIDDEN: 403,
BAD_REQUEST: 400,
NOT_FOUND: 404,
UNAUTHORIZED: 401,
TOO_MANY_REQS: 429,
SOMETHING_WRONG: 501,
MOVED_PERMANENTLY: 301,
SUPPORT_DOC: 294,
SERVER_ERROR: 500,
UNPROCESSABLE_CONTENT: 422,
};
export const HTTP_TEXTS = {
UNAUTHORIZED: "You're unauthorized to access this resource.",
INTERNAL_ERROR: "Internal server error, please try again later.",
SOMETHING_WENT_WRONG:
"Something went wrong while processing your request, please try again.",
NO_CS_USER: "No user found with the credentials",
SUCCESS_LOGIN: "Login Successful.",
TOKEN_ERROR: "Error occurred during token generation.",
LOGIN_ERROR: "Error occurred during login",
ROUTE_ERROR: "Sorry, the requested resource is not available.",
PROJECT_NOT_FOUND: "Sorry, the requested project does not exists.",
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",
DESTINATION_STACK_UPDATED:
"Project's migration destination stack updated successfully",
DESTINATION_STACK_NOT_FOUND: "Destination stack does not exist",
DESTINATION_STACK_ERROR: "Error occurred during verifying destination stack",
MIGRATION_DELETED: "Project's migration deleted successfully.",
INVALID_ID: "Provided $ ID is invalid.",
MIGRATION_EXISTS: "Project's migration already exists.",
CONTENT_TYPE_NOT_FOUND: "ContentType does not exist",
INVALID_CONTENT_TYPE: "Provide valid ContentType data",
RESET_CONTENT_MAPPING:
"ContentType has been successfully restored to its initial mapping",
};
export const HTTP_RESPONSE_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
Connection: "close",
};
export const METHODS_TO_INCLUDE_DATA_IN_AXIOS = [
"PUT",
"POST",
"DELETE",
"PATCH",
];
export const VALIDATION_ERRORS = {
INVALID_EMAIL: "Given email ID is invalid.",
EMAIL_LIMIT: "Email's max limit reached.",
LENGTH_LIMIT: "$'s max limit reached.",
STRING_REQUIRED: "Provided $ should be a string.",
INVALID_REGION: "Provided region doesn't exists.",
};
export const PROJECT_POPULATE_FIELDS = "content_mapper";
export const CONTENT_TYPE_POPULATE_FIELDS =
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Request, Response } from "express";
import { userService } from "../services/user.service";
import { constants } from "../constants";
import { HTTP_CODES } from "../constants";

const getUserProfile = async (req: Request, res: Response) => {
const user = await userService.getUserProfile(req);
res.status(constants.HTTP_CODES.OK).json(user);
res.status(HTTP_CODES.OK).json(user);
};

export const userController = {
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/auth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import { Request, Response, NextFunction } from "express";
import jwt from "jsonwebtoken";
import { config } from "../config";
import { constants } from "../constants";
import { HTTP_CODES } from "../constants";

export const authenticateUser = (
req: Request,
res: Response,
next: NextFunction
) => {
const status = constants.HTTP_CODES.UNAUTHORIZED;
const status = HTTP_CODES.UNAUTHORIZED;
const token = req.get("app_token");

if (!token)
Expand Down
6 changes: 3 additions & 3 deletions src/middlewares/unmatched-routes.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Request, Response } from "express";
import { constants } from "../constants";
import { HTTP_CODES, HTTP_TEXTS } from "../constants";

export const unmatchedRoutesMiddleware = (req: Request, res: Response) => {
const status = constants.HTTP_CODES.NOT_FOUND;
const status = HTTP_CODES.NOT_FOUND;
res.status(status).json({
error: { code: status, message: constants.HTTP_TEXTS.ROUTE_ERROR },
error: { code: status, message: HTTP_TEXTS.ROUTE_ERROR },
});
};
6 changes: 3 additions & 3 deletions src/models/auditLog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { constants } from "../constants";
import { MODULES, MODULES_ACTIONS } from "../constants";
import { Schema, model, Document } from "mongoose";

interface Action {
Expand Down Expand Up @@ -27,11 +27,11 @@ const auditLogSchema = new Schema<AuditLogDocument>({
user_id: { type: String, required: true },
user_first_name: { type: String, required: true },
user_last_name: { type: String, required: true },
module: { type: String, required: true, enum: constants.MODULES },
module: { type: String, required: true, enum: MODULES },
action: {
type: String,
required: true,
enum: constants.MODULES_ACTIONS,
enum: MODULES_ACTIONS,
},
},
],
Expand Down
4 changes: 2 additions & 2 deletions src/models/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// src/models/Authentication.ts

import { Schema, model, Document } from "mongoose";
import { constants } from "../constants";
import { CS_REGIONS } from "../constants";

interface AuthenticationDocument extends Document {
user_id: string;
Expand All @@ -12,7 +12,7 @@ interface AuthenticationDocument extends Document {
const authenticationSchema = new Schema<AuthenticationDocument>(
{
user_id: { type: String, required: true },
region: { type: String, required: true, enum: constants.CS_REGIONS },
region: { type: String, required: true, enum: CS_REGIONS },
authtoken: { type: String, required: true },
},
{ timestamps: { createdAt: "created_at", updatedAt: "updated_at" } }
Expand Down
4 changes: 2 additions & 2 deletions src/models/project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Schema, model, Document } from "mongoose";
import { constants } from "../constants";
import { CS_REGIONS } from "../constants";

interface LegacyCMS {
cms: string;
Expand Down Expand Up @@ -32,7 +32,7 @@ interface ProjectDocument extends Document {

const projectSchema = new Schema<ProjectDocument>(
{
region: { type: String, required: true, enum: constants.CS_REGIONS },
region: { type: String, required: true, enum: CS_REGIONS },
org_id: { type: String, required: true },
owner: { type: String, required: true },
created_by: { type: String, required: true },
Expand Down
1 change: 0 additions & 1 deletion src/routes/projects.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ router.put(
// Delete a project route
router.delete("/:projectId", asyncRouter(projectController.deleteProject));


export default router;
13 changes: 6 additions & 7 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { config } from "../config";
import { safePromise } from "../utils/index";
import https from "../utils/https.utils";
import { LoginServiceType, AppTokenPayload } from "../models/types";
import { constants } from "../constants";
import { HTTP_CODES, HTTP_TEXTS } from "../constants";
import { generateToken } from "../utils/jwt.utils";
import {
BadRequestError,
Expand Down Expand Up @@ -39,14 +39,13 @@ const login = async (req: Request): Promise<LoginServiceType> => {
status: err?.response?.status,
};

if (res?.status === constants.HTTP_CODES.SUPPORT_DOC)
if (res?.status === HTTP_CODES.SUPPORT_DOC)
return {
data: res?.data,
status: res?.status,
};

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

const appTokenPayload: AppTokenPayload = {
region: userData?.region,
Expand All @@ -69,10 +68,10 @@ const login = async (req: Request): Promise<LoginServiceType> => {

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

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

Expand Down
Loading