Skip to content

Commit

Permalink
Implement API endpoint to fetch products belonging to a specific vend…
Browse files Browse the repository at this point in the history
…or (#156)
  • Loading branch information
wayneleon1 authored Jul 19, 2024
1 parent 2a1f852 commit 6121d7f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/__test__/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ describe('Product Controller Tests', () => {
expect(response.body.message).toEqual('Data retrieved successfully');
expect(Array.isArray(response.body.data)).toBeTruthy();
});
it('should retrieve all products by Vendor', async () => {
const response = await request(app)
.get('/api/v1/product/mine')
.set('Authorization', `Bearer ${token}`);
expect(response.statusCode).toEqual(200);
expect(response.body.message).toEqual('Data retrieved successfully');
expect(Array.isArray(response.body.data)).toBeTruthy();
});

it('should retrieve a single product by ID', async () => {
const response = await request(app)
Expand Down
30 changes: 28 additions & 2 deletions src/controller/productController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,32 @@ export const getBestSellingProducts = async (req: Request, res: Response) => {
res.json(result);
};

export const getMyProducts = errorHandler(
async (req: Request, res: Response) => {
const vendorId = req.user!.id;



const products = await productRepository.find({
where: {
vendor: {
id: vendorId,
},
},
select: {
category: {
name: true,
},
reviews: {
content: true,
rating: true,
user: {
firstName: true,
},
},
},
relations: ['category', 'reviews'],
});
return res
.status(200)
.json({ message: 'Data retrieved successfully', data: products });
}
);
17 changes: 17 additions & 0 deletions src/docs/productDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@
* description: Products deleted successfully
* '500':
* description: Failed to delete products
*
*/
/**
* @swagger
* /api/v1/product/mine:
* get:
* summary: Get all products by Vendor
* tags: [Product]
* security:
* - bearerAuth: []
* responses:
* '200':
* description: Successful operation
* '500':
* description: Internal server error
* '401':
* description: Unauthorized
*/

/**
Expand Down
11 changes: 8 additions & 3 deletions src/routes/productRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
updateProductAvailability,
checkProductAvailability,
getBestSellingProducts,
getMyProducts,
} from '../controller/productController';
import { IsLoggedIn } from '../middlewares/isLoggedIn';
import { checkRole } from '../middlewares/authorize';
Expand All @@ -26,21 +27,25 @@ productRouter
.get(getAllProducts)
.delete(IsLoggedIn, deleteAllProduct);

productRouter
.route('/mine')
.get(IsLoggedIn, checkRole(['Vendor']), getMyProducts);

productRouter.route('/recommended').get(getRecommendedProducts);
productRouter.route('/bestselling').get(getBestSellingProducts);

productRouter
.route('/:productId')
.get(getProduct)
.put(IsLoggedIn, checkRole(['Vendor']), updateProduct)
.delete(IsLoggedIn, deleteProduct);
.put(IsLoggedIn, checkRole(['Vendor', 'Admin']), updateProduct)
.delete(IsLoggedIn, checkRole(['Vendor', 'Admin']), deleteProduct);

productRouter
.route('/:productId/availability')
.get(IsLoggedIn, checkProductAvailability)
.put(
IsLoggedIn,
checkRole(['Vendor']),
checkRole(['Vendor', 'Admin']),
validateAvailability,
updateProductAvailability
);
Expand Down

0 comments on commit 6121d7f

Please sign in to comment.