From 0b65071c40f96286bd18a9b3787b14f715d4de86 Mon Sep 17 00:00:00 2001 From: Lixian Zhou Date: Mon, 16 Mar 2026 15:30:46 -0400 Subject: [PATCH] fix --- app/controllers/employee.controller.js | 81 +++++++++++++++++++------- 1 file changed, 61 insertions(+), 20 deletions(-) diff --git a/app/controllers/employee.controller.js b/app/controllers/employee.controller.js index 8211558..41b55fb 100644 --- a/app/controllers/employee.controller.js +++ b/app/controllers/employee.controller.js @@ -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 }; - 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 : []; + 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 []; + }; - // 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; + try { + 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);