Skip to content

Commit

Permalink
Merge pull request #3557 from LiteFarmOrg/LF-4555/Update_getTasksForF…
Browse files Browse the repository at this point in the history
…arm_to_return_all_custom_tasks_in_GET_API_response

LF-4555: Update getTasksForFarm to return all custom tasks in GET API response
  • Loading branch information
antsgar authored Dec 3, 2024
2 parents e54dfd3 + 51e4a39 commit bc424e6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/api/src/controllers/taskController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,16 @@ function removeNullTypes(task) {

//TODO: optimize after plant_task and transplant_task refactor
async function getTasksForFarm(farm_id) {
const [managementTasks, locationTasks, plantTasks, transplantTasks] = await Promise.all([
const customTaskTypesForFarm = await TaskTypeModel.query()
.select('task_type_id')
.where({ farm_id });
const [
managementTasks,
locationTasks,
plantTasks,
transplantTasks,
customTasks,
] = await Promise.all([
TaskModel.query()
.select('task.task_id')
.whereNotDeleted()
Expand Down Expand Up @@ -1055,8 +1064,15 @@ async function getTasksForFarm(farm_id) {
)
.join('crop_variety', 'crop_variety.crop_variety_id', 'management_plan.crop_variety_id')
.where('crop_variety.farm_id', farm_id),
TaskModel.query()
.select('task.task_id')
.whereNotDeleted()
.whereIn(
'task_type_id',
customTaskTypesForFarm.map(({ task_type_id }) => task_type_id),
),
]);
return [...managementTasks, ...locationTasks, ...plantTasks, ...transplantTasks];
return [...managementTasks, ...locationTasks, ...plantTasks, ...transplantTasks, ...customTasks];
}

async function getManagementPlans(task_id, typeOfTask) {
Expand Down
27 changes: 27 additions & 0 deletions packages/api/tests/task.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,20 @@ describe('Task tests', () => {
});
});

test(`should get custom tasks that don't have locations, managementPlans, or animals`, async (done) => {
const [{ user_id, farm_id }] = await mocks.userFarmFactory({}, fakeUserFarm(1));
const [{ task_type_id }] = await mocks.task_typeFactory({ promisedFarm: [{ farm_id }] });
await mocks.taskFactory({
promisedUser: [{ user_id }],
promisedTaskType: [{ task_type_id }],
});
getTasksRequest({ farm_id, user_id }, (err, res) => {
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
done();
});
});

xtest('should get all tasks related to a farm, but not from different farms of that user', async (done) => {
const [firstUserFarm] = await mocks.userFarmFactory({}, fakeUserFarm(1));
const [secondUserFarmWithSameUser] = await mocks.userFarmFactory(
Expand Down Expand Up @@ -1623,6 +1637,19 @@ describe('Task tests', () => {
});
});

test('should create a custom task without locations, managementPlans or animals', async (done) => {
const [{ user_id, farm_id }] = await mocks.userFarmFactory({}, fakeUserFarm(1));
const [{ task_type_id }] = await mocks.task_typeFactory({ promisedFarm: [{ farm_id }] });
const data = { ...mocks.fakeTask({ task_type_id }) };

postTaskRequest({ user_id, farm_id }, 'custom_task', data, async (err, res) => {
expect(res.status).toBe(201);
const createdTask = await knex('task').where({ task_id: res.body.task_id }).first();
expect(createdTask).toBeDefined();
done();
});
});

test('should fail to create a task were a worker is trying to assign someone else', async (done) => {
const {
farm_id,
Expand Down

0 comments on commit bc424e6

Please sign in to comment.