diff --git a/package.json b/package.json index f9de463..979c8c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "api", - "version": "1.0.1", + "version": "1.1.0", "description": "", "author": "", "private": true, diff --git a/src/products/dto/product.dto.ts b/src/products/dto/product.dto.ts index deb1532..090ed43 100644 --- a/src/products/dto/product.dto.ts +++ b/src/products/dto/product.dto.ts @@ -141,6 +141,15 @@ export class ProductQueryDTO extends PaginationQueryDTO { @IsBoolean() isVisible?: boolean; + @IsOptional() + @Transform(({ value }) => { + if (value === 'true') return true; + if (value === 'false') return false; + return undefined; + }) + @IsBoolean() + withPromo?: boolean; + constructor( page: number, limit: number, @@ -152,6 +161,7 @@ export class ProductQueryDTO extends PaginationQueryDTO { genericProductId?: string[], priceRange?: number[], isVisible?: boolean, + withPromo?: boolean, id?: string[], ) { super(page, limit); @@ -163,6 +173,7 @@ export class ProductQueryDTO extends PaginationQueryDTO { this.genericProductId = genericProductId ? genericProductId : []; this.priceRange = priceRange ? priceRange : []; this.isVisible = isVisible; + this.withPromo = withPromo; this.id = id ? id : []; } } diff --git a/src/products/products.controller.ts b/src/products/products.controller.ts index c9d377a..bcf98b7 100644 --- a/src/products/products.controller.ts +++ b/src/products/products.controller.ts @@ -150,6 +150,7 @@ export class ProductsController { priceRange, isVisible, id, + withPromo, } = pagination; const { products, total } = await this.productsServices.getProductQueryBuilder( @@ -164,6 +165,7 @@ export class ProductsController { priceRange, isVisible, id, + withPromo, ); return { @@ -181,7 +183,7 @@ export class ProductsController { async getProductRecommendations(@Req() req: CustomRequest) { const userId = req.user.id; const recommendations = await this.recommendationService.recommend(userId); - const products = await this.productsServices.getProducts( + const products = await this.productsServices.getProductQueryBuilder( 1, 10, undefined, diff --git a/src/products/products.service.ts b/src/products/products.service.ts index 35aac10..bcd7ab4 100644 --- a/src/products/products.service.ts +++ b/src/products/products.service.ts @@ -22,6 +22,7 @@ export class ProductsService { priceRange?: number[], isVisible?: boolean, ids?: string[], + withPromo?: boolean, ) { const query = this.productPresentationRepository.createQueryBuilder( 'product_presentation', @@ -31,8 +32,13 @@ export class ProductsService { .innerJoinAndSelect('product.images', 'images') .innerJoinAndSelect('product.manufacturer', 'manufacturer') .innerJoinAndSelect('product.categories', 'categories') - .innerJoinAndSelect('product_presentation.presentation', 'presentation') - .leftJoinAndSelect('product_presentation.promo', 'promo'); + .innerJoinAndSelect('product_presentation.presentation', 'presentation'); + + if (withPromo) { + query.innerJoinAndSelect('product_presentation.promo', 'promo'); + } else { + query.leftJoinAndSelect('product_presentation.promo', 'promo'); + } if (searchQuery) { query.where( @@ -49,6 +55,17 @@ export class ProductsService { }), ); } + + if (withPromo) { + query + .andWhere('promo.startAt <= :currentDate', { + currentDate: new Date(), + }) + .andWhere('promo.expiredAt >= :currentDate', { + currentDate: new Date(), + }); + } + if (ids && ids.length > 0) { query.andWhere('product_presentation.id IN (:...ids)', { ids }); } @@ -97,7 +114,8 @@ export class ProductsService { query.andWhere('product_presentation.isVisible = true'); } query - .orderBy('product_presentation.createdAt', 'DESC') + .orderBy('product.priority', 'ASC') + .addOrderBy('product_presentation.id', 'ASC') .skip((page - 1) * limit) .take(limit); const [products, total] = await query.getManyAndCount(); diff --git a/src/sales/sales.service.ts b/src/sales/sales.service.ts index cd48143..7c9ee9d 100644 --- a/src/sales/sales.service.ts +++ b/src/sales/sales.service.ts @@ -94,8 +94,9 @@ export class SalesService { return { date: formatted, - predictedTotal: parseInt( - (predictedValues as number[][])[index][0].toFixed(2), + predictedTotal: Math.max( + parseInt((predictedValues as number[][])[index][0].toFixed(2)), + 0, ), }; });