Skip to content

Project 3 - Ecommerce App (Marina and Ian)#55

Open
ianthehamster wants to merge 71 commits intorocketacademy:mainfrom
ianthehamster:main
Open

Project 3 - Ecommerce App (Marina and Ian)#55
ianthehamster wants to merge 71 commits intorocketacademy:mainfrom
ianthehamster:main

Conversation

@ianthehamster
Copy link

No description provided.

Mannushka and others added 26 commits April 10, 2024 21:30
add logic to fetch all orders of the current user
add fetching order address in getOne req
Fix product qty in orders, update stock upon submitting an order
implement transaction in post order request
@@ -1 +1,3 @@
node_modules/ No newline at end of file
Copy link

Choose a reason for hiding this comment

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

no readme

).routes();
const ordersRouter = new OrdersRouter(ordersController).routes();
const categoriesRouter = new CategoriesRouter(categoriesController).routes();
const usersRouter = new UsersRouter(usersController).routes();
Copy link

Choose a reason for hiding this comment

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

so you use checkJwt for the Products router, but not for the users or orders router? That seems a bit of an odd choice to me from a security perspective!

});
}
}
Order.init(
Copy link

Choose a reason for hiding this comment

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

what is your primary key? Try to always ensure that the model is a exact representation of your table in the DB

});

// Define association with categories here
this.belongsTo(models.category);
Copy link

Choose a reason for hiding this comment

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

products belong to categories? that seems wrong to me


async getAllAddresses(req, res) {
try {
const address = await this.model.sequelize.query(
Copy link

Choose a reason for hiding this comment

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

why do you use a raw query here?

Comment on lines +68 to +83
const productToBeBought = await this.model.findByPk(productId);

const { title, price, description } = productToBeBought.dataValues;

const purchasedProduct = {
title: title,
price: price,
description: description,
stock_purchased: stock_purchased,
};

const stock_left = productToBeBought.dataValues.stock_left;

await productToBeBought.update({
stock_left: stock_left - stock_purchased,
});
Copy link

Choose a reason for hiding this comment

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

What if you couldn't find a product? Then there is a lot of room for bugs here. Validating your requests params and values is key, as much as validating that your DB queries yield a result

Comment on lines +89 to +99
const { title, price, description, stock_left, img, categoryId } = req.body;

try {
const newProduct = await this.model.create({
title: title,
price: price,
description: description,
stock_left: stock_left,
img: img,
categoryId: categoryId,
});
Copy link

Choose a reason for hiding this comment

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

Here you blindly trust the client. Any request values that come in you just insert into the DB. What if certain values are undefined, what if some values are JS scripts or SQL? All this opens up your app to vulnerabilities

Comment on lines +248 to +257
Messages: [
{
From: {
Email: 'ianchow9898@gmail.com',
Name: 'Techie E-Store',
},
To: [
{
Email: userEmail,
Name: userFirstName,
Copy link

Choose a reason for hiding this comment

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

Whenever your controllers get too big, and we possibly use external providers to facilitate some functionality, it might make sense to change your architecture and folder structure. Here we could use a stripe service for example and an email service. This would abstract away all this code from the controller. You could google for three-tier architecture, hexagonal architecture, domain-driven design and other topics. If we update our controller to accept and respond to requests, and use services to handle business logic, our code gets easier to read and write as well.

}

async getUserBasedOnEmail(req, res) {
const { email } = req.body;
Copy link

Choose a reason for hiding this comment

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

now this is really bad. I can just use any hacked email list, and retrieve any user registered on your website. That means I get everyone's first name, last name and phone number. Scams incoming $$$$$$$

Copy link

Choose a reason for hiding this comment

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

What you should do:

  • Protect the route behind your authentication service
  • Make sure via the token, that only the user that the token belongs to has access to retrieve the user as well
  • Do not return sensitive information on the endpoint if possible

}
}

// Not working
Copy link

Choose a reason for hiding this comment

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

Then why keep it in the code? :D

Comment on lines +20 to +45
'/stripe',
this.controller.createStripeProduct.bind(this.controller),
);
router.post(
'/stripeCreate',
this.controller.seedStripeProducts.bind(this.controller),
);

router.post(
'/remainingStripeProducts',
this.controller.seedRemainingStripeProducts.bind(this.controller),
);

router.post(
'/create-checkout-session-external',
this.controller.makePaymentMultiple.bind(this.controller),
);

router.post(
'/get-stripe-prices',
this.controller.getAllPricesFromStripe.bind(this.controller),
);

router.post(
'/send-email-success',
this.controller.sendMailToCustomer.bind(this.controller),
Copy link

Choose a reason for hiding this comment

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

I don't agree with these routes. Those could fit better towards the REST pattern.

router.get('/', this.controller.getAllAddresses.bind(this.controller));
router.post('/', this.controller.postNewAddress.bind(this.controller));
router.get(
'/get-address-id',

Choose a reason for hiding this comment

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

it is a get route already, we don't need to define that in the route string itself


try {
const categoryToBeUpdated = await this.model.findByPk(categoryId);

Choose a reason for hiding this comment

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

basically can check here if category exists. If no, respond to the client

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.

3 participants