updateUserProfile method

Future<Map<String, dynamic>> updateUserProfile({
  1. required String userId,
  2. required String tenantId,
  3. String? firstName,
  4. String? lastName,
  5. String? language,
  6. String? location,
  7. String? email,
  8. String? username,
  9. String? profilePhotoPath,
})

Update user profile

Implementation

Future<Map<String, dynamic>> updateUserProfile({
  required String userId,
  required String tenantId,
  String? firstName,
  String? lastName,
  String? language,
  String? location,
  String? email,
  String? username,
  String? profilePhotoPath,
}) async {
  const mutation = r'''
    mutation UpdateTenantUser($input: UpdateTenantUserInput!) {
      updateTenantUser(input: $input) {
        success
        message
        tenantUser {
          id
          firstName
          lastName
          phoneNumber
          location
          profile_Photo
          image
          video
          updatedAt
        }
      }
    }
  ''';

  final Map<String, dynamic> input = {
    'id': userId,
    'tenantID': tenantId,
  };

  if (firstName != null && firstName.isNotEmpty) input['firstName'] = firstName;
  if (lastName != null && lastName.isNotEmpty) input['lastName'] = lastName;
  if (language != null && language.isNotEmpty) input['language'] = language;
  if (location != null && location.isNotEmpty) input['location'] = location;
  if (email != null && email.isNotEmpty) input['email'] = email;
  if (username != null && username.isNotEmpty) input['username'] = username;

  print('DEBUG: Input before file upload: $input');
  print('DEBUG: Profile photo path: $profilePhotoPath');

  try {
    Map<String, dynamic> response;

    if (profilePhotoPath != null && profilePhotoPath.isNotEmpty) {
      print('Attempting file upload with field name: profile_Photo');

      response = await _api.uploadMultipleFilesViaGraphQL(
        mutationName: 'updateTenantUser',
        variables: {'input': input},
        fileFields: {
          'profile_Photo': profilePhotoPath,
        },
      );

      print('Upload response: $response');
    } else {
      response = await _api.graphql(mutation, {'input': input});
    }

    final data = response['data']?['updateTenantUser'] ??
        response['updateTenantUser'] ??
        response;

    print(' Parsed data: $data');

    if (data == null || data['success'] != true) {
      return {
        'success': false,
        'message': data?['message'] ?? 'Update failed',
      };
    }

    return {
      'success': true,
      'message': data['message'] ?? 'Profile updated successfully',
      'tenantUser': data['tenantUser'],
    };
  } catch (e, stackTrace) {
    print('UPDATE PROFILE ERROR: $e');
    print('Stack trace: $stackTrace');
    return {
      'success': false,
      'message': 'Exception: $e',
    };
  }
}