Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lf 4380 api route for edit update route for edit animal and batch details #3484

Draft
wants to merge 20 commits into
base: integration
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
02567b0
LF-4380 Add edit route on animals and batches
Duncan-Brain Aug 23, 2024
f0907a2
LF-4380 Make edit endpoints for controller
Duncan-Brain Aug 26, 2024
909ec4b
LF-4380 Allow specifiying enum key for tests
Duncan-Brain Sep 27, 2024
d7ec5c0
LF-4380 Add use relationships to tableCleanup
Duncan-Brain Sep 27, 2024
a9005fd
LF-4380 Add edit test and use async await on requests
Duncan-Brain Sep 27, 2024
6ecee80
LF-4380 Add tests for animal batches and add async await syntax to re…
Duncan-Brain Sep 27, 2024
6b879c9
LF-4380 Allow origin_id to be edited
Duncan-Brain Sep 27, 2024
12fd4b1
LF-4380 Add comment for delineating Remove tests
Duncan-Brain Oct 2, 2024
41e2a67
LF-4380 Move add animal functions to be reused in edit to utility fun…
Duncan-Brain Oct 2, 2024
8581fee
LF-4380 Replace code with utility function to be reused in edit, and …
Duncan-Brain Oct 2, 2024
f994fa6
LF-4380 Add utility functions to add batch and comments about groups too
Duncan-Brain Oct 2, 2024
4338df8
LF-4380 Update animals and batch edit endpoint to use types breeds an…
Duncan-Brain Oct 2, 2024
304d1ee
LF-4380 Consolidate middleware for reuse in edit endpoint
Duncan-Brain Oct 2, 2024
3e4543b
LF-4380 Extract logical checks for reuse
Duncan-Brain Oct 4, 2024
2d398ee
LF-4380 WIP - Add type and breed checks to edit and refine functions
Duncan-Brain Oct 4, 2024
7e2dfa8
LF-4380 Genericize type function
Duncan-Brain Oct 4, 2024
9690443
LF-4380 Reword check breed matched type to account for editing
Duncan-Brain Oct 4, 2024
3eb111a
LF-4380 Finish consolidating breed checks to mirror type checks
Duncan-Brain Oct 4, 2024
dddfdc2
LF-4380 Pass animalOrBatchRecord as prop, rename function, add comments
Duncan-Brain Oct 4, 2024
a4310da
LF-4380 Add sex detail check to edit and adapt logical check for exis…
Duncan-Brain Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 90 additions & 45 deletions packages/api/src/controllers/animalBatchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
import { Model, transaction } from 'objection';
import AnimalBatchModel from '../models/animalBatchModel.js';
import baseController from './baseController.js';
import CustomAnimalBreedModel from '../models/customAnimalBreedModel.js';
import CustomAnimalTypeModel from '../models/customAnimalTypeModel.js';
import { handleObjectionError } from '../util/errorCodes.js';
import { assignInternalIdentifiers } from '../util/animal.js';
import { assignInternalIdentifiers, checkAndAddCustomTypeAndBreed } from '../util/animal.js';
import { uploadPublicImage } from '../util/imageUpload.js';

const animalBatchController = {
Expand Down Expand Up @@ -60,50 +58,14 @@ const animalBatchController = {
const { farm_id } = req.headers;
const result = [];

// avoid attempts to add an already created type or breed to the DB
// where multiple batches have the same type_name or breed_name
const typeIdsMap = {};
const typeBreedIdsMap = {};
// Create utility object used in type and breed
req.body.typeIdsMap = {};
req.body.typeBreedIdsMap = {};

for (const animalBatch of req.body) {
if (animalBatch.type_name) {
let typeId = typeIdsMap[animalBatch.type_name];

if (!typeId) {
const newType = await baseController.postWithResponse(
CustomAnimalTypeModel,
{ type: animalBatch.type_name, farm_id },
req,
{ trx },
);
typeId = newType.id;
typeIdsMap[animalBatch.type_name] = typeId;
}
animalBatch.custom_type_id = typeId;
delete animalBatch.type_name;
}

if (animalBatch.breed_name) {
const typeColumn = animalBatch.default_type_id ? 'default_type_id' : 'custom_type_id';
const typeId = animalBatch.type_name
? typeIdsMap[animalBatch.type_name]
: animalBatch.default_type_id || animalBatch.custom_type_id;
const typeBreedKey = `${typeColumn}_${typeId}_${animalBatch.breed_name}`;
let breedId = typeBreedIdsMap[typeBreedKey];

if (!breedId) {
const newBreed = await baseController.postWithResponse(
CustomAnimalBreedModel,
{ farm_id, [typeColumn]: typeId, breed: animalBatch.breed_name },
req,
{ trx },
);
breedId = newBreed.id;
typeBreedIdsMap[typeBreedKey] = breedId;
}
animalBatch.custom_breed_id = breedId;
delete animalBatch.breed_name;
}
await checkAndAddCustomTypeAndBreed(req, animalBatch, farm_id, trx);
// TODO: allow animal group addition on creation like animals
// await checkAndAddGroup(req, animal, farm_id, trx);

// Remove farm_id if it happens to be set in animal object since it should be obtained from header
delete animalBatch.farm_id;
Expand All @@ -115,8 +77,17 @@ const animalBatchController = {
{ trx },
);

// TODO: allow animal group addition on creation like animals
// Format group_ids
// const groupIdMap =
// individualAnimalBatchResult.group_ids?.map((group) => group.animal_group_id) || [];
// individualAnimalBatchResult.group_ids = groupIdMap;

result.push(individualAnimalBatchResult);
}
// delete utility objects
delete req.body.typeIdsMap;
delete req.body.typeBreedIdsMap;

await trx.commit();

Expand All @@ -128,6 +99,80 @@ const animalBatchController = {
};
},

editAnimalBatches() {
return async (req, res) => {
const trx = await transaction.start(Model.knex());

try {
const { farm_id } = req.headers;

// Create utility object used in type and breed
req.body.typeIdsMap = {};
req.body.typeBreedIdsMap = {};

// select only allowed properties to edit
for (const animalBatch of req.body) {
await checkAndAddCustomTypeAndBreed(req, animalBatch, farm_id, trx);
// TODO: allow animal group editing
// await checkAndAddGroup(req, animal, farm_id, trx);

const {
id,
count,
custom_breed_id,
custom_type_id,
default_breed_id,
default_type_id,
name,
notes,
photo_url,
organic_status,
supplier,
price,
sex_detail,
origin_id,
group_ids,
animal_batch_use_relationships,
} = animalBatch;

await baseController.upsertGraph(
AnimalBatchModel,
{
id,
count,
custom_breed_id,
custom_type_id,
default_breed_id,
default_type_id,
name,
notes,
photo_url,
organic_status,
supplier,
price,
sex_detail,
origin_id,
group_ids,
animal_batch_use_relationships,
},
req,
{ trx },
);
}

// delete utility objects
delete req.body.typeIdsMap;
delete req.body.typeBreedIdsMap;

await trx.commit();
// Do not send result revalidate using tags on frontend
return res.status(204).send();
} catch (error) {
handleObjectionError(error, res, trx);
}
};
},

removeAnimalBatches() {
return async (req, res) => {
const trx = await transaction.start(Model.knex());
Expand Down
Loading
Loading