Removed unused modules + error handling

This commit is contained in:
Kfir Dayan 2023-06-11 00:28:02 +03:00
parent ca14c2ec4f
commit 84af08450e
6 changed files with 14 additions and 8 deletions

View file

@ -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';

View file

@ -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({

View file

@ -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,

View file

@ -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;

View file

@ -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';

View file

@ -1,4 +1,4 @@
import express, { Request } from 'express';
import express from 'express';
import { authenticateToken } from '../middlewares/checkAuth';
import { createProduct, listProducts } from '../controllers/ProductController';