isSuccess && !$code) { do { $code = \Illuminate\Support\Str::random(10); } while (ConversionHistory::whereRaw('BINARY code = ?', [$code])->exists()); } return ConversionHistory::create([ 'code' => $code, 'original_url' => $result->originalUrl, 'affiliate_url' => $result->affiliateUrl, 'converter_type' => $result->converterType, 'session_name' => $result->sessionUsed, 'status' => $result->isSuccess ? 'success' : 'failed', 'error_message' => $result->errorMessage, 'execution_time' => $result->executionTime, 'zalo_thread_id' => $zaloThreadId, 'zalo_chat_name' => $zaloChatName, 'customer_name' => $customerName, 'customer_phone' => $customerPhone, 'product_title' => $productTitle, 'product_image' => $productImage, 'product_price' => $productPrice, 'estimated_commission' => $estimatedCommission, ]); } /** * Get recent conversion history. */ public function getRecent(int $limit = 20) { return ConversionHistory::orderByDesc('created_at')->limit($limit)->get(); } /** * Get paginated history for the UI. */ public function getPaginated(int $perPage = 20): LengthAwarePaginator { return ConversionHistory::orderByDesc('created_at')->paginate($perPage); } }