Skip to content

Commit

Permalink
add guard when uploading a product with price as string and fix produ…
Browse files Browse the repository at this point in the history
…ct images quality
  • Loading branch information
radekm2000 committed Apr 10, 2024
1 parent 4ff4d8a commit fb7a039
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
5 changes: 4 additions & 1 deletion server/ecommerce/src/products/products.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { FileInterceptor } from '@nestjs/platform-express';
import {
ProductWithImageAndUser,
ProductWithImageAndUserSchema,
ProductWithoutImageDto,
} from 'src/utils/dtos/product.dto';
import { AuthGuard } from 'src/auth/auth.guard';
import { AuthUser } from 'src/decorators/user.decorator';
Expand All @@ -27,6 +28,7 @@ import { QueryParams } from 'src/utils/dtos/types';
import { ZodValidationPipe } from 'src/utils/pipes/ZodValidationPipe';
import { Response } from 'express';
import { StripeService } from 'src/stripe/stripe.service';
import { ParseAndValidateProductPipe } from 'src/utils/pipes/ParseAndValidateProductPipe';

@Controller('products')
export class ProductsController {
Expand Down Expand Up @@ -109,10 +111,11 @@ export class ProductsController {
@UseGuards(AuthGuard)
@UseInterceptors(FileInterceptor('file'))
async uploadProduct(
@Body() body: any,
@Body(new ParseAndValidateProductPipe()) body: ProductWithoutImageDto,
@UploadedFile() file: Express.Multer.File,
@AuthUser() authUser: AuthUser,
) {
console.log(body);
return await this.productsService.uploadProduct(body, file, authUser.sub);
}
}
16 changes: 11 additions & 5 deletions server/ecommerce/src/products/products.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { Brand, Order, QueryParams } from 'src/utils/dtos/types';
import { ProductNotificationService } from 'src/product-notification/product-notification.service';
import { Image } from 'src/utils/entities/image.entity';
import { createProductFromJson } from 'src/utils/dtos/product.dto';
import {
ProductWithoutImageDto,
createProductFromJson,
} from 'src/utils/dtos/product.dto';
import * as sharp from 'sharp';
import { randomUUID } from 'crypto';
import { UsersService } from 'src/users/users.service';
Expand Down Expand Up @@ -286,13 +289,16 @@ export class ProductsService {
return;
}

async uploadProduct(body: any, file: Express.Multer.File, userId: number) {
const productBody = createProductFromJson(body.data);
async uploadProduct(
productBody: ProductWithoutImageDto,
file: Express.Multer.File,
userId: number,
) {
const buffer = await sharp(file.buffer)
.resize({
height: 500,
height: 300,
width: 500,
fit: 'contain',
fit: 'cover',
})
.toBuffer();
const productImageName = `${randomUUID()}${file.originalname}`;
Expand Down
7 changes: 6 additions & 1 deletion server/ecommerce/src/utils/dtos/product.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ export const ProductWithoutImageDtoSchema = z.object({
description: z.string(),
brand: z.string(),
category: z.string(),
price: z.string().transform((value) => parseInt(value)),
price: z
.string()
.transform((value) => parseInt(value))
.refine((value) => !isNaN(value), {
message: 'Price must be a number',
}),
});

export type ProductWithoutImageDto = z.infer<
Expand Down
14 changes: 14 additions & 0 deletions server/ecommerce/src/utils/pipes/ParseAndValidateProductPipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable, PipeTransform } from '@nestjs/common';
import {
ProductWithoutImageDto,
ProductWithoutImageDtoSchema,
} from '../dtos/product.dto';

@Injectable()
export class ParseAndValidateProductPipe implements PipeTransform {
transform(value: any): ProductWithoutImageDto {
const parsedContent = JSON.parse(value.data);

return ProductWithoutImageDtoSchema.parse(parsedContent);
}
}

0 comments on commit fb7a039

Please sign in to comment.