Skip to content

Ben Doggett - Node Eval#177

Open
bhdoggett wants to merge 16 commits into
projectshft:masterfrom
bhdoggett:master
Open

Ben Doggett - Node Eval#177
bhdoggett wants to merge 16 commits into
projectshft:masterfrom
bhdoggett:master

Conversation

@bhdoggett
Copy link
Copy Markdown

No description provided.

Comment thread app/server.js Outdated
}

for (let i = 0; i < users.length; i++) {
if (users[i].email === currentValidatedUser.email) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this logic can be extracted to a function

Comment thread app/server.js Outdated
return response.status(404).json({ message: "Invalid product id" });
}

const { currentValidatedUser, isValidToken } = authenticate(request);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this should be before anything. Everything after this line is irrelevant, if the user is not authenticated

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I moved this to to top of all the relevant api routes

Comment thread app/server.js Outdated
}

for (let i = 0; i < users.length; i++) {
if (users[i].email === currentValidatedUser.email) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

dry, you are doing this again

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Also, you can use uses.find

Comment thread app/server.js Outdated

for (let i = 0; i < users.length; i++) {
if (users[i].email === currentValidatedUser.email) {
const productIndex = users[i].cart.findIndex(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

why not users[i].cart.find ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nesting loops is ok for this level but its not maintainable for larger datasets of either users of products. This will grow exponentially the iterations. This area is a very important part of the interview questions.
Check suggestion:

Comment thread app/server.js Outdated
Comment on lines +178 to +192
(item) => item.id === product.id
);

if (productIndex === -1) {
return response
.status(404)
.json({ message: "Product not found in cart" });
}

users[i].cart.splice(productIndex, 1);

return response.status(200).json({
message: `Item with product id ${product.id} deleted from cart`,
});
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

const user = users.find(u => u.email === currentValidatedUser.email);

if (!user) {
  return response.status(404).json({ message: "User not found" });
}

const productIndex = user.cart.findIndex(item => item.id === product.id);

if (productIndex === -1) {
  return response.status(404).json({ message: "Product not found in cart" });
}

user.cart.splice(productIndex, 1);

return response.status(200).json({
  message: `Item with product id ${product.id} deleted from cart`,
});

Comment thread test/server.test.js
});

describe('Login', () => {});
it("Should return an error message for invalid login credentials", function (done) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you should test both scenarios of no user name and no password in different tests

Comment thread test/server.test.js Outdated
.end((err, res) => {
res.status.should.equal(200);
res.body.should.be.an("array");
res.body.length.should.be.above(0);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this is not really testing anything, if you have a bug and return a bad item for a different brand, this test will pass and you will have a bug in production.
You should use .equals and test agains a mock.

Comment thread test/server.test.js Outdated
.end((err, res) => {
res.status.should.equal(200);
res.body.should.be.an("array");
res.body.length.should.be.above(0);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

same as above, this test is not testing anything

@bhdoggett
Copy link
Copy Markdown
Author

@ronyrosenberg Thanks Rony, really appreciate this. Some of this feels like it should be more instinctive by now (e.g. using .find rather than for loops, and general DRY principles). Not planning to resubmit for grading but I did just push a more recent commit with many of these changes.

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