File size: 10,474 Bytes
25f22bf
 
c7d5529
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from flask import Blueprint, request, jsonify, current_app
from flask_jwt_extended import jwt_required, get_jwt_identity
from backend.services.linkedin_service import LinkedInService
import secrets

accounts_bp = Blueprint('accounts', __name__)

@accounts_bp.route('/', methods=['OPTIONS'])
@accounts_bp.route('', methods=['OPTIONS'])
def handle_options():
    """Handle OPTIONS requests for preflight CORS checks."""
    return '', 200

@accounts_bp.route('/', methods=['GET'])
@accounts_bp.route('', methods=['GET'])
@jwt_required()
def get_accounts():
    """
    Get all social media accounts for the current user.
    
    Returns:
        JSON: List of social media accounts
    """
    try:
        user_id = get_jwt_identity()
        
        # Check if Supabase client is initialized
        if not hasattr(current_app, 'supabase') or current_app.supabase is None:
            # Add CORS headers to error response
            response_data = jsonify({
                'success': False,
                'message': 'Database connection not initialized'
            })
            response_data.headers.add('Access-Control-Allow-Origin', 'http://localhost:3000')
            response_data.headers.add('Access-Control-Allow-Credentials', 'true')
            return response_data, 500
        
        # Fetch accounts from Supabase
        response = (
            current_app.supabase
            .table("Social_network")
            .select("*")
            .eq("id_utilisateur", user_id)
            .execute()
        )
        
        accounts = response.data if response.data else []
        
        # Add CORS headers explicitly
        response_data = jsonify({
            'success': True,
            'accounts': accounts
        })
        response_data.headers.add('Access-Control-Allow-Origin', 'http://localhost:3000')
        response_data.headers.add('Access-Control-Allow-Credentials', 'true')
        return response_data, 200
        
    except Exception as e:
        current_app.logger.error(f"Get accounts error: {str(e)}")
        # Add CORS headers to error response
        response_data = jsonify({
            'success': False,
            'message': 'An error occurred while fetching accounts'
        })
        response_data.headers.add('Access-Control-Allow-Origin', 'http://localhost:3000')
        response_data.headers.add('Access-Control-Allow-Credentials', 'true')
        return response_data, 500

@accounts_bp.route('/', methods=['POST'])
@accounts_bp.route('', methods=['POST'])
@jwt_required()
def add_account():
    """
    Add a new social media account for the current user.
    
    Request Body:
        account_name (str): Account name
        social_network (str): Social network name
        
    Returns:
        JSON: Add account result
    """
    try:
        user_id = get_jwt_identity()
        data = request.get_json()
        
        # Validate required fields
        if not data or not all(k in data for k in ('account_name', 'social_network')):
            return jsonify({
                'success': False,
                'message': 'Account name and social network are required'
            }), 400
        
        account_name = data['account_name']
        social_network = data['social_network']
        
        # For LinkedIn, initiate OAuth flow
        if social_network.lower() == 'linkedin':
            linkedin_service = LinkedInService()
            # Generate a random state for security
            state = secrets.token_urlsafe(32)
            
            # Store state in session or database for verification later
            # For now, we'll return it to the frontend
            authorization_url = linkedin_service.get_authorization_url(state)
            
            return jsonify({
                'success': True,
                'message': 'Please authenticate with LinkedIn',
                'authorization_url': authorization_url,
                'state': state
            }), 200
        else:
            return jsonify({
                'success': False,
                'message': 'Unsupported social network'
            }), 400
        
    except Exception as e:
        current_app.logger.error(f"Add account error: {str(e)}")
        return jsonify({
            'success': False,
            'message': f'An error occurred while adding account: {str(e)}'
        }), 500

