From 6f0a6af7a4fde5229df498edeca1efda3422ecac Mon Sep 17 00:00:00 2001 From: Sasha Date: Sun, 5 Jan 2025 21:52:35 +0100 Subject: [PATCH] fix: better violation names on statistics page (BAL-3294) (#2932) * feat: better violation names on statistics page * fix: CodeRabbit comments --------- Co-authored-by: Alon Peretz <8467965+alonp99@users.noreply.github.com> --- .../useBusinessReportMetricsQuery.ts | 8 ++++- .../PortfolioRiskStatistics.tsx | 4 +-- .../usePortfolioRiskStatisticsLogic.tsx | 5 ++-- .../dtos/business-report-metrics-dto.ts | 30 ++++++++++++++----- .../merchant-monitoring-client.ts | 8 ++++- 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/apps/backoffice-v2/src/domains/business-reports/hooks/queries/useBusinessReportMetricsQuery/useBusinessReportMetricsQuery.ts b/apps/backoffice-v2/src/domains/business-reports/hooks/queries/useBusinessReportMetricsQuery/useBusinessReportMetricsQuery.ts index f22904ae54..c9c6de69e6 100644 --- a/apps/backoffice-v2/src/domains/business-reports/hooks/queries/useBusinessReportMetricsQuery/useBusinessReportMetricsQuery.ts +++ b/apps/backoffice-v2/src/domains/business-reports/hooks/queries/useBusinessReportMetricsQuery/useBusinessReportMetricsQuery.ts @@ -12,7 +12,13 @@ export const MetricsResponseSchema = z.object({ high: z.number(), critical: z.number(), }), - violationCounts: z.record(z.string(), z.number()), + violationCounts: z.array( + z.object({ + name: z.string(), + id: z.string(), + count: z.number(), + }), + ), }); export const fetchBusinessReportMetrics = async () => { diff --git a/apps/backoffice-v2/src/pages/Statistics/components/PortfolioRiskStatistics/PortfolioRiskStatistics.tsx b/apps/backoffice-v2/src/pages/Statistics/components/PortfolioRiskStatistics/PortfolioRiskStatistics.tsx index 5185f5ec40..8248120f68 100644 --- a/apps/backoffice-v2/src/pages/Statistics/components/PortfolioRiskStatistics/PortfolioRiskStatistics.tsx +++ b/apps/backoffice-v2/src/pages/Statistics/components/PortfolioRiskStatistics/PortfolioRiskStatistics.tsx @@ -172,11 +172,11 @@ export const PortfolioRiskStatistics: FunctionComponent - {filteredRiskIndicators.map(({ name, count }, index) => ( + {filteredRiskIndicators.map(({ name, count, id }, index) => ( diff --git a/apps/backoffice-v2/src/pages/Statistics/components/PortfolioRiskStatistics/hooks/usePortfolioRiskStatisticsLogic/usePortfolioRiskStatisticsLogic.tsx b/apps/backoffice-v2/src/pages/Statistics/components/PortfolioRiskStatistics/hooks/usePortfolioRiskStatisticsLogic/usePortfolioRiskStatisticsLogic.tsx index f9effccab4..317377c19d 100644 --- a/apps/backoffice-v2/src/pages/Statistics/components/PortfolioRiskStatistics/hooks/usePortfolioRiskStatisticsLogic/usePortfolioRiskStatisticsLogic.tsx +++ b/apps/backoffice-v2/src/pages/Statistics/components/PortfolioRiskStatistics/hooks/usePortfolioRiskStatisticsLogic/usePortfolioRiskStatisticsLogic.tsx @@ -20,11 +20,10 @@ export const usePortfolioRiskStatisticsLogic = ({ }, [], ); - const totalRiskIndicators = Object.values(violationCounts).reduce((acc, curr) => acc + curr, 0); + const totalRiskIndicators = violationCounts.reduce((acc, { count }) => acc + count, 0); const filteredRiskIndicators = useMemo( () => - Object.entries(violationCounts) - .map(([name, count]) => ({ name, count })) + violationCounts .sort((a, b) => (riskIndicatorsSorting === 'asc' ? a.count - b.count : b.count - a.count)) .slice(0, 5), [violationCounts, riskIndicatorsSorting], diff --git a/services/workflows-service/src/business-report/dtos/business-report-metrics-dto.ts b/services/workflows-service/src/business-report/dtos/business-report-metrics-dto.ts index ab75feabd6..9b3798de43 100644 --- a/services/workflows-service/src/business-report/dtos/business-report-metrics-dto.ts +++ b/services/workflows-service/src/business-report/dtos/business-report-metrics-dto.ts @@ -1,5 +1,5 @@ -import { IsNumber, IsObject, ValidateNested } from 'class-validator'; import { Type } from 'class-transformer'; +import { IsArray, IsNumber, IsString, ValidateNested } from 'class-validator'; import { ApiProperty } from '@nestjs/swagger'; @@ -47,12 +47,26 @@ export class BusinessReportMetricsDto { riskLevelCounts!: RiskLevelCountsDto; @ApiProperty({ - description: 'Counts of violations by type', - example: { PROHIBITED_CONTENT: 2, MISSING_INFORMATION: 1 }, - type: 'object', - additionalProperties: { type: 'number' }, + description: 'Detected violations counts', + example: [{ id: 'PROHIBITED_CONTENT', name: 'Prohibited content', count: 2 }], + type: 'array', }) - @IsObject() - @Type(() => Object) - violationCounts!: Record; + @IsArray() + @ValidateNested({ each: true }) + @Type(() => ViolationCountDto) + violationCounts!: ViolationCountDto[]; +} + +export class ViolationCountDto { + @ApiProperty() + @IsString() + id!: string; + + @ApiProperty() + @IsString() + name!: string; + + @ApiProperty() + @IsNumber() + count!: number; } diff --git a/services/workflows-service/src/business-report/merchant-monitoring-client.ts b/services/workflows-service/src/business-report/merchant-monitoring-client.ts index fc7afa33e6..cc72de82b0 100644 --- a/services/workflows-service/src/business-report/merchant-monitoring-client.ts +++ b/services/workflows-service/src/business-report/merchant-monitoring-client.ts @@ -64,7 +64,13 @@ const MetricsResponseSchema = z.object({ high: z.number(), critical: z.number(), }), - violationCounts: z.record(z.string(), z.number()), + violationCounts: z.array( + z.object({ + name: z.string(), + id: z.string(), + count: z.number(), + }), + ), }); @Injectable()