diff --git a/src/controllers/productsController.ts b/src/controllers/productsController.ts index 66817feb..365de94a 100644 --- a/src/controllers/productsController.ts +++ b/src/controllers/productsController.ts @@ -20,7 +20,7 @@ import OrderItems from '../database/models/orderItems'; export const createProduct = async (req: Request, res: Response) => { const transaction = await sequelize.transaction(); try { - const { categoryId, name, description, colors, sizes } = req.body as ProductAttributes & { + const { categoryName, name, description, colors, sizes } = req.body as ProductAttributes & { sizes: SizeAttributes[]; }; const seller = (await req.user) as User; @@ -56,13 +56,13 @@ export const createProduct = async (req: Request, res: Response) => { // Create product const product = await Product.create( - { sellerId, name, description, categoryId, colors, images: productImages }, + { sellerId, name, description, categoryName, colors, images: productImages }, { transaction } ); // Create sizes if (sizes || sizes.length > 0) { - for (const sizeData of sizes) { + for (const sizeData of JSON.parse(sizes)) { await Size.create({ ...sizeData, productId: product.id }, { transaction }); } } @@ -278,12 +278,42 @@ export const getAllProduct = async (req: Request, res: Response) => { } }; +// Function to get all products by a particular seller +export const getAllProductsBySeller = async (req: Request, res: Response): Promise => { + try { + const { sellerId } = req.params; + + if (!sellerId) { + res.status(400).json({ error: 'Invalid sellerId' }); + return; + } + + const products = await Product.findAll({ + where: { + sellerId, + }, + attributes: ['id', 'name', 'description', 'images', 'categoryName'], // Select only necessary fields + order: [['createdAt', 'DESC']], + }); + + res.status(200).json({ + ok: true, + data: products, + }); + } catch (error) { + sendInternalErrorResponse(res, error); + } +}; + // a function to get a certain product by ID export const getProductById = async (req: Request, res: Response) => { try { const { productId } = req.params; const product = await Product.findByPk(productId, { - include: [{ model: Size, as: 'sizes' },{ model: Review, as: 'reviews', include: [{ model: User, as: 'user', attributes: ['photoUrl', 'firstName'] }] },], + include: [ + { model: Size, as: 'sizes' }, + { model: Review, as: 'reviews', include: [{ model: User, as: 'user', attributes: ['photoUrl', 'firstName'] }] }, + ], }); if (!product) { @@ -514,4 +544,3 @@ export const calculateAverageRating = async (req: Request, res: Response) => { sendInternalErrorResponse(res, error); } }; - diff --git a/src/database/migrations/20240717004630-update-product-attribute.js b/src/database/migrations/20240717004630-update-product-attribute.js new file mode 100644 index 00000000..ec1a27f4 --- /dev/null +++ b/src/database/migrations/20240717004630-update-product-attribute.js @@ -0,0 +1,28 @@ +'use strict'; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + // Rename the column + await queryInterface.renameColumn( + 'products', + 'categoryId', + 'categoryName', + { + type: Sequelize.STRING, + } + ); + }, + + async down(queryInterface, Sequelize) { + // Revert the change in case of rollback + await queryInterface.renameColumn( + 'products', + 'categoryName', + 'categoryId', + { + type: Sequelize.STRING, + } + ); + }, +}; diff --git a/src/database/models/Product.ts b/src/database/models/Product.ts index 9391df66..ca2fd382 100644 --- a/src/database/models/Product.ts +++ b/src/database/models/Product.ts @@ -12,7 +12,7 @@ export interface ProductAttributes { description: string; images: string[]; colors?: string[]; - categoryId: string; + categoryName: string; sizes?: any; createdAt?: Date; updatedAt?: Date; @@ -23,7 +23,7 @@ export class Product extends Model implements ProductAttribut public sellerId!: string; public name!: string; public description!: string; - public categoryId!: string; + public categoryName!: string; public images!: string[]; public colors!: string[]; public sizes!: any; @@ -65,7 +65,7 @@ Product.init( onUpdate: 'CASCADE', allowNull: false, }, - categoryId: { + categoryName: { type: DataTypes.UUID, references: { model: 'Category', @@ -76,6 +76,6 @@ Product.init( { sequelize: sequelize, timestamps: true, modelName: 'Product', tableName: 'products' } ); -Product.belongsTo(Category, { foreignKey: 'categoryId' }); +Product.belongsTo(Category, { foreignKey: 'categoryName' }); Product.belongsTo(User, { foreignKey: 'sellerId', as: 'user' }); Product.hasMany(Size, { foreignKey: 'productId', as: 'sizes' }); diff --git a/src/routes/productRoutes.ts b/src/routes/productRoutes.ts index d8a37df0..7130d02b 100644 --- a/src/routes/productRoutes.ts +++ b/src/routes/productRoutes.ts @@ -12,6 +12,7 @@ import { provideReviewToProduct, calculateAverageRating, deleteReview, + getAllProductsBySeller, } from '../controllers/productsController'; import multerUpload from '../helpers/multer'; import { checkUserRoles, isAuthenticated } from '../middlewares/authMiddlewares'; @@ -43,5 +44,6 @@ router.put('/:sizeId/unavailable', isAuthenticated, checkUserRoles('seller'), ma router.post('/:productId/review/', isAuthenticated, multerUpload.single('feedbackImage'), provideReviewToProduct); router.delete('/:productId/review/:reviewId', isAuthenticated, deleteReview); router.get('/:productId/review/statistics', calculateAverageRating); +router.get('/seller-products/:sellerId', isAuthenticated, checkUserRoles('seller'), getAllProductsBySeller); export default router;