From 581a59d991056b78e8de31ca7565079c875a936a Mon Sep 17 00:00:00 2001 From: Antonella Sgarlatta Date: Fri, 6 Dec 2024 10:27:48 -0300 Subject: [PATCH 1/2] LF-4564 unnest tasks from animals --- .../src/controllers/animalBatchController.js | 1 - .../api/src/controllers/animalController.js | 1 - packages/api/tests/animal.test.js | 2 -- packages/api/tests/animal_batch.test.js | 2 -- .../Inventory/useAnimalOrBatchRemoval.ts | 34 ++++++++----------- 5 files changed, 14 insertions(+), 26 deletions(-) diff --git a/packages/api/src/controllers/animalBatchController.js b/packages/api/src/controllers/animalBatchController.js index ca9f5ec8c8..8306b9bd8f 100644 --- a/packages/api/src/controllers/animalBatchController.js +++ b/packages/api/src/controllers/animalBatchController.js @@ -37,7 +37,6 @@ const animalBatchController = { animal_union_batch: true, sex_detail: true, animal_batch_use_relationships: true, - tasks: true, }); return res.status(200).send( rows.map(({ animal_union_batch, ...rest }) => ({ diff --git a/packages/api/src/controllers/animalController.js b/packages/api/src/controllers/animalController.js index ea75b0aa8f..951dd9a38b 100644 --- a/packages/api/src/controllers/animalController.js +++ b/packages/api/src/controllers/animalController.js @@ -36,7 +36,6 @@ const animalController = { .withGraphFetched({ animal_union_batch: true, animal_use_relationships: true, - tasks: true, }); return res.status(200).send( rows.map(({ animal_union_batch, ...rest }) => ({ diff --git a/packages/api/tests/animal.test.js b/packages/api/tests/animal.test.js index a7a1b8102c..4b70c99774 100644 --- a/packages/api/tests/animal.test.js +++ b/packages/api/tests/animal.test.js @@ -163,13 +163,11 @@ describe('Animal Tests', () => { ...firstAnimal, internal_identifier: res.body[0].internal_identifier, animal_use_relationships: [], - tasks: [], }).toMatchObject(res.body[0]); expect({ ...secondAnimal, internal_identifier: res.body[1].internal_identifier, animal_use_relationships: [], - tasks: [], }).toMatchObject(res.body[1]); } }); diff --git a/packages/api/tests/animal_batch.test.js b/packages/api/tests/animal_batch.test.js index ea587f8490..562c6a7ad9 100644 --- a/packages/api/tests/animal_batch.test.js +++ b/packages/api/tests/animal_batch.test.js @@ -186,13 +186,11 @@ describe('Animal Batch Tests', () => { ...firstAnimalBatch, internal_identifier: 1, animal_batch_use_relationships: [], - tasks: [], }).toMatchObject(res.body[0]); expect({ ...secondAnimalBatch, internal_identifier: 2, animal_batch_use_relationships: [], - tasks: [], }).toMatchObject(res.body[1]); } }); diff --git a/packages/webapp/src/containers/Animals/Inventory/useAnimalOrBatchRemoval.ts b/packages/webapp/src/containers/Animals/Inventory/useAnimalOrBatchRemoval.ts index f4cf7fe0ac..8fbfd8fad4 100644 --- a/packages/webapp/src/containers/Animals/Inventory/useAnimalOrBatchRemoval.ts +++ b/packages/webapp/src/containers/Animals/Inventory/useAnimalOrBatchRemoval.ts @@ -29,7 +29,7 @@ import { AnimalOrBatchKeys } from '../types'; import { useDispatch, useSelector } from 'react-redux'; import { useTranslation } from 'react-i18next'; import { completedTasksSelector, abandonedTasksSelector } from '../../taskSlice'; -import { Animal } from '../../../store/api/types'; +import { Animal, AnimalBatch } from '../../../store/api/types'; import { getLocalDateInYYYYDDMM } from '../../../util/date'; import { getTasks } from '../../Task/saga'; @@ -174,8 +174,8 @@ const useAnimalOrBatchRemoval = ( } }; - const getFinalizedTaskIdsSet = useCallback(() => { - return new Set([...completedTasks, ...abandonedTasks].map(({ task_id }) => task_id)); + const getFinalizedTasks = useCallback(() => { + return Array.from(new Set([...completedTasks, ...abandonedTasks])); }, [completedTasks, abandonedTasks]); const hasFinalizedTasks = useMemo(() => { @@ -183,23 +183,17 @@ const useAnimalOrBatchRemoval = ( return false; } - const selectedInventoryIdsSet = new Set(selectedInventoryIds); - const finalizedTaskIdsSet = getFinalizedTaskIdsSet(); - - let inventoryFoundCount = 0; - for (let { id, tasks } of animalTasksWithInventoryIds) { - if (selectedInventoryIdsSet.has(id)) { - inventoryFoundCount++; - - if (tasks.some(({ task_id }) => finalizedTaskIdsSet.has(task_id))) { - return true; - } - } - // Stop iterating once all selected inventory IDs have been processed. - if (inventoryFoundCount === selectedInventoryIdsSet.size) { - return false; - } - } + const finalizedTasks = getFinalizedTasks(); + + return selectedInventoryIds.some((id) => + finalizedTasks.filter( + ({ animals, animal_batches }: { animals: Animal[]; animal_batches: AnimalBatch[] }) => { + const animalIds = animals.map(({ id }) => `${id}`); + const batchIds = animal_batches.map(({ id }) => `${id}`); + return animalIds.includes(id) || batchIds.includes(id); + }, + ), + ); }, [removalModalOpen, completedTasks, abandonedTasks, selectedInventoryIds]); return { onConfirmRemoveAnimals, removalModalOpen, setRemovalModalOpen, hasFinalizedTasks }; From 4ecdb9060a98b8fb611f0a903e6c79dd41afcb21 Mon Sep 17 00:00:00 2001 From: Duncan-Brain Date: Fri, 6 Dec 2024 09:39:31 -0500 Subject: [PATCH 2/2] Parse id for check --- .../Animals/Inventory/useAnimalOrBatchRemoval.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/webapp/src/containers/Animals/Inventory/useAnimalOrBatchRemoval.ts b/packages/webapp/src/containers/Animals/Inventory/useAnimalOrBatchRemoval.ts index 8fbfd8fad4..3a0f162bd1 100644 --- a/packages/webapp/src/containers/Animals/Inventory/useAnimalOrBatchRemoval.ts +++ b/packages/webapp/src/containers/Animals/Inventory/useAnimalOrBatchRemoval.ts @@ -185,15 +185,18 @@ const useAnimalOrBatchRemoval = ( const finalizedTasks = getFinalizedTasks(); - return selectedInventoryIds.some((id) => - finalizedTasks.filter( + return selectedInventoryIds.some((animalOrBatchId) => { + const { id, kind } = parseInventoryId(animalOrBatchId); + return finalizedTasks.filter( ({ animals, animal_batches }: { animals: Animal[]; animal_batches: AnimalBatch[] }) => { const animalIds = animals.map(({ id }) => `${id}`); const batchIds = animal_batches.map(({ id }) => `${id}`); - return animalIds.includes(id) || batchIds.includes(id); + return kind === AnimalOrBatchKeys.ANIMAL + ? animalIds.includes(`${id}`) + : batchIds.includes(`${id}`); }, - ), - ); + ).length; + }); }, [removalModalOpen, completedTasks, abandonedTasks, selectedInventoryIds]); return { onConfirmRemoveAnimals, removalModalOpen, setRemovalModalOpen, hasFinalizedTasks };