From 9e04bed42846330f369c52e62a9c425e7dc1ad08 Mon Sep 17 00:00:00 2001 From: ambroisegithub Date: Mon, 27 May 2024 14:27:06 +0200 Subject: [PATCH] buyer should checkout --- src/__test__/cartController.test.ts | 225 ++++++++++++++-------------- src/controller/cartController.ts | 15 +- src/routes/index.ts | 7 +- 3 files changed, 117 insertions(+), 130 deletions(-) diff --git a/src/__test__/cartController.test.ts b/src/__test__/cartController.test.ts index f8d91251..511da57a 100644 --- a/src/__test__/cartController.test.ts +++ b/src/__test__/cartController.test.ts @@ -208,119 +208,116 @@ describe('Cart controller tests', () => { expect(response.body.count).toBeGreaterThanOrEqual(0); }); - // New tests for checkout, cancel checkout, and get all orders - - it('should place an order successfully', async () => { - const cartResponse = await request(app) - .post('/api/v1/cart') - .set('Authorization', `Bearer ${buyerToken}`) - .send({ - productId: productId, - quantity: 2, - }); - - expect(cartResponse.statusCode).toEqual(201); - expect(cartResponse.body.msg).toEqual('Item added to cart successfully'); - expect(cartResponse.body.cartItem).toBeDefined(); - - const checkoutResponse = await request(app) - .post('/api/v1/checkout') - .set('Authorization', `Bearer ${buyerToken}`) - .send({ - deliveryInfo: '123 Delivery St.', - paymentInfo: 'VISA 1234', - couponCode: 'DISCOUNT10', - }); - - expect(checkoutResponse.statusCode).toEqual(201); - expect(checkoutResponse.body.msg).toEqual('Order placed successfully'); - expect(checkoutResponse.body.order).toBeDefined(); - expect(checkoutResponse.body.trackingNumber).toBeDefined(); - orderId = checkoutResponse.body.order.id; - }); - - - - - it('should cancel an order successfully', async () => { - const response = await request(app) - .delete(`/api/v1/checkout/cancel-order/${orderId}`) - .set('Authorization', `Bearer ${buyerToken}`); - - expect(response.statusCode).toEqual(200); - expect(response.body.msg).toEqual('Order canceled successfully'); - }); - - it('should return 404 if order is not found while canceling', async () => { - const nonExistentOrderId = 9999; - const response = await request(app) - .delete(`/api/v1/checkout/cancel-order/${nonExistentOrderId}`) - .set('Authorization', `Bearer ${buyerToken}`); - - expect(response.statusCode).toEqual(404); - expect(response.body.msg).toEqual('Order not found'); - }); - - it('should return 401 if user is not found while checking out', async () => { - // Simulate a request with a non-existent user ID - const invalidUserToken = 'Bearer invalid-user-token'; - - const response = await request(app) - .post('/api/v1/checkout') - .set('Authorization', invalidUserToken) - .send({ - deliveryInfo: '123 Delivery St.', - paymentInfo: 'VISA 1234', - couponCode: 'DISCOUNT10', - }); - - expect(response.statusCode).toEqual(401); - expect(response.body.msg).toBeUndefined(); - }); - - it('should return all orders', async () => { - const response = await request(app) - .get('/api/v1/checkout/getall-order') - .set('Authorization', `Bearer ${buyerToken}`); - - expect(response.statusCode).toEqual(200); - expect(response.body.orders).toBeDefined(); - }); - - it('should return 400 if cart is empty while checking out', async () => { - // Clear the cart before attempting to checkout - await cartRepository.delete({}); - - const response = await request(app) - .post('/api/v1/checkout') - .set('Authorization', `Bearer ${buyerToken}`) - .send({ - deliveryInfo: '123 Delivery St.', - paymentInfo: 'VISA 1234', - couponCode: 'DISCOUNT10', - }); - - expect(response.statusCode).toEqual(400); - expect(response.body.msg).toEqual('Cart is empty'); - }); - - it('should return 404 if order is not found while canceling', async () => { - const nonExistentOrderId = 9999; - - const response = await request(app) - .delete(`/api/v1/checkout/cancel-order/${nonExistentOrderId}`) - .set('Authorization', `Bearer ${buyerToken}`); - - expect(response.statusCode).toEqual(404); - expect(response.body.msg).toEqual('Order not found'); - }); - - it('should delete all orders', async () => { - const response = await request(app) - .delete('/api/v1/checkout/removeall-order') - .set('Authorization', `Bearer ${buyerToken}`); - - expect(response.statusCode).toEqual(200); - expect(response.body.msg).toEqual('All orders deleted successfully'); + describe('Checkout Tests', () => { + it('should place an order successfully', async () => { + const cartResponse = await request(app) + .post('/api/v1/cart') + .set('Authorization', `Bearer ${buyerToken}`) + .send({ + productId: productId, + quantity: 2, + }); + + expect(cartResponse.statusCode).toEqual(201); + expect(cartResponse.body.msg).toEqual('Item added to cart successfully'); + expect(cartResponse.body.cartItem).toBeDefined(); + + const checkoutResponse = await request(app) + .post('/api/v1/checkout') + .set('Authorization', `Bearer ${buyerToken}`) + .send({ + deliveryInfo: '123 Delivery St.', + paymentInfo: 'VISA 1234', + couponCode: 'DISCOUNT10', + }); + + expect(checkoutResponse.statusCode).toEqual(201); + expect(checkoutResponse.body.msg).toEqual('Order placed successfully'); + expect(checkoutResponse.body.order).toBeDefined(); + expect(checkoutResponse.body.trackingNumber).toBeDefined(); + orderId = checkoutResponse.body.order.id; + }); + + it('should cancel an order successfully', async () => { + const response = await request(app) + .delete(`/api/v1/checkout/cancel-order/${orderId}`) + .set('Authorization', `Bearer ${buyerToken}`); + + expect(response.statusCode).toEqual(200); + expect(response.body.msg).toEqual('Order canceled successfully'); + }); + + it('should return 404 if order is not found while canceling', async () => { + const nonExistentOrderId = 9999; + const response = await request(app) + .delete(`/api/v1/checkout/cancel-order/${nonExistentOrderId}`) + .set('Authorization', `Bearer ${buyerToken}`); + + expect(response.statusCode).toEqual(404); + expect(response.body.msg).toEqual('Order not found'); + }); + + it('should return 401 if user is not found while checking out', async () => { + // Simulate a request with a non-existent user ID + const invalidUserToken = 'Bearer invalid-user-token'; + + const response = await request(app) + .post('/api/v1/checkout') + .set('Authorization', invalidUserToken) + .send({ + deliveryInfo: '123 Delivery St.', + paymentInfo: 'VISA 1234', + couponCode: 'DISCOUNT10', + }); + + expect(response.statusCode).toEqual(401); + expect(response.body.msg).toBeUndefined(); + }); + + it('should return all orders', async () => { + const response = await request(app) + .get('/api/v1/checkout/getall-order') + .set('Authorization', `Bearer ${buyerToken}`); + + expect(response.statusCode).toEqual(200); + expect(response.body.orders).toBeDefined(); + }); + + it('should return 400 if cart is empty while checking out', async () => { + // Clear the cart before attempting to checkout + await cartRepository.delete({}); + + const response = await request(app) + .post('/api/v1/checkout') + .set('Authorization', `Bearer ${buyerToken}`) + .send({ + deliveryInfo: '123 Delivery St.', + paymentInfo: 'VISA 1234', + couponCode: 'DISCOUNT10', + }); + + expect(response.statusCode).toEqual(400); + expect(response.body.msg).toEqual('Cart is empty'); + }); + + it('should return 404 if order is not found while canceling', async () => { + const nonExistentOrderId = 9999; + + const response = await request(app) + .delete(`/api/v1/checkout/cancel-order/${nonExistentOrderId}`) + .set('Authorization', `Bearer ${buyerToken}`); + + expect(response.statusCode).toEqual(404); + expect(response.body.msg).toEqual('Order not found'); + }); + + it('should delete all orders', async () => { + const response = await request(app) + .delete('/api/v1/checkout/removeall-order') + .set('Authorization', `Bearer ${buyerToken}`); + + expect(response.statusCode).toEqual(200); + expect(response.body.msg).toEqual('All orders deleted successfully'); + }); }); }); diff --git a/src/controller/cartController.ts b/src/controller/cartController.ts index c8b4948c..19ae322e 100644 --- a/src/controller/cartController.ts +++ b/src/controller/cartController.ts @@ -191,12 +191,6 @@ export const checkout = errorHandler(async (req: Request, res: Response) => { for (const item of cartItems) { const product = item.product; - if (item.quantity > product.quantity) { - return res.status(400).json({ - msg: 'quantity exceeds available stock', - }); - } - let price = product.salesPrice * item.quantity; // Apply any applicable coupon for each product @@ -240,6 +234,7 @@ export const checkout = errorHandler(async (req: Request, res: Response) => { }); }); + export const deleteAllOrders = errorHandler( async (req: Request, res: Response) => { const deletedOrders = await orderRepository.delete({}); @@ -270,14 +265,8 @@ export const cancelOrder = errorHandler(async (req: Request, res: Response) => { return res.status(404).json({ msg: 'Order not found' }); } - for (const orderDetail of order.orderDetails) { - if (orderDetail.product) { - orderDetail.product.quantity += orderDetail.quantity; - await dbConnection.getRepository('Product').save(orderDetail.product); - } - } - await orderRepository.remove(order); return res.status(200).json({ msg: 'Order canceled successfully' }); }); + diff --git a/src/routes/index.ts b/src/routes/index.ts index d310b939..bdf772b4 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -5,8 +5,8 @@ import productRoutes from './productRoutes'; import categoryRoutes from './categoryRoutes'; import buyerRoutes from './buyerRoutes'; import cartRoutes from '../routes/cartRoutes'; -import couponRouter from './couponRoute' - +import couponRouter from './couponRoute'; +import chekoutRoutes from './checkoutRoutes'; const router = Router(); router.use('/user', userRouter); @@ -15,6 +15,7 @@ router.use('/product', productRoutes); router.use('/category', categoryRoutes); router.use('/buyer', buyerRoutes); router.use('/cart', cartRoutes); -router.use('/coupons', couponRouter) +router.use('/coupons', couponRouter); +router.use('/checkout', chekoutRoutes); export default router;