AnonSec Team
Server IP : 72.167.149.90  /  Your IP : 216.73.216.228
Web Server : Apache
System : Linux server.zoomride.app 4.18.0-553.158.1.el8_10.x86_64 #1 SMP Wed Aug 26 03:18:33 EDT 2026 x86_64
User : zoomride2022 ( 1001)
PHP Version : 8.3.33
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0750) :  /home/zoomride2022/public_html/backend.survo.app/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/zoomride2022/public_html/backend.survo.app/index.js
//express
const express = require("express");
const app = express();

//cors
const cors = require("cors");

app.use(cors());
app.use(express.json());

//logging middleware
const logger = require("morgan");
app.use(logger("dev"));

//path
const path = require("path");

//fs
const fs = require("fs");

//dotenv
require("dotenv").config({ path: ".env" });

//import model
const Setting = require("./models/setting.model");

//settingJson
const settingJson = require("./setting");

//Declare global variable
global.settingJSON = {};

//handle global.settingJSON when pm2 restart
async function initializeSettings() {
  try {
    const setting = await Setting.findOne().sort({ createdAt: -1 });
    if (setting) {
      console.log("In setting initialize Settings");
      global.settingJSON = setting;
    } else {
      global.settingJSON = settingJson;
    }
  } catch (error) {
    console.error("Failed to initialize settings:", error);
  }
}

module.exports = { initializeSettings };

//Declare the function as a global variable to update the setting.js file
global.updateSettingFile = (settingData) => {
  const settingJSON = JSON.stringify(settingData, null, 2);
  fs.writeFileSync("setting.js", `module.exports = ${settingJSON};`, "utf8");

  global.settingJSON = settingData; // Update global variable
  console.log("Settings file updated.");
};

//connection.js
const db = require("./util/connection");

//route.js
const routes = require("./routes/route");
app.use("/api", routes);

//socket io
const http = require("http");
const server = http.createServer(app);
global.io = require("socket.io")(server);

//socket.js
require("./socket");

app.use("/storage", express.static(path.join(__dirname, "storage")));

db.on("error", () => {
  console.log("Connection Error: ");
});

db.once("open", async () => {
  console.log("Mongo: successfully connected to db");

  //Initialize settings before starting the server
  await initializeSettings();

  //set port and listen the request
  server.listen(process?.env.PORT, () => {
    console.log("Hello World ! listening on " + process?.env?.PORT);
  });
});

//cron
const cron = require("node-cron");

//moment
const moment = require("moment");

//mongoose
const mongoose = require("mongoose");

//import model
const Appointment = require("./models/appointment.model");
const Provider = require("./models/provider.model");

const updateAttendance = require("./util/updateAttendance");

//provider who are not attend are count as absent for the day (To run a job every day at 11:59 PM)
cron.schedule("59 23 * * *", async () => {
  try {
    const providers = await Provider.find({ isActive: true });

    for (const provider of providers) {
      const providerId = provider._id;
      await updateAttendance(providerId, 2);
    }

    await Provider.updateMany({ isActive: true }, { isAttend: false, showDialog: false });

    console.log("Cron job executed successfully.");
  } catch (error) {
    console.error("Error executing cron job:", error);
  }
});

//appointment cancel (To run a job every day at 11:59 PM)
cron.schedule("59 23 * * *", async () => {
  try {
    const todayDate = moment().format("YYYY-MM-DD");

    const bookingsToUpdate = await Appointment.find({
      date: todayDate,
      status: { $in: [1, 2] },
    });

    for (const booking of bookingsToUpdate) {
      booking.status = "cancel";
      booking.cancel.person = 1;
      booking.cancel.reason = "Automatically Cancelled by System Administrator 🛑";
      booking.cancel.time = moment().format("HH:mm a");
      booking.cancel.date = moment().format("YYYY-MM-DD");
      await booking.save();
    }

    console.log(`${bookingsToUpdate.length} bookings updated successfully.`);
  } catch (error) {
    console.error("Error executing cron job:", error);
  }
});

AnonSec - 2021