Skip to content

Commit

Permalink
Merge pull request #95 from atlp-rwanda/187933617-bug-fix-add-product
Browse files Browse the repository at this point in the history
Bug fix add product with size simultaneously
  • Loading branch information
niyontwali authored Jul 11, 2024
2 parents 358f307 + 7d4f2ba commit d4789da
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
47 changes: 25 additions & 22 deletions src/controllers/productsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/database/models/index.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
4 changes: 1 addition & 3 deletions src/routes/productRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { Router } from 'express';
import {
createProduct,
createSize,
deleteProductById,
getAllProduct,
getProductById,
Expand All @@ -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),
Expand All @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions src/test/index.ts
Original file line number Diff line number Diff line change
@@ -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
});

0 comments on commit d4789da

Please sign in to comment.