This commit is contained in:
@@ -323,33 +323,74 @@ exports.getEmployeesWithUsernameOrEmail = (req, res) => {
|
||||
};
|
||||
|
||||
exports.getExternalEmployeesList = async (req, res) => {
|
||||
const requestBody = {
|
||||
username: HR_INTEGRATION_USERNAME,
|
||||
password: HR_INTEGRATION_PASSWORD,
|
||||
status: "active"
|
||||
};
|
||||
const requestOptions = {
|
||||
timeout: 15000,
|
||||
httpsAgent: HR_INSECURE_HTTPS_AGENT
|
||||
};
|
||||
const currentSite = splitSite.findSiteNumber(req);
|
||||
const requestBodies = [
|
||||
{
|
||||
username: HR_INTEGRATION_USERNAME,
|
||||
password: HR_INTEGRATION_PASSWORD,
|
||||
site: currentSite,
|
||||
status: "active"
|
||||
},
|
||||
{
|
||||
username: HR_INTEGRATION_USERNAME,
|
||||
password: HR_INTEGRATION_PASSWORD,
|
||||
site: currentSite
|
||||
},
|
||||
{
|
||||
username: HR_INTEGRATION_USERNAME,
|
||||
password: HR_INTEGRATION_PASSWORD,
|
||||
status: "active"
|
||||
},
|
||||
{
|
||||
username: HR_INTEGRATION_USERNAME,
|
||||
password: HR_INTEGRATION_PASSWORD
|
||||
}
|
||||
];
|
||||
const endpoints = [HR_EMPLOYEE_LIST_ENDPOINT, HR_EMPLOYEE_LIST_FALLBACK_ENDPOINT];
|
||||
const normalizeEmployeesList = (raw) => {
|
||||
if (Array.isArray(raw)) return raw;
|
||||
if (Array.isArray(raw?.data)) return raw.data;
|
||||
if (Array.isArray(raw?.employees)) return raw.employees;
|
||||
if (Array.isArray(raw?.result)) return raw.result;
|
||||
if (Array.isArray(raw?.results)) return raw.results;
|
||||
return [];
|
||||
};
|
||||
|
||||
try {
|
||||
console.log("[HR Integration] Requesting employee list from:", HR_EMPLOYEE_LIST_ENDPOINT);
|
||||
let response = await axios.post(HR_EMPLOYEE_LIST_ENDPOINT, requestBody, requestOptions);
|
||||
let list = Array.isArray(response?.data) ? response.data : [];
|
||||
|
||||
// Backward compatibility for older integration endpoint naming.
|
||||
if (list.length === 0) {
|
||||
console.log("[HR Integration] Empty result, retrying fallback endpoint:", HR_EMPLOYEE_LIST_FALLBACK_ENDPOINT);
|
||||
const fallbackResponse = await axios.post(HR_EMPLOYEE_LIST_FALLBACK_ENDPOINT, requestBody, requestOptions);
|
||||
const fallbackList = Array.isArray(fallbackResponse?.data) ? fallbackResponse.data : [];
|
||||
if (fallbackList.length > 0) {
|
||||
response = fallbackResponse;
|
||||
list = fallbackList;
|
||||
let bestResponseData = null;
|
||||
for (const endpoint of endpoints) {
|
||||
for (const requestBody of requestBodies) {
|
||||
try {
|
||||
console.log("[HR Integration] Requesting employee list from:", endpoint, "payload:", requestBody);
|
||||
const response = await axios.post(endpoint, requestBody, requestOptions);
|
||||
const list = normalizeEmployeesList(response?.data);
|
||||
console.log(
|
||||
"[HR Integration] employee list response meta:",
|
||||
{ endpoint, count: list.length, responseType: typeof response?.data }
|
||||
);
|
||||
console.log("[HR Integration] raw employee list response:", response?.data);
|
||||
bestResponseData = response?.data;
|
||||
if (list.length > 0) {
|
||||
return res.send(list);
|
||||
}
|
||||
} catch (attemptError) {
|
||||
console.log("[HR Integration] employee list attempt failed:", {
|
||||
endpoint,
|
||||
payload: requestBody,
|
||||
status: attemptError?.response?.status,
|
||||
data: attemptError?.response?.data,
|
||||
message: attemptError?.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
console.log("[HR Integration] /employees/list response:", response?.data);
|
||||
res.send(list);
|
||||
}
|
||||
const emptyList = normalizeEmployeesList(bestResponseData);
|
||||
console.log("[HR Integration] all attempts completed; returning list length:", emptyList.length);
|
||||
return res.send(emptyList);
|
||||
} catch (err) {
|
||||
console.log("[HR Integration] /employees/list error status:", err?.response?.status);
|
||||
console.log("[HR Integration] /employees/list error data:", err?.response?.data);
|
||||
|
||||
Reference in New Issue
Block a user