Skip to content

Commit

Permalink
Merge pull request #3518 from LiteFarmOrg/LF-4471/Add_or_update_model…
Browse files Browse the repository at this point in the history
…s_for_animal_movement_task

LF-4471: Add or update models for animal movement task
  • Loading branch information
antsgar authored Nov 8, 2024
2 parents ed92fa0 + e3f9106 commit fba0450
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/api/src/controllers/taskController.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,22 @@ const taskController = {
const graphTasks = await TaskModel.query()
.whereNotDeleted()
.withGraphFetched(
`[locations.[location_defaults], managementPlans, soil_amendment_task, soil_amendment_task_products(filterDeleted).[purpose_relationships], field_work_task.[field_work_task_type], cleaning_task, pest_control_task, harvest_task.[harvest_use], plant_task, transplant_task, irrigation_task.[irrigation_type]]
`,
`[
locations.[location_defaults],
managementPlans,
animals,
animal_batches,
soil_amendment_task,
soil_amendment_task_products(filterDeleted).[purpose_relationships],
field_work_task.[field_work_task_type],
cleaning_task,
pest_control_task,
harvest_task.[harvest_use],
plant_task,
transplant_task,
irrigation_task.[irrigation_type],
animal_movement_task.[purpose_relationships],
]`,
)
.whereIn('task_id', taskIds);
const filteredTasks = graphTasks.map(removeNullTypes);
Expand Down
16 changes: 16 additions & 0 deletions packages/api/src/models/animalBatchModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import AnimalBatchGroupRelationshipModel from './animalBatchGroupRelationshipMod
import AnimalUnionBatchIdViewModel from './animalUnionBatchIdViewModel.js';
import { checkAndTrimString } from '../util/util.js';
import AnimalBatchUseRelationshipModel from './animalBatchUseRelationshipModel.js';
import TaskAnimalBatchRelationshipModel from './taskAnimalBatchRelationshipModel.js';
import TaskModel from './taskModel.js';

class AnimalBatchModel extends baseModel {
static get tableName() {
Expand Down Expand Up @@ -95,6 +97,7 @@ class AnimalBatchModel extends baseModel {
sire: { type: ['string', 'null'] },
supplier: { type: ['string', 'null'], maxLength: 255 },
price: { type: ['number', 'null'] },
location_id: { type: ['string', 'null'] },
...this.baseProperties,
},
additionalProperties: false,
Expand Down Expand Up @@ -141,6 +144,19 @@ class AnimalBatchModel extends baseModel {
to: 'animal_batch_use_relationship.animal_batch_id',
},
},
tasks: {
modelClass: TaskModel,
relation: Model.ManyToManyRelation,
join: {
from: 'animal_batch.id',
through: {
modelClass: TaskAnimalBatchRelationshipModel,
from: 'task_animal_batch_relationship.animal_batch_id',
to: 'task_animal_batch_relationship.task_id',
},
to: 'task.task_id',
},
},
};
}
}
Expand Down
16 changes: 16 additions & 0 deletions packages/api/src/models/animalModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import AnimalGroupRelationshipModel from './animalGroupRelationshipModel.js';
import Model from './baseFormatModel.js';
import { checkAndTrimString } from '../util/util.js';
import AnimalUseRelationshipModel from './animalUseRelationshipModel.js';
import TaskModel from './taskModel.js';
import TaskAnimalRelationshipModel from './taskAnimalRelationshipModel.js';

class Animal extends baseModel {
static get tableName() {
Expand Down Expand Up @@ -99,6 +101,7 @@ class Animal extends baseModel {
organic_status: { type: 'string', enum: ['Non-Organic', 'Transitional', 'Organic'] },
supplier: { type: ['string', 'null'], maxLength: 255 },
price: { type: ['number', 'null'] },
location_id: { type: ['string', 'null'] },
...this.baseProperties,
},
additionalProperties: false,
Expand Down Expand Up @@ -136,6 +139,19 @@ class Animal extends baseModel {
to: 'animal_use_relationship.animal_id',
},
},
tasks: {
modelClass: TaskModel,
relation: Model.ManyToManyRelation,
join: {
from: 'animal.id',
through: {
modelClass: TaskAnimalRelationshipModel,
from: 'task_animal_relationship.animal_id',
to: 'task_animal_relationship.task_id',
},
to: 'task.task_id',
},
},
};
}
}
Expand Down
42 changes: 42 additions & 0 deletions packages/api/src/models/animalMovementPurposeModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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/>.
*/
import Model from './baseFormatModel.js';

