80 lines
2.7 KiB
JavaScript
80 lines
2.7 KiB
JavaScript
import mongoose from "mongoose";
|
|
import { AdjustedHoursModel } from "../../../models/vaModel.js";
|
|
|
|
// Get all adjustment requests (with filtering options)
|
|
async function get_adjustment_requests_list(req, res) {
|
|
try {
|
|
const { status, projectId, vaId } = req.query;
|
|
|
|
// Build filter object
|
|
const filter = {};
|
|
// if (status) filter.status = status;
|
|
// if (projectId) filter.projectId = projectId;
|
|
// if (vaId) filter.vaId = vaId;
|
|
|
|
if (status && status !== "undefined") filter.status = status;
|
|
if (projectId && mongoose.Types.ObjectId.isValid(projectId)) {
|
|
filter.projectId = new mongoose.Types.ObjectId(projectId);
|
|
}
|
|
if (vaId && mongoose.Types.ObjectId.isValid(vaId)) {
|
|
filter.vaId = new mongoose.Types.ObjectId(vaId);
|
|
}
|
|
|
|
const adjustments = await AdjustedHoursModel.find(filter)
|
|
.sort({ createdAt: -1 })
|
|
.populate('vaId', 'userDetails.name userDetails.email')
|
|
.populate('projectId', 'name')
|
|
.lean();
|
|
|
|
// Format response
|
|
const formattedData = adjustments.map(adj => ({
|
|
id: adj._id,
|
|
va: {
|
|
id: adj.vaId._id,
|
|
name: adj.vaId.userDetails?.name,
|
|
email: adj.vaId.userDetails?.email
|
|
},
|
|
project: {
|
|
id: adj.projectId._id,
|
|
name: adj.projectId.name
|
|
},
|
|
date: adj.date,
|
|
loginTime: adj.loginTime,
|
|
logoutTime: adj.logoutTime,
|
|
comment: adj.comment,
|
|
status: adj.status,
|
|
requestedAt: adj.createdAt,
|
|
reviewedAt: adj.reviewedAt,
|
|
rejectionReason: adj.rejectionReason,
|
|
adjustedHours: formatTime((new Date(adj.logoutTime) - new Date(adj.loginTime)) / 1000)
|
|
}));
|
|
|
|
return res.status(200).json({
|
|
success: true,
|
|
data: formattedData,
|
|
pagination: {
|
|
currentPage: adjustments.page,
|
|
totalPages: adjustments.totalPages,
|
|
totalItems: adjustments.totalDocs,
|
|
hasNext: adjustments.hasNextPage,
|
|
hasPrev: adjustments.hasPrevPage
|
|
}
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error("Get adjustment requests error:", error);
|
|
return res.status(500).json({
|
|
success: false,
|
|
message: "Internal Server Error"
|
|
});
|
|
}
|
|
}
|
|
|
|
function formatTime(seconds) {
|
|
const h = Math.floor(seconds / 3600);
|
|
const m = Math.floor((seconds % 3600) / 60);
|
|
const s = Math.floor(seconds % 60);
|
|
return `${h}h ${m}m ${s}s`;
|
|
}
|
|
|
|
export { get_adjustment_requests_list } |