This commit is contained in:
SathishKannaa-HexrFactory
2025-01-22 16:59:14 +05:30
commit a28398259c
42 changed files with 2313 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
import mongoose, { Schema, Connection, Model } from "mongoose";
interface ConnectionCache {
[key: string]: Connection;
}
const connections: ConnectionCache = {};
const MainModel = <T>(
db: string,
modelName: string,
schema: Schema<T>,
collectionName: string
): Model<T> => {
const db1_url = `${process.env.MONGO_URI}${db}`;
// Check if the connection already exists
if (connections[db]) {
return connections[db].model<T>(modelName, schema, collectionName);
}
try {
const db1 = mongoose.createConnection(db1_url, {
maxPoolSize: 50,
});
// Cache the connection
connections[db] = db1;
// Log connection success or handle errors
db1.on("connected", () => {
console.log(`Connected to MongoDB database: ${db}`);
});
db1.on("error", (err) => {
console.error(`MongoDB connection error for database ${db}:`, err.message);
});
return db1.model<T>(modelName, schema, collectionName);
} catch (error) {
console.error("Database connection error:", (error as Error).message);
throw error;
}
};
export default MainModel;

View File

@@ -0,0 +1,57 @@
import mongoose, { Document, Schema } from 'mongoose';
import MainModel from '../../connect/mongoose';
// Interface for TypeScript with PascalCase
export interface floorItenms extends Document {
modeluuid: string;
modelfileID: string;
modelname: string
isLocked:boolean
isVisible:boolean
position: []
rotation: {
x: number;
y: number;
z: number;
};
}
// Define the Mongoose Schema
const floorItemsSchema: Schema = new Schema({
modeluuid: { type: String },
modelfileID: { type: String },
modelname: { type: String },
position: { type: Array},
isLocked:{type:Boolean},
isVisible:{type:Boolean},
rotation: {
x: { type: Number, required: true },
y: { type: Number, required: true },
z: { type: Number, required: true }
}
});
// Model for MongoDB collection
// const cameraModel = model<Camera>("Camera", cameraSchema);
// export default cameraModel;
// const floorItemsModel = (db: string) => {
// const mongoUrl = process.env.MONGO_URI || '';
// if (!mongoUrl) {
// throw new Error('MONGO_URI environment variable is not set');
// }
// // Connect to the database
// const dbConnection = mongoose.createConnection(mongoUrl, {
// dbName: db, // Specify the database name here
// serverSelectionTimeoutMS: 30000,
// });
// return dbConnection.model<floorItenms>('floorItenms', floorItenmsSchema,`floorItenms`);
// }
// export default floorItemsModel;
const floorItemsModel = (db:string) => {
return MainModel(db, "floorItems", floorItemsSchema, "floorItems")
};
export default floorItemsModel;

View File

@@ -0,0 +1,46 @@
import mongoose, { Document, Schema } from 'mongoose';
import MainModel from '../../connect/mongoose';
// Interface for TypeScript with PascalCase
export interface wallitems extends Document {
modeluuid: string;
modelname: string
type: string
csgposition: []
csgscale: []
position: []
quaternion: []
scale: []
}
// Define the Mongoose Schema
const wallItemsSchema: Schema = new Schema({
modeluuid: { type: String,unique:true },
modelname: { type: String},
type: { type: String },
csgposition: { type: Array},
csgscale: { type: Array,},
position: { type: Array },
quaternion: { type: Array},
scale: { type: Array}
});
// const wallItenmModel = (db: string) => {
// const mongoUrl = process.env.MONGO_URI || '';
// if (!mongoUrl) {
// throw new Error('MONGO_URI environment variable is not set');
// }
// // Connect to the database
// const dbConnection = mongoose.createConnection(mongoUrl, {
// dbName: db, // Specify the database name here
// serverSelectionTimeoutMS: 30000,
// });
// return dbConnection.model<wallitenms>('wallitenms', wallItenmsSchema, `wallitenms`);
// }
// export default wallItenmModel;
const wallItenmModel = (db:string) => {
return MainModel(db, "wallitems", wallItemsSchema, "wallitems")
};
export default wallItenmModel;

