import { LdapSettingsType } from '@/types/ldap'; import Config from '@/config/config'; import { logError } from '@/utils/errorHandler'; const apiServer = Config.apiServer; async function fetchLdapSettings(jwt: string): Promise { const response = await fetch(`${apiServer}/ldap`, { method: 'GET', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${jwt}`, }, }); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } return response.json(); } async function updateLdapSettings(jwt: string, settings: LdapSettingsType): Promise { const response = await fetch(`${apiServer}/ldap`, { method: 'PUT', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${jwt}`, }, body: JSON.stringify(settings), }); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } return response.json(); } async function testLdapConnection( jwt: string, settings: LdapSettingsType, testUsername: string, testPassword: string ): Promise<{ ok: boolean; message?: string }> { try { const response = await fetch(`${apiServer}/ldap/test`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${jwt}`, }, body: JSON.stringify({ ...settings, testUsername, testPassword }), }); return response.json(); } catch (error: unknown) { logError('Error testing LDAP connection:', error); return { ok: false, message: 'Request failed' }; } } export { fetchLdapSettings, updateLdapSettings, testLdapConnection };