HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vmi1674223.contaboserver.net 5.4.0-182-generic #202-Ubuntu SMP Fri Apr 26 12:29:36 UTC 2024 x86_64
User: root (0)
PHP: 7.4.3-4ubuntu2.22
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //home/evaluation-leave/controllers/sessionController.js
const Session = require("../models/session");

module.exports = {
  sessions: async (req, res) => {
    try {
      const result = await Session.findAllSessions();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry no Session has been setup." });
      }

      console.log(`Session, successfully pulled on ${new Date()} `);

      return res.status(200).json(result);
    } catch (error) {
      console.error("Error occurred:", error);
      res.status(500).json({ error: true, message: "Internal Server Error" });
    }
  },
  activeSessions: async (req, res) => {
    try {
      const result = await Session.findActiveSession();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry no active Session at the Moment." });
      }

      console.log(`Active Session, successfully pulled on ${new Date()} `);

      return res.status(200).json(result);
    } catch (error) {
      console.error("Error occurred:", error);
      res.status(500).json({ error: true, message: "Internal Server Error" });
    }
  },

  sessionById: async (req, res) => {
    const { id } = req.body;
    try {
      const result = await Session.findSessionById(id);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry that Session does not exist." });
      }

      console.log(
        `Session with id ${id}, successfully pulled on ${new Date()} `
      );

      return res.status(200).json(result);
    } catch (error) {
      console.error("Error occurred:", error);
      res.status(500).json({ error: true, message: "Internal Server Error" });
    }
  },

  createSession: async (req, res) => {
    const { name } = req.body;

    try {
      const serv = await Session.findSessionByName(name);
      if (serv) {
        return res.status(400).json({
          message:
            "Sorry there is an existing Session with that name. Choose a different name",
        });
      }

      const newSession = { name };

      const result = await Session.createSession(newSession);

      console.log(`successfully Created an a new Session on ${new Date()}`);
      return res.status(201).json(result);
    } catch (error) {
      console.error("Error occurred:", error);
      res.status(500).json({ error: true, message: "Internal Server Error" });
    }
  },

  updateAvailableSession: async (req, res) => {
    const { id, name } = req.body;

    try {
      const serv = await Session.findSessionById(id);
      if (!serv) {
        return res.status(400).json({
          message: "Sorry there seems to be a problem. Try again latter",
        });
      }

      const updatedSession = {
        id,
        name,
        updated_by: req.userId,
        updated_at: new Date(),
      };

      const result = await Session.updateSessionList(updatedSession);

      console.log(
        `Successfully Updated Session ${updatedSession.name} at ${new Date()}`
      );
      return res.status(201).json(result);
    } catch (error) {
      console.error("Error occurred:", error);
      res.status(500).json({ error: true, message: "Internal Server Error" });
    }
  },
};