Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "api",
"version": "1.0.1",
"version": "1.1.0",
"description": "",
"author": "",
"private": true,
Expand Down
11 changes: 11 additions & 0 deletions src/products/dto/product.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -152,6 +161,7 @@ export class ProductQueryDTO extends PaginationQueryDTO {
genericProductId?: string[],
priceRange?: number[],
isVisible?: boolean,
withPromo?: boolean,
id?: string[],
) {
super(page, limit);
Expand All @@ -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 : [];
}
}
4 changes: 3 additions & 1 deletion src/products/products.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export class ProductsController {
priceRange,
isVisible,
id,
withPromo,
} = pagination;
const { products, total } =
await this.productsServices.getProductQueryBuilder(
Expand All @@ -164,6 +165,7 @@ export class ProductsController {
priceRange,
isVisible,
id,
withPromo,
);

return {
Expand All @@ -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,
Expand Down
24 changes: 21 additions & 3 deletions src/products/products.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class ProductsService {
priceRange?: number[],
isVisible?: boolean,
ids?: string[],
withPromo?: boolean,
) {
const query = this.productPresentationRepository.createQueryBuilder(
'product_presentation',
Expand All @@ -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(
Expand All @@ -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 });
}
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/sales/sales.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
};
});
Expand Down
Loading