File size: 1,276 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { AggregateOperations, FieldMetadataType } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';

const PERCENT_AGGREGATE_OPERATIONS = new Set([
  AggregateOperations.PERCENTAGE_EMPTY,
  AggregateOperations.PERCENTAGE_NOT_EMPTY,
]);

const COUNT_AGGREGATE_OPERATIONS = new Set([
  AggregateOperations.COUNT,
  AggregateOperations.COUNT_UNIQUE_VALUES,
  AggregateOperations.COUNT_EMPTY,
  AggregateOperations.COUNT_NOT_EMPTY,
  AggregateOperations.COUNT_TRUE,
  AggregateOperations.COUNT_FALSE,
]);

type TransformAggregateValueParams = {
  rawValue: unknown;
  aggregateFieldType: FieldMetadataType;
  aggregateOperation: AggregateOperations;
};

export const transformAggregateValue = ({
  rawValue,
  aggregateFieldType,
  aggregateOperation,
}: TransformAggregateValueParams): number => {
  if (!isDefined(rawValue)) {
    return 0;
  }

  const numericValue = Number(rawValue);

  if (isNaN(numericValue)) {
    return 0;
  }

  if (COUNT_AGGREGATE_OPERATIONS.has(aggregateOperation)) {
    return numericValue;
  }

  if (PERCENT_AGGREGATE_OPERATIONS.has(aggregateOperation)) {
    return numericValue * 100;
  }

  if (aggregateFieldType === FieldMetadataType.CURRENCY) {
    return numericValue / 1_000_000;
  }

  return numericValue;
};