Skip to content

Commit

Permalink
Merge pull request #3582 from LiteFarmOrg/hide-deleted-in-error-fix
Browse files Browse the repository at this point in the history
LF-4564 unnest tasks from animals
  • Loading branch information
kathyavini authored Dec 6, 2024
2 parents 5749aed + b6e0ff0 commit 7d87bf7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
1 change: 0 additions & 1 deletion packages/api/src/controllers/animalBatchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => ({
Expand Down
1 change: 0 additions & 1 deletion packages/api/src/controllers/animalController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => ({
Expand Down
2 changes: 0 additions & 2 deletions packages/api/tests/animal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
});
Expand Down
2 changes: 0 additions & 2 deletions packages/api/tests/animal_batch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -174,32 +174,29 @@ 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(() => {
if (!removalModalOpen) {
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((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 kind === AnimalOrBatchKeys.ANIMAL
? animalIds.includes(`${id}`)
: batchIds.includes(`${id}`);
},
).length;
});
}, [removalModalOpen, completedTasks, abandonedTasks, selectedInventoryIds]);

return { onConfirmRemoveAnimals, removalModalOpen, setRemovalModalOpen, hasFinalizedTasks };
Expand Down

0 comments on commit 7d87bf7

Please sign in to comment.