diff --git a/.env.example b/.env.example index 5e2ba1f..a5aaaaa 100644 --- a/.env.example +++ b/.env.example @@ -3,3 +3,4 @@ DB_USERNAME=root DB_PASSWORD=password JWT_SECRET=your_secret_key DATABASE_URL="URI" +SENDGRID_API_KEY="API_KEY" diff --git a/src/controllers/CartController.ts b/src/controllers/CartController.ts index 86186ec..0b8464f 100644 --- a/src/controllers/CartController.ts +++ b/src/controllers/CartController.ts @@ -1,5 +1,6 @@ import { Request, Response } from 'express'; -import { Cart, ICart } from '../mongoose/Schema'; +import { Cart, ICart, Product, Order, IOrder } from '../mongoose/Schema'; +import { sendEmailasync } from '../services/sendGrid'; export async function addToCart(req: Request, res: Response) { try { @@ -21,7 +22,7 @@ export async function addToCart(req: Request, res: Response) { } else { cart.products[productId] += 1; } - cart.markModified('products'); // Mark 'products' field as modified + cart.markModified('products'); } await cart.save(); @@ -32,8 +33,6 @@ export async function addToCart(req: Request, res: Response) { } } - - export async function listCart(req: Request, res: Response) { try { const { userId } = req.body; @@ -48,3 +47,30 @@ export async function listCart(req: Request, res: Response) { res.status(500).json({ error: 'An error occurred while listing the cart.' }); } } + +export async function checkout(req: Request, res: Response) { + + const { userId } = req.body; + const usersCart = await Cart.findOne({ userId }); + console.log(usersCart) + if (!usersCart) { + res.status(404).json({ error: 'Cart not found.' }); + return; + } + + const order = await Order.create({ + userId, + products: usersCart.products, + }); + + console.log(`order: ${order}`) + await removeCart(userId); + console.log(`cart removed`) + sendEmailasync(userId); + + res.status(200).json(order); +} + +async function removeCart(userId: string) { + await Cart.deleteOne({ userId }); +} \ No newline at end of file diff --git a/src/mongoose/Schema.ts b/src/mongoose/Schema.ts index c225ee3..5e0ffb0 100644 --- a/src/mongoose/Schema.ts +++ b/src/mongoose/Schema.ts @@ -26,6 +26,14 @@ export interface ICart extends Document { updatedAt: Date; } +export interface IOrder extends Document { + userId: string; + products: { [itemId: string]: number }; + emailSent: boolean; + createdAt: Date; + updatedAt: Date; +} + const UserSchema: Schema = new Schema({ firstName: { type: String, required: true }, lastName: { type: String, required: true }, @@ -52,10 +60,19 @@ const CartSchema: Schema = new Schema({ updatedAt: { type: Date, default: Date.now }, }); +const OrderSchema: Schema = new Schema({ + userId: { type: Schema.Types.ObjectId, ref: 'User', required: true }, + products: { type: Schema.Types.Mixed, default: {} }, + emailSent: { type: Boolean, default: false }, + createdAt: { type: Date, default: Date.now }, + updatedAt: { type: Date, default: Date.now }, +}); + ProductSchema.index({ name: 1, userId: 1 }, { unique: true }); const User = mongoose.model('User', UserSchema); const Product = mongoose.model('Product', ProductSchema); const Cart = mongoose.model('Cart', CartSchema); +const Order = mongoose.model('Order', OrderSchema); -export { User, Product, Cart }; +export { User, Product, Cart, Order }; diff --git a/src/routes/cart.ts b/src/routes/cart.ts index c307fde..5d5644c 100644 --- a/src/routes/cart.ts +++ b/src/routes/cart.ts @@ -1,6 +1,6 @@ import express, { Request } from 'express'; import { authenticateToken } from '../middlewares/checkAuth'; -import { addToCart, listCart } from '../controllers/CartController'; +import { addToCart, listCart, checkout } from '../controllers/CartController'; @@ -9,4 +9,6 @@ const cartRouter = express.Router(); cartRouter.post('/', authenticateToken, addToCart); cartRouter.get('/', authenticateToken, listCart); +cartRouter.post('/checkout', authenticateToken, checkout); + export default cartRouter; \ No newline at end of file diff --git a/src/services/sendGrid.ts b/src/services/sendGrid.ts new file mode 100644 index 0000000..a92969a --- /dev/null +++ b/src/services/sendGrid.ts @@ -0,0 +1,13 @@ +import { config } from "dotenv"; +import { User, Order } from "../mongoose/Schema"; +config(); + +export async function sendEmailasync (userId: string) { + const msg = { + to: await User.findOne({ _id: userId }).select('email'), + from: 'Ecom', + subject: 'Order Confirmation', + text: 'Your order has been placed', + html: 'Thank you for shopping with us!' + }; +}; \ No newline at end of file