import { ApiPropertyOptional } from '@nestjs/swagger'; import { Transform, Type } from 'class-transformer'; import { IsBoolean, IsDate, IsInt, IsNumber, IsOptional, IsString, Min } from 'class-validator'; export class FlatQueryCustomerDto { @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 Customer where id equals` }) @IsOptional() @IsInt() @Type(() => Number) id?: number; @ApiPropertyOptional({ description: `Filter Customer where email equals` }) @IsOptional() @IsString() email?: string; @ApiPropertyOptional({ description: `Filter Customer where firstName equals` }) @IsOptional() @IsString() firstName?: string; @ApiPropertyOptional({ description: `Filter Customer where lastName equals` }) @IsOptional() @IsString() lastName?: string; @ApiPropertyOptional({ description: `Filter Customer where phone equals` }) @IsOptional() @IsString() phone?: string; @ApiPropertyOptional({ description: `Filter Customer where createdAt greater than or equal` }) @IsOptional() @IsDate() @Type(() => Date) createdAt_gte?: Date; @ApiPropertyOptional({ description: `Filter Customer where createdAt less than or equal` }) @IsOptional() @IsDate() @Type(() => Date) createdAt_lte?: Date; @ApiPropertyOptional({ description: `Filter Customer where updatedAt greater than or equal` }) @IsOptional() @IsDate() @Type(() => Date) updatedAt_gte?: Date; @ApiPropertyOptional({ description: `Filter Customer 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; } }