diff --git a/packages/api/src/controllers/taskController.js b/packages/api/src/controllers/taskController.js index aeb0971896..fa653de2b0 100644 --- a/packages/api/src/controllers/taskController.js +++ b/packages/api/src/controllers/taskController.js @@ -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() @@ -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) { diff --git a/packages/api/tests/task.test.js b/packages/api/tests/task.test.js index 93fb75592b..f1f0c60951 100644 --- a/packages/api/tests/task.test.js +++ b/packages/api/tests/task.test.js @@ -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( @@ -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,