Merge branch 'main' of https://github.com/mahdahar/clqms-be
This commit is contained in:
commit
cac46552f6
@ -59,7 +59,8 @@ class Zones extends BaseController {
|
||||
|
||||
try {
|
||||
// Ambil data dari API pusat (CRM)
|
||||
$response = $client->get('https://services-summit.my.id/api/zones');
|
||||
$response = $client->get('http://services-summit.my.id/api/zones'); // Prod
|
||||
// $response = $client->get('https://services-summit.my.id/api/zones'); // Dev
|
||||
$result = json_decode($response->getBody(), true);
|
||||
|
||||
if (!isset($result['data']) || !is_array($result['data'])) {
|
||||
@ -78,7 +79,7 @@ class Zones extends BaseController {
|
||||
'inserted' => $record['inserted'],
|
||||
'updated' => $record['updated'],
|
||||
'deleted' => $record['deleted'],
|
||||
// 'total' => count($crmData)
|
||||
'skipped' => $record['skipped']
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
|
||||
@ -33,7 +33,8 @@ class DummySeeder extends Seeder {
|
||||
// users
|
||||
$data = [
|
||||
['id'=>1, 'role_id'=>1, 'username'=>'zaka', 'password'=>'$2y$12$vSB7PpKOUKEyFKbeExiGkuujRfQbR.yl6YVudDpfy24FemZopBG0m'],
|
||||
['id'=>2, 'role_id'=>1, 'username'=>'tes' , 'password'=>'$2y$12$KwPedIPb7K/0IR/8/FcwdOMG4eBNNAXSjXnbkB26SwjH4Nf7PaYBe']
|
||||
['id'=>2, 'role_id'=>1, 'username'=>'tes' , 'password'=>'$2y$12$KwPedIPb7K/0IR/8/FcwdOMG4eBNNAXSjXnbkB26SwjH4Nf7PaYBe'],
|
||||
['id'=>3, 'role_id'=>1, 'username'=>'tes2', 'password'=>'$2y$12$vSB7PpKOUKEyFKbeExiGkuujRfQbR.yl6YVudDpfy24FemZopBG0m'],
|
||||
];
|
||||
$this->db->table('users')->insertBatch($data);
|
||||
|
||||
|
||||
@ -37,41 +37,50 @@ class ZonesModel extends BaseModel {
|
||||
|
||||
// synchronize with crm.zones
|
||||
public function synchronize($crmData) {
|
||||
|
||||
$db = \Config\Database::connect();
|
||||
$db->transBegin();
|
||||
|
||||
try {
|
||||
// Ambil semua data zoneid di database lokal
|
||||
$localData = $this->findAll();
|
||||
$localIds = array_column($localData, 'zoneid');
|
||||
$localMap = array_column($localData, null, 'zoneid');
|
||||
$crmIds = array_column($crmData, 'zoneid');
|
||||
|
||||
// Siapkan ID dari data CRM
|
||||
$crmIds = array_column($crmData, 'zoneid');
|
||||
$record = ['inserted' => 0, 'updated' => 0, 'deleted' => 0, 'skipped' => 0];
|
||||
|
||||
// Hitung statistik
|
||||
$record = [
|
||||
'inserted' => 0,
|
||||
'updated' => 0,
|
||||
'deleted' => 0,
|
||||
];
|
||||
|
||||
// Insert atau Update data
|
||||
foreach ($crmData as $zone) {
|
||||
if (!isset($zone['zoneid'])) continue;
|
||||
|
||||
$exists = $this->find($zone['zoneid']);
|
||||
if (isset($localMap[$zone['zoneid']])) {
|
||||
// Ambil data lokal untuk perbandingan
|
||||
$localRow = $localMap[$zone['zoneid']];
|
||||
|
||||
// Buat array subset dari kolom yang relevan untuk dibandingkan
|
||||
$fieldsToCompare = ['parentzoneid', 'zonecode', 'zoneclass', 'zonename'];
|
||||
|
||||
$isDifferent = false;
|
||||
foreach ($fieldsToCompare as $field) {
|
||||
$crmVal = $zone[$field] ?? null;
|
||||
$localVal = $localRow[$field] ?? null;
|
||||
|
||||
if ($crmVal != $localVal) { // gunakan != agar beda tipe tetap terdeteksi
|
||||
$isDifferent = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($isDifferent) {
|
||||
$this->update($zone['zoneid'], [
|
||||
'parentzoneid' => $zone['parentzoneid'],
|
||||
'zonecode' => $zone['zonecode'],
|
||||
'zoneclass' => $zone['zoneclass'],
|
||||
'zonename' => $zone['zonename']
|
||||
]);
|
||||
$this->checkDbError($db, 'Update Zones');
|
||||
$record['updated']++;
|
||||
} else {
|
||||
$record['skipped']++; // tandai bahwa data identik, tidak diupdate
|
||||
}
|
||||
|
||||
if ($exists) {
|
||||
// Update jika sudah ada
|
||||
$this->update($zone['zoneid'], [
|
||||
'parentzoneid' => $zone['parentzoneid'],
|
||||
'zonecode' => $zone['zonecode'],
|
||||
'zoneclass' => $zone['zoneclass'],
|
||||
'zonename' => $zone['zonename']
|
||||
]);
|
||||
$this->checkDbError($db, 'Update Zones');
|
||||
$record['updated']++;
|
||||
} else {
|
||||
// Insert jika belum ada
|
||||
$this->insert($zone);
|
||||
@ -80,27 +89,26 @@ class ZonesModel extends BaseModel {
|
||||
}
|
||||
}
|
||||
|
||||
// Hapus data yang sudah tidak ada di CRM
|
||||
foreach ($localIds as $localId) {
|
||||
// Hapus data yang tidak ada di CRM
|
||||
foreach ($localMap as $localId => $row) {
|
||||
if (!in_array($localId, $crmIds)) {
|
||||
$this->delete($localId);
|
||||
$this->checkDbError($db, 'Delete Zones');
|
||||
$record['deleted']++;;
|
||||
$record['deleted']++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$db->transCommit();
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$db->transRollback();
|
||||
throw $e;
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$db->transComplete();
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
|
||||
// Check Error and Send Spesific Messages
|
||||
private function checkDbError($db, string $context) {
|
||||
$error = $db->error();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user