fix handle sync using button

This commit is contained in:
arifal
2025-03-18 08:10:06 +07:00
parent 5e139bc29c
commit e940b8d6c7
4 changed files with 61 additions and 20 deletions

View File

@@ -5,6 +5,7 @@ use App\Models\DataSetting;
use App\Models\ImportDatasource;
use App\Models\PbgTaskGoogleSheet;
use Carbon\Carbon;
use Exception;
use Google_Client;
use Google_Service_Sheets;
use Log;
@@ -15,7 +16,7 @@ class ServiceGoogleSheet
protected $spreadsheetID;
protected $service_sheets;
protected $import_datasource;
public function __construct(int $import_datasource_id)
public function __construct()
{
$this->client = new Google_Client();
$this->client->setApplicationName("Sibedas Google Sheets API");
@@ -27,21 +28,15 @@ class ServiceGoogleSheet
$this->spreadsheetID = env("SPREAD_SHEET_ID");
$this->service_sheets = new Google_Service_Sheets($this->client);
$this->import_datasource = ImportDatasource::findOrFail($import_datasource_id);
}
public function run_service(){
$run_one = $this->sync_big_data();
if(!$run_one){
$this->import_datasource->update(['status' => 'failed']);
return false;
try{
$this->sync_big_data();
$this->sync_google_sheet_data();
}catch(Exception $e){
throw $e;
}
$run_two = $this->sync_google_sheet_data();
if(!$run_two){
$this->import_datasource->update(['status' => 'failed']);
return false;
}
return true;
}
public function sync_google_sheet_data() {
try {
@@ -49,7 +44,7 @@ class ServiceGoogleSheet
if (empty($sheet_data) || count($sheet_data) < 2) {
Log::warning("sync_google_sheet_data: No valid data found.");
return false;
throw new \Exception("sync_google_sheet_data: No valid data found.");
}
$cleanValue = function ($value) {
@@ -157,9 +152,10 @@ class ServiceGoogleSheet
}
Log::info("sync google sheet done");
return true;
} catch (\Exception $e) {
Log::error("sync_google_sheet_data failed", ['error' => $e->getMessage()]);
return false;
throw $e;
}
}