| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Actions\Controller\ProductActions; |
| 4 |
|
| 5 |
use SyncBasalam\JobManager; |
| 6 |
use SyncBasalam\Actions\Controller\ActionController; |
| 7 |
|
| 8 |
defined('ABSPATH') || exit; |
| 9 |
|
| 10 |
class UpdateAllProducts extends ActionController |
| 11 |
{ |
| 12 |
public function __invoke() |
| 13 |
{ |
| 14 |
$updateType = isset($_POST['update_type']) ? sanitize_text_field(wp_unslash($_POST['update_type'])) : null; |
| 15 |
if (!$updateType) { |
| 16 |
return wp_send_json_error(['message' => 'نوع بروزرسانی نا� |
| 17 |
عتبر است.'], 400); |
| 18 |
} |
| 19 |
$jobManager = syncBasalamContainer()->get(JobManager::class); |
| 20 |
|
| 21 |
if ($updateType === 'full') { |
| 22 |
$existingJob = $jobManager->getJob([ |
| 23 |
'job_type' => 'sync_basalam_update_all_products', |
| 24 |
'status' => 'pending', |
| 25 |
]); |
| 26 |
|
| 27 |
if ($existingJob) { |
| 28 |
return wp_send_json_error(['message' => 'در حال حاضر یک ع� |
| 29 |
لیات بروزرسانی کا� |
| 30 |
ل در صف انتظار است.'], 409); |
| 31 |
} |
| 32 |
|
| 33 |
$initialData = json_encode([ |
| 34 |
'offset' => 0, |
| 35 |
]); |
| 36 |
|
| 37 |
$response = $jobManager->createJob( |
| 38 |
'sync_basalam_update_all_products', |
| 39 |
'pending', |
| 40 |
$initialData |
| 41 |
); |
| 42 |
|
| 43 |
if (!$response) { |
| 44 |
return wp_send_json_error(['message' => 'خطا در ایجاد فرایند بروزرسانی.'], 500); |
| 45 |
} |
| 46 |
|
| 47 |
wp_send_json_success([ |
| 48 |
'message' => 'بروزرسانی ت� |
| 49 |
ا� |
| 50 |
اطلاعات � |
| 51 |
حصول با � |
| 52 |
وفقیت آغاز شد.', |
| 53 |
], 200); |
| 54 |
} else { |
| 55 |
$existingJob = $jobManager->getJob([ |
| 56 |
'job_type' => 'sync_basalam_bulk_update_products', |
| 57 |
'status' => 'pending', |
| 58 |
]); |
| 59 |
|
| 60 |
if ($existingJob) { |
| 61 |
return wp_send_json_error(['message' => 'در حال حاضر یک ع� |
| 62 |
لیات بروزرسانی در صف انتظار است.'], 409); |
| 63 |
} |
| 64 |
|
| 65 |
$response = $jobManager->createJob( |
| 66 |
'sync_basalam_bulk_update_products', |
| 67 |
'pending', |
| 68 |
0, |
| 69 |
); |
| 70 |
|
| 71 |
if (!$response) { |
| 72 |
return wp_send_json_error(['message' => 'خطا در ایجاد فرایند بروزرسانی فوری � |
| 73 |
حصولات.'], 500); |
| 74 |
} |
| 75 |
|
| 76 |
wp_send_json_success(['message' => 'فرایند بروزرسانی قی� |
| 77 |
ت و � |
| 78 |
وجودی با � |
| 79 |
وفقیت آغاز شد.'], 200); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|