FIX_SOF_DELETE
This commit is contained in:
parent
3e4447003a
commit
6974930309
@ -329,13 +329,12 @@ class ClaimController extends ResourceController
|
|||||||
|
|
||||||
$updatedBy = $reqData['updated_by'] ?? $reqData['created_by'] ?? null;
|
$updatedBy = $reqData['updated_by'] ?? $reqData['created_by'] ?? null;
|
||||||
|
|
||||||
// Soft-delete active files whose ids are not sent in claim_file_ids.
|
// Soft-delete only the file ids sent in remove_file_id.
|
||||||
// When claim_file_ids is present (even empty), missing ids are soft-deleted.
|
|
||||||
$deletedFileIds = [];
|
$deletedFileIds = [];
|
||||||
if (array_key_exists('claim_file_ids', $reqData)) {
|
if (!empty($reqData['remove_file_id'])) {
|
||||||
$deletedFileIds = $this->softDeleteMissingClaimFiles(
|
$deletedFileIds = $this->softDeleteClaimFilesByIds(
|
||||||
(int) $claimId,
|
(int) $claimId,
|
||||||
$reqData['claim_file_ids'],
|
$reqData['remove_file_id'],
|
||||||
$updatedBy
|
$updatedBy
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -550,39 +549,44 @@ class ClaimController extends ResourceController
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Soft-delete active claim files whose ids are not in the keep list.
|
* Soft-delete active claim files by the given ids only.
|
||||||
* Accepts array, comma-separated string, or single id.
|
* 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
|
* @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($removeFileIds)) {
|
||||||
|
$decoded = json_decode($removeFileIds, true);
|
||||||
if (is_string($keepFileIds)) {
|
if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
|
||||||
$keepFileIds = array_filter(array_map('trim', explode(',', $keepFileIds)), 'strlen');
|
$removeFileIds = $decoded;
|
||||||
}
|
} else {
|
||||||
|
$removeFileIds = array_filter(array_map('trim', explode(',', $removeFileIds)), 'strlen');
|
||||||
if (!is_array($keepFileIds)) {
|
|
||||||
$keepFileIds = [$keepFileIds];
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($keepFileIds as $id) {
|
|
||||||
if ($id !== null && $id !== '' && is_numeric($id)) {
|
|
||||||
$keepIds[] = (int) $id;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$keepIds = array_values(array_unique($keepIds));
|
|
||||||
|
|
||||||
$query = $this->ClaimFilesModel
|
if (!is_array($removeFileIds)) {
|
||||||
->where('claim_id', $claimId)
|
$removeFileIds = [$removeFileIds];
|
||||||
->where('is_active', 1);
|
|
||||||
|
|
||||||
if (!empty($keepIds)) {
|
|
||||||
$query->whereNotIn('id', $keepIds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$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)) {
|
if (empty($filesToDelete)) {
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user