All files / libs/reporting/plan-dashboards/shared/src/lib/schema widgets-schema.ts

100% Statements 148/148
100% Branches 0/0
100% Functions 0/0
100% Lines 148/148

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 1491x 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 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 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 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  
import * as z from 'zod';
 
import {
  CustomReportAggregationOperation,
  type CustomReportAggregation,
  type CustomReportConfigurationField,
} from '@amalia/reporting/custom-reports/shared';
import { ChartColor, ChartSeriesType, ChartType, KPIChartDisplayMode } from '@amalia/reporting/dashboards-v2/types';
import {
  PlanDashboardChallengeWidgetVisualization,
  PlanDashboardKpiChartWidgetTimeframe,
  PlanDashboardWidgetType,
  type PlanDashboardChallengeWidgetConfiguration,
  type PlanDashboardCombinedChartWidgetConfiguration,
  type PlanDashboardCustomChartWidgetConfiguration,
  type PlanDashboardCustomChartWidgetConfigurationBase,
  type PlanDashboardDonutKpiChartWidgetSeriesConfiguration,
  type PlanDashboardGaugeKpiChartWidgetSeriesConfiguration,
  type PlanDashboardKpiChartWidgetConfiguration,
  type PlanDashboardKpiChartWidgetSeriesConfigurationBase,
  type PlanDashboardPayoutCurveWidgetConfiguration,
  type PlanDashboardSimpleKpiChartWidgetSeriesConfiguration,
  type PlanDashboardStatementSummaryWidgetConfiguration,
  type PlanDashboardTargetVsAchievementChartWidgetConfiguration,
  type PlanDashboardWidgetConfiguration,
  type PlanDashboardWidgetConfigurationBase,
} from '@amalia/reporting/plan-dashboards/types';
 
// common props for all widgets
const baseWidgetSchema = z.strictObject({
  id: z.uuidv4(),
  size: z.number().int().min(1).max(12),
  height: z.number().int().min(1).max(4),
}) satisfies z.ZodType<PlanDashboardWidgetConfigurationBase>;
 
// statement summary widget schema
const statementSummaryWidgetSchema = baseWidgetSchema.extend({
  widgetType: z.literal(PlanDashboardWidgetType.STATEMENT_SUMMARY),
  showRuleSections: z.boolean(),
  showNonPayoutRules: z.boolean(),
}) satisfies z.ZodType<PlanDashboardStatementSummaryWidgetConfiguration>;
 
// challenge widget schema
const challengeWidgetSchema = baseWidgetSchema.extend({
  widgetType: z.literal(PlanDashboardWidgetType.CHALLENGE),
  ruleId: z.uuidv4(),
  visualization: z.enum(PlanDashboardChallengeWidgetVisualization),
}) satisfies z.ZodType<PlanDashboardChallengeWidgetConfiguration>;
 
// payout curve widget schema
const payoutCurveWidgetSchema = baseWidgetSchema.extend({
  widgetType: z.literal(PlanDashboardWidgetType.PAYOUT_CURVE),
  ruleId: z.uuidv4(),
  ruleChartId: z.string().regex(/^\d+$/u, 'Must be a numeric string'),
}) satisfies z.ZodType<PlanDashboardPayoutCurveWidgetConfiguration>;
 
// custom charts widgets schemas
const customReportAggregationSchema = z.strictObject({
  operation: z.enum(CustomReportAggregationOperation),
}) satisfies z.ZodType<CustomReportAggregation>;
 
const yAxisSchema = z.strictObject({
  identifier: z.string(),
  alias: z.string().optional(),
  joins: z.array(z.string()).optional(),
  aggregation: customReportAggregationSchema.optional(),
}) satisfies z.ZodType<CustomReportConfigurationField>;
 
const baseCustomChartWidgetSchema = baseWidgetSchema.extend({
  label: z.string().min(1).max(200),
  widgetType: z.literal(PlanDashboardWidgetType.CUSTOM_CHART),
}) satisfies z.ZodType<PlanDashboardCustomChartWidgetConfigurationBase>;
 
