import apiClient from './apiClient'; class LinkedInAuthService { /** * Initiate LinkedIn OAuth flow * @returns {Promise} Promise that resolves to authorization URL and state */ async initiateAuth() { try { const response = await apiClient.post('/accounts', { account_name: 'LinkedIn Account', social_network: 'LinkedIn' }); if (response.data.success && response.data.authorization_url) { if (import.meta.env.VITE_NODE_ENV === 'development') { console.log('🔗 [LinkedIn] Auth initiated successfully'); } return { success: true, authorization_url: response.data.authorization_url, state: response.data.state }; } else { throw new Error(response.data.message || 'Failed to initiate LinkedIn authentication'); } } catch (error) { if (import.meta.env.VITE_NODE_ENV === 'development') { console.error('🔗 [LinkedIn] Auth initiation error:', error.response?.data || error.message); } return { success: false, message: error.response?.data?.message || 'Failed to start LinkedIn authentication' }; } } /** * Handle LinkedIn OAuth callback * @param {string} code - Authorization code from LinkedIn * @param {string} state - State parameter for security verification * @returns {Promise} Promise that resolves to callback result */ async handleCallback(code, state) { try { const response = await apiClient.post('/accounts/callback', { code: code, state: state, social_network: 'LinkedIn' }); if (import.meta.env.VITE_NODE_ENV === 'development') { console.log('🔗 [LinkedIn] OAuth callback handled successfully'); } return response.data; } catch (error) { if (import.meta.env.VITE_NODE_ENV === 'development') { console.error('🔗 [LinkedIn] OAuth callback error:', error.response?.data || error.message); } return { success: false, message: error.response?.data?.message || 'Failed to complete LinkedIn authentication' }; } } /** * Get all LinkedIn accounts for the current user * @returns {Promise} Promise that resolves to accounts data */ async getLinkedInAccounts() { try { if (import.meta.env.VITE_NODE_ENV === 'development') { console.log('🔗 [LinkedIn] Fetching LinkedIn accounts...'); } const response = await apiClient.get('/accounts'); // Handle different response formats let accounts = []; if (response.data && response.data.success && response.data.accounts) { // Format: { success: true, accounts: [...] } accounts = response.data.accounts; } else if (Array.isArray(response.data)) { // Format: [...] accounts = response.data; } else { throw new Error('Unexpected response format from API'); } // Filter for LinkedIn accounts only const linkedinAccounts = accounts.filter( account => account && account.social_network && account.social_network.toLowerCase() === 'linkedin' ); if (import.meta.env.VITE_NODE_ENV === 'development') { console.log('🔗 [LinkedIn] Retrieved LinkedIn accounts:', linkedinAccounts); } return { success: true, accounts: linkedinAccounts }; } catch (error) { if (import.meta.env.VITE_NODE_ENV === 'development') { console.error('🔗 [LinkedIn] Get LinkedIn accounts error:', error.response?.data || error.message); } return { success: false, accounts: [], message: error.response?.data?.message || 'Failed to fetch LinkedIn accounts' }; } } /** * Delete a LinkedIn account * @param {string} accountId - Account ID to delete * @returns {Promise} Promise that resolves to deletion result */ async deleteLinkedInAccount(accountId) { try { const response = await apiClient.delete(`/accounts/${accountId}`); if (import.meta.env.VITE_NODE_ENV === 'development') { console.log('🔗 [LinkedIn] LinkedIn account deleted successfully'); } return response.data; } catch (error) { if (import.meta.env.VITE_NODE_ENV === 'development') { console.error('🔗 [LinkedIn] Delete LinkedIn account error:', error.response?.data || error.message); } return { success: false, message: error.response?.data?.message || 'Failed to delete LinkedIn account' }; } } /** * Set a LinkedIn account as primary * @param {string} accountId - Account ID to set as primary * @returns {Promise} Promise that resolves to update result */ async setPrimaryAccount(accountId) { try { const response = await apiClient.put(`/accounts/${accountId}/primary`); if (import.meta.env.VITE_NODE_ENV === 'development') { console.log('🔗 [LinkedIn] Primary account set successfully'); } return response.data; } catch (error) { if (import.meta.env.VITE_NODE_ENV === 'development') { console.error('🔗 [LinkedIn] Set primary account error:', error.response?.data || error.message); } return { success: false, message: error.response?.data?.message || 'Failed to set primary account' }; } } } export default new LinkedInAuthService();