Skip to content

Commit 85e4fe2

Browse files
committed
tests: Added unit test for notes service
1 parent aa98fde commit 85e4fe2

File tree

4 files changed

+422
-19
lines changed

4 files changed

+422
-19
lines changed

api/src/modules/note/notes.controllers.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@ const notesService = require('./notes.service');
44

55
const getAllNotes = async (req, res, next) => {
66
try {
7-
const { title } = req.query;
7+
const { query } = req;
88
const userId = req.user.sub;
99

10-
const filters = {
11-
userId,
12-
...(title && { title }),
13-
};
14-
15-
const note = await notesService.getAllNotes(filters);
10+
const note = await notesService.getAllNotes({ ...query, userId });
1611

1712
res.status(200).json({ note, sucess: true });
1813
} catch (error) {

api/src/modules/note/notes.service.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,87 @@
11
const Boom = require('@hapi/boom');
2+
const { Op } = require('sequelize');
23
const { v4: uuidv4 } = require('uuid');
34

45
const noteRepository = require('./notes.repository');
56

67
const formatNotes = (note) => {
78
return {
89
id: note.id,
9-
vaccine: note.title,
10+
title: note.title,
1011
description: note.description || null,
1112
userId: note.user ? note.user.id : null,
1213
user: note.user ? note.user.name : null,
1314
createdAt: note.createdAt.toISOString().split('T')[0] || note.createdAt,
1415
};
1516
};
1617

17-
const getAllNotes = async (filters) => {
18+
const buildFilters = (query) => {
19+
const { title, userId } = query;
20+
21+
return {
22+
...(title && { title: { [Op.iLike]: `%${title}%` } }),
23+
...(userId && { userId }),
24+
};
25+
};
26+
27+
const getAllNotes = async (query) => {
28+
if (!query || typeof query !== 'object') {
29+
throw Boom.badRequest('query was not provided');
30+
}
31+
32+
if (!query?.userId) {
33+
throw Boom.badData('userId was not provided');
34+
}
35+
36+
const filters = buildFilters(query);
37+
1838
const notes = await noteRepository.findAllNotes(filters);
1939
if (!notes?.length === 0) return [];
40+
2041
return notes.map((note) => formatNotes(note));
2142
};
2243

2344
const getNote = async (userId, noteId) => {
45+
if (!userId) throw Boom.notFound('userId was not provided');
46+
if (!noteId) throw Boom.notFound('noteId was not provided');
47+
2448
const note = await noteRepository.findOne(userId, noteId);
2549
if (!note?.id) throw Boom.notFound('Note does not exist');
50+
2651
return formatNotes(note);
2752
};
2853

2954
const createNote = async (noteData) => {
55+
if (!noteData?.userId) {
56+
throw Boom.badRequest('userId was not provided');
57+
}
58+
if (!noteData?.title && !noteData?.description) {
59+
throw Boom.badRequest('noteData was not provided');
60+
}
61+
3062
const note = {
3163
id: uuidv4(),
3264
title: noteData.title,
33-
description: noteData.description || null,
65+
description: noteData?.description || null,
3466
userId: noteData.userId,
3567
createdAt: noteData.createdAt || new Date().toISOString().split('T')[0],
3668
};
3769

3870
const newnote = await noteRepository.create(note);
39-
if (!newnote?.id)
71+
if (!newnote?.id) {
4072
throw Boom.badRequest('Something went wrong creating the note');
73+
}
74+
4175
return newnote;
4276
};
4377

4478
const updateNote = async (userId, noteId, noteData) => {
79+
if (!userId) throw Boom.badRequest('userId was not provided');
80+
if (!noteId) throw Boom.badRequest('noteId was not provided');
81+
if (!noteData?.title && !noteData?.description) {
82+
throw Boom.badRequest('noteData was not provided');
83+
}
84+
4585
const note = await getNote(userId, noteId);
4686
if (!note?.id) throw Boom.notFound('Note does not exist');
4787

@@ -54,17 +94,28 @@ const updateNote = async (userId, noteId, noteData) => {
5494
noteId,
5595
formattedNoteData,
5696
);
97+
5798
if (updatedRows === 0) {
5899
throw Boom.badRequest('Something went wrong creating the note');
59100
}
101+
60102
return updatedNote;
61103
};
62104

63105
const deleteNote = async (userId, noteId) => {
106+
if (!userId) throw Boom.badRequest('userId was not provided');
107+
if (!noteId) throw Boom.badRequest('noteId was not provided');
108+
64109
const note = await getNote(userId, noteId);
65110
if (!note?.id) throw Boom.conflict('note does not exists');
66111

67-
return noteRepository.destroy(noteId);
112+
const affectedRows = await noteRepository.destroy(noteId);
113+
114+
if (affectedRows === 0) {
115+
throw Boom.badRequest('Something went wrong deleting the note');
116+
}
117+
118+
return affectedRows;
68119
};
69120

70121
module.exports = {

api/src/modules/vaccination/vaccination.controllers.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ const vaccinationService = require('./vaccination.service');
44

55
const getAllVaccination = async (req, res, next) => {
66
try {
7-
const { vaccine, animalId } = req.query;
7+
const { query } = req;
88
const userId = req.user.sub;
99

10-
const filters = {
11-
...(vaccine && { vaccine }),
12-
...(animalId && { animalId }),
13-
};
14-
1510
const vaccinations = await vaccinationService.getAllVaccination(
1611
userId,
17-
filters,
12+
query,
1813
);
1914

2015
res.status(200).json({ vaccinations, sucess: true });

0 commit comments

Comments
 (0)