From dc2ce4eda9036f2f204dc9488def561a57e0c545 Mon Sep 17 00:00:00 2001 From: patrickhag Date: Mon, 22 Jul 2024 11:51:56 +0200 Subject: [PATCH] Bug fix to fix update of the product --- src/controllers/productsController.ts | 49 ++++++++------------------- src/helpers/token.generator.ts | 6 ++-- src/middlewares/authMiddlewares.ts | 2 +- src/routes/productRoutes.ts | 2 +- 4 files changed, 20 insertions(+), 39 deletions(-) diff --git a/src/controllers/productsController.ts b/src/controllers/productsController.ts index 365de94a..913fc4f1 100644 --- a/src/controllers/productsController.ts +++ b/src/controllers/productsController.ts @@ -80,6 +80,7 @@ export const createProduct = async (req: Request, res: Response) => { }; export const updateProduct = async (req: Request, res: Response) => { + const transaction = await sequelize.transaction(); try { const { productId } = req.params; @@ -89,48 +90,28 @@ export const updateProduct = async (req: Request, res: Response) => { colors: req.body.colors, }; - // update images - if (req.files) { - const product = await Product.findByPk(productId); - const foundImages = product?.images; - - // delete already existing images - if (foundImages instanceof Array) { - for (let i = 0; i < foundImages.length; i++) { - const strImg = foundImages[i].toString(); - const imageId = extractImageId(strImg) as string; - await destroyImage(imageId); - product!.images = []; - } - } + // update product + await Product.update(data, { where: { id: productId }, transaction }); - // update new images - const images: unknown = req.files; - const productImages = []; - if (images instanceof Array && images.length > 3) { - for (const image of images) { - const imageBuffer: Buffer = image.buffer; - const url = await uploadImage(imageBuffer); - productImages.push(url); - Product.update({ images: productImages }, { where: { id: productId } }); - } - } else { - return res.status(400).json({ - message: 'Product should have at least 4 images', - }); + // update sizes if provided + if (req.body.sizes) { + const sizes = req.body.sizes as SizeAttributes[]; + for (const sizeData of sizes) { + await Size.update({ ...sizeData }, { where: { productId }, transaction }); } } - // update product - Product.update(data, { where: { id: productId } }).then(() => { - res.status(200).json({ - ok: true, - message: 'Product updated successfully', - }); + + await transaction.commit(); + res.status(200).json({ + ok: true, + message: 'Product updated successfully', }); } catch (error) { + await transaction.rollback(); sendInternalErrorResponse(res, error); } }; + // get size export const getAllSizes = async (req: Request, res: Response) => { try { diff --git a/src/helpers/token.generator.ts b/src/helpers/token.generator.ts index 368b37dd..6a51d1bc 100644 --- a/src/helpers/token.generator.ts +++ b/src/helpers/token.generator.ts @@ -4,13 +4,13 @@ import dotenv from 'dotenv'; dotenv.config(); export interface UserPayload { id: string; - email?: string; + role?: string; } // Function to generate token -export const userToken = async (userId: string, userEmail?: string) => { +export const userToken = async (userId: string, roleName?: string) => { const payload: UserPayload = { id: userId, - email: userEmail ?? undefined, + role: roleName, }; const token: string = jwt.sign(payload, process.env.SECRET_KEY as string, { expiresIn: process.env.JWT_EXPIRATION as string, diff --git a/src/middlewares/authMiddlewares.ts b/src/middlewares/authMiddlewares.ts index 80e86bb5..24b15006 100644 --- a/src/middlewares/authMiddlewares.ts +++ b/src/middlewares/authMiddlewares.ts @@ -84,7 +84,7 @@ export const verifyIfSeller = async (user: any, req: Request, res: Response) => sendOTP(req, res, user.dataValues.email); } else { // Authenticate user with jwt - const token = await userToken(user.id); + const token = await userToken(user.id, userRole?.name); res.status(200).json({ ok: true, diff --git a/src/routes/productRoutes.ts b/src/routes/productRoutes.ts index 7130d02b..7f1d9cfd 100644 --- a/src/routes/productRoutes.ts +++ b/src/routes/productRoutes.ts @@ -27,7 +27,7 @@ router.post( createProduct ); -router.put( +router.patch( '/:productId/update-product', multerUpload.array('images', 8), isAuthenticated,