Skip to content

Commit

Permalink
fix(review-swagger-docs): fix swagger documentation of thereview task (
Browse files Browse the repository at this point in the history
…#114)

- add missing security tag in review docs

[Fixes #113]

update profile (#72) (#104)

review controller

adding testing

fix lint issue

update profile (#72) (#104)

review controller

adding testing

fix lint issue
  • Loading branch information
jkarenzi authored and bertrandshema committed May 31, 2024
1 parent f7e2d7f commit acfeda4
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 4 deletions.
76 changes: 76 additions & 0 deletions src/__test__/orderController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import request from 'supertest';
import app from '../app';
import { Order } from '../database/models/orderEntity';
import { afterAllHook, beforeAllHook } from './testSetup';
import dbConnection from '../database';

const orderRepository = dbConnection.getRepository(Order);

beforeAll(beforeAllHook);
afterAll(afterAllHook);

describe('Order Routes', () => {
describe('PUT /order/:orderId', () => {
it('should update order status to Failed', async () => {
const order = orderRepository.create({
status: 'Pending',
totalAmount: 40,
trackingNumber: '34343653',
});
await orderRepository.save(order);

const response = await request(app)
.put(`/api/v1/order/${order.id}`)
.send({ status: 'Failed' });

expect(response.status).toBe(200);
expect(response.body).toEqual({ msg: 'Order status updated to Failed' });
});

it('should return 400 if orderId is invalid', async () => {
const response = await request(app)
.put('/api/v1/order/invalid')
.send({ status: 'Failed' });

expect(response.status).toBe(400);
expect(response.body).toEqual({ msg: 'Invalid orderId' });
});

it('should return 400 if status is invalid', async () => {
const order = orderRepository.create({
status: 'Pending',
totalAmount: 40,
trackingNumber: '34343653',
});
await orderRepository.save(order);

const response = await request(app)
.put(`/api/v1/order/${order.id}`)
.send({ status: 'InvalidStatus' });

expect(response.status).toBe(400);
expect(response.body).toEqual({ msg: 'Invalid status' });
});

it('should return 404 if order is not found', async () => {
const response = await request(app)
.put('/api/v1/order/9999')
.send({ status: 'Failed' });

expect(response.status).toBe(404);
expect(response.body).toEqual({ msg: 'Order Not Found' });
});

it('should return 500 if there is a server error', async () => {
jest
.spyOn(orderRepository, 'findOne')
.mockRejectedValue(new Error('Database error'));

const response = await request(app)
.put('/api/v1/order/1')
.send({ status: 'Failed' });

expect(response.status).toBe(500);
});
});
});
52 changes: 52 additions & 0 deletions src/controller/OrderController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Request, Response } from 'express';
import dbConnection from '../database';
import errorHandler from '../middlewares/errorHandler';
import { Order } from '../database/models/orderEntity';

const orderRepository = dbConnection.getRepository(Order);

export const updateOrderStatus = errorHandler(
async (req: Request, res: Response) => {
const { orderId } = req.params;
const { status } = req.body;

// Convert orderId to a number
const numericOrderId = parseInt(orderId, 10);
if (isNaN(numericOrderId)) {
return res.status(400).json({ msg: 'Invalid orderId' });
}

// Validating the status
const validStatuses = [
'Pending',
'Failed',
'Canceled',
'Paid',
'Shipping',
'Delivered',
'Returned',
'Completed',
];
if (!validStatuses.includes(status)) {
return res.status(400).json({ msg: 'Invalid status' });
}

const order = await orderRepository.findOne({
where: {
id: numericOrderId,
},
});

if (!order) {
return res.status(404).json({ msg: 'Order Not Found' });
}

// Update the order status
order.status = status;
await orderRepository.save(order);

return res
.status(200)
.json({ msg: `Order status updated to ${order.status}` });
}
);
8 changes: 5 additions & 3 deletions src/database/models/orderEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ export class Order {
@UpdateDateColumn()
updatedAt: Date;

@OneToMany(() => OrderDetails, orderDetails => orderDetails.order, { cascade: true })
@OneToMany(() => OrderDetails, (orderDetails) => orderDetails.order, {
cascade: true,
nullable: true,
})
orderDetails: OrderDetails[];

@Column({ type: 'boolean', default: false, nullable: true })
paid: boolean | null;

}
59 changes: 59 additions & 0 deletions src/docs/cartDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,62 @@
* 500:
* description: Internal Server Error
*/

/**
* @swagger
* /api/v1/order/{orderId}:
* put:
* summary: Update the status of an order
* tags: [Order]
* parameters:
* - name: orderId
* in: path
* required: true
* description: ID of the order to update
* schema:
* type: integer
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* status:
* type: string
* enum: [Pending, Failed, Canceled, Paid, Shipping, Delivered, Returned, Completed]
* example: Failed
* responses:
* 200:
* description: Order status updated successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* msg:
* type: string
* example: Order status updated to Failed
* 400:
* description: Invalid request
* content:
* application/json:
* schema:
* type: object
* properties:
* msg:
* type: string
* example: Invalid status
* 404:
* description: Order not found
* content:
* application/json:
* schema:
* type: object
* properties:
* msg:
* type: string
* example: Order Not Found
* 500:
* description: Internal Server Error
*/
4 changes: 3 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import cartRoutes from '../routes/cartRoutes';
import couponRouter from './couponRoute';
import chekoutRoutes from './checkoutRoutes';
import reviewRoute from './reviewRoutes';
import orderRoutes from './orderRoutes';

const router = Router();

Expand All @@ -19,6 +20,7 @@ router.use('/buyer', buyerRoutes);
router.use('/cart', cartRoutes);
router.use('/coupons', couponRouter);
router.use('/checkout', chekoutRoutes);
router.use('/review',reviewRoute)
router.use('/review', reviewRoute);
router.use('/order', orderRoutes);

export default router;
9 changes: 9 additions & 0 deletions src/routes/orderRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Router } from 'express';

import { updateOrderStatus } from '../controller/orderController';

const orderRoutes = Router();

orderRoutes.route('/:orderId').put(updateOrderStatus);

export default orderRoutes;

0 comments on commit acfeda4

Please sign in to comment.