diff --git a/app/Controllers/Contact.php b/app/Controllers/Contact.php index 2ea7e95..65bd62a 100644 --- a/app/Controllers/Contact.php +++ b/app/Controllers/Contact.php @@ -63,7 +63,7 @@ class Contact extends Controller { if (!$ContactID) { return $this->failValidationErrors('ContactID is required.'); } - + $contact = $this->db->table('contact')->where('ContactID', $ContactID)->get()->getRow(); if (!$contact) { return $this->failNotFound("data with {$ContactID} not found."); @@ -103,12 +103,12 @@ class Contact extends Controller { if (!$ContactID) { throw new \RuntimeException('Failed to insert contact'); } } - if (!empty($input['Details'])) { - $result = $detailModel->syncDetails($ContactID, $input['Details']); - if ($result['status'] !== 'success') { - throw new \RuntimeException('Failed to sync details: ' . $result['message']); - } + + $result = $detailModel->syncDetails($ContactID, $input['Details']); + if ($result['status'] !== 'success') { + throw new \RuntimeException('Failed to sync details: ' . $result['message']); } + $db->transComplete(); diff --git a/app/Models/ContactDetailModel.php b/app/Models/ContactDetailModel.php index 9925594..e5e2488 100644 --- a/app/Models/ContactDetailModel.php +++ b/app/Models/ContactDetailModel.php @@ -11,48 +11,48 @@ class ContactDetailModel extends Model { public function syncDetails(int $ContactID, array $contactDetails) { try { - $keptSiteIDs = []; + $keptSiteIDs = []; - foreach ($contactDetails as $detail) { - if (empty($detail['SiteID'])) { - continue; - } - - $detail['ContactID'] = $ContactID; - - $existing = $this->where('ContactID', $ContactID) - ->where('SiteID', $detail['SiteID']) - ->first(); - - if ($existing) { - $this->update($existing[$this->primaryKey], $detail); - } else { - $this->insert($detail); - } - - $keptSiteIDs[] = $detail['SiteID']; + foreach ($contactDetails as $detail) { + if (empty($detail['SiteID'])) { + continue; } - // Delete missing rows - if (!empty($keptSiteIDs)) { - $this->where('ContactID', $ContactID) - ->whereNotIn('SiteID', $keptSiteIDs) - ->delete(); + $detail['ContactID'] = $ContactID; + + $existing = $this->where('ContactID', $ContactID) + ->where('SiteID', $detail['SiteID']) + ->first(); + + if ($existing) { + $this->update($existing[$this->primaryKey], $detail); } else { - $this->where('ContactID', $ContactID)->delete(); + $this->insert($detail); } - return [ - 'status' => 'success', - 'inserted' => count($contactDetails) - count($keptSiteIDs), - 'kept' => count($keptSiteIDs), - ]; + $keptSiteIDs[] = $detail['SiteID']; + } + + // Delete missing rows + if (!empty($keptSiteIDs)) { + $this->where('ContactID', $ContactID) + ->whereNotIn('SiteID', $keptSiteIDs) + ->delete(); + } else { + $this->where('ContactID', $ContactID)->delete(); + } + + return [ + 'status' => 'success', + 'inserted' => count($contactDetails) - count($keptSiteIDs), + 'kept' => count($keptSiteIDs), + ]; } catch (\Throwable $e) { log_message('error', 'syncDetails error: ' . $e->getMessage()); return [ - 'status' => 'error', - 'message' => $e->getMessage(), + 'status' => 'error', + 'message' => $e->getMessage(), ]; } }