Skip to content

Commit

Permalink
chnage in chackout
Browse files Browse the repository at this point in the history
  • Loading branch information
sevelinCa committed Jul 25, 2024
1 parent e9f1417 commit 840acd8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/controllers/Payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const checkout = async (req: Request, res: Response) => {
};
})
);
console.log(line_items)


const session = await stripe.checkout.sessions.create({
Expand Down
11 changes: 11 additions & 0 deletions src/controllers/checkout.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Cart from "../database/models/cart";
import CartItem from "../database/models/cartitem";
import { Request, Response } from "express";
import { v4 as uuidv4 } from 'uuid';
import Product from "../database/models/product";


export const createOrder = async (req: Request, res: Response) => {
Expand Down Expand Up @@ -41,6 +42,16 @@ export const createOrder = async (req: Request, res: Response) => {
totalAmount: totalAmount,
client
});
for (const item of orderItems) {
const product = await Product.findOne({ where: { productId: item.productId } });
if (product) {
product.quantity -= item.quantity;
if (product.quantity < 0) {
return res.status(400).json({ message: `Not enough stock for product ${product.productId}` });
}
await product.save();
}
}
await CartItem.destroy({ where: { cartId: cart.cartId } });
res.status(201).json({ message: 'Order placed successfully', order });
} catch (error: any) {
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/orderController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ export const getSellerOrder = async (req: Request, res: Response) => {
}
}

console.log("products_____:", products)

res.status(200).send(products);
} catch (error) {
console.log(error);
Expand Down

0 comments on commit 840acd8

Please sign in to comment.