133 lines
4.6 KiB
JavaScript
133 lines
4.6 KiB
JavaScript
import jwt from "jsonwebtoken";
|
|
import { ProjectModel, VAModel, LoggedModel } from "../../../models/vaModel.js";
|
|
|
|
function formatHMS(ms) {
|
|
const totalSeconds = Math.floor(ms / 1000);
|
|
const h = Math.floor(totalSeconds / 3600);
|
|
const m = Math.floor((totalSeconds % 3600) / 60);
|
|
const s = totalSeconds % 60;
|
|
return `${h}h ${m}m ${s}s`;
|
|
}
|
|
|
|
async function va_projects_log_list_function(req, res) {
|
|
try {
|
|
const { projectId, from, to } = req.params;
|
|
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;
|
|
|
|
// Date filtering
|
|
const now = new Date();
|
|
const fromDate = from && from !== "undefined" ? new Date(from) : new Date(now.setHours(0, 0, 0, 0));
|
|
const toDate = to && to !== "undefined" ? new Date(to) : new Date();
|
|
toDate.setHours(23, 59, 59, 999);
|
|
|
|
let projectList = [];
|
|
if (projectId) {
|
|
const project = await ProjectModel.findById(projectId);
|
|
if (!project) {
|
|
return res.status(404).json({ success: false, message: "Project not found" });
|
|
}
|
|
|
|
// Check if VA is assigned to this project
|
|
const isAssigned = project.vaAssigned.some(id => id.toString() === vaId.toString());
|
|
if (!isAssigned) {
|
|
return res.status(403).json({ success: false, message: "VA not assigned to this project" });
|
|
}
|
|
|
|
projectList = [project];
|
|
} else {
|
|
|
|
const all = await ProjectModel.find().lean();
|
|
console.log(all.map(p => p.vaAssigned));
|
|
|
|
console.log("VA ID:", vaId.toString());
|
|
|
|
projectList = await ProjectModel.find({ vaAssigned: vaUser._id });
|
|
}
|
|
|
|
if (!projectList.length) {
|
|
return res.status(200).json({ success: true, message: "No assigned projects found", data: [] });
|
|
}
|
|
|
|
const finalReport = [];
|
|
|
|
for (const project of projectList) {
|
|
const logs = await LoggedModel.find({
|
|
vaId,
|
|
projectId: project._id,
|
|
time: { $gte: fromDate, $lte: toDate }
|
|
})
|
|
.sort({ time: 1 })
|
|
.lean();
|
|
|
|
let totalWorkMs = 0;
|
|
let currentLogin = null;
|
|
|
|
for (const entry of logs) {
|
|
if (entry.loggedType === "LOGIN") {
|
|
currentLogin = entry;
|
|
} else if (entry.loggedType === "LOGOUT" && currentLogin) {
|
|
totalWorkMs += new Date(entry.time) - new Date(currentLogin.time);
|
|
currentLogin = null;
|
|
}
|
|
}
|
|
|
|
// still logged in → count till now
|
|
if (currentLogin) {
|
|
totalWorkMs += new Date() - new Date(currentLogin.time);
|
|
}
|
|
|
|
const lastLog = logs[logs.length - 1];
|
|
const activityStatus = lastLog?.loggedType === "LOGIN" ? "ACTIVE" : "AWAY";
|
|
|
|
finalReport.push({
|
|
projectId: project._id,
|
|
projectName: project.name,
|
|
activityStatus,
|
|
totalWorkTime: formatHMS(totalWorkMs),
|
|
totalWorkMs,
|
|
totalLogs: logs.length
|
|
});
|
|
}
|
|
|
|
// Summary for current VA
|
|
const totalProjects = finalReport.length;
|
|
const activeProjects = finalReport.filter(r => r.activityStatus === "ACTIVE").length;
|
|
const awayProjects = totalProjects - activeProjects;
|
|
const totalMs = finalReport.reduce((acc, r) => acc + r.totalWorkMs, 0);
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
vaId,
|
|
vaName: `${vaUser.userDetails.firstName} ${vaUser.userDetails.lastName}`,
|
|
totalProjects,
|
|
activeProjects,
|
|
awayProjects,
|
|
totalWorkTime: formatHMS(totalMs),
|
|
totalWorkMs: totalMs,
|
|
data: finalReport
|
|
});
|
|
|
|
} catch (err) {
|
|
res.status(500).json({ success: false, message: err.message });
|
|
}
|
|
}
|
|
|
|
export { va_projects_log_list_function };
|