View File

@@ -0,0 +1,87 @@
import mongoose, { Document, Schema } from 'mongoose';
import MainModel from '../../connect/mongoose';
// Interface for TypeScript with PascalCase
export interface Camera extends Document {
userId: string;
position: {
x: number;
y: number;
z: number;
}
target: {
x: { type: Number, required: true },
y: { type: Number, required: true },
z: { type: Number, required: true }
}
rotation: {
x: { type: Number, required: true },
y: { type: Number, required: true },
z: { type: Number, required: true }
}
}
// Define the Mongoose Schema
const cameraSchema: Schema = new Schema({
userId: { type: String, unique: true },
position: {
x: { type: Number, required: true },
y: { type: Number, required: true },
z: { type: Number, required: true }
},
target: {
x: { type: Number, required: true },
y: { type: Number, required: true },
z: { type: Number, required: true }
},
rotation: {
x: { type: Number, required: true },
y: { type: Number, required: true },
z: { type: Number, required: true }
}
});
// Model for MongoDB collection
// const cameraModel = model<Camera>("Camera", cameraSchema);
// export default cameraModel;
// const cameraModel = (db: string) => {
// const mongoUrl = process.env.MONGO_URI || '';
// if (!mongoUrl) {
// throw new Error('MONGO_URI environment variable is not set');
// }
// // Connect to the database
// const dbConnection = mongoose.createConnection(mongoUrl, {
// dbName: db, // Specify the database name here
// serverSelectionTimeoutMS: 30000,
// });
// return dbConnection.model<Camera>('Camera', cameraSchema,`Camera`);
// }
// export default cameraModel;
// const cameraModel = (db: string) => {
// const mongoUrl = process.env.MONGO_URI || '';
// if (!mongoUrl) {
// throw new Error('MONGO_URI environment variable is not set');
// }
// const dbConnection = mongoose.createConnection(mongoUrl, {
// dbName: db,
// serverSelectionTimeoutMS: 60000, // Increased timeout
// });
// dbConnection.on('error', (err) => {
// console.error(`MongoDB connection error for database ${db}:`, err);
// });
// dbConnection.once('open', () => {
// console.log(`Connected to MongoDB database: ${db}`);
// });
// return dbConnection.model<Camera>('Camera', cameraSchema, 'Camera');
// };
// export default cameraModel
const cameraModel = (db:string) => {
return MainModel(db, "Camera", cameraSchema, "Camera")
};
export default cameraModel;

View File

@@ -0,0 +1,38 @@
import mongoose, { Document, Schema } from 'mongoose';
import MainModel from '../../connect/mongoose';
// Interface for TypeScript with PascalCase
export interface environment extends Document {
userId: string;
roofVisibility:boolean
wallVisibility:boolean
}
// Define the Mongoose Schema
const environmentSchema: Schema = new Schema({
userId: { type: String, unique: true },
roofVisibility: { type: Boolean ,default:false},
wallVisibility: { type: Boolean ,default:false},
});
// Model for MongoDB collection
// const cameraModel = model<Camera>("Camera", cameraSchema);
// export default cameraModel;
// const environmentModel = (db: string) => {
// const mongoUrl = process.env.MONGO_URI || '';
// if (!mongoUrl) {
// throw new Error('MONGO_URI environment variable is not set');
// }
// // Connect to the database
// const dbConnection = mongoose.createConnection(mongoUrl, {
// dbName: db, // Specify the database name here
// serverSelectionTimeoutMS: 30000,
// });
// return dbConnection.model<environment>('environments', environmentSchema,`environments`);
// }
// export default environmentModel;
const environmentModel = (db:string) => {
return MainModel(db, "environments", environmentSchema, "environments")
};
export default environmentModel;

View File

