Skip to content

Commit

Permalink
Merge pull request #101 from atlp-rwanda/187991566-fix-update-product
Browse files Browse the repository at this point in the history
Bug fix to fix update of the product
  • Loading branch information
niyontwali authored Jul 24, 2024
2 parents 8fa6eea + eeb229e commit 6fc7551
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions src/controllers/productsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,57 +80,49 @@ 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;

const data = {
name: req.body.name,
description: req.body.description,
colors: req.body.colors,
categoryName: req.body.categoryName,
};

// 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 = [];
}
}
// find product by id
const foundProduct = Product.findByPk(productId);

// 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',
});
}
if (!foundProduct) {
return res.status(400).json({ message: 'Product not found' });
}

// update product
Product.update(data, { where: { id: productId } }).then(() => {
res.status(200).json({
const [affectedRows] = await Product.update(data, { where: { id: productId }, transaction });

// update sizes if provided
if (req.body.sizes) {
const sizes = req.body.sizes;
for (const sizeData of sizes) {
await Size.update({ ...sizeData }, { where: { productId }, transaction });
}
}

await transaction.commit();

if (affectedRows !== 0) {
return 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 {
Expand Down Expand Up @@ -362,7 +354,7 @@ export const deleteProductById = async (req: Request, res: Response) => {
}

// deleting product related sizes
const sizes = await Size.findAll({ where: { id }, transaction });
await Size.findAll({ where: { id }, transaction });
await Size.destroy({ where: { id }, transaction });

// deleting the product itself
Expand Down

0 comments on commit 6fc7551

Please sign in to comment.