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/leaveController.js
const Leave = require("../models/leave");
const Email = require("../models/email");
const users = require("../models/users");
const moment = require('moment');

module.exports = {
  allLeaves: async (req, res) => {
    try {
      const result = await Leave.findAllLeaves();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry no Leaves available so far." });
      }

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

  allLeaveTypes: async (req, res) => {
    try {
      const result = await Leave.findAllLeaveTypes();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry no Leaves Types available." });
      }

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

  openLeaves: async (req, res) => {
    try {
      const result = await Leave.findOpenLeaves();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry no Open Leaves at the Moment." });
      }

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

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

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

  findMyLeaves: async (req, res) => {
    try {
      const result = await Leave.findMyLeaves(req.userId);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave history." });
      }

      console.log(
        `Leave history for user with id ${req.userId
        }, 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" });
    }
  },

  findHodLeavesApproval: async (req, res) => {
    try {
      const result = await Leave.findHodLeavesApproval(req.userId);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave request pending." });
      }

      console.log(
        `HOD Leave request approval for user with id ${req.userId
        }, 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" });
    }
  },

  findDVCLeavesApproval: async (req, res) => {
    try {
      const result = await Leave.findDVCLeavesApproval(req.userId);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave request pending." });
      }

      console.log(
        `DVC Leave request approval for user with id ${req.userId
        }, 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" });
    }
  },

  findHodLeavesClosed: async (req, res) => {
    try {
      const result = await Leave.findHodLeavesClosed(req.userId);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave History." });
      }

      console.log(
        `HOD Closed Leave for user with id ${req.userId
        }, 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" });
    }
  },

  findDVCLeavesClosed: async (req, res) => {
    try {
      const result = await Leave.findDVCLeavesClosed(req.userId);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave History." });
      }

      console.log(
        `DVC Closed Leave request user with id ${req.userId
        }, 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" });
    }
  },

  findHodLeavesRejected: async (req, res) => {
    try {
      const result = await Leave.findHodLeavesRejected(req.userId);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Reject Leave History." });
      }

      console.log(
        `HOD Rejectd Leave request for user with id ${req.userId
        }, 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" });
    }
  },

  findDVCLeavesRejected: async (req, res) => {
    try { 
      const result = await Leave.findDVCLeavesRejected(req.userId);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Reject Leave History." });
      }

      console.log(
        `DVC Rejectd Leave request for user with id ${req.userId
        }, 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" });
    }
  },

  createLeave: async (req, res) => {
    const {
      from_date,
      leave_type_id,
      to_date,
      no_days,
      contact,
      responsibility,
      description,
      sup_id,
    } = req.body;

    try {
      const pendingLeave = await Leave.findMyOpenLeave(req.userId);
      if (pendingLeave) {
        return res.status(400).json({
          message:
            "Sorry there is an existing open leave that. You can only apply this once",
        });
      }

      const newLeave = {
        user_id: req.userId,
        from_date: moment(from_date).format('YYYY-MM-DD HH:mm:ss'),
        leave_type_id,
        to_date: moment(to_date).format('YYYY-MM-DD HH:mm:ss'),
        no_days,
        contact,
        responsibility,
        description,
        sup_id,
        // supervisor_name
        // supervisor_email,
      };

      const result = await Leave.createLeave(newLeave);

      console.log(
        `successfully Created an a new Leave application on ${new Date()}`
      );

      const supervisor = await users.findUserById(newLeave.sup_id);
      // console.log(supervisor)

      /**Sending Email on succesful Registration */ 

	const subject = "Leave Application Notification.";
const content = `
    <div style="border: 1px solid #337ab7; border-radius: 6px; padding: 20px; font-family: Arial, sans-serif;">
        <div style="background-color: #fff; padding: 20px; border-radius: 6px 6px 0 0; text-align: left;">
            <img src="https://riarauniversity.ac.ke/wp-content/uploads/2014/10/logo-riara-e1655472443775.png" height="80px" width="230px" alt="Riara University">
        </div>
        <div style="padding: 20px;">
            <p style="font-size: 16px; line-height: 1.5; color: #333;">
                Dear ${supervisor.fname},<br><br>
                I hope this email finds you well. You have received a leave approval notification for your staff.<br>
                Kindly login and check the request.<br><br>
            </p>
            <h2 style="text-align: left; color: #333;">Days requested: ${newLeave.no_days}. Between ${newLeave.from_date} and ${newLeave.to_date} </h2>
        </div>
        <div style="padding: 20px; border-top: 1px solid #ddd;">
            <p style="font-size: 14px; color: #333;">Kind Regards,</p>
            <p style="font-size: 14px; color: #333;">Hr Team</p>
            <small style="font-size: 12px; color: #999;"><i>This is a system generated mail. Please do not reply to it.</i></small>
        </div>
    </div>
`;      
      /**Activate letter */
      const mailResult = await Email.sendEmail(
        supervisor.email,
        subject,
        content
      );
      return res.status(201).json({ result, mailResult });

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

  acceptLeaveRequest: async (req, res) => {
    const { id, dvc_id, description, applicant_id } = req.body;

    try {
      const serv = await Leave.findLeaveById(id);
      if (!serv) {
        return res.status(400).json({
          message:
            "Sorry there seems to be a problem. The leave is not correctly mapped.",
        });
      }

      const acceptLeave = {
        id,
        dvc_id,
        description,
        user_id: req.userId,
        updated_by: req.userId,
        updated_at: new Date(),
      };

      const result = await Leave.acceptLeaveRequest(acceptLeave);

      console.log(
        `Successfully Accepted the Leave with id ${acceptLeave.id
        } at ${new Date()}`
      );
 const supervisor = await users.findUserById(dvc_id);
      // console.log(supervisor)

      /**Sending Email on succesful Registration */ 

	const subject = "Leave Application Notification.";
const content = `
    <div style="border: 1px solid #337ab7; border-radius: 6px; padding: 20px; font-family: Arial, sans-serif;">
        <div style="background-color: #fff; padding: 20px; border-radius: 6px 6px 0 0; text-align: left;">
            <img src="https://riarauniversity.ac.ke/wp-content/uploads/2014/10/logo-riara-e1655472443775.png" height="80px" width="230px" alt="Riara University">
        </div>
        <div style="padding: 20px;">
            <p style="font-size: 16px; line-height: 1.5; color: #333;">
                Dear ${supervisor.fname},<br><br>
                I hope this email finds you well. You have a leave approval notification for your staff.<br>
                Kindly login and check the request.<br><br>
            </p>
            <h2 style="text-align: left; color: #333;">Supervisor's comment: ${acceptLeave.description}.</h2>
        </div>
        <div style="padding: 20px; border-top: 1px solid #ddd;">
            <p style="font-size: 14px; color: #333;">Kind Regards,</p>
            <p style="font-size: 14px; color: #333;">Hr Team</p>
            <small style="font-size: 12px; color: #999;"><i>This is a system generated mail. Please do not reply to it.</i></small>
        </div>
    </div>
`;      
      /**Activate letter */
      const mailResult = await Email.sendEmail(
        supervisor.email,
        subject,
        content
      );
      return res.status(201).json({ result, mailResult });

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

  declineLeaveRequest: async (req, res) => {
    const { id, description, applicant_id } = req.body;

    try {
      const serv = await Leave.findLeaveById(id);
      if (!serv) {
        return res.status(400).json({
          message:
            "Sorry there seems to be a problem. The leave is not correctly mapped.",
        });
      }

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

      const result = await Leave.declineLeaveRequest(acceptLeave);

      console.log(
        `Successfully Declined the Leave request with id ${acceptLeave.id
        } at ${new Date()}`
      );

 const applicant = await users.findUserById(applicant_id);
      // console.log(applicant)

      /**Sending Email on succesful Registration */ 

	const subject = "Leave Application Notification.";
const content = `
    <div style="border: 1px solid #337ab7; border-radius: 6px; padding: 20px; font-family: Arial, sans-serif;">
        <div style="background-color: #fff; padding: 20px; border-radius: 6px 6px 0 0; text-align: left;">
            <img src="https://riarauniversity.ac.ke/wp-content/uploads/2014/10/logo-riara-e1655472443775.png" height="80px" width="230px" alt="Riara University">
        </div>
        <div style="padding: 20px;">
            <p style="font-size: 16px; line-height: 1.5; color: #333;">
                Dear ${applicant.fname},<br><br>
                I hope this email finds you well. You have a leave request has been declined by your supervisor.<br>
                Kindly login and check the reason.<br><br>
            </p>
            <h2 style="text-align: left; color: #333;">Supervisor's comment: ${acceptLeave.description}.</h2>
        </div>
        <div style="padding: 20px; border-top: 1px solid #ddd;">
            <p style="font-size: 14px; color: #333;">Kind Regards,</p>
            <p style="font-size: 14px; color: #333;">Hr Team</p>
            <small style="font-size: 12px; color: #999;"><i>This is a system generated mail. Please do not reply to it.</i></small>
        </div>
    </div>
`;      
      /**Activate letter */
      const mailResult = await Email.sendEmail(
        applicant.email,
        subject,
        content
      );
      return res.status(201).json({ result, mailResult });

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

  approveLeaveRequest: async (req, res) => {
    const { id, description, applicant_id } = req.body;

    try {
      const serv = await Leave.findLeaveById(id);
      if (!serv) {
        return res.status(400).json({
          message:
            "Sorry there seems to be a problem. The leave is not correctly mapped.",
        });
      }

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

      const result = await Leave.approveLeave(leave);

      console.log(
        `Successfully Approved the Leave request with id ${leave.id
        } at ${new Date()}`
      );

 const applicant = await users.findUserById(applicant_id);
     // console.log(applicant)

      /**Sending Email on succesful Registration */ 

	const subject = "Leave Application Notification.";
const content = `
    <div style="border: 1px solid #337ab7; border-radius: 6px; padding: 20px; font-family: Arial, sans-serif;">
        <div style="background-color: #fff; padding: 20px; border-radius: 6px 6px 0 0; text-align: left;">
            <img src="https://riarauniversity.ac.ke/wp-content/uploads/2014/10/logo-riara-e1655472443775.png" height="80px" width="230px" alt="Riara University">
        </div>
        <div style="padding: 20px;">
            <p style="font-size: 16px; line-height: 1.5; color: #333;">
                Dear ${applicant.fname},<br><br>
                I hope this email finds you well. You have a leave request has been accepted and approved by your supervisor.<br>
                We wish you all the best. Remember to stay safe always.<br><br>
            </p>
            <h2 style="text-align: left; color: #333;">Supervisor's comment: ${leave.description}.</h2>
        </div>
        <div style="padding: 20px; border-top: 1px solid #ddd;">
            <p style="font-size: 14px; color: #333;">Kind Regards,</p>
            <p style="font-size: 14px; color: #333;">Hr Team</p>
            <small style="font-size: 12px; color: #999;"><i>This is a system generated mail. Please do not reply to it.</i></small>
        </div>
    </div>
`;      
      /**Activate letter */
      const mailResult = await Email.sendEmail(
        applicant.email,
        subject,
        content
      );
      return res.status(201).json({ result, mailResult });

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

  rejectLeaveRequest: async (req, res) => {
    const { id, description, applicant_id } = req.body;

    try {
      const serv = await Leave.findLeaveById(id);
      if (!serv) {
        return res.status(400).json({
          message:
            "Sorry there seems to be a problem. The leave is not correctly mapped.",
        });
      }

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

      const result = await Leave.rejectLeave(leave);

      console.log(
        `Successfully Rejected the Leave request with id ${leave.id
        } at ${new Date()}`
      );

 const applicant = await users.findUserById(applicant_id);
      // console.log(applicant)

      /**Sending Email on succesful Registration */ 

	const subject = "Leave Application Notification.";
const content = `
    <div style="border: 1px solid #337ab7; border-radius: 6px; padding: 20px; font-family: Arial, sans-serif;">
        <div style="background-color: #fff; padding: 20px; border-radius: 6px 6px 0 0; text-align: left;">
            <img src="https://riarauniversity.ac.ke/wp-content/uploads/2014/10/logo-riara-e1655472443775.png" height="80px" width="230px" alt="Riara University">
        </div>
        <div style="padding: 20px;">
            <p style="font-size: 16px; line-height: 1.5; color: #333;">
                Dear ${applicant.fname},<br><br>
                I hope this email finds you well. You have a leave request has been Rejected by the DVC.<br>
                Kindly login and check the reason.<br><br>
            </p>
            <h2 style="text-align: left; color: #333;">Approver's comment: ${leave.description}.</h2>
        </div>
        <div style="padding: 20px; border-top: 1px solid #ddd;">
            <p style="font-size: 14px; color: #333;">Kind Regards,</p>
            <p style="font-size: 14px; color: #333;">Hr Team</p>
            <small style="font-size: 12px; color: #999;"><i>This is a system generated mail. Please do not reply to it.</i></small>
        </div>
    </div>
`;      
      /**Activate letter */
      const mailResult = await Email.sendEmail(
        applicant.email,
        subject,
        content
      );
      return res.status(201).json({ result, mailResult });

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

  /**Reports
   * For HOD
   * For DVC
   * For HR
   * For VC
   */
  findApprovedReportForHod: async (req, res) => {
    try {
      const result = await Leave.findHodLeavesApproved(req.userId);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry there are no list of Approved leave Reports." });
      }

      console.log(
        `HOD Leave Approved Reports pulled by user with id ${req.userId
        }, successfully 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" });
    }
  },

  findAcceptedReportForHod: async (req, res) => {
    try {
      const result = await Leave.findHodLeavesAccepted(req.userId);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry there are no list of Pennding Approval Reports." });
      }

      console.log(
        `HOD Pending Approval Reports pulled by user with id ${req.userId
        }, successfully 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" });
    }
  },

  findDeclinedReportForHod: async (req, res) => {
    try {
      const result = await Leave.findHodLeavesDeclined(req.userId);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry there are no list of Declined Leave Reports." });
      }

      console.log(
        `HOD Declined Leave Reports pulled by user with id ${req.userId 
        }, successfully 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" });
    }
  },

  findRejectedReportForHod: async (req, res) => {
    try {
      const result = await Leave.findHodLeavesRejected(req.userId);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry there are no list of Rejected Leave Reports." });
      }

      console.log(
        `HOD Rejected Leave Reports pulled by user with id ${req.userId
        }, successfully 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" });
    }
  },
  findApprovedReportForDvc: async (req, res) => {
    try {
      const result = await Leave.findDVCLeavesApproved();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry there are no list of Approved Leave Reports." });
      }

      console.log(
        `DVC Approved Leave Reports pulled by user with id ${req.userId
        }, successfully 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" });
    }
  },

  findRejectedReportForDvc: async (req, res) => {
    try {
      const result = await Leave.findDVCLeavesRejected();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry there are no list of Rejected Leave Reports." });
      }

      console.log(
        `DVC Rejected Leave Reports pulled by user with id ${req.userId
        }, successfully 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" });
    }
  },

  findOpenLeavesReport: async (req, res) => {
    try {
      const result = await Leave.findOpenLeaves();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave History." });
      }

      console.log(
        `User with id ${req.userId
        }, successfully pulled Open leaves 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" });
    }
  },

  findApprovedLeavesReport: async (req, res) => {
    try {
      const result = await Leave.findApprovedLeaves();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave History." });
      }

      console.log(
        `User with id ${req.userId
        }, successfully pulled Approved leaves 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" });
    }
  },

  findRejectedLeavesReport: async (req, res) => {
    try {
      const result = await Leave.findRejectedLeaves();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave History." });
      }

      console.log(
        `User with id ${req.userId
        }, successfully pulled Rejected leaves 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" });
    }
  },

  findDeclinedLeavesReport: async (req, res) => {
    try {
      const result = await Leave.findDeclinedLeaves();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave History." });
      }

      console.log(
        `User with id ${req.userId
        }, successfully pulled Declined leaves 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" });
    }
  },

  /**Leave Type */

  findWeeklyLeaveTypeGraph: async (req, res) => {
    try {
      const result = await Leave.findWeeklyLeaveTypeGraph();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave Report." });
      }

      console.log(
        `User with id ${req.userId
        }, successfully pulled WeeklyLeaveTypeGraph 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" });
    }
  },

  findMonthlyLeaveTypeGraph: async (req, res) => {
    try {
      const result = await Leave.findMonthlyLeaveTypeGraph();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave Report." });
      }

      console.log(
        `User with id ${req.userId
        }, successfully pulled MonthlyLeaveTypeGraph leaves 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" });
    }
  },

  /**Department Wise */
  
  findWeeklyLeaveDepartmentGraph: async (req, res) => {
    try {
      const result = await Leave.findWeeklyLeaveDepartmentGraph();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave Report." });
      }

      console.log(
        `User with id ${req.userId
        }, successfully pulled MonthlyLeaveTypeGraph leaves 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" });
    }
  },

  
  findMothlyLeaveDepartmentGraph: async (req, res) => {
    try {
      const result = await Leave.findMothlyLeaveDepartmentGraph();
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry You have no Leave Report." });
      }

      console.log(
        `User with id ${req.userId
        }, successfully pulled MonthlyLeaveTypeGraph leaves 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" });
    }
  },

/**
 * MOnth wise Reports (JAN)
 */
findJanuaryGraph: async (req, res) => {
  try {
    const result = await Leave.findJanuaryGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled JanuaryGraph leaves 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" });
  }
},

/**
 * MOnth wise Reports (FEB)
 */
findFebruaryGraph: async (req, res) => {
  try {
    const result = await Leave.findFebruaryGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled FebruaryGraph leaves 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" });
  }
},

/**
 * MOnth wise Reports (MAR)
 */
findMarchGraph: async (req, res) => {
  try {
    const result = await Leave.findMarchGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled MarchGraph leaves 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" });
  }
},

/**
 * MOnth wise Reports (APRIL)
 */
findAprilGraph: async (req, res) => {
  try {
    const result = await Leave.findAprilGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled AprilGraph leaves 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" });
  }
},

/**
 * MOnth wise Reports (MAY)
 */
findMayGraph: async (req, res) => {
  try {
    const result = await Leave.findMayGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled MayGraph leaves 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" });
  }
},

/**
 * MOnth wise Reports (JUN)
 */
findJuneGraph: async (req, res) => {
  try {
    const result = await Leave.findJuneGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled JuneGraph leaves 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" });
  }
},

/**
 * MOnth wise Reports (JULY)
 */
findJulyGraph: async (req, res) => {
  try {
    const result = await Leave.findJulyGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled JulyGraph leaves 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" });
  }
},

/**
 * MOnth wise Reports (AUG)
 */
findAugustGraph: async (req, res) => {
  try {
    const result = await Leave.findAugustGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled AugustGraph leaves 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" });
  }
},


/**
 * MOnth wise Reports (SEP)
 */
findSeptemberGraph: async (req, res) => {
  try {
    const result = await Leave.findSeptemberGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled SeptemberGraph leaves 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" });
  }
},

/**
 * MOnth wise Reports (OCT)
 */
findOctoberGraph: async (req, res) => {
  try {
    const result = await Leave.findOctoberGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled OctoberGraph leaves 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" });
  }
},

/**
 * MOnth wise Reports (NOV)
 */
findNovemberGraph: async (req, res) => {
  try {
    const result = await Leave.findNovemberGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled NovemberGraph leaves 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" });
  }
},

/**
 * MOnth wise Reports (DEC)
 */
findDecemberGraph: async (req, res) => {
  try {
    const result = await Leave.findDecemberGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled DecemberGraph leaves 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" });
  }
},



/**
 * Yearly Applied Reports
 */
findAppliedChatGraph: async (req, res) => {
  try {
    const result = await Leave.findAppliedChartGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled All APplied leaves 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" });
  }
},


/**
 * Yearly Approved Reports
 */
findApprovedChatGraph: async (req, res) => {
  try {
    const result = await Leave.findApprovedChartGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled All Approved leaves 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" });
  }
},


/**
 * Yearly Rejected Reports
 */
findRejectedChatGraph: async (req, res) => {
  try {
    const result = await Leave.findRejectedChartGraph();
    if (!result) {
      return res
        .status(404)
        .json({ message: "Sorry You have no Leave Report." });
    }

    console.log(
      `User with id ${req.userId
      }, successfully pulled All Rejected leaves 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" });
  }
},
};