@@ -0,0 +1,43 @@
import mongoose, { Document, Schema } from "mongoose";
import MainModel from "../../connect/mongoose";
const positionSchema = new mongoose.Schema({
x: { type: Number, }, // Optional position fields
y: { type: Number, },
z: { type: Number},
});
// Define a schema for the individual line
const Vector3 = new mongoose.Schema({
position: { type: positionSchema, required: false }, // Optional position
uuid: { type: String, required: false }, // Optional uuid
});
// Define the main schema
const LineSchema = new mongoose.Schema({
layer: { type: Number, required: true }, // Layer is mandatory
line: { type: [Vector3], required: true }, // Array of line objects
type: { type: String, required: false }, // Optional type
});
// Database connection and model creation
// const lineModel = (db: string) => {
// const mongoUrl = process.env.MONGO_URI || "";
// if (!mongoUrl) {
// throw new Error("MONGO_URI environment variable is not set");
// }
// // Connect to the database
// const dbConnection = mongoose.createConnection(mongoUrl, {
// dbName: db, // Specify the database name here
// serverSelectionTimeoutMS: 30000,
// });
// // Return the model
// return dbConnection.model("lines", LineSchema, "lines");
// };
// export default lineModel;
const lineModel = (db:string) => {
return MainModel(db, "lines", LineSchema, "lines")
};
export default lineModel;

View File

@@ -0,0 +1,69 @@
import mongoose, { Document, Schema } from "mongoose";
import MainModel from "../connect/mongoose";
export interface User extends Document {
userName: String;
email: String;
password: String;
role: String;
profilePicture: String;
isShare:Boolean,
activeStatus:string
}
const signupschema: Schema = new Schema({
userName: {
type: String,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
min: 8,
required: true,
},
role: {
type: String,
default: "User",
enum: ["User", "Admin", "Project Manager", "Manager", "Owner"],
},
profilePicture: {
type: String,
// default: "default-profile-picture.jpg"
},
isShare:{
type:Boolean,
default:false
},
activeStatus:{
type:String,
enum: ["online", "offline"],
default: "offline"
}
});
// const userModel = (db: string) => {
// const mongoUrl = process.env.MONGO_URI || "";
// if (!mongoUrl) {
// throw new Error("MONGO_URI environment variable is not set");
// }
// // Connect to the database
// const dbConnection = mongoose.createConnection(mongoUrl, {
// dbName: db, // Specify the database name here
// serverSelectionTimeoutMS: 30000,
// });
// // Return the model
// return dbConnection.model("Users", signupschema, "Users");
// };
// export default userModel;
const userModel = (db:string) => {
return MainModel(db, "Users", signupschema, "Users")
};
export default userModel;

View File

@@ -0,0 +1,24 @@
const bcrypt = require("bcryptjs");
const saltRounds = 10;
const hashGenerate = async (Password:String) => {
try {
const salt = await bcrypt.genSalt(saltRounds);
const hash = await bcrypt.hash(Password, salt);
return hash;
} catch (error) {
return error;
}
};
const hashValidator = async (password:String, hashedPassword:String) => {
try {
const result = await bcrypt.compare(password, hashedPassword);
return result;
} catch (error) {
return false;
}
};
module.exports.hashGenerate = hashGenerate;
module.exports.hashValidator = hashValidator;

View File

@@ -0,0 +1,38 @@
import { Request, Response, NextFunction } from 'express';
import * as Jwt from 'jsonwebtoken'; // Correct way to import jsonwebtoken
// Define a new interface extending Request
interface AuthenticatedRequest extends Request {
user?: {
email: string;
// Add more fields as needed based on your JWT payload
};
}
const tokenGenerator = (email: string) => {
const token = Jwt.sign({ email: email }, "Milestone", {
expiresIn: "3hours",
});
return token;
};
const tokenValidator = (req: AuthenticatedRequest, res: Response, next: NextFunction): void => {
const token: string | undefined = req.headers.token as string | undefined;
if (!token) {
res.status(403).json({
msg: "No token present",
});
return; // Make sure to return after sending a response
}
try {
const decoded = Jwt.verify(token,"Milestone") as { email: string }; // adjust if your JWT payload has more fields
req.user = decoded;
next();
} catch (err) {
res.status(401).json({
msg: "Invalid Token",
});
}
};
export { tokenValidator,tokenGenerator };