Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 8x 8x 8x 8x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 3x 3x 3x 2x 2x 3x 3x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { ApiProperty } from '@nestjs/swagger';
import {
isArray,
isBoolean,
isEnum,
IsEnum,
isIn,
IsString,
isString,
Validate,
ValidationArguments,
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator';
import { match } from 'ts-pattern';
import { CustomReportAggregationOperation } from '@amalia/reporting/custom-reports/shared';
import {
ChartColor,
ChartDisplaySettingsCountIdentifier,
ChartDisplaySettingsYAxisIdentifier,
ChartSeriesType,
ChartType,
DashboardChartConfiguration,
DistributionChartCountType,
KPIChartDisplayMode,
TextChartTextAlignment,
TextChartTextSize,
type ChartDisplaySettings,
type ChartDisplaySettingsDistributionSegment,
type DashboardChart,
} from '@amalia/reporting/dashboards-v2/types';
// Class validator cannot perform an "OR" validation.
// https://github.com/typestack/class-validator/issues/160
@ValidatorConstraint({ name: 'DisplaySettingsValidation', async: false })
class DisplaySettingsValidation implements ValidatorConstraintInterface {
public validate(_text: string, args: ValidationArguments) {
const config = args.object as DashboardChartDto<ChartType>;
return match(config.type)
.with(ChartType.COMBINED_CHART, () => {
const combinedChartConfig = config as unknown as DashboardChart<ChartType.COMBINED_CHART>;
return (
isString(combinedChartConfig.displaySettings.xAxis.identifier) &&
isArray(combinedChartConfig.displaySettings.kpis) &&
combinedChartConfig.displaySettings.kpis.every(
(kpi) =>
isString(kpi.yAxis.identifier) &&
isEnum(kpi.yAxis.aggregation?.operation, CustomReportAggregationOperation) &&
isEnum(kpi.color, ChartColor) &&
isIn(kpi.plot, Object.values(ChartSeriesType)),
)
);
})
.with(ChartType.DISTRIBUTION_CHART, () => {
const distributionChartConfig = config as unknown as DashboardChart<ChartType.DISTRIBUTION_CHART>;
return (
isArray(distributionChartConfig.displaySettings.kpis) &&
distributionChartConfig.displaySettings.kpis.every(
(kpi) =>
isString(kpi.yAxis.identifier) &&
isEnum(kpi.yAxis.aggregation?.operation, CustomReportAggregationOperation) &&
isEnum(kpi.color, ChartColor) &&
isEnum(kpi.plot, ChartSeriesType) &&
isIn(kpi.count, Object.values(ChartDisplaySettingsCountIdentifier)),
) &&
(distributionChartConfig.displaySettings.distribution === null ||
(isArray(distributionChartConfig.displaySettings.distribution) &&
this.isDistributionSegmentsValid(distributionChartConfig.displaySettings.distribution))) &&
isIn(distributionChartConfig.displaySettings.countType, Object.values(DistributionChartCountType))
);
})
.with(ChartType.KPI_CARD_CHART, () => {
const kpiCardChartConfig = config as unknown as DashboardChart<ChartType.KPI_CARD_CHART>;
return (
isString(kpiCardChartConfig.displaySettings.kpi.identifier) &&
isEnum(kpiCardChartConfig.displaySettings.kpi.aggregation?.operation, CustomReportAggregationOperation) &&
(kpiCardChartConfig.displaySettings.displayMode !== KPIChartDisplayMode.DOUGHNUT ||
isEnum(kpiCardChartConfig.displaySettings.color, ChartColor)) &&
(kpiCardChartConfig.displaySettings.displayMode === KPIChartDisplayMode.GAUGE
? this.isSegmentsValueValid(kpiCardChartConfig.displaySettings.segmentsValue)
: !kpiCardChartConfig.displaySettings.segmentsValue)
);
})
.with(ChartType.LIST_CHART, () => {
const listChartConfig = config as unknown as DashboardChart<ChartType.LIST_CHART>;
return (
isIn(listChartConfig.displaySettings.yAxis.identifier, Object.values(ChartDisplaySettingsYAxisIdentifier)) &&
isArray(listChartConfig.displaySettings.kpis) &&
listChartConfig.displaySettings.kpis.every(
(kpi) =>
isString(kpi.xAxis.identifier) &&
isEnum(kpi.xAxis.aggregation?.operation, CustomReportAggregationOperation) &&
isEnum(kpi.color, ChartColor),
)
);
})
.with(
ChartType.TARGET_VS_ACHIEVEMENT_CHART,
// Not implemented in dashboards yet.
() => false,
)
.with(ChartType.TEXT_CHART, () => {
const textChartConfig = config as unknown as DashboardChart<ChartType.TEXT_CHART>;
return (
isIn(textChartConfig.displaySettings.textSize, Object.values(TextChartTextSize)) &&
isIn(textChartConfig.displaySettings.textAlignment, Object.values(TextChartTextAlignment)) &&
isString(textChartConfig.displaySettings.text) &&
isBoolean(textChartConfig.displaySettings.withTransparentBackground)
);
})
.exhaustive();
}
private isSegmentsValueValid(
segmentsValue: DashboardChart<ChartType.KPI_CARD_CHART>['displaySettings']['segmentsValue'],
) {
if (!segmentsValue || segmentsValue.length !== 3) {
return false;
}
return !segmentsValue.some(Number.isNaN) && segmentsValue.every((value) => typeof value === 'number');
}
private isDistributionSegmentsValid(distribution: ChartDisplaySettingsDistributionSegment[]) {
if (!isArray(distribution)) {
return false;
}
return distribution.every(
(segment) =>
typeof segment === 'object' &&
(segment.lowerBound === null || typeof segment.lowerBound === 'number') &&
(segment.upperBound === null || typeof segment.upperBound === 'number'),
);
}
public defaultMessage(args: ValidationArguments) {
return `displaySettings are not valid for the type ${(args.object as DashboardChart).type}`;
}
}
class CustomReportIdValidation implements ValidatorConstraintInterface {
public validate(_text: string, args: ValidationArguments) {
const config = args.object as DashboardChartDto<ChartType>;
// For text charts, customReportId must be null.
// For other chart types, it must be a string.
return config.type === ChartType.TEXT_CHART ? config.customReportId === null : isString(config.customReportId);
}
public defaultMessage(_args: ValidationArguments) {
return `customReportId must be a string or null`;
}
}
export class DashboardChartDto<
TChartType extends ChartType = ChartType,
> implements DashboardChartConfiguration<TChartType> {
@ApiProperty()
@IsString()
public readonly name!: string;
@ApiProperty()
@Validate(CustomReportIdValidation)
public readonly customReportId!: string | null;
@IsEnum(ChartType)
@ApiProperty({ type: 'string' })
public readonly type!: TChartType;
@ApiProperty()
@Validate(DisplaySettingsValidation)
public readonly displaySettings!: ChartDisplaySettings<TChartType>;
}
|