| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers\Reports; |
| 4 |
|
| 5 |
use FluentCart\App\Services\Report\RetentionSnapshotService; |
| 6 |
use FluentCart\Framework\Http\Request\Request; |
| 7 |
use FluentCart\App\Http\Controllers\Controller; |
| 8 |
|
| 9 |
class RetentionSnapshotController extends Controller |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Generate retention snapshots via Action Scheduler (background job) |
| 13 |
*/ |
| 14 |
public function generate(Request $request) |
| 15 |
{ |
| 16 |
$productId = $request->get('product_id'); |
| 17 |
if ($productId) { |
| 18 |
$productId = (int) $productId; |
| 19 |
} |
| 20 |
|
| 21 |
// Check if Action Scheduler is available |
| 22 |
if (!function_exists('as_enqueue_async_action')) { |
| 23 |
// Fallback: run synchronously if Action Scheduler not available |
| 24 |
$service = new RetentionSnapshotService(); |
| 25 |
$result = $service->generate($productId, null); |
| 26 |
|
| 27 |
return [ |
| 28 |
'success' => $result['success'], |
| 29 |
'message' => $result['message'], |
| 30 |
'stats' => $result['stats'] ?? [], |
| 31 |
'mode' => 'synchronous', |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
// Use timestamp as tracking ID - create it ONCE |
| 36 |
$trackingId = time(); |
| 37 |
|
| 38 |
// Queue the job via Action Scheduler |
| 39 |
// Note: Action Scheduler passes array values as separate arguments to the callback |
| 40 |
as_schedule_single_action( |
| 41 |
time(), |
| 42 |
'fluent_cart_generate_retention_snapshots', |
| 43 |
[$productId, $trackingId], // Will be passed as generateSnapshots($productId, $trackingId) |
| 44 |
'fluent-cart-snapshots' |
| 45 |
); |
| 46 |
|
| 47 |
// Store job start time |
| 48 |
update_option('fluent_cart_snapshot_job_' . $trackingId, [ |
| 49 |
'status' => 'pending', |
| 50 |
'started_at' => current_time('mysql'), |
| 51 |
'product_id' => $productId, |
| 52 |
]); |
| 53 |
|
| 54 |
return [ |
| 55 |
'success' => true, |
| 56 |
'message' => 'Snapshot generation queued', |
| 57 |
'job_id' => $trackingId, |
| 58 |
'mode' => 'background', |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Check status of a snapshot generation job |
| 64 |
*/ |
| 65 |
public function checkStatus(Request $request) |
| 66 |
{ |
| 67 |
$jobId = $request->get('params.job_id'); |
| 68 |
|
| 69 |
if (!$jobId) { |
| 70 |
return [ |
| 71 |
'success' => false, |
| 72 |
'message' => 'Job ID required', |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
$jobData = get_option('fluent_cart_snapshot_job_' . $jobId); |
| 77 |
|
| 78 |
if (!$jobData) { |
| 79 |
return [ |
| 80 |
'success' => false, |
| 81 |
'message' => 'Job not found', |
| 82 |
'job_id' => $jobId, |
| 83 |
]; |
| 84 |
} |
| 85 |
|
| 86 |
// If job data shows completed or failed, return that status |
| 87 |
if (isset($jobData['status']) && in_array($jobData['status'], ['completed', 'failed'])) { |
| 88 |
return [ |
| 89 |
'success' => true, |
| 90 |
'status' => $jobData['status'], |
| 91 |
'message' => $jobData['message'] ?? 'Job ' . $jobData['status'], |
| 92 |
'stats' => $jobData['stats'] ?? [], |
| 93 |
'data' => $jobData, |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
// Otherwise, it's still running |
| 98 |
return [ |
| 99 |
'success' => true, |
| 100 |
'status' => 'running', |
| 101 |
'message' => 'Job is still running', |
| 102 |
'data' => $jobData, |
| 103 |
]; |
| 104 |
} |
| 105 |
} |
| 106 |
|