Skip to content

Ben Doggett - Node Eval#177

Open
bhdoggett wants to merge 16 commits intoprojectshft:masterfrom
bhdoggett:master
Open

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

Conversation

@bhdoggett
Copy link

No description provided.

app/server.js Outdated
}

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

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

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

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

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
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

app/server.js Outdated
}

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

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

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

app/server.js Outdated

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

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 ?

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:

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`,
});
}

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`,
});

});

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

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

.end((err, res) => {
res.status.should.equal(200);
res.body.should.be.an("array");
res.body.length.should.be.above(0);

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.

.end((err, res) => {
res.status.should.equal(200);
res.body.should.be.an("array");
res.body.length.should.be.above(0);

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
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