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

Implement API endpoint to fetch products belonging to a specific vendor #156

Merged
merged 1 commit into from
Jul 19, 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
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
Loading