class AnimalMovementPurpose extends Model {
static get tableName() {
return 'animal_movement_purpose';
}

static get idColumn() {
return 'id';
}

// Optional JSON schema. This is not the database schema! Nothing is generated
// based on this. This is only used for validation. Whenever a model instance
// is created it is checked against this schema. http://json-schema.org/.
static get jsonSchema() {
return {
type: 'object',
required: ['key'],
properties: {
id: { type: 'integer' },
key: { type: 'string' },
},
additionalProperties: false,
};
}
}

export default AnimalMovementPurpose;
65 changes: 65 additions & 0 deletions packages/api/src/models/animalMovementTaskModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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/>.
*/

import Model from './baseFormatModel.js';
import TaskModel from './taskModel.js';
import AnimalMovementTaskPurposeRelationshipModel from './animalMovementTaskPurposeRelationshipModel.js';

class AnimalMovementTask extends Model {
static get tableName() {
return 'animal_movement_task';
}

static get idColumn() {
return 'task_id';
}

// Optional JSON schema. This is not the database schema! Nothing is generated
// based on this. This is only used for validation. Whenever a model instance
// is created it is checked against this schema. http://json-schema.org/.
static get jsonSchema() {
return {
type: 'object',
required: [],
properties: {
task_id: { type: 'integer' },
},
additionalProperties: false,
};
}

static get relationMappings() {
return {
task: {
relation: Model.BelongsToOneRelation,
modelClass: TaskModel,
join: {
from: 'animal_movement_task.task_id',
to: 'task.task_id',
},
},
purpose_relationships: {
relation: Model.HasManyRelation,
modelClass: AnimalMovementTaskPurposeRelationshipModel,
join: {
from: 'animal_movement_task.task_id',
to: 'animal_movement_task_purpose_relationship.task_id',
},
},
};
}
}

export default AnimalMovementTask;
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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/>.
*/

import Model from './baseFormatModel.js';

class AnimalMovementTaskPurposeRelationship extends Model {
static get tableName() {
return 'animal_movement_task_purpose_relationship';
}

static get idColumn() {
return ['task_id', 'purpose_id'];
}

// Optional JSON schema. This is not the database schema! Nothing is generated
// based on this. This is only used for validation. Whenever a model instance
// is created it is checked against this schema. http://json-schema.org/.
static get jsonSchema() {
return {
type: 'object',
required: ['task_id', 'purpose_id'],
properties: {
task_id: { type: 'integer' },
purpose_id: { type: 'integer' },
other_purpose: { type: ['string', 'null'], minLength: 1, maxLength: 255 },
},
additionalProperties: false,
};
}
}

export default AnimalMovementTaskPurposeRelationship;
43 changes: 43 additions & 0 deletions packages/api/src/models/taskAnimalBatchRelationshipModel.js
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/>.
*/

import Model from './baseFormatModel.js';

class TaskAnimalBatchRelationshipModel extends Model {
static get tableName() {
return 'task_animal_batch_relationship';
}

static get idColumn() {
return ['task_id', 'animal_batch_id'];
}

// Optional JSON schema. This is not the database schema! Nothing is generated
// based on this. This is only used for validation. Whenever a model instance
// is created it is checked against this schema. http://json-schema.org/.
static get jsonSchema() {
return {
type: 'object',
required: ['task_id', 'animal_batch_id'],
properties: {
task_id: { type: 'integer' },
animal_batch_id: { type: 'integer' },
},
additionalProperties: false,
};
}
}

export default TaskAnimalBatchRelationshipModel;
43 changes: 43 additions & 0 deletions packages/api/src/models/taskAnimalRelationshipModel.js
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/>.
*/

import Model from './baseFormatModel.js';

class TaskAnimalRelationshipModel extends Model {
static get tableName() {
return 'task_animal_relationship';
}

static get idColumn() {
return ['task_id', 'animal_id'];
}

// Optional JSON schema. This is not the database schema! Nothing is generated
// based on this. This is only used for validation. Whenever a model instance
// is created it is checked against this schema. http://json-schema.org/.
static get jsonSchema() {
return {
type: 'object',
required: ['task_id', 'animal_id'],
properties: {
task_id: { type: 'integer' },
animal_id: { type: 'integer' },
},
additionalProperties: false,
};
}
}

export default TaskAnimalRelationshipModel;
Loading

0 comments on commit fba0450

Please sign in to comment.