Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonKnight30 committed Oct 27, 2024
0 parents commit 508cb48
Show file tree
Hide file tree
Showing 57 changed files with 9,108 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PORT= 4001
MongoDBURI="mongodb+srv://prajwalkoppad30:[email protected]/?retryWrites=true&w=majority&appName=Cluster0"
JWT_SECRET=sleepyboiswinsleeplesscodingsaga
24 changes: 24 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
164 changes: 164 additions & 0 deletions backend/controller/leave.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import LeaveApplication from "../models/leave.model.js";
import User from "../models/user.model.js";

// Apply for Leave
export const applyForLeave = async (req, res) => {
try {
const { placeOfVisit, reason, dateOfLeaving, arrivalDate, emergencyContact } = req.body;
const userID = req.body.userID; // Ensure you get this from local storage

// Fetch user details to check if required fields are filled
const user = await User.findById(userID);

// Check if required user details are filled
if (!user.phnumber || !user.roomNumber || !user.year || !user.course || !user.hostel) {
return res.status(400).json({ message: "Please complete your profile with phone number, room number, year, and course before applying for leave." });
}

// Check for pending leave applications for the user
const existingApplication = await LeaveApplication.findOne({
userID,
status: 'Pending'
});

if (existingApplication) {
return res.status(400).json({ message: "You already have a pending leave application." });
}

// If no pending leave application, proceed to create a new leave application
const newApplication = new LeaveApplication({
userID,
placeOfVisit,
reason,
dateOfLeaving,
arrivalDate,
emergencyContact
});

await User.findByIdAndUpdate(userID, { leaveApplication: newApplication._id });

await newApplication.save();
res.status(201).json({
message: "Leave application submitted successfully.",
application: newApplication
});
} catch (error) {
console.log("error:", error.message);
res.status(500).json({ message: "Internal server error" });
}
};

// Get Leave Status for a User
export const getLeaveStatus = async (req, res) => {
try {
const { userID } = req.body; // Ensure you're getting userID correctly

// Use findOne to get only one leave application for the user
const leaveApplication = await LeaveApplication.findOne({ userID });

if (!leaveApplication) {
return res.status(404).json({ message: "No leave applications found" });
}

res.status(200).json(leaveApplication); // Send the single leave application as a JSON response
} catch (error) {
console.log("Error:", error.message);
res.status(500).json({ message: "Internal server error" });
}
};


// Warden approval
export const updateLeaveStatus = async (req, res) => {
try {
const { applicationId } = req.params; // Get applicationId from request parameters
const { status } = req.body; // Get status from request body

// Validate the status
if (!['Pending', 'Approved', 'Rejected'].includes(status)) {
return res.status(400).json({ message: "Invalid status. Must be either 'Pending', 'Approved', or 'Rejected'." });
}

// Find the leave application and update the status
const updatedApplication = await LeaveApplication.findByIdAndUpdate(
applicationId,
{ status },
{ new: true } // Return the updated document
);

if (!updatedApplication) {
return res.status(404).json({ message: "Leave application not found" });
}

res.status(200).json({
message: "Leave application status updated successfully.",
application: updatedApplication
});
} catch (error) {
console.log("Error:", error.message);
res.status(500).json({ message: "Internal server error" });
}
};

// Leave extension
export const updateLeaveExtension = async (req, res) => {
try {
const { applicationId } = req.params;
const { newArrivalDate } = req.body;

const leaveApplication = await LeaveApplication.findById(applicationId);

if (!leaveApplication) {
return res.status(404).json({ message: "Leave application not found" });
}

// Update the arrival date and mark the status as 'Extension'
leaveApplication.arrivalDate = newArrivalDate;
leaveApplication.status = 'Extension';

await leaveApplication.save();

res.status(200).json({
message: "Leave extension requested successfully.",
application: leaveApplication
});
} catch (error) {
console.log("Error:", error.message);
res.status(500).json({ message: "Internal server error" });
}
};

// All leave applications for warden
export const getAllLeaveApplications = async (req, res) => {
try {
const applications = await LeaveApplication.find().populate("userID", "name rollNumber email");
res.status(200).json(applications);
} catch (error) {
console.error("Error fetching leave applications:", error);
res.status(500).json({ message: "Server error" });
}
};

// Scan leave application
export const scanLeaveApplication = async (req, res) => {
try {
const { applicationId } = req.params; // Get applicationId from request parameters
const leaveApplication = await LeaveApplication.findById(applicationId);

if (!leaveApplication) {
return res.status(404).json({ message: "Leave application not found" });
}

// Update the scanned field with the current date and time
leaveApplication.scanned = new Date();
await leaveApplication.save();

res.status(200).json({
message: "Leave application scanned successfully.",
application: leaveApplication
});
} catch (error) {
console.log("Error:", error.message);
res.status(500).json({ message: "Internal server error" });
}
};
106 changes: 106 additions & 0 deletions backend/controller/outing.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import OutingRequest from "../models/outing.model.js";
import User from "../models/user.model.js";

// Apply for Outing
export const applyForOuting = async (req, res) => {
try {
const { placeOfVisit, reason, emergencyContact } = req.body;
const userID = req.body.userID; // will be fetched from local storage on frontend

// Check if the user already has an approved outing request
const existingApplication = await OutingRequest.findOne({
userID,
status: 'Approved'
});

if (existingApplication) {
return res.status(400).json({ message: "You already have an existing outing application." });
}

// If no existing approved application, create a new outing request
const newApplication = new OutingRequest({
userID,
placeOfVisit,
reason,
outTime: new Date(), // Set outTime to the current date and time
inTime: "", // You can also set a default value for inTime if needed
emergencyContact
});

// Save the outing request ID to the user's document
await User.findByIdAndUpdate(userID, { outingRequest: newApplication._id });

await newApplication.save();
res.status(201).json({
message: "Outing application submitted successfully.",
application: newApplication
});
} catch (error) {
console.log("error:", error.message);
res.status(500).json({ message: "Internal server error" });
}
};

// Get Outing Status
export const getOutingStatus = async (req, res) => {
try {
const { userID } = req.body; // Ensure you're getting userID correctly

// Use findOne to get only one outing request for the user
const outingRequest = await OutingRequest.findOne({ userID }).populate('userID', 'name email'); // Populate user details if needed

if (!outingRequest) {
return res.status(404).json({ message: "No outing applications found." });
}

// Return the single outing request
res.status(200).json({
message: "Outing application retrieved successfully.",
outingRequest
});
} catch (error) {
console.log("Error:", error.message);
res.status(500).json({ message: "Internal server error." });
}
};


export const getAllOutings = async (req, res) => {
try {
// Fetch all outing requests from the database
const allOutings = await OutingRequest.find();

if (allOutings.length === 0) {
return res.status(404).json({ message: "No outing applications found." });
}

// Return the list of all outing requests
res.status(200).json(allOutings);
} catch (error) {
console.log("Error:", error.message);
res.status(500).json({ message: "Internal server error." });
}
};

export const scanOutingApplication = async (req, res) => {
try {
const { applicationId } = req.params; // Get applicationId from request parameters
const outingApplication = await OutingRequest.findById(applicationId);

if (!outingApplication) {
return res.status(404).json({ message: "Outing application not found" });
}

// Update the scanned field with the current date and time
outingApplication.scanned = new Date();
await outingApplication.save();

res.status(200).json({
message: "Outing application scanned successfully.",
application: outingApplication
});
} catch (error) {
console.log("Error:", error.message);
res.status(500).json({ message: "Internal server error" });
}
};
Loading

0 comments on commit 508cb48

Please sign in to comment.