Skip to content

Commit

Permalink
Bug fix to fix update of the product
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhag committed Jul 22, 2024
1 parent 72f7b68 commit dc2ce4e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 39 deletions.
49 changes: 15 additions & 34 deletions src/controllers/productsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/token.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/authMiddlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/productRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ router.post(
createProduct
);

router.put(
router.patch(
'/:productId/update-product',
multerUpload.array('images', 8),
isAuthenticated,
Expand Down

0 comments on commit dc2ce4e

Please sign in to comment.