added filter by isDeleted
This commit is contained in:
parent
5cddfb43de
commit
c4aeaa2c45
@ -1,6 +1,6 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
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 {
|
||||
ASC = 'asc',
|
||||
@ -33,8 +33,15 @@ export class QueryFormDto {
|
||||
@ApiPropertyOptional({ example: '' })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@MaxLength(100)
|
||||
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 })
|
||||
@IsEnum(SortBy)
|
||||
@IsOptional()
|
||||
|
||||
@ -1,12 +1,26 @@
|
||||
import { QueryFormDto, SortOrder } from '../dto/query-form.dto';
|
||||
|
||||
export class FormQueryBuilder {
|
||||
// Build filter
|
||||
private static escapeRegex(value: string): string {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // escapes: . * + ? ^ $ { } ( ) | [ ] \
|
||||
}
|
||||
|
||||
static buildFilter(query: QueryFormDto): Record<string, unknown> {
|
||||
const filter: Record<string, unknown> = {};
|
||||
|
||||
// text serach
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user