import { ApiPropertyOptional } from '@nestjs/swagger'; import { Transform, Type } from 'class-transformer'; import { IsBoolean, IsDate, IsInt, IsNumber, IsOptional, IsString, Min } from 'class-validator'; export class FlatQueryOrderItemDto { @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 OrderItem where id equals` }) @IsOptional() @IsInt() @Type(() => Number) id?: number; @ApiPropertyOptional({ description: `Filter OrderItem where quantity equals` }) @IsOptional() @IsInt() @Type(() => Number) quantity?: number; @ApiPropertyOptional({ description: `Filter OrderItem where quantity greater than or equal` }) @IsOptional() @IsInt() @Type(() => Number) quantity_gte?: number; @ApiPropertyOptional({ description: `Filter OrderItem where quantity less than or equal` }) @IsOptional() @IsInt() @Type(() => Number) quantity_lte?: number; @ApiPropertyOptional({ description: `Filter OrderItem where price equals` }) @IsOptional() @IsNumber() @Type(() => Number) price?: number; @ApiPropertyOptional({ description: `Filter OrderItem where price greater than or equal` }) @IsOptional() @IsNumber() @Type(() => Number) price_gte?: number; @ApiPropertyOptional({ description: `Filter OrderItem where price less than or equal` }) @IsOptional() @IsNumber() @Type(() => Number) price_lte?: number; @ApiPropertyOptional({ description: `Filter OrderItem where orderId equals` }) @IsOptional() @IsInt() @Type(() => Number) orderId?: number; @ApiPropertyOptional({ description: `Filter OrderItem where productId equals` }) @IsOptional() @IsInt() @Type(() => Number) productId?: number; @ApiPropertyOptional({ description: `Filter OrderItem where createdAt greater than or equal` }) @IsOptional() @IsDate() @Type(() => Date) createdAt_gte?: Date; @ApiPropertyOptional({ description: `Filter OrderItem where createdAt less than or equal` }) @IsOptional() @IsDate() @Type(() => Date) createdAt_lte?: Date; @ApiPropertyOptional({ description: `Filter OrderItem where updatedAt greater than or equal` }) @IsOptional() @IsDate() @Type(() => Date) updatedAt_gte?: Date; @ApiPropertyOptional({ description: `Filter OrderItem 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; } }