40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
import authFetch from "../../../utils/authFetch.js";
|
|
|
|
async function va_credits_get(req, res) {
|
|
try {
|
|
const clientId = req.body.clientId;
|
|
const salesforceUrl = `${process.env.SF_DOCTRACKER_API_URL}services/apexrest/docva/api/credit/${clientId}`;
|
|
|
|
const response = await authFetch(salesforceUrl, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
// Add any required headers for Salesforce authentication
|
|
// 'Authorization': 'Bearer ' + process.env.SF_ACCESS_TOKEN
|
|
},
|
|
});
|
|
|
|
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);
|
|
|
|
res.status(201).json({
|
|
success: true,
|
|
data: result.data
|
|
});
|
|
} 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;
|
|
}
|
|
}
|
|
|
|
export { va_credits_get }; |