done with checkout + started mail
This commit is contained in:
parent
0583ba5f51
commit
720abc8c02
5 changed files with 65 additions and 6 deletions
|
@ -3,3 +3,4 @@ DB_USERNAME=root
|
||||||
DB_PASSWORD=password
|
DB_PASSWORD=password
|
||||||
JWT_SECRET=your_secret_key
|
JWT_SECRET=your_secret_key
|
||||||
DATABASE_URL="URI"
|
DATABASE_URL="URI"
|
||||||
|
SENDGRID_API_KEY="API_KEY"
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { Request, Response } from 'express';
|
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) {
|
export async function addToCart(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
|
@ -21,7 +22,7 @@ export async function addToCart(req: Request, res: Response) {
|
||||||
} else {
|
} else {
|
||||||
cart.products[productId] += 1;
|
cart.products[productId] += 1;
|
||||||
}
|
}
|
||||||
cart.markModified('products'); // Mark 'products' field as modified
|
cart.markModified('products');
|
||||||
}
|
}
|
||||||
|
|
||||||
await cart.save();
|
await cart.save();
|
||||||
|
@ -32,8 +33,6 @@ export async function addToCart(req: Request, res: Response) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export async function listCart(req: Request, res: Response) {
|
export async function listCart(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const { userId } = req.body;
|
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.' });
|
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 });
|
||||||
|
}
|
|
@ -26,6 +26,14 @@ export interface ICart extends Document {
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IOrder extends Document {
|
||||||
|
userId: string;
|
||||||
|
products: { [itemId: string]: number };
|
||||||
|
emailSent: boolean;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
||||||
|
|
||||||
const UserSchema: Schema = new Schema({
|
const UserSchema: Schema = new Schema({
|
||||||
firstName: { type: String, required: true },
|
firstName: { type: String, required: true },
|
||||||
lastName: { type: String, required: true },
|
lastName: { type: String, required: true },
|
||||||
|
@ -52,10 +60,19 @@ const CartSchema: Schema = new Schema({
|
||||||
updatedAt: { type: Date, default: Date.now },
|
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 });
|
ProductSchema.index({ name: 1, userId: 1 }, { unique: true });
|
||||||
|
|
||||||
const User = mongoose.model<IUser>('User', UserSchema);
|
const User = mongoose.model<IUser>('User', UserSchema);
|
||||||
const Product = mongoose.model<IProduct>('Product', ProductSchema);
|
const Product = mongoose.model<IProduct>('Product', ProductSchema);
|
||||||
const Cart = mongoose.model<ICart>('Cart', CartSchema);
|
const Cart = mongoose.model<ICart>('Cart', CartSchema);
|
||||||
|
const Order = mongoose.model<IOrder>('Order', OrderSchema);
|
||||||
|
|
||||||
export { User, Product, Cart };
|
export { User, Product, Cart, Order };
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import express, { Request } from 'express';
|
import express, { Request } from 'express';
|
||||||
import { authenticateToken } from '../middlewares/checkAuth';
|
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.post('/', authenticateToken, addToCart);
|
||||||
cartRouter.get('/', authenticateToken, listCart);
|
cartRouter.get('/', authenticateToken, listCart);
|
||||||
|
|
||||||
|
cartRouter.post('/checkout', authenticateToken, checkout);
|
||||||
|
|
||||||
export default cartRouter;
|
export default cartRouter;
|
13
src/services/sendGrid.ts
Normal file
13
src/services/sendGrid.ts
Normal file
|
@ -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: '<strong>Thank you for shopping with us!</strong>'
|
||||||
|
};
|
||||||
|
};
|
Loading…
Reference in a new issue