src/Controller/PlanityWebSocketController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\PlanityService;
  4. use Exception;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use GuzzleHttp\Client;
  11. class PlanityWebSocketController extends AbstractController
  12. {
  13.     #[Route('/admin/search-clients-planity'name'search_clients'methods: ['GET'])]
  14.     public function searchClientsFromPlanity(Request $request): JsonResponse
  15.     {
  16.         try {
  17.             $searchTerm $request->query->get('q');
  18.             $client = new Client();
  19.             $response $client->post(
  20.                 'https://day79mubw3-dsn.algolia.net/1/indexes/business_customers/query',
  21.                 [
  22.                     'headers' => [
  23.                         'accept' => 'application/json',
  24.                         'content-type' => 'application/x-www-form-urlencoded',
  25.                         'x-algolia-agent' => 'Algolia for JavaScript (3.35.1); Browser',
  26.                         'x-algolia-application-id' => 'DAY79MUBW3',
  27.                         'x-algolia-api-key' => 'YjE0OWQzYWNkYzNkNThjMDM1MTk1MTMzZmM0NzQ5YzdkYmI0YjAzNDhiZThhM2RhNGEzNjA5NWJhZjdkNjQ1NGZpbHRlcnM9YnVzaW5lc3NJZCUzQS1Oak1aem5iVnBHZmpwR2M2aDho'
  28.                     ],
  29.                     'body' => json_encode([
  30.                         'params' => "query={$searchTerm}&hitsPerPage=5"
  31.                     ])
  32.                 ]
  33.             );
  34.             $data json_decode((string) $response->getBody(), true);
  35.             return new JsonResponse($data);
  36.         } catch (\Exception $e) {
  37.             return new JsonResponse(['error' => $e->getMessage()], 500);
  38.         }
  39.     }
  40.     #[Route('/admin/getTelFromPlanity'name'get_tel_from_planity'methods: ['GET'])]
  41.     public function getTelCustomer(PlanityService $planityService): JsonResponse
  42.     {
  43.        return $planityService->getPlanning(date('Y-m-d'strtotime('now -1 day')));
  44.     }
  45.     #[Route('/admin/all-clients-planity'name'get_all_clients'methods: ['GET'])]
  46.     public function getAllClientsFromPlanity(): JsonResponse
  47.     {
  48.         try {
  49.             $client = new Client();
  50.             $allClients = [];
  51.             $page 0;
  52.             $hitsPerPage 1000;
  53.             do {
  54.                 $response $client->post(
  55.                     'https://day79mubw3-dsn.algolia.net/1/indexes/business_customers/query',
  56.                     [
  57.                         'headers' => [
  58.                             'accept' => 'application/json',
  59.                             'content-type' => 'application/x-www-form-urlencoded',
  60.                             'x-algolia-agent' => 'Algolia for JavaScript (3.35.1); Browser',
  61.                             'x-algolia-application-id' => 'DAY79MUBW3',
  62.                             'x-algolia-api-key' => 'YjE0OWQzYWNkYzNkNThjMDM1MTk1MTMzZmM0NzQ5YzdkYmI0YjAzNDhiZThhM2RhNGEzNjA5NWJhZjdkNjQ1NGZpbHRlcnM9YnVzaW5lc3NJZCUzQS1Oak1aem5iVnBHZmpwR2M2aDho'
  63.                         ],
  64.                         'body' => json_encode([
  65.                             'params' => "query=&page={$page}&hitsPerPage={$hitsPerPage}&filters=deletedAt=0&attributesToHighlight=[]&attributesToRetrieve=[\"id\",\"name\",\"phone\",\"email\",\"deletedAt\",\"businessId\"]"
  66.                         ])
  67.                     ]
  68.                 );
  69.                 $data json_decode((string) $response->getBody(), true);
  70.                 $allClients array_merge($allClients$data['hits']);
  71.                 $page++;
  72.             } while ($page $data['nbPages']);
  73.             $uniqueClients = [];
  74.             $seenCombinations = [];
  75.             $duplicates = [];
  76.             foreach ($allClients as $hit) {
  77.                 $email strtolower($hit['email'] ?? '');
  78.                 $fullName strtolower(trim($hit['name'] ?? ''));
  79.                 // Créer une clé unique basée sur email ET nom complet
  80.                 $comparisonKey $email '|' $fullName;
  81.                 // Si pas d'email, on skip
  82.                 if (empty($email)) {
  83.                     continue;
  84.                 }
  85.                 if (isset($seenCombinations[$comparisonKey])) {
  86.                     if (!isset($duplicates[$comparisonKey])) {
  87.                         $duplicates[$comparisonKey] = [
  88.                             'count' => 1,
  89.                             'data' => $hit
  90.                         ];
  91.                     }
  92.                     $duplicates[$comparisonKey]['count']++;
  93.                     continue;
  94.                 }
  95.                 $seenCombinations[$comparisonKey] = true;
  96.                 // Séparation du prénom et nom
  97.                 $nameParts explode(' '$hit['name']);
  98.                 $firstName $nameParts[0] ?? '';
  99.                 $lastName = isset($nameParts[1]) ? implode(' 'array_slice($nameParts1)) : '';
  100.                 $uniqueClients[] = [
  101.                     'id' => $hit['id'] ?? '',
  102.                     'email' => $email,
  103.                     'firstName' => $firstName,
  104.                     'lastName' => $lastName,
  105.                     'phone' => $hit['phone'] ?? ''
  106.                 ];
  107.             }
  108.             return new JsonResponse([
  109.                 'statistiques' => [
  110.                     'total_initial' => count($allClients),
  111.                     'total_unique' => count($uniqueClients),
  112.                     'nombre_doublons' => count($duplicates),
  113.                     'exemples_doublons' => array_slice($duplicates05true)
  114.                 ],
  115.                 'clients' => $uniqueClients
  116.             ]);
  117.         } catch (\Exception $e) {
  118.             return new JsonResponse(['error' => $e->getMessage()], 500);
  119.         }
  120.     }
  121. }