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