Skip to content

Commit 7b48f89

Browse files
authored
Merge pull request #98 from atlp-rwanda/187967401-fix-get-products-by-seller
fix-get-products-by-seller
2 parents 4cf7555 + 72f7b68 commit 7b48f89

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

Diff for: src/controllers/productsController.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import OrderItems from '../database/models/orderItems';
2020
export const createProduct = async (req: Request, res: Response) => {
2121
const transaction = await sequelize.transaction();
2222
try {
23-
const { categoryId, name, description, colors, sizes } = req.body as ProductAttributes & {
23+
const { categoryName, name, description, colors, sizes } = req.body as ProductAttributes & {
2424
sizes: SizeAttributes[];
2525
};
2626
const seller = (await req.user) as User;
@@ -56,13 +56,13 @@ export const createProduct = async (req: Request, res: Response) => {
5656

5757
// Create product
5858
const product = await Product.create(
59-
{ sellerId, name, description, categoryId, colors, images: productImages },
59+
{ sellerId, name, description, categoryName, colors, images: productImages },
6060
{ transaction }
6161
);
6262

6363
// Create sizes
6464
if (sizes || sizes.length > 0) {
65-
for (const sizeData of sizes) {
65+
for (const sizeData of JSON.parse(sizes)) {
6666
await Size.create({ ...sizeData, productId: product.id }, { transaction });
6767
}
6868
}
@@ -278,12 +278,42 @@ export const getAllProduct = async (req: Request, res: Response) => {
278278
}
279279
};
280280

281+
// Function to get all products by a particular seller
282+
export const getAllProductsBySeller = async (req: Request, res: Response): Promise<void> => {
283+
try {
284+
const { sellerId } = req.params;
285+
286+
if (!sellerId) {
287+
res.status(400).json({ error: 'Invalid sellerId' });
288+
return;
289+
}
290+
291+
const products = await Product.findAll({
292+
where: {
293+
sellerId,
294+
},
295+
attributes: ['id', 'name', 'description', 'images', 'categoryName'], // Select only necessary fields
296+
order: [['createdAt', 'DESC']],
297+
});
298+
299+
res.status(200).json({
300+
ok: true,
301+
data: products,
302+
});
303+
} catch (error) {
304+
sendInternalErrorResponse(res, error);
305+
}
306+
};
307+
281308
// a function to get a certain product by ID
282309
export const getProductById = async (req: Request, res: Response) => {
283310
try {
284311
const { productId } = req.params;
285312
const product = await Product.findByPk(productId, {
286-
include: [{ model: Size, as: 'sizes' },{ model: Review, as: 'reviews', include: [{ model: User, as: 'user', attributes: ['photoUrl', 'firstName'] }] },],
313+
include: [
314+
{ model: Size, as: 'sizes' },
315+
{ model: Review, as: 'reviews', include: [{ model: User, as: 'user', attributes: ['photoUrl', 'firstName'] }] },
316+
],
287317
});
288318

289319
if (!product) {
@@ -514,4 +544,3 @@ export const calculateAverageRating = async (req: Request, res: Response) => {
514544
sendInternalErrorResponse(res, error);
515545
}
516546
};
517-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
/** @type {import('sequelize-cli').Migration} */
4+
module.exports = {
5+
async up(queryInterface, Sequelize) {
6+
// Rename the column
7+
await queryInterface.renameColumn(
8+
'products',
9+
'categoryId',
10+
'categoryName',
11+
{
12+
type: Sequelize.STRING,
13+
}
14+
);
15+
},
16+
17+
async down(queryInterface, Sequelize) {
18+
// Revert the change in case of rollback
19+
await queryInterface.renameColumn(
20+
'products',
21+
'categoryName',
22+
'categoryId',
23+
{
24+
type: Sequelize.STRING,
25+
}
26+
);
27+
},
28+
};

Diff for: src/database/models/Product.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface ProductAttributes {
1212
description: string;
1313
images: string[];
1414
colors?: string[];
15-
categoryId: string;
15+
categoryName: string;
1616
sizes?: any;
1717
createdAt?: Date;
1818
updatedAt?: Date;
@@ -23,7 +23,7 @@ export class Product extends Model<ProductAttributes> implements ProductAttribut
2323
public sellerId!: string;
2424
public name!: string;
2525
public description!: string;
26-
public categoryId!: string;
26+
public categoryName!: string;
2727
public images!: string[];
2828
public colors!: string[];
2929
public sizes!: any;
@@ -65,7 +65,7 @@ Product.init(
6565
onUpdate: 'CASCADE',
6666
allowNull: false,
6767
},
68-
categoryId: {
68+
categoryName: {
6969
type: DataTypes.UUID,
7070
references: {
7171
model: 'Category',
@@ -76,6 +76,6 @@ Product.init(
7676
{ sequelize: sequelize, timestamps: true, modelName: 'Product', tableName: 'products' }
7777
);
7878

79-
Product.belongsTo(Category, { foreignKey: 'categoryId' });
79+
Product.belongsTo(Category, { foreignKey: 'categoryName' });
8080
Product.belongsTo(User, { foreignKey: 'sellerId', as: 'user' });
8181
Product.hasMany(Size, { foreignKey: 'productId', as: 'sizes' });

Diff for: src/routes/productRoutes.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
provideReviewToProduct,
1313
calculateAverageRating,
1414
deleteReview,
15+
getAllProductsBySeller,
1516
} from '../controllers/productsController';
1617
import multerUpload from '../helpers/multer';
1718
import { checkUserRoles, isAuthenticated } from '../middlewares/authMiddlewares';
@@ -43,5 +44,6 @@ router.put('/:sizeId/unavailable', isAuthenticated, checkUserRoles('seller'), ma
4344
router.post('/:productId/review/', isAuthenticated, multerUpload.single('feedbackImage'), provideReviewToProduct);
4445
router.delete('/:productId/review/:reviewId', isAuthenticated, deleteReview);
4546
router.get('/:productId/review/statistics', calculateAverageRating);
47+
router.get('/seller-products/:sellerId', isAuthenticated, checkUserRoles('seller'), getAllProductsBySeller);
4648

4749
export default router;

0 commit comments

Comments
 (0)