Skip to content

Commit

Permalink
Merge pull request #3573 from LiteFarmOrg/Cleanup_GET_tasks_request
Browse files Browse the repository at this point in the history
Remove unnecessary payload from GET and POST tasks response Bodies
  • Loading branch information
antsgar authored Dec 6, 2024
2 parents 1758146 + 053b186 commit 6c79b38
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
8 changes: 4 additions & 4 deletions packages/api/src/controllers/taskController.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ const taskController = {
locations.[location_defaults],
managementPlans,
taskType,
animals(filterDeleted, selectMinimalProperties).[animal_union_batch, default_type, custom_type, default_breed, custom_breed],
animal_batches(filterDeleted, selectMinimalProperties).[animal_union_batch, default_type, custom_type, default_breed, custom_breed],
animals(filterDeleted, selectId).[animal_union_batch],
animal_batches(filterDeleted, selectId).[animal_union_batch],
soil_amendment_task,
soil_amendment_task_products.[purpose_relationships],
irrigation_task.[irrigation_type],
Expand Down Expand Up @@ -872,8 +872,8 @@ const taskController = {
`[
locations.[location_defaults],
managementPlans,
animals(filterDeleted, selectMinimalProperties).[animal_union_batch, default_type, custom_type, default_breed, custom_breed],
animal_batches(filterDeleted, selectMinimalProperties).[animal_union_batch, default_type, custom_type, default_breed, custom_breed],
animals(filterDeleted, selectId).[animal_union_batch],
animal_batches(filterDeleted, selectId).[animal_union_batch],
soil_amendment_task,
soil_amendment_task_products(filterDeleted).[purpose_relationships],
field_work_task.[field_work_task_type],
Expand Down
4 changes: 0 additions & 4 deletions packages/api/src/models/animalBatchModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,6 @@ class AnimalBatchModel extends baseModel {
const { ref } = AnimalBatchModel;
query.where(ref('deleted'), false);
},
selectMinimalProperties(query) {
const { ref } = AnimalBatchModel;
query.select(ref('id'), ref('name'), ref('location_id'));
},
selectId(query) {
const { ref } = AnimalBatchModel;
query.select(ref('id'));
Expand Down
4 changes: 0 additions & 4 deletions packages/api/src/models/animalModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,6 @@ class Animal extends baseModel {
const { ref } = Animal;
query.where(ref('deleted'), false);
},
selectMinimalProperties(query) {
const { ref } = Animal;
query.select(ref('id'), ref('name'), ref('location_id'));
},
selectId(query) {
const { ref } = Animal;
query.select(ref('id'));
Expand Down
36 changes: 20 additions & 16 deletions packages/api/tests/animalTask.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,9 @@ const expectedCompletedTaskData = {
complete_date: `${fakeCompletionData.complete_date}T00:00:00.000`,
};

// simulates the data for a completed movement task as returned by a GET request
const simulateMovementTaskCompletion = (task) => {
const { location_id } = task.locations[0];
return {
...task,
...expectedCompletedTaskData,
animals: task.animals.map((animal) => ({ ...animal, location_id })),
animal_batches: task.animal_batches.map((batch) => ({ ...batch, location_id })),
};
};

const simulateTaskCompletion = (task, type) => {
const complete = {
animal_movement_task: simulateMovementTaskCompletion,
// Add a function here for task types requiring custom completion logic
}[type];

return complete?.(task) || { ...task, ...expectedCompletedTaskData };
Expand Down Expand Up @@ -840,11 +829,26 @@ describe('Animal task tests', () => {
return completeTaskRequest({ user_id, farm_id }, data, task_id, 'animal_movement_task');
};

const checkAnimalMovement = (original, completedTask) => {
const checkAnimalMovement = async (original, completedTask) => {
expect(completedTask.animals.length).toBe(original.animals.length);
expect(completedTask.animal_batches.length).toBe(original.animal_batches.length);
[...completedTask.animals, ...completedTask.animal_batches].forEach((animalOrBatch) => {
expect(animalOrBatch.location_id).toBe(location_id);
const animalsData = await knex('animal')
.select('location_id')
.whereIn(
'id',
completedTask.animals.map(({ id }) => id),
);
const batchesData = await knex('animal_batch')
.select('location_id')
.whereIn(
'id',
completedTask.animal_batches.map(({ id }) => id),
);
expect(completedTask.animals.length + completedTask.animal_batches.length).toBe(
animalsData.length + batchesData.length,
);
[...animalsData, ...batchesData].forEach((data) => {
expect(data.location_id).toBe(location_id);
});
};

Expand Down Expand Up @@ -881,7 +885,7 @@ describe('Animal task tests', () => {
const res = await getTasksRequest({ user_id, farm_id });

const completedTask = res.body.find((retrievedTask) => retrievedTask.task_id === task_id);
checkAnimalMovement(task, completedTask);
await checkAnimalMovement(task, completedTask);
expect(completedTask.animal_movement_task.task_id).toBe(task_id);

const finalPurposes = purposesInPatchReq || initialPurposes || [];
Expand Down

0 comments on commit 6c79b38

Please sign in to comment.