migrate/app/Http/Controllers/CustomersController.php

140 lines
4.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Services\MagentoCategoryMigrationService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
class CustomersController extends Controller
{
protected $migrationService;
public function __construct(MagentoCategoryMigrationService $migrationService)
{
$this->migrationService = $migrationService;
}
/**
* Show the customers page
*/
public function index()
{
$m1Customers = $this->migrationService->getMagento1Customers();
$m2Customers = $this->migrationService->getMagento2Customers();
$m1CustomersNotInM2 = $this->migrationService->getM1CustomersNotInM2();
$m2CustomersNotInM1 = $this->migrationService->getM2CustomersNotInM1();
return view('customers.index', [
'm1Customers' => $m1Customers,
'm2Customers' => $m2Customers,
'm1CustomersNotInM2' => $m1CustomersNotInM2,
'm2CustomersNotInM1' => $m2CustomersNotInM1,
]);
}
/**
* Migrate all customers from Magento 1 to Magento 2
*/
public function migrateCustomers(Request $request)
{
try {
// Handle both JSON and form data
$dryRun = false;
$progressKey = null;
if ($request->isJson()) {
$dryRun = $request->json()->get('dry_run', false);
$progressKey = $request->json()->get('progress_key', null);
} else {
$dryRun = $request->input('dry_run', false);
$progressKey = $request->input('progress_key', null);
}
// Convert string "true"/"false" to boolean if needed
if (is_string($dryRun)) {
$dryRun = filter_var($dryRun, FILTER_VALIDATE_BOOLEAN);
}
$result = $this->migrationService->migrateCustomers($dryRun, $progressKey);
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Customer migration error: ' . $e->getMessage());
Log::error('Stack trace: ' . $e->getTraceAsString());
return response()->json([
'success' => false,
'message' => 'Migration failed: ' . $e->getMessage(),
'added' => 0,
'updated' => 0,
'errors' => 0,
'log' => []
], 500);
}
}
/**
* Get customer migration progress
*/
public function getMigrationProgress(Request $request)
{
try {
$progressKey = $request->input('progress_key');
if (empty($progressKey)) {
return response()->json([
'success' => false,
'message' => 'Progress key is required'
], 400);
}
$progress = Cache::get($progressKey);
if (!$progress) {
return response()->json([
'success' => false,
'message' => 'Progress not found',
'progress' => null
], 404);
}
return response()->json([
'success' => true,
'progress' => $progress
]);
} catch (\Exception $e) {
Log::error('Get customer migration progress error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Failed to get progress: ' . $e->getMessage()
], 500);
}
}
/**
* Delete a single customer from Magento 2
*/
public function deleteM2Customer(Request $request, $customerId)
{
try {
$result = $this->migrationService->deleteM2Customer($customerId);
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Delete M2 customer error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Deletion failed: ' . $e->getMessage()
], 500);
}
}
}