import { ApiPropertyOptional } from '@nestjs/swagger'; import { Transform, Type } from 'class-transformer'; import { IsBoolean, IsDate, IsInt, IsNumber, IsOptional, IsString, Min } from 'class-validator'; export class FlatQueryCategoryDto { @ApiPropertyOptional({ description: 'Number of records to take', default: 10 }) @IsOptional() @IsInt() @Min(1) @Type(() => Number) take = 10; @ApiPropertyOptional({ description: 'Number of records to skip', default: 0 }) @IsOptional() @IsInt() @Min(0) @Type(() => Number) skip = 0; @ApiPropertyOptional({ description: `Filter Category where id equals` }) @IsOptional() @IsInt() @Type(() => Number) id?: number; @ApiPropertyOptional({ description: `Filter Category where name equals` }) @IsOptional() @IsString() name?: string; @ApiPropertyOptional({ description: `Filter Category where description equals` }) @IsOptional() @IsString() description?: string; @ApiPropertyOptional({ description: `Filter Category where parentId equals` }) @IsOptional() @IsInt() @Type(() => Number) parentId?: number; @ApiPropertyOptional({ description: `Filter Category where createdAt greater than or equal` }) @IsOptional() @IsDate() @Type(() => Date) createdAt_gte?: Date; @ApiPropertyOptional({ description: `Filter Category where createdAt less than or equal` }) @IsOptional() @IsDate() @Type(() => Date) createdAt_lte?: Date; @ApiPropertyOptional({ description: `Filter Category where updatedAt greater than or equal` }) @IsOptional() @IsDate() @Type(() => Date) updatedAt_gte?: Date; @ApiPropertyOptional({ description: `Filter Category where updatedAt less than or equal` }) @IsOptional() @IsDate() @Type(() => Date) updatedAt_lte?: Date; /** * Transform flat query parameters to Prisma query structure */ toPrismaQuery() { const result: any = { take: this.take, skip: this.skip, where: {} }; // Process all filter parameters for (const [key, value] of Object.entries(this)) { if (key === 'take' || key === 'skip' || value === undefined) { continue; } const parts = key.split('_'); if (parts.length === 2) { // Handle field_operator pattern (e.g. firstName_contains) const [fieldName, operator] = parts; // Initialize the field object if it doesn't exist if (!result.where[fieldName]) { result.where[fieldName] = {}; } // Set the operator result.where[fieldName][operator] = value; } else if (parts.length === 1) { // Handle direct field name (equals operator) const fieldName = key; // Initialize the field object if it doesn't exist if (!result.where[fieldName]) { result.where[fieldName] = {}; } // Use equals operator result.where[fieldName]['equals'] = value; } } return result; } }