added filter by isDeleted
This commit is contained in:
parent
5cddfb43de
commit
c4aeaa2c45
@ -1,6 +1,6 @@
|
|||||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
import { IsEnum, IsInt, IsOptional, IsString, Max, Min } from 'class-validator';
|
import { IsBoolean, IsEnum, IsInt, IsOptional, IsString, Max, MaxLength, Min } from 'class-validator';
|
||||||
|
|
||||||
export enum SortOrder {
|
export enum SortOrder {
|
||||||
ASC = 'asc',
|
ASC = 'asc',
|
||||||
@ -33,8 +33,15 @@ export class QueryFormDto {
|
|||||||
@ApiPropertyOptional({ example: '' })
|
@ApiPropertyOptional({ example: '' })
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
|
@MaxLength(100)
|
||||||
search?: string;
|
search?: string;
|
||||||
|
|
||||||
|
@ApiPropertyOptional({ example: false, description: 'true = deleted only, false = active only, omit = all' })
|
||||||
|
@Type(() => Boolean)
|
||||||
|
@IsBoolean()
|
||||||
|
@IsOptional()
|
||||||
|
isDeleted?: boolean;
|
||||||
|
|
||||||
@ApiPropertyOptional({ enum: SortBy, example: SortBy.CREATED_AT })
|
@ApiPropertyOptional({ enum: SortBy, example: SortBy.CREATED_AT })
|
||||||
@IsEnum(SortBy)
|
@IsEnum(SortBy)
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
|
|||||||
@ -1,12 +1,26 @@
|
|||||||
import { QueryFormDto, SortOrder } from '../dto/query-form.dto';
|
import { QueryFormDto, SortOrder } from '../dto/query-form.dto';
|
||||||
|
|
||||||
export class FormQueryBuilder {
|
export class FormQueryBuilder {
|
||||||
// Build filter
|
private static escapeRegex(value: string): string {
|
||||||
|
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // escapes: . * + ? ^ $ { } ( ) | [ ] \
|
||||||
|
}
|
||||||
|
|
||||||
static buildFilter(query: QueryFormDto): Record<string, unknown> {
|
static buildFilter(query: QueryFormDto): Record<string, unknown> {
|
||||||
const filter: Record<string, unknown> = {};
|
const filter: Record<string, unknown> = {};
|
||||||
|
|
||||||
|
// text serach
|
||||||
if (query.search) {
|
if (query.search) {
|
||||||
filter.name = { $regex: query.search, $options: 'i' };
|
const sanitized = this.escapeRegex(query.search.trim()); // trim + escape
|
||||||
|
filter.name = { $regex: sanitized, $options: 'i' };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// soft delete search
|
||||||
|
if (query.isDeleted === true) {
|
||||||
|
filter.deletedAt = { $ne: null };
|
||||||
|
} else if (query.isDeleted === false) {
|
||||||
|
filter.deletedAt = null;
|
||||||
|
}
|
||||||
|
|
||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user