/
home
/
alixis5
/
integrate.sellotalent.com
/
scripts
/
/home/alixis5/integrate.sellotalent.com/scripts
mkdir
upload
Name
Size
Mode
Actions
get_job_details.php
1046
0644
edit
dl
rm
re_sync_all.php
3720
0644
edit
dl
rm
Edit:
/home/alixis5/integrate.sellotalent.com/scripts/re_sync_all.php
(3720B)
<?php /** * RE-SYNC ALL RECORDS TO AI VECTOR DATABASE * * This script fetches all applicants, jobs, and applications from EspoCRM * and triggers the RAG synchronization internal endpoint to update Pinecone. */ require __DIR__ . '/../vendor/autoload.php'; // Load Environment if (file_exists(__DIR__ . '/../.env')) { $dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..'); $dotenv->load(); } $apiUrl = rtrim($_ENV['ESPO_API_URL'], '/') . '/'; $apiKey = $_ENV['ESPO_API_KEY']; $internalSecret = $_ENV['INTERNAL_SYNC_SECRET']; $gatewayUrl = 'http://localhost/SelloTalentAPIsV01/api/v1/internal/rag-sync'; echo "=== SelloTalent AI Vector Re-Sync Start ===\n"; echo "Connecting to CRM: $apiUrl\n"; $client = new GuzzleHttp\Client(['verify' => false]); /** * Helper to fetch all records of a type with simplified pagination */ function fetchAllIds($client, $apiUrl, $apiKey, $entityType) { echo "Fetching all $entityType records...\n"; $ids = []; $offset = 0; $maxSize = 200; $response = $client->get($apiUrl . $entityType, [ 'headers' => ['X-Api-Key' => $apiKey], 'query' => ['offset' => $offset, 'maxSize' => $maxSize] ]); $data = json_decode($response->getBody(), true); while (true) { if (empty($data['list'])) break; foreach ($data['list'] as $item) { $ids[] = [ 'id' => $item['id'], 'accountId' => $item['accountId'] ?? null ]; } $offset += $maxSize; if (count($data['list']) < $maxSize) break; } echo "Found " . count($ids) . " $entityType records.\n"; return $ids; } /** * Trigger sync for a specific record */ function triggerSync($client, $gatewayUrl, $secret, $type, $id, $accountId) { if (!$accountId) { echo " [SKIP] $type/$id: No accountId found.\n"; return false; } try { $response = $client->post($gatewayUrl, [ 'headers' => ['X-Internal-Secret' => $secret, 'Content-Type' => 'application/json'], 'json' => [ 'entityType' => $type, 'entityId' => $id, 'tenantRef' => $accountId, 'action' => 'upsert' ] ]); if ($response->getStatusCode() === 200) { echo " [OK] $type/$id synced.\n"; return true; } else { echo " [FAIL] $type/$id: HTTP " . $response->getStatusCode() . "\n"; return false; } } catch (\Exception $e) { echo " [ERROR] $type/$id: " . $e->getMessage() . "\n"; return false; } } // 1. SYNC APPLICANTS $applicants = fetchAllIds($client, $apiUrl, $apiKey, 'CApplicant'); $countA = 0; foreach ($applicants as $cand) { if (triggerSync($client, $gatewayUrl, $internalSecret, 'CApplicant', $cand['id'], $cand['accountId'])) { $countA++; } } // 2. SYNC JOBS $jobs = fetchAllIds($client, $apiUrl, $apiKey, 'CJobOpening'); $countJ = 0; foreach ($jobs as $job) { if (triggerSync($client, $gatewayUrl, $internalSecret, 'CJobOpening', $job['id'], $job['accountId'])) { $countJ++; } } // 3. SYNC APPLICATIONS $applications = fetchAllIds($client, $apiUrl, $apiKey, 'CApplication'); $countApp = 0; foreach ($applications as $app) { if (triggerSync($client, $gatewayUrl, $internalSecret, 'CApplication', $app['id'], $app['accountId'])) { $countApp++; } } echo "\n=== Sync Completed ===\n"; echo "Applicants updated: $countA/" . count($applicants) . "\n"; echo "Jobs updated: $countJ/" . count($jobs) . "\n"; echo "Applications updated: $countApp/" . count($applications) . "\n"; echo "========================================\n";
Save
cmd:
run