Skip to content

Commit

Permalink
Merge branch integration into POC-switch-to-pnpm-workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
navDhammu committed Nov 9, 2024
2 parents d2b8e49 + e808cba commit 43e674a
Show file tree
Hide file tree
Showing 38 changed files with 828 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 { fileURLToPath } from 'url';
import path, { dirname } from 'path';
import fs from 'fs';

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export const up = async function (knex) {
try {
await knex.schema.dropView('animal_union_batch_id_view');

// Recreate animal_union_batch_id_view VIEW
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const sqlFilePath = path.join(
__dirname,
'../sql/20241108052304_animal_union_batch_id_view.sql',
);
const sqlQuery = fs.readFileSync(sqlFilePath).toString();
await knex.raw(sqlQuery);
} catch (error) {
console.error('Error in migration up:', error);
throw error; // Rethrow the error to ensure the migration fails
}
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export const down = async (knex) => {
try {
await knex.schema.dropView('animal_union_batch_id_view');

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const sqlFilePath = path.join(
__dirname,
'../sql/20240207214919_animal_union_batch_id_view.sql',
);
const sqlQuery = fs.readFileSync(sqlFilePath).toString();
await knex.raw(sqlQuery);
} catch (error) {
console.error('Error in migration down:', error);
throw error; // Rethrow the error to ensure the migration fails
}
};
40 changes: 40 additions & 0 deletions packages/api/db/sql/20241108052304_animal_union_batch_id_view.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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/>.
*/

CREATE VIEW animal_union_batch_id_view AS
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY farm_id ORDER BY created_at, id, batch)::INTEGER AS internal_identifier
FROM (
SELECT
id,
farm_id,
FALSE AS batch,
created_at
FROM
animal a

UNION ALL

SELECT
id,
farm_id,
TRUE AS batch,
created_at
FROM
animal_batch ab
) animal_union_batch_id_view
ORDER BY
created_at, id, batch;
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;
Loading

0 comments on commit 43e674a

Please sign in to comment.