Fixed Adjustment timesheet issues

This commit is contained in:
Aakash Mittal 2025-11-14 19:48:45 +05:30
parent 28226c30f8
commit f9b8cd886a
2 changed files with 52 additions and 6 deletions

View File

@ -1,6 +1,24 @@
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import { VAModel, ProjectModel, LoggedModel } from "../../../models/vaModel.js"; import { VAModel, ProjectModel, LoggedModel } from "../../../models/vaModel.js";
// Helper function to parse date string in DD-MM-YYYY format
function parseDate(dateString) {
if (!dateString) return null;
const parts = dateString.split('-');
if (parts.length !== 3) return null;
const day = parseInt(parts[0], 10);
const month = parseInt(parts[1], 10) - 1; // Months are 0-indexed in JavaScript
const year = parseInt(parts[2], 10);
// Validate date components
if (isNaN(day) || isNaN(month) || isNaN(year)) return null;
if (day < 1 || day > 31 || month < 0 || month > 11 || year < 1900) return null;
return new Date(year, month, day);
}
// Getting logged hours in a specific project of VA // Getting logged hours in a specific project of VA
async function va_log_work_report_callback(req, res) { async function va_log_work_report_callback(req, res) {
try { try {
@ -27,15 +45,27 @@ async function va_log_work_report_callback(req, res) {
const payload = req.body || {}; const payload = req.body || {};
const { fromDate, toDate } = payload; const { fromDate, toDate } = payload;
const start = fromDate ? new Date(fromDate) : new Date(); let start = fromDate ? parseDate(fromDate) : new Date();
const end = toDate ? new Date(toDate) : new Date(); let end = toDate ? parseDate(toDate) : new Date();
if (!fromDate && !toDate) { if ((fromDate && (!start || isNaN(start.getTime()))) || (toDate && (!end || isNaN(end.getTime())))) {
start.setHours(0, 0, 0, 0); return res.status(400).json({
end.setHours(23, 59, 59, 999); success: false,
message: "Invalid Date format. Use DD-MM-YYYY"
});
} }
const logs = await LoggedModel.find({ if (!fromDate && !toDate) {
// If no dates provided, default to today
start.setHours(0, 0, 0, 0);
end.setHours(23, 59, 59, 999);
} else {
// If dates are provided, set proper time boundaries
if (fromDate) start.setHours(0, 0, 0, 0);
if (toDate) end.setHours(23, 59, 59, 999);
}
let logs = await LoggedModel.find({
vaId, vaId,
time: { $gte: start, $lte: end }, time: { $gte: start, $lte: end },
}) })
@ -58,6 +88,13 @@ async function va_log_work_report_callback(req, res) {
let lastLogin = null; let lastLogin = null;
let lastProjectId = null; let lastProjectId = null;
const hasAdjustmentLogs = logs.some(log => log.isAdjustment === true);
if(hasAdjustmentLogs) {
logs = logs.filter(log => log.isAdjustment === true);
}
// ⏱️ Calculate work duration // ⏱️ Calculate work duration
for (const log of logs) { for (const log of logs) {
const time = new Date(log.time); const time = new Date(log.time);
@ -109,6 +146,7 @@ async function va_log_work_report_callback(req, res) {
}); });
} catch (error) { } catch (error) {
console.log(error)
return res.status(500).send("Internal Server Error"); return res.status(500).send("Internal Server Error");
} }
} }

View File

@ -237,6 +237,14 @@ const loggedSchema = new Schema({
projectId: { projectId: {
type: Types.ObjectId, type: Types.ObjectId,
ref: "Project" ref: "Project"
},
isAdjustment: {
type: Boolean,
default: false
},
adjustmentId: {
type: Types.ObjectId,
ref: "AdjustedHours"
} }
}) })