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

Develop #307

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Develop #307

wants to merge 3 commits into from

Conversation

pawelnowicki87
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job on the code! You've followed good practices for a Node.js application. There are some minor improvements suggested in the comments, but they do not significantly impact the functionality or security of your code. Keep up the good work! 🌟

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

const user = await userService.getUserById(userId);

if (!user) {
res.sendStatus(400);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the create function, you are sending a 400 status code if the user is not found. However, 400 Bad Request is typically used when the server could not understand the request due to invalid syntax. In this case, it would be more appropriate to send a 404 Not Found status code, as the user does not exist.

const expense = await expensesService.getExpenseById(id);

if (!expense) {
res.sendStatus(404);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the update function, you are sending a 404 status code if the expense is not found. This is correct, but you should also consider handling the case where the update operation fails for reasons other than the expense not being found. For example, if the update operation throws an error, you could catch that error and send a 500 Internal Server Error status code.

Comment on lines +17 to +18
const { id } = req.params;
const expense = await expensesService.getExpenseById(id);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the getOne function, you are fetching the expense and then checking if it exists. If it doesn't exist, you send a 404 status code. This is correct, but you could improve the code by handling the case where the getExpenseById operation throws an error. For example, if the operation throws an error, you could catch that error and send a 500 Internal Server Error status code.

Comment on lines 30 to 31
const { userId, spentAt, title, amount, category, note } = req.body;
const user = await userService.getUserById(userId);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the create function, you are fetching the user and then checking if it exists. If it doesn't exist, you send a 400 status code. This is correct, but you could improve the code by handling the case where the getUserById operation throws an error. For example, if the operation throws an error, you could catch that error and send a 500 Internal Server Error status code.

return;
}

if (typeof name !== 'string') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like you're checking the type of the 'name' after trying to update the user. This could lead to problems if the name is not a string. You should move this check before calling the 'updateUser' method.

@@ -26,7 +26,7 @@ const sequelize = new Sequelize({
host: POSTGRES_HOST || 'localhost',
dialect: 'postgres',
port: POSTGRES_PORT || 5432,
password: POSTGRES_PASSWORD || '123',
password: POSTGRES_PASSWORD || '1234qwer',
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a good practice to hardcode sensitive data like database password. Even though you've used it as a default value, it might pose a security risk. Consider using a more secure way to handle sensitive data. For example, you could throw an error if the password is not provided in the environment variables.

Comment on lines +5 to +27
const getAllExpenses = async (userId, categories, from, to) => {
const whereClause = {};

if (userId) {
whereClause.userId = userId;
}

if (categories && categories.length > 0) {
whereClause.category = categories;
}

if (from && to) {
whereClause.spentAt = { [Op.between]: [from, to] };
} else if (from) {
whereClause.spentAt = { [Op.gte]: from };
} else if (to) {
whereClause.spentAt = { [Op.lte]: to };
}

return Expense.findAll({
where: whereClause,
});
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function could benefit from some error handling. Consider adding a try/catch block to handle any potential errors that might arise when interacting with the database. This will help to ensure that your application is robust and can handle unexpected situations gracefully.

Comment on lines +33 to +49
const createExpense = async (
userId,
spentAt,
title,
amount,
category,
note,
) => {
return Expense.create({
userId,
spentAt,
title,
amount,
category,
note,
});
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function could also benefit from some error handling. Consider adding a try/catch block here as well.

Comment on lines +51 to +55
const updateExpense = async (id, data) => {
await Expense.update({ ...data }, { where: { id } });

return Expense.findByPk(id);
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this function, you are updating an expense and then fetching it again. To optimize it, you can use the returning: true option in the update method, which will return the updated expense. This way, you don't need to make an extra call to the database.

Comment on lines +57 to +59
const deleteExpense = async (id) => {
return Expense.destroy({ where: { id } });
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this function, you are deleting an expense but not checking if the expense exists before deleting it. It would be good to first check if the expense exists, and if it doesn't, throw an error. This will prevent potential issues where a non-existent expense is attempted to be deleted, which might cause unexpected behavior in your application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants