diff --git a/src/controllers/CartController.ts b/src/controllers/CartController.ts index 972a9c8..2e13696 100644 --- a/src/controllers/CartController.ts +++ b/src/controllers/CartController.ts @@ -1,5 +1,5 @@ import { Request, Response } from 'express'; -import { Cart, ICart, Product, Order, IOrder } from '../mongoose/Schema'; +import { Cart, ICart, Order } from '../mongoose/Schema'; import { sendEmailasync } from '../services/sendGrid'; import { config } from 'dotenv'; diff --git a/src/controllers/ProductController.ts b/src/controllers/ProductController.ts index 7b0d35a..9d4991c 100644 --- a/src/controllers/ProductController.ts +++ b/src/controllers/ProductController.ts @@ -5,7 +5,13 @@ export async function createProduct(req: Request, res: Response) { try { const { name, description, price, userId } = req.body; if(!name || !description || !price || !userId) { - res.status(400).json({ error: 'Name, description, price and user id are required.' }); + res.status(400).json({ error: 'Name, description, price are required.' }); + return; + } + + const productExists = await Product.exists({ name, userId }); + if(productExists) { + res.status(400).json({ error: 'Product already exists.' }); return; } const product: IProduct = await Product.create({ diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index f6cd72c..e9151c1 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -14,10 +14,11 @@ export async function createUser(req: Request, res: Response) { // checkIfUserExists return true if the user exists const userExists = await User.exists({ email }); if(userExists) { - return res.status(400).json({ error: 'User already exists' }); + return res.status(400).json({ error: 'User already exists, Try login :)' }); } const hashedPassword = await bcrypt.hash(password, 10); + const user: IUser = await User.create({ firstName, lastName, diff --git a/src/middlewares/checkAuth.ts b/src/middlewares/checkAuth.ts index 959c49f..9c62104 100644 --- a/src/middlewares/checkAuth.ts +++ b/src/middlewares/checkAuth.ts @@ -1,6 +1,5 @@ -import express, { Request, Response, NextFunction } from 'express'; -import jwt, { JwtPayload } from 'jsonwebtoken'; -import cookieParser from 'cookie-parser'; +import { Request, Response, NextFunction } from 'express'; +import jwt from 'jsonwebtoken'; interface AuthenticatedRequest extends Request { userId?: string; diff --git a/src/routes/cart.ts b/src/routes/cart.ts index 5d5644c..ac71e34 100644 --- a/src/routes/cart.ts +++ b/src/routes/cart.ts @@ -1,4 +1,4 @@ -import express, { Request } from 'express'; +import express from 'express'; import { authenticateToken } from '../middlewares/checkAuth'; import { addToCart, listCart, checkout } from '../controllers/CartController'; diff --git a/src/routes/product.ts b/src/routes/product.ts index 5225b6b..aa16636 100644 --- a/src/routes/product.ts +++ b/src/routes/product.ts @@ -1,4 +1,4 @@ -import express, { Request } from 'express'; +import express from 'express'; import { authenticateToken } from '../middlewares/checkAuth'; import { createProduct, listProducts } from '../controllers/ProductController';