fix: improve contact detail update errors

Surface specific validation and database failures when updating contact details so API responses are actionable.
This commit is contained in:
mahdahar 2026-04-17 10:07:33 +07:00
parent 7b2c65ac9a
commit 5aebc255e8
2 changed files with 12 additions and 1 deletions

0
.codex
View File

View File

@ -67,7 +67,7 @@ class ContactDetailModel extends BaseModel {
{ {
try { try {
if (!empty($operations['edited']) && !$this->updateDetails($contactID, $operations['edited'])) { if (!empty($operations['edited']) && !$this->updateDetails($contactID, $operations['edited'])) {
return ['status' => 'error', 'message' => 'Failed to update contact details']; return ['status' => 'error', 'message' => $this->lastDetailError ?? 'Failed to update contact details'];
} }
if (!empty($operations['deleted']) && !$this->softDeleteDetails($contactID, $operations['deleted'])) { if (!empty($operations['deleted']) && !$this->softDeleteDetails($contactID, $operations['deleted'])) {
@ -108,6 +108,7 @@ class ContactDetailModel extends BaseModel {
foreach ($items as $detail) { foreach ($items as $detail) {
$detailID = $detail['ContactDetID'] ?? null; $detailID = $detail['ContactDetID'] ?? null;
if (!$detailID || !ctype_digit((string) $detailID)) { if (!$detailID || !ctype_digit((string) $detailID)) {
$this->lastDetailError = 'ContactDetID is required and must be an integer.';
return false; return false;
} }
@ -117,6 +118,7 @@ class ContactDetailModel extends BaseModel {
->first(); ->first();
if (empty($existing)) { if (empty($existing)) {
$this->lastDetailError = "Detail record {$detailID} not found for Contact {$contactID}.";
return false; return false;
} }
@ -124,6 +126,13 @@ class ContactDetailModel extends BaseModel {
unset($updateData['ContactID']); unset($updateData['ContactID']);
if ($updateData !== [] && !$this->update((int) $detailID, $updateData)) { if ($updateData !== [] && !$this->update((int) $detailID, $updateData)) {
$dbError = $this->db->error();
$this->lastDetailError = sprintf(
'Failed to update detail %d for Contact %d%s',
(int) $detailID,
$contactID,
!empty($dbError['message']) ? ': ' . $dbError['message'] : ''
);
return false; return false;
} }
} }
@ -131,6 +140,8 @@ class ContactDetailModel extends BaseModel {
return true; return true;
} }
private ?string $lastDetailError = null;
private function softDeleteDetails(int $contactID, array $ids): bool private function softDeleteDetails(int $contactID, array $ids): bool
{ {
$ids = array_values(array_unique(array_map('intval', $ids))); $ids = array_values(array_unique(array_map('intval', $ids)));