['web', 'auth']], function () { Route::get('/docs/integration-guide', [DocsController::class, 'integrationGuide']) ->name('docs.integration-guide') ->middleware('permission:help.api_integration'); Route::get('/techne-form', [TechneFormController::class, 'show'])->name('techne.form'); Route::post('/techne-form', [TechneFormController::class, 'store'])->name('techne.form.store'); Route::get('/techne-form/count', [TechneFormController::class, 'count'])->name('techne.form.count'); }); Route::get('/agi-script/getoperator', [MaskingController::class, 'process']); // API endpoint for fetching lead description by phone number Route::get('/api/lead-description/{phone}', function ($phone) { // 🔥 Normalize function (single source of truth) $normalize = function ($number) { // remove all non digits $num = preg_replace('/\D+/', '', $number); // remove country code variations (Egypt example) $num = preg_replace('/^002/', '', $num); $num = preg_replace('/^2/', '', $num); // keep last 11 digits max if (strlen($num) > 11) { $num = substr($num, -11); } // ensure starts with 0 if (!str_starts_with($num, '0')) { $num = '0' . $num; } return $num; }; // normalize incoming phone $incoming = $normalize($phone); // 🔍 search leads (normalize DB values on the fly) $lead = Lead::where(function ($q) use ($incoming, $normalize) { $q->whereRaw("CONCAT('0', RIGHT(REPLACE(REPLACE(phone,' ',''),'+',''),10)) = ?", [$incoming]) ->orWhereRaw("CONCAT('0', RIGHT(REPLACE(REPLACE(mobile,' ',''),'+',''),10)) = ?", [$incoming]); }) ->orderBy('id', 'desc') ->first(); return response()->json([ 'description' => $lead->description ?? null, 'name' => $lead->full_name ?? null, 'found' => $lead ? true : false, 'phone_normalized' => $incoming ]); }); Route::get('/goTo/profile/{num}', function ($num) { if (strlen($num) > 11) { $num = substr($num, -11); } // Check for external profile URL setting $externalProfileUrl = \Modules\Platform\Core\Helper\CompanySettings::get( \Modules\Platform\Core\Helper\SettingsHelper::S_EXTERNAL_PROFILE_URL ); if (!empty($externalProfileUrl)) { $url = str_replace('{num}', urlencode($num), $externalProfileUrl); return redirect()->away($url); } // Search in leads first $lead = Lead::where(function ($query) use ($num) { $query->where('phone', 'LIKE', "%$num%") ->orWhere('mobile', 'LIKE', "%$num%") ->orWhere('email', 'LIKE', "%$num%") ->orWhere('phone', 'LIKE', "%0$num%") ->orWhere('mobile', 'LIKE', "%0$num%") ->orWhere('phone', 'LIKE', "%2$num%") ->orWhere('mobile', 'LIKE', "%2$num%") ->orWhere('phone', 'LIKE', "%+2$num%") ->orWhere('mobile', 'LIKE', "%+2$num%"); })->pluck('id')->last(); // If found in leads, redirect to its page if ($lead) { return redirect()->route('leads.leads.show', ['lead' => $lead]); } // Search in contacts if not found in leads $contact = Contact::where(function ($query) use ($num) { $query->where('phone', 'LIKE', "%$num%") ->orWhere('mobile', 'LIKE', "%$num%") ->orWhere('email', 'LIKE', "%$num%") ->orWhere('phone', 'LIKE', "%0$num%") ->orWhere('mobile', 'LIKE', "%0$num%") ->orWhere('phone', 'LIKE', "%2$num%") ->orWhere('mobile', 'LIKE', "%2$num%") ->orWhere('phone', 'LIKE', "%+2$num%") ->orWhere('mobile', 'LIKE', "%+2$num%"); })->pluck('id')->last(); // If found in contacts, redirect to its page if ($contact) { return redirect()->route('contacts.contacts.show', ['contact' => $contact]); } // If not found at all, redirect to create a new lead with phone pre-filled return redirect()->route('leads.leads.create', ['phone' => $num]); }); Auth::routes(); // OAuth Routes Route::get('auth/{provider}', 'Auth\SocialAuthController@redirectToProvider'); Route::get('auth/{provider}/callback', 'Auth\SocialAuthController@handleProviderCallback');