Skip to content

Commit

Permalink
Merge pull request #3569 from LiteFarmOrg/LF-4553b-better-middleware-…
Browse files Browse the repository at this point in the history
…approach

LF-4553b Allow patching date and assignee on custom tasks without location
  • Loading branch information
Duncan-Brain authored Dec 6, 2024
2 parents f1db435 + 4e86b13 commit f42af14
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 82 deletions.
28 changes: 5 additions & 23 deletions packages/api/src/middleware/acl/hasFarmAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const entitiesGetters = {
nomination_id: fromNomination,
transplant_task: fromTransPlantTask,
product_id: fromProduct,
taskIdTaskType: fromTaskIdTaskType,
};
import userFarmModel from '../../models/userFarmModel.js';

Expand Down Expand Up @@ -85,6 +84,11 @@ async function fromTaskId(task_id) {
.join('task_type', 'task.task_type_id', 'task_type.task_type_id')
.where({ task_id })
.first();

if (taskType?.farm_id) {
return { farm_id: taskType.farm_id };
}

//TODO: planting transplant task authorization test
if (['PLANT_TASK', 'TRANSPLANT_TASK'].includes(taskType?.task_translation_key)) {
const task_type = taskType.task_translation_key.toLowerCase();
Expand Down Expand Up @@ -205,28 +209,6 @@ async function fromLocations(locations) {
}
}

async function fromTaskIdTaskType(req) {
if (!req.params || !req.params.task_id) {
return {};
}

try {
const task_id = req.params.task_id;

const task = await knex('task').where({ task_id }).first();

if (!task) return {};

const taskType = await knex('task_type').where({ task_type_id: task.task_type_id }).first();

if (!taskType) return {};

return { farm_id: taskType.farm_id };
} catch (e) {
return {};
}
}

async function fromLocationIds(location_ids) {
if (!location_ids || !location_ids.length) {
return {};
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/routes/taskRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ router.patch(
router.patch(
'/complete/custom_task/:task_id',
modelMapping['custom_task'],
hasFarmAccess({ mixed: 'taskIdTaskType' }),
hasFarmAccess({ params: 'task_id' }),
checkScope(['edit:task']),
checkCompleteTask('custom_task'),
taskController.completeTask('custom_task'),
Expand Down
5 changes: 4 additions & 1 deletion packages/api/tests/mock.factories.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,12 @@ function fakeFertilizer(defaultData = {}) {
}

async function taskFactory(
{ promisedUser = usersFactory(), promisedTaskType = task_typeFactory() } = {},
{ promisedUser = usersFactory(), promisedTaskType, promisedFarm } = {},
task = fakeTask(),
) {
if (!promisedTaskType) {
promisedTaskType = task_typeFactory({ promisedFarm });
}
const [user, taskType] = await Promise.all([promisedUser, promisedTaskType]);
const [{ user_id }] = user;
const [{ task_type_id }] = taskType;
Expand Down
Loading

0 comments on commit f42af14

Please sign in to comment.