-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(review-swagger-docs): fix swagger documentation of thereview task (…
…#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
1 parent
f7e2d7f
commit acfeda4
Showing
6 changed files
with
204 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` }); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |