File size: 3,300 Bytes
25f22bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import apiClient from './apiClient';

class AccountService {
  /**
   * Get all social accounts for the current user
   * @returns {Promise} Promise that resolves to the accounts data
   */
  async getAll() {
    try {
      const response = await apiClient.get('/accounts');
      
      if (import.meta.env.VITE_NODE_ENV === 'development') {
        console.log('πŸ“± [Account] Retrieved accounts:', response.data);
      }
      
      return response;
    } catch (error) {
      if (import.meta.env.VITE_NODE_ENV === 'development') {
        console.error('πŸ“± [Account] Get accounts error:', error.response?.data || error.message);
      }
      throw error;
    }
  }

  /**
   * Add a new social account
   * @param {Object} accountData - Account data
   * @param {string} accountData.account_name - Account name
   * @param {string} accountData.social_network - Social network name
   * @returns {Promise} Promise that resolves to the add account response
   */
  async create(accountData) {
    try {
      const response = await apiClient.post('/accounts', {
        account_name: accountData.account_name,
        social_network: accountData.social_network
      });
      
      if (import.meta.env.VITE_NODE_ENV === 'development') {
        console.log('πŸ“± [Account] Account created:', response.data);
      }
      
      return response;
    } catch (error) {
      if (import.meta.env.VITE_NODE_ENV === 'development') {
        console.error('πŸ“± [Account] Create account error:', error.response?.data || error.message);
      }
      throw error;
    }
  }

  /**
   * Delete a social account
   * @param {string} accountId - Account ID
   * @returns {Promise} Promise that resolves to the delete account response
   */
  async delete(accountId) {
    try {
      const response = await apiClient.delete(`/accounts/${accountId}`);
      
      if (import.meta.env.VITE_NODE_ENV === 'development') {
        console.log('πŸ“± [Account] Account deleted:', response.data);
      }
      
      return response;
    } catch (error) {
      if (import.meta.env.VITE_NODE_ENV === 'development') {
        console.error('πŸ“± [Account] Delete account error:', error.response?.data || error.message);
      }
      throw error;
    }
  }

  /**
   * Handle OAuth callback
   * @param {Object} callbackData - OAuth callback data
   * @param {string} callbackData.code - Authorization code
   * @param {string} callbackData.state - State parameter
   * @param {string} callbackData.social_network - Social network name
   * @returns {Promise} Promise that resolves to the callback response
   */
  async handleCallback(callbackData) {
    try {
      const response = await apiClient.post('/accounts/callback', {
        code: callbackData.code,
        state: callbackData.state,
        social_network: callbackData.social_network
      });
      
      if (import.meta.env.VITE_NODE_ENV === 'development') {
        console.log('πŸ“± [Account] OAuth callback handled:', response.data);
      }
      
      return response;
    } catch (error) {
      if (import.meta.env.VITE_NODE_ENV === 'development') {
        console.error('πŸ“± [Account] OAuth callback error:', error.response?.data || error.message);
      }
      throw error;
    }
  }
}

export default new AccountService();