Compare commits
2 Commits
2ca5146846
...
c04264f054
| Author | SHA1 | Date |
|---|---|---|
|
|
c04264f054 | |
|
|
a7d770498c |
|
|
@ -6,7 +6,7 @@
|
|||
"scripts": {
|
||||
"start": "nodemon --inspect src/server.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"deploy": "curl -X POST http://16.170.168.132:9000/hooks/deploy-va-middleware",
|
||||
"deploy": "curl -X POST http://35.162.222.18:9000/hooks/deploy-va-middleware",
|
||||
"build": "npx webpack --mode production",
|
||||
"dev": "npx webpack --mode development"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { VAModel, LeavesModel } from "../../../models/vaModel.js";
|
|||
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" });
|
||||
}
|
||||
|
|
@ -25,9 +25,9 @@ async function va_leave_apply_function(req, res) {
|
|||
const { dateFrom, dateTo, leaveType, comment } = req.body;
|
||||
|
||||
if (!dateFrom || !dateTo || !leaveType || !comment) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "dateFrom, dateTo, leaveType, and comment are required"
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "dateFrom, dateTo, leaveType, and comment are required"
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -36,49 +36,49 @@ async function va_leave_apply_function(req, res) {
|
|||
try {
|
||||
startDate = new Date(dateFrom);
|
||||
endDate = new Date(dateTo);
|
||||
|
||||
|
||||
// Set to start of day for accurate calculation
|
||||
startDate.setHours(0, 0, 0, 0);
|
||||
endDate.setHours(0, 0, 0, 0);
|
||||
|
||||
|
||||
if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid date format. Use valid date format (YYYY-MM-DD)"
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid date format. Use valid date format (YYYY-MM-DD)"
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid date format"
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid date format"
|
||||
});
|
||||
}
|
||||
|
||||
// Ensure start date is before or equal to end date
|
||||
if (startDate > endDate) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Start date must be before or equal to end date"
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Start date must be before or equal to end date"
|
||||
});
|
||||
}
|
||||
|
||||
// Ensure dates are not in the past
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
|
||||
|
||||
if (startDate < today) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Cannot apply for leave for past dates"
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Cannot apply for leave for past dates"
|
||||
});
|
||||
}
|
||||
|
||||
// Validate leave type
|
||||
const validLeaveTypes = ["SICK", "PTO"];
|
||||
if (!validLeaveTypes.includes(leaveType)) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid leave type. Must be SICK or PTO"
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid leave type. Must be SICK or PTO"
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -87,9 +87,9 @@ async function va_leave_apply_function(req, res) {
|
|||
const totalDays = Math.floor(timeDifference / (1000 * 60 * 60 * 24)) + 1; // +1 to include both start and end dates
|
||||
|
||||
if (totalDays <= 0) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid date range"
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Invalid date range"
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -106,9 +106,9 @@ async function va_leave_apply_function(req, res) {
|
|||
});
|
||||
|
||||
if (existingLeave) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "You already have a pending leave request that overlaps with these dates"
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "You already have a pending leave request that overlaps with these dates"
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -125,9 +125,9 @@ async function va_leave_apply_function(req, res) {
|
|||
});
|
||||
|
||||
if (existingApprovedLeave) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "You already have an approved leave that overlaps with these dates"
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "You already have an approved leave that overlaps with these dates"
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -147,94 +147,95 @@ async function va_leave_apply_function(req, res) {
|
|||
await leaveRequest.save();
|
||||
|
||||
try {
|
||||
// const salesforceUrl = `${process.env.SF_DOCTRACKER_API_URL}services/apexrest/docva/api/timesheet-adjustment`;
|
||||
// const sfData = {
|
||||
// "id": leaveRequest._id,
|
||||
// "va": {
|
||||
// "id": vaId
|
||||
// },
|
||||
// "dateFrom": startDate.toISOString().split('T')[0],
|
||||
// "dateTo": endDate.toISOString().split('T')[0],
|
||||
// "totalDays": totalDays,
|
||||
// "leaveType": leaveType,
|
||||
// "comment": processedComment.plainText, // Send plain text to Salesforce
|
||||
// "attachments": processedComment.extractedImages, // Send extracted images info
|
||||
// "status": "PENDING",
|
||||
// "rejectionReason": ""
|
||||
// }
|
||||
const salesforceUrl = `${process.env.SF_DOCTRACKER_API_URL}services/apexrest/docva/api/leave-request`;
|
||||
const sfData = {
|
||||
"leaveId": leaveRequest._id.toString(),
|
||||
// "va": {
|
||||
// "id": vaId
|
||||
// },
|
||||
"va": vaId.toString(),
|
||||
"dateFrom": startDate.toISOString().split('T')[0],
|
||||
"dateTo": endDate.toISOString().split('T')[0],
|
||||
"totalDays": totalDays,
|
||||
"leaveType": leaveType,
|
||||
"comment": processedComment.plainText, // Send plain text to Salesforce
|
||||
"attachments": processedComment.extractedImages, // Send extracted images info
|
||||
"status": "PENDING",
|
||||
"rejectionReason": ""
|
||||
}
|
||||
|
||||
// const response = await fetch(salesforceUrl, {
|
||||
// method: 'POST',
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/json',
|
||||
// // Add any required headers for Salesforce authentication
|
||||
// // 'Authorization': 'Bearer ' + process.env.SF_ACCESS_TOKEN
|
||||
// },
|
||||
// body: JSON.stringify({"data": sfData})
|
||||
// });
|
||||
|
||||
// if (!response.ok) {
|
||||
// const errorText = await response.text();
|
||||
// throw new Error(`Salesforce API responded with status ${response.status}: ${errorText}`);
|
||||
// }
|
||||
const response = await fetch(salesforceUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
// Add any required headers for Salesforce authentication
|
||||
// 'Authorization': 'Bearer ' + process.env.SF_ACCESS_TOKEN
|
||||
},
|
||||
body: JSON.stringify({ "data": sfData })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Salesforce API responded with status ${response.status}: ${errorText}`);
|
||||
}
|
||||
|
||||
// const result = await response.json();
|
||||
// // console.log("✅ Successfully synced VA data to Salesforce:", result);
|
||||
|
||||
return res.status(201).json({
|
||||
success: true,
|
||||
message: "Leave request submitted successfully",
|
||||
data: {
|
||||
leaveId: leaveRequest._id,
|
||||
dateFrom: startDate.toISOString().split('T')[0],
|
||||
dateTo: endDate.toISOString().split('T')[0],
|
||||
totalDays: totalDays,
|
||||
leaveType,
|
||||
comment: processedComment.processedHTML,
|
||||
// attachments: processedComment.extractedImages,
|
||||
status: "PENDING"
|
||||
},
|
||||
// SyncSalesforce: result
|
||||
});
|
||||
// console.log("✅ Successfully synced VA data to Salesforce:", result);
|
||||
|
||||
// return res.status(201).json({
|
||||
// success: true,
|
||||
// message: "Leave request submitted successfully",
|
||||
// data: {
|
||||
// leaveId: leaveRequest._id,
|
||||
// dateFrom: startDate.toISOString().split('T')[0],
|
||||
// dateTo: endDate.toISOString().split('T')[0],
|
||||
// totalDays: totalDays,
|
||||
// leaveType,
|
||||
// comment: processedComment.processedHTML,
|
||||
// // attachments: processedComment.extractedImages,
|
||||
// status: "PENDING"
|
||||
// },
|
||||
// // SyncSalesforce: result
|
||||
// });
|
||||
} catch (error) {
|
||||
console.error("Failed to sync with Salesforce:", error.message);
|
||||
console.log(error)
|
||||
|
||||
|
||||
// Create a custom error to distinguish Salesforce errors
|
||||
const salesforceError = new Error(`Salesforce sync failed: ${error.message}`);
|
||||
salesforceError.isSalesforceError = true;
|
||||
throw salesforceError;
|
||||
}
|
||||
|
||||
// return res.status(201).json({
|
||||
// success: true,
|
||||
// message: "Leave request submitted successfully",
|
||||
// data: {
|
||||
// leaveId: leaveRequest._id,
|
||||
// dateFrom: startDate.toISOString().split('T')[0],
|
||||
// dateTo: endDate.toISOString().split('T')[0],
|
||||
// totalDays: totalDays,
|
||||
// leaveType,
|
||||
// comment,
|
||||
// status: "PENDING"
|
||||
// }
|
||||
// });
|
||||
return res.status(201).json({
|
||||
success: true,
|
||||
message: "Leave request submitted successfully",
|
||||
data: {
|
||||
leaveId: leaveRequest._id,
|
||||
dateFrom: startDate.toISOString().split('T')[0],
|
||||
dateTo: endDate.toISOString().split('T')[0],
|
||||
totalDays: totalDays,
|
||||
leaveType,
|
||||
comment,
|
||||
status: "PENDING"
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error("Leave apply error:", error);
|
||||
|
||||
|
||||
if (error.name === 'JsonWebTokenError') {
|
||||
return res.status(401).json({ success: false, message: "Invalid token" });
|
||||
}
|
||||
|
||||
|
||||
if (error.name === 'TokenExpiredError') {
|
||||
return res.status(401).json({ success: false, message: "Token expired" });
|
||||
}
|
||||
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Internal Server Error",
|
||||
error: error.message
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -277,7 +278,7 @@ function extractPlainTextFromHTML(html) {
|
|||
.replace(/<[^>]*>/g, ' ') // Replace tags with spaces
|
||||
.replace(/\s+/g, ' ') // Collapse multiple spaces
|
||||
.trim();
|
||||
|
||||
|
||||
// Decode common HTML entities
|
||||
plainText = plainText
|
||||
.replace(/ /g, ' ')
|
||||
|
|
@ -304,7 +305,7 @@ function extractAndProcessBase64Images(html) {
|
|||
try {
|
||||
// Regex to find base64 images in HTML (various formats)
|
||||
const base64Regex = /<img[^>]+src="data:image\/([a-zA-Z0-9+]+);base64,([^"]+)"[^>]*>/gi;
|
||||
|
||||
|
||||
let match;
|
||||
let imageIndex = 0;
|
||||
|
||||
|
|
@ -316,7 +317,7 @@ function extractAndProcessBase64Images(html) {
|
|||
// Create a unique filename
|
||||
const timestamp = Date.now();
|
||||
const filename = `leave_image_${timestamp}_${imageIndex}.${imageType}`;
|
||||
|
||||
|
||||
// Calculate approximate file size
|
||||
const size = Math.floor((base64Data.length * 3) / 4); // Base64 size calculation
|
||||
|
||||
|
|
@ -332,7 +333,7 @@ function extractAndProcessBase64Images(html) {
|
|||
|
||||
// Replace base64 with a reference in HTML
|
||||
const replacementHTML = `<img src="attachment://${filename}" alt="Leave evidence ${imageIndex + 1}" data-filename="${filename}" data-image-index="${imageIndex}" class="leave-image">`;
|
||||
|
||||
|
||||
processedHTML = processedHTML.replace(fullMatch, replacementHTML);
|
||||
|
||||
imageIndex++;
|
||||
|
|
|
|||
|
|
@ -138,12 +138,12 @@ async function va_timesheet_adjustment_function(req, res) {
|
|||
try {
|
||||
const salesforceUrl = `${process.env.SF_DOCTRACKER_API_URL}services/apexrest/docva/api/timesheet-adjustment`;
|
||||
const sfData = {
|
||||
"id": adjustmentRequest._id,
|
||||
"id": adjustmentRequest._id.toString(),
|
||||
"va": {
|
||||
"id": vaId
|
||||
"id": vaId.toString()
|
||||
},
|
||||
"project": {
|
||||
"id": projectId
|
||||
"id": projectId.toString()
|
||||
},
|
||||
"date": adjustmentDate.toISOString().split('T')[0],
|
||||
"loginTime": loginDateTime.toISOString(),
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>Download DocTracker</title>
|
||||
<title>DocTracker</title>
|
||||
|
||||
<style>
|
||||
body {
|
||||
|
|
|
|||
Loading…
Reference in New Issue