Update Zones Sync v1
This commit is contained in:
parent
a3b025ef6c
commit
bd6184fddf
@ -90,4 +90,9 @@ $routes->get('/api/containerdef/', 'Specimen\ContainerDef::index');
|
|||||||
$routes->get('/api/containerdef/(:num)', 'Specimen\ContainerDef::show/$1');
|
$routes->get('/api/containerdef/(:num)', 'Specimen\ContainerDef::show/$1');
|
||||||
$routes->post('/api/containerdef', 'Specimen\ContainerDef::create');
|
$routes->post('/api/containerdef', 'Specimen\ContainerDef::create');
|
||||||
$routes->patch('/api/containerdef', 'Specimen\ContainerDef::update');
|
$routes->patch('/api/containerdef', 'Specimen\ContainerDef::update');
|
||||||
$routes->delete('/api/containerdef', 'Specimen\ContainerDef::delete');
|
$routes->delete('/api/containerdef', 'Specimen\ContainerDef::delete');
|
||||||
|
|
||||||
|
$routes->get('/api/zones', 'Zones::index');
|
||||||
|
$routes->get('/api/zones/syncronize', 'Zones::syncronize');
|
||||||
|
// $routes->get('/api/provinces', 'Api\ZonesApi::getProvinces');
|
||||||
|
// $routes->get('/api/cities', 'Api\ZonesApi::getCities');
|
||||||
135
app/Controllers/Zones.php
Normal file
135
app/Controllers/Zones.php
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Controllers;
|
||||||
|
|
||||||
|
use CodeIgniter\API\ResponseTrait;
|
||||||
|
use App\Controllers\BaseController;
|
||||||
|
// use App\Models\PatVisit\PatVisitModel;
|
||||||
|
|
||||||
|
class Zones extends BaseController {
|
||||||
|
use ResponseTrait;
|
||||||
|
|
||||||
|
// protected $model;
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
// $this->model = new PatVisitModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
// public function __construct() {
|
||||||
|
// $this->db = \Config\Database::connect();
|
||||||
|
// $this->model = new PatientModel();
|
||||||
|
// $this->rules = [
|
||||||
|
// 'PatientID' => 'required|max_length[50]',
|
||||||
|
// 'AlternatePID' => 'permit_empty|max_length[50]'
|
||||||
|
// ];
|
||||||
|
// }
|
||||||
|
|
||||||
|
public function index() {
|
||||||
|
|
||||||
|
// Buat instance HTTP client
|
||||||
|
$client = \Config\Services::curlrequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// $response = $client->get('https://services-summit.my.id/api/provinces');
|
||||||
|
$response = $client->get('http://crmcomposer.local/api/zones');
|
||||||
|
|
||||||
|
// Ambil body responsenya
|
||||||
|
$body = $response->getBody();
|
||||||
|
|
||||||
|
// Decode JSON ke array PHP
|
||||||
|
$data = json_decode($body, true);
|
||||||
|
|
||||||
|
return $this->respond(['status'=>'success', 'message'=>"data fetched successfully", 'data'=>$data['data']], 200);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
|
||||||
|
return $this->respond([ 'status' => 'error', 'message' => $e->getMessage() ], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function synchronize()
|
||||||
|
{
|
||||||
|
$client = \Config\Services::curlrequest();
|
||||||
|
$zonesModel = new ZonesModel();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 1️⃣ Ambil data dari API pusat (CRM)
|
||||||
|
$response = $client->get('https://services-summit.my.id/api/zones');
|
||||||
|
$result = json_decode($response->getBody(), true);
|
||||||
|
|
||||||
|
if (!isset($result['data']) || !is_array($result['data'])) {
|
||||||
|
return $this->response->setJSON([
|
||||||
|
'status' => 'error',
|
||||||
|
'message' => 'Invalid or empty response from CRM API'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$crmData = $result['data'];
|
||||||
|
|
||||||
|
// 2️⃣ Ambil semua data zoneid di database lokal
|
||||||
|
$localData = $zonesModel->findAll();
|
||||||
|
$localIds = array_column($localData, 'zoneid');
|
||||||
|
|
||||||
|
// 3️⃣ Siapkan ID dari data CRM
|
||||||
|
$crmIds = array_column($crmData, 'zoneid');
|
||||||
|
|
||||||
|
// Hitung statistik
|
||||||
|
$inserted = 0;
|
||||||
|
$updated = 0;
|
||||||
|
$deleted = 0;
|
||||||
|
|
||||||
|
// 4️⃣ Mulai transaksi agar aman
|
||||||
|
$db = \Config\Database::connect();
|
||||||
|
$db->transStart();
|
||||||
|
|
||||||
|
// 5️⃣ Insert atau Update data
|
||||||
|
foreach ($crmData as $zone) {
|
||||||
|
if (!isset($zone['zoneid'])) continue;
|
||||||
|
|
||||||
|
$exists = $zonesModel->find($zone['zoneid']);
|
||||||
|
|
||||||
|
if ($exists) {
|
||||||
|
// Update jika sudah ada
|
||||||
|
$zonesModel->update($zone['zoneid'], [
|
||||||
|
'parentzoneid' => $zone['parentzoneid'],
|
||||||
|
'zonecode' => $zone['zonecode'],
|
||||||
|
'zoneclass' => $zone['zoneclass'],
|
||||||
|
'zonename' => $zone['zonename']
|
||||||
|
]);
|
||||||
|
$updated++;
|
||||||
|
} else {
|
||||||
|
// Insert jika belum ada
|
||||||
|
$zonesModel->insert($zone);
|
||||||
|
$inserted++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6️⃣ Hapus data yang sudah tidak ada di CRM
|
||||||
|
foreach ($localIds as $localId) {
|
||||||
|
if (!in_array($localId, $crmIds)) {
|
||||||
|
$zonesModel->delete($localId);
|
||||||
|
$deleted++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7️⃣ Commit transaksi
|
||||||
|
$db->transComplete();
|
||||||
|
|
||||||
|
return $this->response->setJSON([
|
||||||
|
'status' => 'success',
|
||||||
|
'message' => 'Zones synchronized successfully',
|
||||||
|
'summary' => [
|
||||||
|
'inserted' => $inserted,
|
||||||
|
'updated' => $updated,
|
||||||
|
'deleted' => $deleted,
|
||||||
|
'total' => count($crmData)
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return $this->response->setJSON([
|
||||||
|
'status' => 'error',
|
||||||
|
'message' => $e->getMessage()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
22
app/Models/SyncCRM/ZonesModel.php
Normal file
22
app/Models/SyncCRM/ZonesModel.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Models\SyncCRM;
|
||||||
|
|
||||||
|
use App\Models\BaseModel;
|
||||||
|
|
||||||
|
class ZonesModel extends BaseModel {
|
||||||
|
protected $table = 'zones';
|
||||||
|
protected $primaryKey = 'zoneid';
|
||||||
|
protected $allowedFields = ['parentzoneid', 'zonecode', 'zoneclass', 'zonename'];
|
||||||
|
|
||||||
|
// Check Error and Send Spesific Messages
|
||||||
|
private function checkDbError($db, string $context) {
|
||||||
|
$error = $db->error();
|
||||||
|
if (!empty($error['code'])) {
|
||||||
|
throw new \Exception(
|
||||||
|
"{$context} failed: {$error['code']} - {$error['message']}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user