Skip to content

Commit

Permalink
buyer should checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisegithub committed May 28, 2024
1 parent eb1df6e commit 9e04bed
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 130 deletions.
225 changes: 111 additions & 114 deletions src/__test__/cartController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
15 changes: 2 additions & 13 deletions src/controller/cartController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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({});
Expand Down Expand Up @@ -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' });
});

7 changes: 4 additions & 3 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

0 comments on commit 9e04bed

Please sign in to comment.