Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#116 Fix failing create review function #117

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/__test__/review.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,13 @@ describe('Review controller test', () => {
expect(responseReview.body.message).toEqual('you are already reviewed the product');

})

it('should return 400 for failed validation on create review', async () => {
const reviewBody = {content:'good', rating:15, productId:'some id'}
const responseReview = await request(app)
.post('/api/v1/review')
.set('Authorization', `Bearer ${buyerToken}`)
.send(reviewBody)
expect(responseReview.statusCode).toEqual(400);
})
})
13 changes: 12 additions & 1 deletion src/controller/reviewController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ import Product from '../database/models/productEntity';
import dbConnection from '../database';
import errorHandler from '../middlewares/errorHandler';
import UserModel from '../database/models/userModel';
import {createReviewSchema} from '../middlewares/createReviewSchema';

const productRepository = dbConnection.getRepository(Product);
const reviewRepository = dbConnection.getRepository(Review);
const userRepository = dbConnection.getRepository(UserModel);

export const createReview = errorHandler(async (req: Request, res: Response) => {
const { content, rating, productId } = req.body;
const formData = req.body;

const validationResult = createReviewSchema.validate(formData);
if (validationResult.error) {
return res
.status(400)
.json({ msg: validationResult.error.details[0].message });
}

const {content, rating, productId} = formData

const userId = req.user!.id;

const product = await productRepository.findOne({
Expand Down
40 changes: 14 additions & 26 deletions src/docs/reviewDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,19 @@
* tags: [Reviews]
* security:
* - bearerAuth: []
* consumes:
* - application/json
* produces:
* - application/json
* parameters:
* - in: body
* name: review
* description: The review object
* required: true
* schema:
* type: object
* properties:
* content:
* type: string
* description: The content of the review
* rating:
* type: integer
* description: The rating of the review (1 to 5)
* productId:
* type: integer
* description: The ID of the product being reviewed
* example:
* content: "This product is amazing!"
* rating: 5
* productId: 50
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* content:
* type: string
* rating:
* type: number
* productId:
* type: number
* responses:
* '201':
* description: Review created successfully
Expand All @@ -42,7 +30,7 @@
* description: A success message
* review:
* '400':
* description: Bad request, check the request body
* description: Bad request, failed validation
* '404':
* description: Product not found
* '409':
Expand Down
8 changes: 8 additions & 0 deletions src/middlewares/createReviewSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Joi from 'joi';


export const createReviewSchema = Joi.object({
content: Joi.string().required(),
rating: Joi.number().min(0).max(5).required(),
productId: Joi.number().required()
})
Loading