From 908b63d09d886dad807a984a109a16b4767df477 Mon Sep 17 00:00:00 2001 From: "Rahul R." Date: Mon, 14 Oct 2024 17:43:28 +0530 Subject: [PATCH] fix: apply filters on logging API --- .../src/api-call-log/api-call-log.service.ts | 39 +++++++++---------- .../dto/api-call-log-filter.dto.ts | 13 ++++--- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/packages/core/src/api-call-log/api-call-log.service.ts b/packages/core/src/api-call-log/api-call-log.service.ts index 247f1314a78..7526e32cf1e 100644 --- a/packages/core/src/api-call-log/api-call-log.service.ts +++ b/packages/core/src/api-call-log/api-call-log.service.ts @@ -27,11 +27,20 @@ export class ApiCallLogService extends TenantAwareCrudService { // Ensure that filters are properly defined const queryOptions: FindManyOptions = { where: {}, - ...filters + take: filters.take ?? 100, // Default to 100 if not provided + skip: filters.skip ? filters.take * (filters.skip - 1) : 0 // Calculate offset }; + // Apply sorting options (if provided) + if (filters.order) { + queryOptions.order = filters.order; // Order, in which entities should be ordered. Default to ASC if no order is provided. + } + // Check if `filters.where` is an array or an object, then apply individual filters if (!Array.isArray(filters)) { + if (filters.organizationId) { + queryOptions.where['organizationId'] = filters.organizationId; + } if (filters.correlationId) { queryOptions.where['correlationId'] = filters.correlationId; } @@ -41,41 +50,29 @@ export class ApiCallLogService extends TenantAwareCrudService { if (filters.ipAddress) { queryOptions.where['ipAddress'] = filters.ipAddress; } + if (filters.method) { + queryOptions.where['method'] = filters.method; + } if (filters.userId) { queryOptions.where['userId'] = filters.userId; } - // Apply date range filters for requestTime if (filters.startRequestTime || filters.endRequestTime) { // The start date for filtering, defaults to the start of today. - const startRequestTime = filters.startRequestTime + const start = filters.startRequestTime ? moment(filters.startRequestTime).toDate() : moment().startOf('day').toDate(); // The end date for filtering, defaults to the end of today. - const endRequestTime = filters.endRequestTime + const end = filters.endRequestTime ? moment(filters.endRequestTime).toDate() - : moment().endOf('day').toDate(); + : moment().endOf('day').toDate(); // Default to end of today if no end date is provided - // Retrieves a date range filter using the `startRequestTime` and `endRequestTime` values. - queryOptions.where['requestTime'] = Between( - startRequestTime, // Default to start of today if no start date is provided - endRequestTime // Default to end of today if no end date is provided - ); + // Retrieves a date range filter using the `start` and `end` values. + queryOptions.where['requestTime'] = Between(start, end); } } - // Apply pagination and sorting options (if provided) - if (filters.take) { - queryOptions.take = filters.take; - } - if (filters.skip) { - queryOptions.skip = filters.skip; - } - if (filters.order) { - queryOptions.order = filters.order; - } - // Perform the query with filters, sorting, and pagination applied return await super.findAll(queryOptions); } diff --git a/packages/core/src/api-call-log/dto/api-call-log-filter.dto.ts b/packages/core/src/api-call-log/dto/api-call-log-filter.dto.ts index cbaf00d9bd9..a6112828bd5 100644 --- a/packages/core/src/api-call-log/dto/api-call-log-filter.dto.ts +++ b/packages/core/src/api-call-log/dto/api-call-log-filter.dto.ts @@ -1,5 +1,6 @@ import { ApiPropertyOptional, IntersectionType, PickType } from '@nestjs/swagger'; import { IsDateString, IsNumber, IsOptional, IsUUID } from 'class-validator'; +import { ID } from '@gauzy/contracts'; import { PaginationParams } from '../../core/crud/pagination-params'; import { TenantOrganizationBaseDTO } from '../../core/dto/tenant-organization-base.dto'; import { ApiCallLog } from '../api-call-log.entity'; @@ -10,27 +11,27 @@ import { ApiCallLog } from '../api-call-log.entity'; export class ApiCallLogFilterDTO extends IntersectionType( TenantOrganizationBaseDTO, PickType(PaginationParams, ['skip', 'take', 'order']), - PickType(ApiCallLog, ['userId', 'ipAddress'] as const) + PickType(ApiCallLog, ['userId', 'ipAddress', 'method'] as const) ) { // Correlation ID to filter the request against. - @ApiPropertyOptional() + @ApiPropertyOptional({ type: () => String }) @IsOptional() @IsUUID() - correlationId?: string; + correlationId?: ID; // Status Code to filter the request against. - @ApiPropertyOptional() + @ApiPropertyOptional({ type: () => Number }) @IsOptional() @IsNumber() statusCode?: number; // Date range filters for requestTime and responseTime - @ApiPropertyOptional() + @ApiPropertyOptional({ type: () => Date }) @IsOptional() @IsDateString() startRequestTime?: Date; - @ApiPropertyOptional() + @ApiPropertyOptional({ type: () => Date }) @IsOptional() @IsDateString() endRequestTime?: Date;