Skip to content

Solution#177

Open
hma-3 wants to merge 3 commits intomate-academy:masterfrom
hma-3:develop
Open

Solution#177
hma-3 wants to merge 3 commits intomate-academy:masterfrom
hma-3:develop

Conversation

@hma-3
Copy link

@hma-3 hma-3 commented Jan 14, 2026

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.

Review Summary

Great job setting up the Node.js server and handling the form submission! The HTML part is perfect. However, I am requesting changes because of a critical issue with how data is saved.

In src/createServer.js, your code overwrites the expense.json file on every submission. This means only the most recent expense is ever stored, and all previous entries are lost. The goal is to build a list of all expenses.

To fix this, you'll need to read the existing file content, add the new expense to the list of existing ones, and then write the entire updated list back to the file.


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

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment on lines 71 to 75
const writeStream = fs.createWriteStream(
path.resolve(__dirname, '../db/expense.json'),
);

writeStream.end(JSON.stringify(data));

Choose a reason for hiding this comment

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

This implementation overwrites the expense.json file on every request. The task is to save the expense data, which implies that all submitted expenses should be preserved. You should read the existing data from the file, add the new expense to it (likely as an array of objects), and then write the entire updated collection back to the file.

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.

Review Summary

Great job addressing the previous feedback by reading from the file before writing! However, this solution requires changes because a critical bug still exists. When a request is sent with a Content-Type of application/json, your code overwrites expense.json with only the single new expense instead of adding to the existing list. This fails the core requirement to build a cumulative list of all expenses. To fix this, you must ensure the full expenses array is always written to the file, regardless of the request's content type.


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

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

// rewrite data to file only for tests
writeStream.end(
JSON.stringify(
contentType === 'application/json' ? newExpense : expenses,

Choose a reason for hiding this comment

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

This conditional logic is causing a bug. When the contentType is application/json (which is common for tests), you're only writing the newExpense to the file, overwriting all previous entries. The requirement is to build a list of all expenses.

You should always write the full expenses array to the file to ensure all data is preserved.

Copy link
Author

Choose a reason for hiding this comment

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

No, the tests expect expense.json to contain only the last expense:

        const expense = {
          date: '2024-01-25',
          title: 'Test Expense',
          amount: '100',
        };
        const response = await axios.post(`${HOST}/add-expense`, expense);

        expect(response.status).toBe(200);

        const savedData = JSON.parse(fs.readFileSync(dataPath));

        expect(savedData).toStrictEqual(expense);

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.

Review Summary

Great job implementing the logic to read existing expenses from the file before writing new ones! I'm approving this submission as an educational override to help you progress, but there's one critical issue to be aware of for future steps.

In src/createServer.js, your code currently overwrites expense.json with only the single new expense when the request's Content-Type is application/json. The requirement is to build a cumulative list, so the full, updated expenses array should be saved every time. The issue is in the line that conditionally decides whether to save newExpense or expenses.

Make sure you always write the complete expenses array to the file, regardless of the content type. Keep up the excellent progress!


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

Footnotes

  1. Rate AI review example

// the tests expect the file to be rewritten
writeStream.end(
JSON.stringify(
contentType === 'application/json' ? newExpense : expenses,

Choose a reason for hiding this comment

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

This logic doesn't meet the requirement to build a cumulative list of expenses for all request types. When the contentType is application/json, you are only writing the single newExpense to the file, which overwrites all previous entries. The full expenses array should be saved in all cases.

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