Skip to content

Commit

Permalink
feature:EF-187 filter product
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhhTien authored and nghiavohuynhdai committed Mar 9, 2024
1 parent 29e7207 commit 084d75d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 23 deletions.
18 changes: 12 additions & 6 deletions src/order/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export class OrderService {
const result = await this.orderRepository.paginate(
{
...filter,
transactionStatus: {
$ne: TransactionStatus.DRAFT
},
status: {
$ne: OrderStatus.DELETED
}
Expand All @@ -49,6 +52,9 @@ export class OrderService {
const order = await this.orderRepository.findOne({
conditions: {
...filter,
transactionStatus: {
$ne: TransactionStatus.DRAFT
},
status: {
$ne: OrderStatus.DELETED
}
Expand Down Expand Up @@ -185,8 +191,8 @@ export class OrderService {
const order = await this.orderRepository.findOneAndUpdate(
{
_id: orderId,
orderStatus: OrderStatus.PENDING
// transactionStatus: TransactionStatus.CAPTURED
orderStatus: OrderStatus.PENDING,
transactionStatus: TransactionStatus.CAPTURED
},
{
$set: { orderStatus: OrderStatus.CONFIRMED },
Expand All @@ -205,8 +211,8 @@ export class OrderService {
const order = await this.orderRepository.findOneAndUpdate(
{
_id: orderId,
orderStatus: OrderStatus.CONFIRMED
// transactionStatus: TransactionStatus.CAPTURED
orderStatus: OrderStatus.CONFIRMED,
transactionStatus: TransactionStatus.CAPTURED
},
{
$set: { orderStatus: OrderStatus.DELIVERING, deliveryDate: new Date() },
Expand All @@ -231,8 +237,8 @@ export class OrderService {
const order = await this.orderRepository.findOneAndUpdate(
{
_id: orderId,
orderStatus: OrderStatus.PENDING
// transactionStatus: TransactionStatus.CAPTURED
orderStatus: OrderStatus.PENDING,
transactionStatus: TransactionStatus.CAPTURED
},
{
$set: { orderStatus: OrderStatus.CANCELED, transactionStatus: TransactionStatus.CANCELED, reason },
Expand Down
24 changes: 20 additions & 4 deletions src/product/controllers/customer.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Controller, Get, Param, UseInterceptors } from '@nestjs/common'
import { Controller, Get, Param, Query, UseInterceptors } from '@nestjs/common'
import { ApiOkResponse, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger'
import { ProductService } from '@product/services/product.service'
import { PaginationQuery } from '@src/common/contracts/dto'
import { DataResponse } from '@src/common/contracts/openapi-builder'
import { Pagination, PaginationParams } from '@src/common/decorators/pagination.decorator'
import { ProductDetailDto, PublicProductPaginateDto } from '@product/dto/product.dto'
import { FilterProductDto, ProductDetailDto, PublicProductPaginateDto } from '@product/dto/product.dto'

@ApiTags('Product - Public')
@Controller('public')
Expand All @@ -14,8 +14,24 @@ export class PublicProductController {
@Get()
@ApiOkResponse({ type: PublicProductPaginateDto })
@ApiQuery({ type: PaginationQuery })
getAllProducts(@Pagination() paginationParams: PaginationParams) {
return this.productService.getAllPublicProducts(paginationParams)
getAllProducts(@Pagination() paginationParams: PaginationParams, @Query() filterProductDto: FilterProductDto) {
const condition = {}
if (filterProductDto.categories) {
condition['categories'] = {
$in: filterProductDto.categories
}
}
if (filterProductDto.name) {
condition['$text'] = {
$search: filterProductDto.name
}
}
if (filterProductDto.fromPrice && filterProductDto.toPrice) {
condition['variants'] = {
$elemMatch: { price: { $gte: filterProductDto.fromPrice, $lte: filterProductDto.toPrice } }
}
}
return this.productService.getAllPublicProducts(condition, paginationParams)
}

@Get(':id')
Expand Down
24 changes: 20 additions & 4 deletions src/product/controllers/provider.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Delete, Get, Param, Post, Put, UseGuards } from '@nestjs/common'
import { Body, Controller, Delete, Get, Param, Post, Put, Query, UseGuards } from '@nestjs/common'
import {
ApiBearerAuth,
ApiCreatedResponse,
Expand All @@ -12,7 +12,7 @@ import { ProductService } from '@product/services/product.service'
import { Product } from '@product/schemas/product.schema'
import { Pagination, PaginationParams } from '@common/decorators/pagination.decorator'
import { ErrorResponse, PaginationQuery, SuccessDataResponse } from '@common/contracts/dto'
import { CreateProductDto, ProductDetailDto, ProductPaginateDto, UpdateProductDto } from '@product/dto/product.dto'
import { CreateProductDto, FilterProductDto, ProductDetailDto, ProductPaginateDto, UpdateProductDto } from '@product/dto/product.dto'
import { Roles } from '@auth/decorators/roles.decorator'
import { JwtAuthGuard } from '@auth/guards/jwt-auth.guard'
import { RolesGuard } from '@auth/guards/roles.guard'
Expand All @@ -31,8 +31,24 @@ export class ProviderProductController {
@Get()
@ApiOkResponse({ type: ProductPaginateDto })
@ApiQuery({ type: PaginationQuery })
getAllProducts(@Pagination() paginationParams: PaginationParams) {
return this.productService.getAllProducts(paginationParams)
getAllProducts(@Pagination() paginationParams: PaginationParams, @Query() filterProductDto: FilterProductDto) {
const condition = {}
if (filterProductDto.categories) {
condition['categories'] = {
$in: filterProductDto.categories
}
}
if (filterProductDto.name) {
condition['$text'] = {
$search: filterProductDto.name
}
}
if (filterProductDto.fromPrice && filterProductDto.toPrice) {
condition['variants'] = {
$elemMatch: { price: { $gte: filterProductDto.fromPrice, $lte: filterProductDto.toPrice } }
}
}
return this.productService.getAllProducts(condition, paginationParams)
}

@Post()
Expand Down
40 changes: 35 additions & 5 deletions src/product/dto/product.dto.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { ApiProperty } from '@nestjs/swagger'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { Product, Variant } from '@product/schemas/product.schema'
import { Category } from '@src/category/schemas/category.schema'
import { DataResponse, PaginateResponse } from '@src/common/contracts/openapi-builder'
import { Type } from 'class-transformer'
import { Transform, Type } from 'class-transformer'
import {
ArrayMaxSize,
ArrayMinSize,
IsArray,
IsInt,
IsNotEmpty,
IsOptional,
IsUrl,
Max,
MaxLength,
Min,
ValidateNested
} from 'class-validator'
import { Types } from 'mongoose'
Expand Down Expand Up @@ -44,9 +48,7 @@ export class PublicProductPaginateDto extends DataResponse(
class PublicProductPaginate extends PaginateResponse(PublicProductDto) {}
) {}

export class ProductPaginateDto extends DataResponse(
class ProductPaginate extends PaginateResponse(Product) {}
) {}
export class ProductPaginateDto extends DataResponse(class ProductPaginate extends PaginateResponse(Product) {}) {}

export class CreateProductDto {
@ApiProperty({
Expand Down Expand Up @@ -162,3 +164,31 @@ export class ProductDetailDto {
@ApiProperty({ type: Category, isArray: true })
categories: Category[]
}

export class FilterProductDto {
@ApiPropertyOptional({ type: [String] })
@IsOptional()
@Transform(({ value }) => (Array.isArray(value) ? value : Array(value)))
@IsArray()
categories: string[]

@ApiPropertyOptional()
@IsOptional()
name: string

@ApiPropertyOptional()
@IsOptional()
@Type(() => Number)
@IsInt()
@Min(0)
@Max(999_999_999)
fromPrice: number

@ApiPropertyOptional()
@IsOptional()
@Type(() => Number)
@IsInt()
@Min(0)
@Max(999_999_999)
toPrice: number
}
3 changes: 3 additions & 0 deletions src/product/schemas/product.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,8 @@ ProductSchema.index(
}
}
)
ProductSchema.index(
{ 'name': 'text' },
)
ProductSchema.plugin(paginate)
ProductSchema.plugin(slug)
10 changes: 6 additions & 4 deletions src/product/services/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ export class ProductService {
}
}

public async getAllProducts(paginationParams: PaginationParams) {
public async getAllProducts(filter: FilterQuery<Product>, paginationParams: PaginationParams) {
return await this.productRepository.paginate(
{
status: {
$ne: ProductStatus.DELETED
}
},
...filter
},
{
...paginationParams,
Expand All @@ -43,12 +44,13 @@ export class ProductService {
)
}

public async getAllPublicProducts(paginationParams: PaginationParams) {
public async getAllPublicProducts(filter: FilterQuery<Product>, paginationParams: PaginationParams) {
const result = await this.productRepository.paginate(
{
status: {
$in: [ProductStatus.ACTIVE]
}
},
...filter
},
{
...paginationParams,
Expand Down

0 comments on commit 084d75d

Please sign in to comment.