diff --git a/src/controllers/va/vaLeaves/vaLeavesApply.controller.js b/src/controllers/va/vaLeaves/vaLeavesApply.controller.js index 35ba56d..d9409ea 100644 --- a/src/controllers/va/vaLeaves/vaLeavesApply.controller.js +++ b/src/controllers/va/vaLeaves/vaLeavesApply.controller.js @@ -147,55 +147,55 @@ 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); + // 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 - }); + // 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) @@ -206,19 +206,19 @@ async function va_leave_apply_function(req, res) { 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);