Added authFetch currently getting access toke for all request as health check api is not available on sf ye

This commit is contained in:
=bhagwanSingh 2025-12-16 19:01:13 +05:30
parent 166cf4db45
commit 320d44f4b5
12 changed files with 72 additions and 16 deletions

View File

@ -71,7 +71,7 @@ async function project_add_sf(req, res) {
hourlyRate: hourlyRate || '0',
clientId: clientObjectId,
vaAssigned: vaAssigned,
dedicatedHours: dedicatedHours ? parseFloat(dedicatedHours) : 0,
dedicatedHours: dedicatedHours || ["0", "0", "0", "0", "0", "0", "0"],
vaWorkingScheduleStart: vaWorkingScheduleStart || null,
vaWorkingScheduleEnd: vaWorkingScheduleEnd || null,
isFlexibleAllowed: isFlexibleAllowed || true,

View File

@ -74,7 +74,7 @@ async function update_project_sf(req, res) {
// Update dedicated hours if provided
if (dedicatedHours !== undefined) {
updateData.dedicatedHours = parseFloat(dedicatedHours) || 0;
updateData.dedicatedHours = dedicatedHours || ["0", "0", "0", "0", "0", "0", "0"];
}
// Update total logged hours if provided

View File

@ -1,6 +1,7 @@
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import { VAModel, LoggedModel, WorkTrackModel } from "../../../models/vaModel.js";
import authFetch from "../../../utils/authFetch.js";
// function to close open work tracks and create logout
async function closePreviousSession(vaId, lastLog) {
@ -53,7 +54,7 @@ async function closePreviousSession(vaId, lastLog) {
"time": logoutLog.time,
}
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',

View File

@ -1,5 +1,6 @@
import jwt from "jsonwebtoken";
import { VAModel, LeavesModel } from "../../../models/vaModel.js";
import authFetch from "../../../utils/authFetch.js";
// Apply for leave
async function va_leave_apply_function(req, res) {
@ -164,7 +165,7 @@ async function va_leave_apply_function(req, res) {
"rejectionReason": ""
}
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',

View File

@ -1,4 +1,5 @@
import { VAModel } from "../../../models/vaModel.js";
import authFetch from "../../../utils/authFetch.js";
async function va_payslip_data_function(req, res) {
try {
@ -11,7 +12,7 @@ async function va_payslip_data_function(req, res) {
const salesforceUrl = `${process.env.SF_DOCTRACKER_API_URL}services/apexrest/docva/api/va-payslip/${vaId}`;
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',

View File

@ -2,7 +2,7 @@ async function va_payroll_schedule_function(req, res) {
try {
const salesforceUrl = `${process.env.SF_DOCTRACKER_API_URL}services/apexrest/docva/api/payroll-schedule`;
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',

View File

@ -1,5 +1,6 @@
import bcrypt from "bcrypt";
import { VAModel } from "../../../models/vaModel.js";
import authFetch from "../../../utils/authFetch.js";
// Register a new VA
async function va_register(req, res) {
@ -53,7 +54,7 @@ async function va_register(req, res) {
const salesforceUrl = `${process.env.SF_DOCTRACKER_API_URL}services/apexrest/docva/api/virtualassistant`;
delete created.userDetails._id;
created.userDetails._id = created._id; // Include VA's MongoDB ID
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',

View File

@ -1,4 +1,5 @@
import { LoggedModel, WorkTrackModel, ProjectModel, VAModel } from "../../../models/vaModel.js";
import authFetch from "../../../utils/authFetch.js";
// Helper function to get start and end of day in UTC
function getDayRange(date = new Date()) {
@ -197,7 +198,7 @@ async function closePreviousSession(vaId, lastLog, logTime = new Date()) {
"time": logoutLog.time,
}
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@ -373,7 +374,7 @@ async function va_project_log_callback(req, res) {
"time": createdLog.time,
}
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@ -471,7 +472,7 @@ async function va_project_log_callback(req, res) {
"time": createdLog.time,
}
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@ -533,7 +534,7 @@ async function va_project_log_callback(req, res) {
"time": createdLog.time,
}
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',

View File

@ -1,5 +1,6 @@
import jwt from "jsonwebtoken";
import { VAModel, ProjectModel, AdjustedHoursModel } from "../../../models/vaModel.js";
import authFetch from "../../../utils/authFetch.js";
// Adjust missing hours by creating new records
async function va_timesheet_adjustment_function(req, res) {
@ -155,7 +156,7 @@ async function va_timesheet_adjustment_function(req, res) {
"adjustedHours": formatTime(adjustedSeconds)
}
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',

View File

@ -1,4 +1,5 @@
import { VAModel, TipsModel, ClientModel } from "../../../models/vaModel.js";
import authFetch from "../../../utils/authFetch.js";
// Approve or reject leave
async function va_tips_manage_function(req, res) {
@ -38,7 +39,7 @@ async function va_tips_manage_function(req, res) {
"tipAmount": Number(parseFloat(createdTip.amount).toFixed(2)), // Convert to cents
}
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@ -116,7 +117,7 @@ async function va_tips_delete(req, res) {
// "tipAmount": Number(parseFloat(createdTip.amount).toFixed(2)), // Convert to cents
// }
const response = await fetch(salesforceUrl, {
const response = await authFetch(salesforceUrl, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',

View File

@ -101,8 +101,8 @@ const projectSchema = new Schema({
}
],
dedicatedHours: {
type: String,
default: "0"
type: Array,
default: ["0", "0", "0", "0", "0", "0", "0"]
},
status: {
type: String,

49
src/utils/authFetch.js Normal file
View File

@ -0,0 +1,49 @@
import fetch from "node-fetch"; // or global fetch in Node 18+
let options = {
token: ''
}
const getAccessToken = async () => {
try {
const response = await fetch(`${process.env.SF_DOCTRACKER_API_URL}services/oauth2/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
grant_type: 'client_credentials',
client_id: process.env.SF_CLIENT_ID,
client_secret: process.env.SF_CLIENT_SECRET,
}),
});
console.log(response)
options.token = (await response.json()).access_token;
return options.token;
} catch (error) {
console.error('Error fetching access token:', error);
throw error;
}
};
export const authFetch = async (url, options = {}) => {
const token = await getAccessToken();
return fetch(url, {
...options,
headers: {
...(options.headers || {}),
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
});
};
export default authFetch;
export { getAccessToken }