Skip to content

Commit 6fc7551

Browse files
authored
Merge pull request #101 from atlp-rwanda/187991566-fix-update-product
Bug fix to fix update of the product
2 parents 8fa6eea + eeb229e commit 6fc7551

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

Diff for: src/controllers/productsController.ts

+25-33
Original file line numberDiff line numberDiff line change
@@ -80,57 +80,49 @@ export const createProduct = async (req: Request, res: Response) => {
8080
};
8181

8282
export const updateProduct = async (req: Request, res: Response) => {
83+
const transaction = await sequelize.transaction();
8384
try {
8485
const { productId } = req.params;
8586

8687
const data = {
8788
name: req.body.name,
8889
description: req.body.description,
8990
colors: req.body.colors,
91+
categoryName: req.body.categoryName,
9092
};
9193

92-
// update images
93-
if (req.files) {
94-
const product = await Product.findByPk(productId);
95-
const foundImages = product?.images;
96-
97-
// delete already existing images
98-
if (foundImages instanceof Array) {
99-
for (let i = 0; i < foundImages.length; i++) {
100-
const strImg = foundImages[i].toString();
101-
const imageId = extractImageId(strImg) as string;
102-
await destroyImage(imageId);
103-
product!.images = [];
104-
}
105-
}
94+
// find product by id
95+
const foundProduct = Product.findByPk(productId);
10696

107-
// update new images
108-
const images: unknown = req.files;
109-
const productImages = [];
110-
if (images instanceof Array && images.length > 3) {
111-
for (const image of images) {
112-
const imageBuffer: Buffer = image.buffer;
113-
const url = await uploadImage(imageBuffer);
114-
productImages.push(url);
115-
Product.update({ images: productImages }, { where: { id: productId } });
116-
}
117-
} else {
118-
return res.status(400).json({
119-
message: 'Product should have at least 4 images',
120-
});
121-
}
97+
if (!foundProduct) {
98+
return res.status(400).json({ message: 'Product not found' });
12299
}
100+
123101
// update product
124-
Product.update(data, { where: { id: productId } }).then(() => {
125-
res.status(200).json({
102+
const [affectedRows] = await Product.update(data, { where: { id: productId }, transaction });
103+
104+
// update sizes if provided
105+
if (req.body.sizes) {
106+
const sizes = req.body.sizes;
107+
for (const sizeData of sizes) {
108+
await Size.update({ ...sizeData }, { where: { productId }, transaction });
109+
}
110+
}
111+
112+
await transaction.commit();
113+
114+
if (affectedRows !== 0) {
115+
return res.status(200).json({
126116
ok: true,
127117
message: 'Product updated successfully',
128118
});
129-
});
119+
}
130120
} catch (error) {
121+
await transaction.rollback();
131122
sendInternalErrorResponse(res, error);
132123
}
133124
};
125+
134126
// get size
135127
export const getAllSizes = async (req: Request, res: Response) => {
136128
try {
@@ -362,7 +354,7 @@ export const deleteProductById = async (req: Request, res: Response) => {
362354
}
363355

364356
// deleting product related sizes
365-
const sizes = await Size.findAll({ where: { id }, transaction });
357+
await Size.findAll({ where: { id }, transaction });
366358
await Size.destroy({ where: { id }, transaction });
367359

368360
// deleting the product itself

0 commit comments

Comments
 (0)