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:
parent
7b2c65ac9a
commit
5aebc255e8
@ -67,7 +67,7 @@ class ContactDetailModel extends BaseModel {
|
||||
{
|
||||
try {
|
||||
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'])) {
|
||||
@ -108,6 +108,7 @@ class ContactDetailModel extends BaseModel {
|
||||
foreach ($items as $detail) {
|
||||
$detailID = $detail['ContactDetID'] ?? null;
|
||||
if (!$detailID || !ctype_digit((string) $detailID)) {
|
||||
$this->lastDetailError = 'ContactDetID is required and must be an integer.';
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -117,6 +118,7 @@ class ContactDetailModel extends BaseModel {
|
||||
->first();
|
||||
|
||||
if (empty($existing)) {
|
||||
$this->lastDetailError = "Detail record {$detailID} not found for Contact {$contactID}.";
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -124,6 +126,13 @@ class ContactDetailModel extends BaseModel {
|
||||
unset($updateData['ContactID']);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -131,6 +140,8 @@ class ContactDetailModel extends BaseModel {
|
||||
return true;
|
||||
}
|
||||
|
||||
private ?string $lastDetailError = null;
|
||||
|
||||
private function softDeleteDetails(int $contactID, array $ids): bool
|
||||
{
|
||||
$ids = array_values(array_unique(array_map('intval', $ids)));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user