irongit
105 lines4.4 KBJavaScript
1const mongoose = require('mongoose');
2const User = require('../model/schema/user');
3const bcrypt = require('bcrypt');
4const { initializeLeadSchema } = require("../model/schema/lead");
5const { initializeContactSchema } = require("../model/schema/contact");
6const { initializePropertySchema } = require("../model/schema/property");
7const { createNewModule } = require("../controllers/customField/customField.js");
8const customField = require('../model/schema/customField.js');
9const { contactFields } = require('./contactFields.js');
10const { leadFields } = require('./leadFields.js');
11const { propertiesFields } = require('./propertiesFields.js');
12
13const initializedSchemas = async () => {
14 await initializeLeadSchema();
15 await initializeContactSchema();
16 await initializePropertySchema();
17
18 const CustomFields = await customField.find({ deleted: false });
19 const createDynamicSchemas = async (CustomFields) => {
20 for (const module of CustomFields) {
21 const { moduleName, fields } = module;
22
23 // Check if schema already exists
24 if (!mongoose.models[moduleName]) {
25 // Create schema object
26 const schemaFields = {};
27 for (const field of fields) {
28 schemaFields[field.name] = { type: field.backendType };
29 }
30 // Create Mongoose schema
31 const moduleSchema = new mongoose.Schema(schemaFields);
32 // Create Mongoose model
33 mongoose.model(moduleName, moduleSchema, moduleName);
34 console.log(`Schema created for module: ${moduleName}`);
35 }
36 }
37 };
38
39 createDynamicSchemas(CustomFields);
40
41}
42
43const connectDB = async (DATABASE_URL, DATABASE) => {
44 try {
45 const DB_OPTIONS = {
46 dbName: DATABASE
47 }
48
49 mongoose.set("strictQuery", false);
50 await mongoose.connect(DATABASE_URL, DB_OPTIONS);
51
52 // const collectionsToDelete = ['abc', 'Report and analytics', 'test', 'krushil', 'bca', 'xyz', 'lkjhg', 'testssssss', 'tel', 'levajav', 'tellevajav', 'Contact'];
53 // const db = mongoose.connection.db;
54 // console.log(db)
55 // for (const collectionName of collectionsToDelete) {
56 // await db.collection(collectionName).drop();
57 // console.log(`Collection ${collectionName} deleted successfully.`);
58 // }
59 await initializedSchemas();
60
61 /* this was temporary */
62 const mockRes = {
63 status: (code) => {
64 return {
65 json: (data) => { }
66 };
67 },
68 json: (data) => { }
69 };
70
71 // Create default modules
72 await createNewModule({ body: { moduleName: 'Leads', fields: leadFields, headings: [], isDefault: true } }, mockRes);
73 await createNewModule({ body: { moduleName: 'Contacts', fields: contactFields, headings: [], isDefault: true } }, mockRes);
74 await createNewModule({ body: { moduleName: 'Properties', fields: propertiesFields, headings: [], isDefault: true } }, mockRes);
75 /* */
76 await initializedSchemas();
77
78 let adminExisting = await User.find({ role: 'superAdmin' });
79 if (adminExisting.length <= 0) {
80 const phoneNumber = 7874263694
81 const firstName = 'Prolink'
82 const lastName = 'Infotech'
83 const username = 'admin@gmail.com'
84 const password = 'admin123'
85 // Hash the password
86 const hashedPassword = await bcrypt.hash(password, 10);
87 // Create a new user
88 const user = new User({ _id: new mongoose.Types.ObjectId('64d33173fd7ff3fa0924a109'), username, password: hashedPassword, firstName, lastName, phoneNumber, role: 'superAdmin' });
89 // Save the user to the database
90 await user.save();
91 console.log("Admin created successfully..");
92 } else if (adminExisting[0].deleted === true) {
93 await User.findByIdAndUpdate(adminExisting[0]._id, { deleted: false });
94 console.log("Admin Update successfully..");
95 } else if (adminExisting[0].username !== "admin@gmail.com") {
96 await User.findByIdAndUpdate(adminExisting[0]._id, { username: 'admin@gmail.com' });
97 console.log("Admin Update successfully..");
98 }
99
100 console.log("Database Connected Successfully..");
101 } catch (err) {
102 console.log("Database Not connected", err.message);
103 }
104}
105module.exports = connectDB