change column type expertise and fix syncronize simbg service

This commit is contained in:
arifal
2025-03-07 14:04:37 +07:00
parent fbaa33ae13
commit c67aa979c2
7 changed files with 58 additions and 34 deletions

View File

@@ -339,20 +339,12 @@ class ServiceSIMBG
$decodedResponse = json_decode($response->getContent(), true);
if (isset($decodedResponse['errors']['code']) && $decodedResponse['errors']['code'] === 'token_not_valid') {
Log::warning("Token is invalid, refreshing token...");
// Regenerate token
$initResToken = $this->getToken();
// Check if new token is valid
if (!empty($initResToken->original['data']['token']['access'])) {
$new_token = $initResToken->original['data']['token']['access'];
// **Fix: Update headers before retrying**
$headers['Authorization'] = "Bearer " . $new_token;
Log::info("Token refreshed successfully, retrying API request...");
continue; // Retry with new token
continue;
} else {
Log::error("Failed to refresh token");
return $this->resError("Failed to refresh token");
@@ -464,29 +456,21 @@ class ServiceSIMBG
'Authorization' => "Bearer " . $token,
];
for ($attempt = 0; $attempt < 2; $attempt++) { // Try twice (original + retry)
for ($attempt = 0; $attempt < 2; $attempt++) {
$res = $this->service_client->get($url, $headers);
// Check if response is JsonResponse and decode it
if ($res instanceof \Illuminate\Http\JsonResponse) {
$decodedResponse = json_decode($res->getContent(), true);
// Handle invalid token case
if (isset($decodedResponse['errors']['code']) && $decodedResponse['errors']['code'] === 'token_not_valid') {
Log::warning("Token is invalid, refreshing token...");
// Regenerate the token
$initResToken = $this->getToken();
// Check if the new token is valid
if (!empty($initResToken->original['data']['token']['access'])) {
$new_token = $initResToken->original['data']['token']['access'];
// **Fix: Update headers with the new token**
$headers['Authorization'] = "Bearer " . $new_token;
Log::info("Token refreshed successfully, retrying API request...");
continue; // Retry the request with the new token
continue;
} else {
Log::error("Failed to refresh token");
return $this->resError("Failed to refresh token");
@@ -494,7 +478,6 @@ class ServiceSIMBG
}
}
// If request succeeds, break out of retry loop
break;
}
@@ -502,7 +485,6 @@ class ServiceSIMBG
$responseData = $res->original ?? [];
$data = $responseData['data']['data'] ?? [];
if (empty($data)) {
Log::error("API response indicates failure", ['url' => $url, 'uuid' => $uuid, 'response' => $responseData]);
return false;
}
@@ -583,7 +565,7 @@ class ServiceSIMBG
foreach ($datas as $data) {
$task_assignments[] = [
'pbg_task_uid' => $uuid, // Assuming this is a foreign key
'pbg_task_uid' => $uuid,
'user_id' => $data['user_id'],
'name' => $data['name'],
'username' => $data['username'],
@@ -592,22 +574,23 @@ class ServiceSIMBG
'role' => $data['role'],
'role_name' => $data['role_name'],
'is_active' => $data['is_active'],
'file' => json_encode($data['file']), // Store as JSON if it's an array
'expertise' => $data['expertise'],
'experience' => $data['experience'],
'file' => !empty($data['file']) ? json_encode($data['file']) : null,
'expertise' => !empty($data['expertise']) ? json_encode($data['expertise']) : null,
'experience' => !empty($data['experience']) ? json_encode($data['experience']) : null,
'is_verif' => $data['is_verif'],
'uid' => $data['uid'], // Unique identifier
'uid' => $data['uid'],
'status' => $data['status'],
'status_name' => $data['status_name'],
'note' => $data['note'],
'ta_id' => $data['id'],
'created_at' => now(),
'updated_at' => now(),
];
}
TaskAssignment::upsert(
$task_assignments, // Data to insert/update
['uid'], // Unique key for conflict resolution
['name', 'username', 'email', 'phone_number', 'role', 'role_name', 'is_active', 'file', 'expertise', 'experience', 'is_verif', 'status', 'status_name', 'note', 'updated_at']
$task_assignments,
['uid'],
['ta_id','name', 'username', 'email', 'phone_number', 'role', 'role_name', 'is_active', 'file', 'expertise', 'experience', 'is_verif', 'status', 'status_name', 'note', 'updated_at']
);
return true;
}catch(Exception $e){