diff --git a/src/controllers/productsController.ts b/src/controllers/productsController.ts index 68f8e148..66817feb 100644 --- a/src/controllers/productsController.ts +++ b/src/controllers/productsController.ts @@ -18,23 +18,27 @@ import Order from '../database/models/order'; import OrderItems from '../database/models/orderItems'; export const createProduct = async (req: Request, res: Response) => { + const transaction = await sequelize.transaction(); try { - const { name, description, colors } = req.body as ProductAttributes; - const { categoryId } = req.params; + const { categoryId, name, description, colors, sizes } = req.body as ProductAttributes & { + sizes: SizeAttributes[]; + }; const seller = (await req.user) as User; const sellerId = seller.id; - // when products exists - const thisProductExists = await Product.findOne({ where: { name } }); + // Check if product exists + const thisProductExists = await Product.findOne({ where: { name }, transaction }); if (thisProductExists) { + await transaction.rollback(); return res.status(400).json({ ok: false, message: 'This Product already exists, You can update the stock levels instead.', data: thisProductExists, }); } - // handle images + + // Handle images const productImages: string[] = []; const images: unknown = req.files; if (images instanceof Array && images.length > 3) { @@ -44,34 +48,33 @@ export const createProduct = async (req: Request, res: Response) => { productImages.push(url); } } else { + await transaction.rollback(); return res.status(400).json({ message: 'Product should have at least 4 images', }); } - // create product - await Product.create({ sellerId, name, description, categoryId, colors, images: productImages }); + // Create product + const product = await Product.create( + { sellerId, name, description, categoryId, colors, images: productImages }, + { transaction } + ); - res.status(201).json({ - ok: true, - message: 'Thank you for adding new product in the store!', - }); - } catch (error) { - sendInternalErrorResponse(res, error); - } -}; - -export const createSize = async (req: Request, res: Response) => { - try { - const { productId } = req.params; - const { size, price, discount, expiryDate, quantity } = req.body as SizeAttributes; + // Create sizes + if (sizes || sizes.length > 0) { + for (const sizeData of sizes) { + await Size.create({ ...sizeData, productId: product.id }, { transaction }); + } + } - await Size.create({ size, price, discount, expiryDate, productId, quantity }); + await transaction.commit(); res.status(201).json({ ok: true, - message: 'Product size added successfully', + message: 'Thank you for adding new product in the store!', + product, }); } catch (error) { + await transaction.rollback(); sendInternalErrorResponse(res, error); } }; diff --git a/src/database/models/index.ts b/src/database/models/index.ts index 6a510047..2175a462 100644 --- a/src/database/models/index.ts +++ b/src/database/models/index.ts @@ -1,7 +1,7 @@ 'use strict'; import { Sequelize, Dialect } from 'sequelize'; -const env: string = process.env.NODE_ENV || 'development'; +const env = process.env.NODE_ENV as string; const config = require('../config/config.js'); diff --git a/src/routes/productRoutes.ts b/src/routes/productRoutes.ts index a598a00f..d8a37df0 100644 --- a/src/routes/productRoutes.ts +++ b/src/routes/productRoutes.ts @@ -2,7 +2,6 @@ import { Router } from 'express'; import { createProduct, - createSize, deleteProductById, getAllProduct, getProductById, @@ -20,7 +19,7 @@ import { checkUserRoles, isAuthenticated } from '../middlewares/authMiddlewares' const router = Router(); router.post( - '/:categoryId/create-product/', + '/create-product/', isAuthenticated, checkUserRoles('seller'), multerUpload.array('images', 8), @@ -34,7 +33,6 @@ router.put( checkUserRoles('seller'), updateProduct ); -router.post('/:productId/add-size', isAuthenticated, checkUserRoles('seller'), createSize); router.get('/', getAllProduct); router.get('/sizes', isAuthenticated, checkUserRoles('seller'), getAllSizes); diff --git a/src/test/index.ts b/src/test/index.ts new file mode 100644 index 00000000..1950c3aa --- /dev/null +++ b/src/test/index.ts @@ -0,0 +1,21 @@ +import { addition } from '../controllers/testController'; + +test('Adding 2 and 1 equals 3', () => { + expect(2 + 1).toBe(3); +}); +test('Addition function adds two numbers correctly', () => { + // Test case 1: Testing addition of positive numbers + expect(addition(2, 3)).toBe(5); // Expected result: 2 + 3 = 5 + + // Test case 2: Testing addition of negative numbers + expect(addition(-2, -3)).toBe(-5); // Expected result: -2 + (-3) = -5 + + // Test case 3: Testing addition of a positive and a negative number + expect(addition(5, -3)).toBe(2); // Expected result: 5 + (-3) = 2 + + // Test case 4: Testing addition of zero and a number + expect(addition(0, 7)).toBe(7); // Expected result: 0 + 7 = 7 + + // Test case 5: Testing addition of a number and zero + expect(addition(4, 0)).toBe(4); // Expected result: 4 + 0 = 4 +});