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/evaluationMatrixcontroller.js
const Matrix = require("../models/evaluationMatrix");

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

      console.log(`Matrices, 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" });
    }
  },

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

      console.log(
        `Matrix 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" });
    }
  },

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

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

      const result = await Matrix.createMatrix(newMatrix);

      console.log(`successfully Created an a new Matrix 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" });
    }
  },
  
  updateAvailableMatrix: async (req, res) => {
    const { id, name, description } = req.body;

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

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

      const result = await Matrix.updateMatrix(updatedmatrix);

      console.log(`Successfully Updated Matrix ${updatedmatrix.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" });
    }
  },

};