const kpiWidgetSeriesConfigurationSchema = z.strictObject({
  yAxis: yAxisSchema,
  timeframe: z.enum(PlanDashboardKpiChartWidgetTimeframe),
}) satisfies z.ZodType<PlanDashboardKpiChartWidgetSeriesConfigurationBase>;
 
const simpleKpiWidgetSeriesConfigurationSchema = kpiWidgetSeriesConfigurationSchema.extend({
  displayMode: z.literal(KPIChartDisplayMode.KPI),
}) satisfies z.ZodType<PlanDashboardSimpleKpiChartWidgetSeriesConfiguration>;
 
const doughnutKpiWidgetSeriesConfigurationSchema = kpiWidgetSeriesConfigurationSchema.extend({
  displayMode: z.literal(KPIChartDisplayMode.DOUGHNUT),
  color: z.enum(ChartColor),
}) satisfies z.ZodType<PlanDashboardDonutKpiChartWidgetSeriesConfiguration>;
 
const gaugeKpiWidgetSeriesConfigurationSchema = kpiWidgetSeriesConfigurationSchema.extend({
  displayMode: z.literal(KPIChartDisplayMode.GAUGE),
  segmentsValue: z.array(z.number().int()),
}) satisfies z.ZodType<PlanDashboardGaugeKpiChartWidgetSeriesConfiguration>;
 
const kpiChartWidgetSchema = baseCustomChartWidgetSchema.extend({
  chartType: z.literal(ChartType.KPI_CARD_CHART),
  series: z.discriminatedUnion('displayMode', [
    simpleKpiWidgetSeriesConfigurationSchema,
    doughnutKpiWidgetSeriesConfigurationSchema,
    gaugeKpiWidgetSeriesConfigurationSchema,
  ]),
}) satisfies z.ZodType<PlanDashboardKpiChartWidgetConfiguration>;
 
const combinedChartWidgetSchema = baseCustomChartWidgetSchema.extend({
  chartType: z.literal(ChartType.COMBINED_CHART),
  customization: z.strictObject({
    showLegend: z.boolean(),
    showLineForYAxis: z.boolean(),
    showValuesOnTooltipOnly: z.boolean(),
  }),
  seriesList: z.array(
    z.strictObject({
      showAverage: z.boolean(),
      showAccumulatedValues: z.boolean(),
      color: z.enum(ChartColor),
      yAxis: yAxisSchema,
      seriesType: z.enum(ChartSeriesType),
    }),
  ),
}) satisfies z.ZodType<PlanDashboardCombinedChartWidgetConfiguration>;
 
const targetVsAchievementChartWidgetSchema = baseCustomChartWidgetSchema.extend({
  chartType: z.literal(ChartType.TARGET_VS_ACHIEVEMENT_CHART),
  customization: z.strictObject({
    showValuesOnTooltipOnly: z.boolean(),
    showLegend: z.boolean(),
    progressBarMaximum: z.number().int().optional(), // 100 for 100%.
  }),
  series: z.strictObject({
    achievement: yAxisSchema,
    target: yAxisSchema,
    earningsAt100Percent: yAxisSchema.nullable(),
    color: z.enum(ChartColor),
  }),
}) satisfies z.ZodType<PlanDashboardTargetVsAchievementChartWidgetConfiguration>;
 
export const customChartWidgetSchema = z.discriminatedUnion('chartType', [
  kpiChartWidgetSchema,
  combinedChartWidgetSchema,
  targetVsAchievementChartWidgetSchema,
]) satisfies z.ZodType<PlanDashboardCustomChartWidgetConfiguration>;
 
export const WidgetSchema = z.discriminatedUnion('widgetType', [
  statementSummaryWidgetSchema,
  customChartWidgetSchema,
  challengeWidgetSchema,
  payoutCurveWidgetSchema,
]) satisfies z.ZodType<PlanDashboardWidgetConfiguration>;
 
export const WidgetsSchema = z.array(WidgetSchema) satisfies z.ZodType<PlanDashboardWidgetConfiguration[]>;