added leave route
This commit is contained in:
parent
aecb4af2ac
commit
c1cf57e0f4
|
|
@ -0,0 +1,42 @@
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
import { VAModel, LeavesModel } from "../../../models/vaModel.js";
|
||||||
|
|
||||||
|
// Adjust missing hours by creating new records
|
||||||
|
async function va_leave_apply_function(req, res) {
|
||||||
|
try {
|
||||||
|
const accessToken = req.headers.token;
|
||||||
|
|
||||||
|
if (!accessToken) {
|
||||||
|
return res.status(401).json({ success: false, message: "Missing token" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const decoded = jwt.verify(accessToken, process.env.JWT_SECRET_ACCESS_TOKEN);
|
||||||
|
if (!decoded || !decoded.email) {
|
||||||
|
return res.status(401).json({ success: false, message: "Invalid token" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const vaEmail = decoded.email;
|
||||||
|
const vaUser = await VAModel.findOne({ "userDetails.email": vaEmail });
|
||||||
|
if (!vaUser) {
|
||||||
|
return res.status(404).json({ success: false, message: "VA not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const vaId = vaUser._id;
|
||||||
|
const { dateFrom, dateTo, days, leaveType, comment } = req.body;
|
||||||
|
|
||||||
|
if (!dateFrom || !dateTo || !days || !leaveType || !comment) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: "dateFrom, dateTo, days, leaveType, and comment are required"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Leave apply error:", error);
|
||||||
|
return res.status(500).json({ success: false, message: "Internal Server Error" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { va_leave_apply_function };
|
||||||
|
|
@ -423,6 +423,52 @@ const adjustedHoursSchema = new Schema({
|
||||||
}]
|
}]
|
||||||
}, { timestamps: true });
|
}, { timestamps: true });
|
||||||
|
|
||||||
|
const leavesSchema = new Schema({
|
||||||
|
vaId: {
|
||||||
|
type: Types.ObjectId,
|
||||||
|
ref: "VA",
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
clientId: {
|
||||||
|
type: Types.ObjectId,
|
||||||
|
ref: "Client",
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
dateFrom: {
|
||||||
|
type: Date,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
dateTo: {
|
||||||
|
type: Date,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
days: {
|
||||||
|
type: String,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
comment: {
|
||||||
|
type: String,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
leaveType: {
|
||||||
|
type: String,
|
||||||
|
enum: ["SICK", "PTO"],
|
||||||
|
default: "SICK",
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
type: String,
|
||||||
|
enum: ["PENDING", "APPROVED", "REJECTED"],
|
||||||
|
default: "PENDING"
|
||||||
|
},
|
||||||
|
reviewedAt: {
|
||||||
|
type: Date
|
||||||
|
},
|
||||||
|
rejectionReason: {
|
||||||
|
type: String // If rejected, store the reason
|
||||||
|
}
|
||||||
|
}, { timestamps: true });
|
||||||
|
|
||||||
/* ---------------------- Model Exports ---------------------- */
|
/* ---------------------- Model Exports ---------------------- */
|
||||||
const VAModel = model("VA", vaSchema);
|
const VAModel = model("VA", vaSchema);
|
||||||
const ClientModel = model("Client", clientSchema);
|
const ClientModel = model("Client", clientSchema);
|
||||||
|
|
@ -431,5 +477,6 @@ const LoggedModel = model("TrackLog", loggedSchema);
|
||||||
const WorkTrackModel = model("TrackWork", vaWorkTrackSchema);
|
const WorkTrackModel = model("TrackWork", vaWorkTrackSchema);
|
||||||
const PasswordResetModel = model("PasswordReset", passwordResetSchema);
|
const PasswordResetModel = model("PasswordReset", passwordResetSchema);
|
||||||
const AdjustedHoursModel = model("AdjustedHours", adjustedHoursSchema);
|
const AdjustedHoursModel = model("AdjustedHours", adjustedHoursSchema);
|
||||||
|
const LeavesModel = model("Leaves", leavesSchema);
|
||||||
|
|
||||||
export { VAModel, ClientModel, ProjectModel, LoggedModel, WorkTrackModel, PasswordResetModel, AdjustedHoursModel };
|
export { VAModel, ClientModel, ProjectModel, LoggedModel, WorkTrackModel, PasswordResetModel, AdjustedHoursModel, LeavesModel };
|
||||||
Loading…
Reference in New Issue