Leave API UCment
This commit is contained in:
parent
2ca5146846
commit
a7d770498c
|
|
@ -147,64 +147,39 @@ 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/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 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})
|
||||
// });
|
||||
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}`);
|
||||
// }
|
||||
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
|
||||
});
|
||||
} 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;
|
||||
}
|
||||
// console.log("✅ Successfully synced VA data to Salesforce:", result);
|
||||
|
||||
// return res.status(201).json({
|
||||
// success: true,
|
||||
|
|
@ -215,10 +190,35 @@ async function va_leave_apply_function(req, res) {
|
|||
// dateTo: endDate.toISOString().split('T')[0],
|
||||
// totalDays: totalDays,
|
||||
// leaveType,
|
||||
// comment,
|
||||
// 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"
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error("Leave apply error:", error);
|
||||
|
|
|
|||
Loading…
Reference in New Issue