Skip to content

Commit

Permalink
Merge pull request #3533 from LiteFarmOrg/LF-4531/Set_up_new_task_aba…
Browse files Browse the repository at this point in the history
…ndonment_reason_for_tasks_with_removed_animals

LF-4531: Set up new task abandonment reason for tasks with removed animals
  • Loading branch information
kathyavini authored Nov 25, 2024
2 parents 5168374 + 99feb06 commit da766c6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2024 LiteFarm.org
* This file is part of LiteFarm.
*
* LiteFarm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LiteFarm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>.
*/

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export const up = async (knex) => {
const reasons = [
'OTHER',
'CROP_FAILURE',
'LABOUR_ISSUE',
'MARKET_PROBLEM',
'WEATHER',
'MACHINERY_ISSUE',
'SCHEDULING_ISSUE',
'NO_ANIMALS',
];
await knex.raw(`
ALTER TABLE task ADD CONSTRAINT abandonment_reason_check
CHECK (abandonment_reason = ANY (ARRAY['${reasons.join(`'::text,'`)}'::text]))
`);
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export const down = async (knex) => {
await knex.raw('ALTER TABLE task DROP CONSTRAINT abandonment_reason_check');
};
5 changes: 5 additions & 0 deletions packages/api/src/middleware/validation/checkTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { checkIsArray, customError } from '../../util/customErrors.js';

const adminRoles = [1, 2, 5];
const taskTypesRequiringProducts = ['soil_amendment_task'];
const clientRestrictedReasons = ['NO_ANIMALS'];

export function noReqBodyCheckYet() {
return async (req, res, next) => {
Expand Down Expand Up @@ -62,6 +63,10 @@ export function checkAbandonTask() {
return res.status(400).send('must have other_abandonment_reason');
}

if (clientRestrictedReasons.includes(abandonment_reason.toUpperCase())) {
return res.status(400).send('The provided abandonment_reason is not allowed');
}

if (!abandon_date) {
return res.status(400).send('must have abandonment_date');
}
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/models/taskModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class TaskModel extends BaseModel {
'WEATHER',
'MACHINERY_ISSUE',
'SCHEDULING_ISSUE',
'NO_ANIMALS',
],
},
other_abandonment_reason: { type: ['string', 'null'] },
Expand Down
21 changes: 21 additions & 0 deletions packages/api/tests/task.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2949,6 +2949,27 @@ describe('Task tests', () => {
done();
});
});

test('Should not be able to abandon a task with disallowed reason', async (done) => {
const [{ user_id, farm_id }] = await mocks.userFarmFactory({}, fakeUserFarm(1));
const date = faker.date.future().toISOString().split('T')[0];
const [task] = await mocks.taskFactory(
{ promisedUser: [{ user_id }] },
mocks.fakeTask({ due_date: date, assignee_user_id: user_id }),
);
const [location] = await mocks.locationFactory({ promisedFarm: [{ farm_id }] });
await mocks.location_tasksFactory({ promisedTask: [task], promisedField: [location] });
abandonTaskRequest(
{ user_id, farm_id },
{ abandonTaskBody, abandonment_reason: 'NO_ANIMALS' },
task.task_id,
async (err, res) => {
expect(res.status).toBe(400);
expect(res.error.text).toBe('The provided abandonment_reason is not allowed');
done();
},
);
});
});

describe('DELETE task tests', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/webapp/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,7 @@
"LABOUR_ISSUE": "Labour issue",
"MACHINERY_ISSUE": "Machinery issue",
"MARKET_PROBLEM": "Market problem",
"NO_ANIMALS": "Auto-abandoned: No animals for this task",
"OTHER": "Other",
"SCHEDULING_ISSUE": "Scheduling issue",
"WEATHER": "Weather"
Expand Down

0 comments on commit da766c6

Please sign in to comment.