From 6974930309243b48f1429a63bddcf5a6723b19bf Mon Sep 17 00:00:00 2001 From: Venkatesh Date: Wed, 5 Aug 2026 11:03:04 +0530 Subject: [PATCH] FIX_SOF_DELETE --- app/Controllers/ClaimController.php | 62 +++++++++++++++-------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/app/Controllers/ClaimController.php b/app/Controllers/ClaimController.php index 6335164..c6005ed 100644 --- a/app/Controllers/ClaimController.php +++ b/app/Controllers/ClaimController.php @@ -329,13 +329,12 @@ class ClaimController extends ResourceController $updatedBy = $reqData['updated_by'] ?? $reqData['created_by'] ?? null; - // Soft-delete active files whose ids are not sent in claim_file_ids. - // When claim_file_ids is present (even empty), missing ids are soft-deleted. + // Soft-delete only the file ids sent in remove_file_id. $deletedFileIds = []; - if (array_key_exists('claim_file_ids', $reqData)) { - $deletedFileIds = $this->softDeleteMissingClaimFiles( + if (!empty($reqData['remove_file_id'])) { + $deletedFileIds = $this->softDeleteClaimFilesByIds( (int) $claimId, - $reqData['claim_file_ids'], + $reqData['remove_file_id'], $updatedBy ); } @@ -550,39 +549,44 @@ class ClaimController extends ResourceController } /** - * Soft-delete active claim files whose ids are not in the keep list. - * Accepts array, comma-separated string, or single id. + * Soft-delete active claim files by the given ids only. + * Expects remove_file_id as an array, e.g. [5, 7]. + * Also accepts JSON string "[5,7]" or comma-separated "5,7" from form-data. * * @return int[] Soft-deleted file ids */ - private function softDeleteMissingClaimFiles(int $claimId, $keepFileIds, $updatedBy = null): array + private function softDeleteClaimFilesByIds(int $claimId, $removeFileIds, $updatedBy = null): array { - $keepIds = []; - - if (is_string($keepFileIds)) { - $keepFileIds = array_filter(array_map('trim', explode(',', $keepFileIds)), 'strlen'); - } - - if (!is_array($keepFileIds)) { - $keepFileIds = [$keepFileIds]; - } - - foreach ($keepFileIds as $id) { - if ($id !== null && $id !== '' && is_numeric($id)) { - $keepIds[] = (int) $id; + if (is_string($removeFileIds)) { + $decoded = json_decode($removeFileIds, true); + if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) { + $removeFileIds = $decoded; + } else { + $removeFileIds = array_filter(array_map('trim', explode(',', $removeFileIds)), 'strlen'); } } - $keepIds = array_values(array_unique($keepIds)); - $query = $this->ClaimFilesModel - ->where('claim_id', $claimId) - ->where('is_active', 1); - - if (!empty($keepIds)) { - $query->whereNotIn('id', $keepIds); + if (!is_array($removeFileIds)) { + $removeFileIds = [$removeFileIds]; } - $filesToDelete = $query->findAll(); + $removeIds = []; + foreach ($removeFileIds as $id) { + if ($id !== null && $id !== '' && is_numeric($id)) { + $removeIds[] = (int) $id; + } + } + $removeIds = array_values(array_unique($removeIds)); + + if (empty($removeIds)) { + return []; + } + + $filesToDelete = $this->ClaimFilesModel + ->where('claim_id', $claimId) + ->where('is_active', 1) + ->whereIn('id', $removeIds) + ->findAll(); if (empty($filesToDelete)) { return [];