diff --git a/app/Controllers/Contact.php b/app/Controllers/Contact.php index be634cd..54db284 100644 --- a/app/Controllers/Contact.php +++ b/app/Controllers/Contact.php @@ -95,7 +95,7 @@ class Contact extends Controller { if ($result) { return $this->respondCreated([ 'status' => 'success', - 'message' => 'Contact created successfully', + 'message' => 'Contact updated successfully', 'data' => $dataContact, ]); } else { @@ -160,6 +160,7 @@ class Contact extends Controller { private function prepareContactDetailData(array $input): array { foreach($input['ContactDetail'] as $detail) { $data[] = [ + "SiteID" => $detail['SiteID'] ?? null, "ContactCode" => $detail['ContactCode'] ?? null, "ContactEmail" => $detail['ContactEmail'] ?? null, "OccupationID" => $detail['OccupationID'] ?? null, diff --git a/app/Models/ContactDetailModel.php b/app/Models/ContactDetailModel.php index 9ca3d8b..2059c93 100644 --- a/app/Models/ContactDetailModel.php +++ b/app/Models/ContactDetailModel.php @@ -8,4 +8,30 @@ class ContactDetailModel extends Model { protected $table = 'contactdetail'; protected $primaryKey = 'ContactDetID'; protected $allowedFields = ['ContactID', 'SiteID', 'ContactCode', 'ContactEmail', 'OccupationID', 'JobTitle', 'Department', 'ContactStartDate', 'ContactEndDate']; + + public function syncDetails(int $contactId, array $details) { + $kept = []; + + foreach ($details as $detail) { + $detail['ContactID'] = $contactId; + + $existing = $this->where('SiteID', $detail['SiteID']) + ->where('ContactID', $contactId) + ->first(); + + if ($existing) { + $this->update($existing[$this->primaryKey], $detail); + $kept[] = $existing[$this->primaryKey]; + } else { + $newId = $this->insert($detail); + $kept[] = $newId; + } + } + + if (!empty($kept)) { + $this->where('ContactID', $contactId) + ->whereNotIn($this->primaryKey, $kept) + ->delete(); + } else { $this->where('ContactID', $contactId)->delete(); } + } } diff --git a/app/Models/ContactModel.php b/app/Models/ContactModel.php index 85b4c22..fcba800 100644 --- a/app/Models/ContactModel.php +++ b/app/Models/ContactModel.php @@ -10,7 +10,7 @@ class ContactModel extends Model { protected $allowedFields = ['NameFirst', 'NameLast', 'Title', 'Initial', 'Birthdate', 'EmailAddress1', 'EmailAddress2', 'Phone', 'MobilePhone1', 'MobilePhone2', 'Specialty', 'SubSpecialty']; public function getContactsWithDetail() { - $rows = $this->select("c.ContactID, cd.ContactCode, c.NameFirst, c.NameLast, c.Specialty") + $rows = $this->select("c.ContactID, cd.SiteID, cd.ContactCode, c.NameFirst, c.NameLast, c.Specialty") ->join("contactdetail cd", "c.ContactID=cd.ContactID", "left") ->join("occupation o","cd.OccupationID=o.OccupationID", "left") ->get()->getResultArray(); @@ -40,7 +40,7 @@ class ContactModel extends Model { foreach ($rows as $row) { if (!empty($row['ContactDetID'])) { $contact['Details'][] = [ - 'SiteID' => $row['SiteID'] ?? null, + 'SiteID' => $row['SiteID'] ?? null, 'ContactDetID' => $row['ContactDetID'], 'ContactCode' => $row['ContactCode'] ?? null, 'ContactEmail' => $row['DetailPhone'] ?? null, @@ -82,23 +82,17 @@ class ContactModel extends Model { } } - public function updateContact(array $contactData, array $contactDetails) { + public function updateContact(int $ContactID, array $contactData, array $contactDetails) { $db = \Config\Database::connect(); $db->transStart(); try { - if (!$this->update($contactData)) { - throw new \Exception('Failed to insert contact'); + if (!$this->update($ContactID, $contactData)) { + throw new \Exception('Failed to update contact'); } - $ContactID = $this->getInsertID(); $detailModel = new \App\Models\ContactDetailModel(); - foreach ($contactDetails as $detail) { - $detail['ContactID'] = $ContactID; - if (!$detailModel->insert($detail)) { - throw new \Exception('Failed to insert contact details'); - } - } + $detailModel->syncDetails($ContactID, $contactDetails); $db->transComplete(); return $db->transStatus();