@accounts_bp.route('/callback', methods=['POST'])
@jwt_required()
def handle_oauth_callback():
    """
    Handle OAuth callback from social network.
    
    Request Body:
        code (str): Authorization code
        state (str): State parameter
        social_network (str): Social network name
        
    Returns:
        JSON: OAuth callback result
    """
    try:
        user_id = get_jwt_identity()
        data = request.get_json()
        
        # Validate required fields
        if not data or not all(k in data for k in ('code', 'state', 'social_network')):
            return jsonify({
                'success': False,
                'message': 'Code, state, and social network are required'
            }), 400
        
        code = data['code']
        state = data['state']
        social_network = data['social_network']
        
        # Verify state (in a real implementation, you would check against stored state)
        # For now, we'll skip this verification
        
        if social_network.lower() == 'linkedin':
            linkedin_service = LinkedInService()
            
            # Exchange code for access token
            token_response = linkedin_service.get_access_token(code)
            access_token = token_response['access_token']
            
            # Get user info
            user_info = linkedin_service.get_user_info(access_token)
            
            # Store account info in Supabase
            response = (
                current_app.supabase
                .table("Social_network")
                .insert({
                    "social_network": social_network,
                    "account_name": user_info.get('name', 'LinkedIn Account'),
                    "id_utilisateur": user_id,
                    "token": access_token,
                    "sub": user_info.get('sub'),
                    "given_name": user_info.get('given_name'),
                    "family_name": user_info.get('family_name'),
                    "picture": user_info.get('picture')
                })
                .execute()
            )
            
            if response.data:
                return jsonify({
                    'success': True,
                    'message': 'Account linked successfully',
                    'account': response.data[0]
                }), 200
            else:
                return jsonify({
                    'success': False,
                    'message': 'Failed to link account'
                }), 500
        else:
            return jsonify({
                'success': False,
                'message': 'Unsupported social network'
            }), 400
        
    except Exception as e:
        current_app.logger.error(f"OAuth callback error: {str(e)}")
        return jsonify({
            'success': False,
            'message': f'An error occurred during OAuth callback: {str(e)}'
        }), 500

@accounts_bp.route('/<account_id>', methods=['OPTIONS'])
def handle_account_options(account_id):
    """Handle OPTIONS requests for preflight CORS checks for specific account."""
    return '', 200

@accounts_bp.route('/<account_id>', methods=['DELETE'])
@jwt_required()
def delete_account(account_id):
    """
    Delete a social media account.
    
    Path Parameters:
        account_id (str): Account ID
        
    Returns:
        JSON: Delete account result
    """
    try:
        user_id = get_jwt_identity()
        
        # Delete account from Supabase
        response = (
            current_app.supabase
            .table("Social_network")
            .delete()
            .eq("id", account_id)
            .eq("id_utilisateur", user_id)
            .execute()
        )
        
        if response.data:
            return jsonify({
                'success': True,
                'message': 'Account deleted successfully'
            }), 200
        else:
            return jsonify({
                'success': False,
                'message': 'Account not found or unauthorized'
            }), 404
        
    except Exception as e:
        current_app.logger.error(f"Delete account error: {str(e)}")
        return jsonify({
            'success': False,
            'message': 'An error occurred while deleting account'
        }), 500

@accounts_bp.route('/<account_id>/primary', methods=['OPTIONS'])
def handle_primary_options(account_id):
    """Handle OPTIONS requests for preflight CORS checks for primary account."""
    return '', 200

@accounts_bp.route('/<account_id>/primary', methods=['PUT'])
@jwt_required()
def set_primary_account(account_id):
    """
    Set an account as primary for the user.
    
    Path Parameters:
        account_id (str): Account ID
        
    Returns:
        JSON: Update result
    """
    try:
        user_id = get_jwt_identity()
        
        # First, get all accounts for this user
        response = (
            current_app.supabase
            .table("Social_network")
            .select("*")
            .eq("id_utilisateur", user_id)
            .execute()
        )
        
        accounts = response.data if response.data else []
        
        # Check if account exists and belongs to user
        account_exists = any(account['id'] == account_id and account['id_utilisateur'] == user_id for account in accounts)
        
        if not account_exists:
            return jsonify({
                'success': False,
                'message': 'Account not found or unauthorized'
            }), 404
        
        # For now, we'll just return success
        # In a real implementation, you might want to add a 'is_primary' field
        # and update all accounts accordingly
        
        return jsonify({
            'success': True,
            'message': 'Account set as primary successfully'
        }), 200
        
    except Exception as e:
        current_app.logger.error(f"Set primary account error: {str(e)}")
        return jsonify({
            'success': False,
            'message': 'An error occurred while setting primary account'
        }), 500