fixing contactdetail empty not delete

This commit is contained in:
mahdahar 2025-09-25 13:42:37 +07:00
parent 501ef06592
commit e5d51dff86
2 changed files with 38 additions and 38 deletions

View File

@ -63,7 +63,7 @@ class Contact extends Controller {
if (!$ContactID) { if (!$ContactID) {
return $this->failValidationErrors('ContactID is required.'); return $this->failValidationErrors('ContactID is required.');
} }
$contact = $this->db->table('contact')->where('ContactID', $ContactID)->get()->getRow(); $contact = $this->db->table('contact')->where('ContactID', $ContactID)->get()->getRow();
if (!$contact) { if (!$contact) {
return $this->failNotFound("data with {$ContactID} not found."); 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 (!$ContactID) { throw new \RuntimeException('Failed to insert contact'); }
} }
if (!empty($input['Details'])) {
$result = $detailModel->syncDetails($ContactID, $input['Details']); $result = $detailModel->syncDetails($ContactID, $input['Details']);
if ($result['status'] !== 'success') { if ($result['status'] !== 'success') {
throw new \RuntimeException('Failed to sync details: ' . $result['message']); throw new \RuntimeException('Failed to sync details: ' . $result['message']);
}
} }
$db->transComplete(); $db->transComplete();

View File

@ -11,48 +11,48 @@ class ContactDetailModel extends Model {
public function syncDetails(int $ContactID, array $contactDetails) { public function syncDetails(int $ContactID, array $contactDetails) {
try { try {
$keptSiteIDs = []; $keptSiteIDs = [];
foreach ($contactDetails as $detail) { foreach ($contactDetails as $detail) {
if (empty($detail['SiteID'])) { if (empty($detail['SiteID'])) {
continue; 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'];
} }
// Delete missing rows $detail['ContactID'] = $ContactID;
if (!empty($keptSiteIDs)) {
$this->where('ContactID', $ContactID) $existing = $this->where('ContactID', $ContactID)
->whereNotIn('SiteID', $keptSiteIDs) ->where('SiteID', $detail['SiteID'])
->delete(); ->first();
if ($existing) {
$this->update($existing[$this->primaryKey], $detail);
} else { } else {
$this->where('ContactID', $ContactID)->delete(); $this->insert($detail);
} }
return [ $keptSiteIDs[] = $detail['SiteID'];
'status' => 'success', }
'inserted' => count($contactDetails) - count($keptSiteIDs),
'kept' => count($keptSiteIDs), // 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) { } catch (\Throwable $e) {
log_message('error', 'syncDetails error: ' . $e->getMessage()); log_message('error', 'syncDetails error: ' . $e->getMessage());
return [ return [
'status' => 'error', 'status' => 'error',
'message' => $e->getMessage(), 'message' => $e->getMessage(),
]; ];
} }
} }