diff --git a/src/controllers/va/vaLog/vaLogWorkReport.js b/src/controllers/va/vaLog/vaLogWorkReport.js index fd9344f..8064a39 100644 --- a/src/controllers/va/vaLog/vaLogWorkReport.js +++ b/src/controllers/va/vaLog/vaLogWorkReport.js @@ -1,6 +1,24 @@ import jwt from "jsonwebtoken"; 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 async function va_log_work_report_callback(req, res) { try { @@ -27,15 +45,27 @@ async function va_log_work_report_callback(req, res) { const payload = req.body || {}; const { fromDate, toDate } = payload; - const start = fromDate ? new Date(fromDate) : new Date(); - const end = toDate ? new Date(toDate) : new Date(); + let start = fromDate ? parseDate(fromDate) : new Date(); + let end = toDate ? parseDate(toDate) : new Date(); - if (!fromDate && !toDate) { - start.setHours(0, 0, 0, 0); - end.setHours(23, 59, 59, 999); + if ((fromDate && (!start || isNaN(start.getTime()))) || (toDate && (!end || isNaN(end.getTime())))) { + return res.status(400).json({ + 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, time: { $gte: start, $lte: end }, }) @@ -58,6 +88,13 @@ async function va_log_work_report_callback(req, res) { let lastLogin = null; let lastProjectId = null; + const hasAdjustmentLogs = logs.some(log => log.isAdjustment === true); + + if(hasAdjustmentLogs) { + logs = logs.filter(log => log.isAdjustment === true); + } + + // ⏱️ Calculate work duration for (const log of logs) { const time = new Date(log.time); @@ -109,6 +146,7 @@ async function va_log_work_report_callback(req, res) { }); } catch (error) { + console.log(error) return res.status(500).send("Internal Server Error"); } } diff --git a/src/models/vaModel.js b/src/models/vaModel.js index 3f6a423..5bb7bac 100644 --- a/src/models/vaModel.js +++ b/src/models/vaModel.js @@ -237,6 +237,14 @@ const loggedSchema = new Schema({ projectId: { type: Types.ObjectId, ref: "Project" + }, + isAdjustment: { + type: Boolean, + default: false + }, + adjustmentId: { + type: Types.ObjectId, + ref: "AdjustedHours" } })