Skip to content

Commit 0672fe3

Browse files
committed
tests: Added unit test for animal service
1 parent a4702b6 commit 0672fe3

File tree

3 files changed

+420
-19
lines changed

3 files changed

+420
-19
lines changed

api/src/modules/animal/animal.controllers.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,12 @@ const animalService = require('./animal.service');
44

55
const getAllAnimals = async (req, res, next) => {
66
try {
7-
const { code, livestockType, animalType, breed, sex, animalId } = req.query;
7+
const { query } = req;
88
const userId = req.user.sub;
99

10-
const filters = {
11-
userId,
12-
...(animalId && { id: animalId }),
13-
...(code && { code }),
14-
...(livestockType && { livestockType }),
15-
...(animalType && { animalType }),
16-
...(breed && { breed }),
17-
...(sex && { sex }),
18-
};
19-
20-
const animals = await animalService.getAllAnimals(filters);
10+
const animals = await animalService.getAllAnimals({ ...query, userId });
2111

22-
res.status(200).json({ animals, sucess: true });
12+
res.status(200).json({ animals });
2313
} catch (error) {
2414
next(error);
2515
}
@@ -43,7 +33,6 @@ const createAnimal = async (req, res, next) => {
4333

4434
res.status(201).json({
4535
message: 'Animal was successfully created',
46-
success: true,
4736
newAnimal: formattedUpdatedAnimal,
4837
});
4938
} catch (error) {
@@ -71,7 +60,6 @@ const updateAnimal = async (req, res, next) => {
7160

7261
res.status(200).json({
7362
message: 'Animal was successfully updated',
74-
success: true,
7563
updatedAnimal: formattedUpdatedAnimal,
7664
});
7765
} catch (error) {
@@ -90,7 +78,6 @@ const deleteAnimal = async (req, res, next) => {
9078

9179
res.status(200).json({
9280
message: 'Animal was successfully deleted',
93-
success: true,
9481
deletedAnimal,
9582
});
9683
} catch (error) {

api/src/modules/animal/animal.service.js

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,56 @@ const formatData = (animalData) => {
1818
};
1919
};
2020

21-
const getAllAnimals = async (filters) => {
21+
const buildFilters = (query) => {
22+
const { code, livestockType, animalType, breed, sex, animalId, userId } =
23+
query;
24+
25+
return {
26+
userId,
27+
...(animalId && { id: animalId }),
28+
...(code && { code }),
29+
...(livestockType && { livestockType }),
30+
...(animalType && { animalType }),
31+
...(breed && { breed }),
32+
...(sex && { sex }),
33+
};
34+
};
35+
36+
const getAllAnimals = async (query) => {
37+
if (!query || typeof query !== 'object') {
38+
throw Boom.badRequest('query was not provided');
39+
}
40+
41+
const filters = buildFilters(query);
42+
2243
const animals = await animalRepository.findAllAnimals(filters);
23-
if (!animals?.length) return [];
44+
if (!animals?.length === 0) return [];
2445

2546
return animals.map((animal) => formatData(animal));
2647
};
2748

2849
const getAnimal = async (userId, animalId) => {
50+
if (!userId) throw Boom.badRequest('userId was not provided');
51+
if (!animalId) throw Boom.badRequest('animalId was not provided');
52+
2953
const animal = await animalRepository.findOne(userId, animalId);
3054
if (!animal?.id) throw Boom.notFound('Animal does not exist');
55+
3156
return formatData(animal);
3257
};
3358

3459
const createAnimal = async (animalData) => {
60+
if (
61+
!animalData?.livestockType ||
62+
!animalData?.animalType ||
63+
!animalData?.code ||
64+
!animalData?.breed ||
65+
!animalData?.sex ||
66+
!animalData?.userId
67+
) {
68+
throw Boom.badRequest('Some values were not provided');
69+
}
70+
3571
const animal = {
3672
id: uuidv4(),
3773
livestockType: animalData.livestockType,
@@ -56,6 +92,10 @@ const updateAnimal = async (userId, animalId, animalData) => {
5692
const animal = await animalRepository.findOne(userId, animalId);
5793
if (!animal?.id) throw Boom.conflict('Animal does not exists');
5894

95+
if (Object.entries(animalData).length === 0) {
96+
throw Boom.badRequest('No data was provided to update the animal');
97+
}
98+
5999
const formattedAnimalData = {
60100
...(animalData.livestockType && {
61101
livestockType: animalData.livestockType,
@@ -67,6 +107,11 @@ const updateAnimal = async (userId, animalId, animalData) => {
67107
...(animalData.motherId && { motherId: animalData.motherId }),
68108
...(animalData.fatherId && { fatherId: animalData.fatherId }),
69109
};
110+
111+
if (Object.keys(formattedAnimalData).length === 0) {
112+
throw Boom.badRequest('There is no data to update');
113+
}
114+
70115
const [updatedRows, [updatedAnimal]] = await animalRepository.update(
71116
animalId,
72117
formattedAnimalData,
@@ -78,7 +123,18 @@ const updateAnimal = async (userId, animalId, animalData) => {
78123
};
79124

80125
const deleteAnimal = async (userId, animalId) => {
81-
return animalRepository.destroy(userId, animalId);
126+
if (!userId) throw Boom.badRequest('userId was not provided');
127+
if (!animalId) throw Boom.badRequest('animalId was not provided');
128+
129+
const deletedRows = await animalRepository.destroy(userId, animalId);
130+
131+
if (deletedRows === 0) {
132+
throw Boom.badRequest(
133+
'Something went wrong deleting the animal or the animal does not exist',
134+
);
135+
}
136+
137+
return deletedRows;
82138
};
83139

84140
module.exports = {

0 commit comments

Comments
 (0)