add siteid to contact index

This commit is contained in:
mahdahar 2025-09-25 09:42:17 +07:00
parent dd999aaf7b
commit 98acaff93b
3 changed files with 34 additions and 13 deletions

View File

@ -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,

View File

@ -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(); }
}
}

View File

@ -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();