File size: 4,297 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
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
import {
  isArray,
  isBoolean,
  isNumber,
  isObject,
  isString,
} from 'class-validator';

import { isDefined, isEmptyObject } from 'twenty-shared/utils';

import { STANDARD_ERROR_MESSAGE } from 'src/engine/api/common/common-query-runners/errors/standard-error-message.constant';
import {
  GraphqlDirectExecutionException,
  GraphqlDirectExecutionExceptionCode,
} from 'src/engine/api/graphql/direct-execution/errors/graphql-direct-execution.exception';
import { type GroupByResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';

export function assertGroupByArgs(
  args: unknown,
): asserts args is GroupByResolverArgs {
  if (!isObject(args)) {
    throw new GraphqlDirectExecutionException(
      'Invalid argument: it must be an object',
      GraphqlDirectExecutionExceptionCode.INVALID_QUERY_INPUT,
      { userFriendlyMessage: STANDARD_ERROR_MESSAGE },
    );
  }

  const argKeys = Object.keys(args);

  const allowedKeys = new Set([
    'filter',
    'orderBy',
    'orderByForRecords',
    'groupBy',
    'viewId',
    'includeRecords',
    'limit',
    'offsetForRecords',
  ]);

  for (const key of argKeys) {
    if (!allowedKeys.has(key)) {
      throw new GraphqlDirectExecutionException(
        `Argument not allowed: ${key}`,
        GraphqlDirectExecutionExceptionCode.INVALID_QUERY_INPUT,
        { userFriendlyMessage: STANDARD_ERROR_MESSAGE },
      );
    }
  }

  if (
    !('groupBy' in args) ||
    (!Array.isArray(args.groupBy) && !isObject(args.groupBy))
  ) {
    throw new GraphqlDirectExecutionException(
      'Missing required argument: "groupBy" must be an array.',
      GraphqlDirectExecutionExceptionCode.INVALID_QUERY_INPUT,
      { userFriendlyMessage: STANDARD_ERROR_MESSAGE },
    );
  }

  if ('filter' in args && isDefined(args.filter) && !isObject(args.filter)) {
    throw new GraphqlDirectExecutionException(
      'Invalid argument: "filter" must be an object',
      GraphqlDirectExecutionExceptionCode.INVALID_QUERY_INPUT,
      { userFriendlyMessage: STANDARD_ERROR_MESSAGE },
    );
  }

  if (
    'orderBy' in args &&
    isDefined(args.orderBy) &&
    !isEmptyObject(args.orderBy) &&
    !isArray(args.orderBy) &&
    !isObject(args.orderBy)
  ) {
    throw new GraphqlDirectExecutionException(
      'Invalid argument: "orderBy" must be an array',
      GraphqlDirectExecutionExceptionCode.INVALID_QUERY_INPUT,
      { userFriendlyMessage: STANDARD_ERROR_MESSAGE },
    );
  }

  if (
    'orderByForRecords' in args &&
    isDefined(args.orderByForRecords) &&
    !isEmptyObject(args.orderByForRecords) &&
    !isArray(args.orderByForRecords) &&
    !isObject(args.orderByForRecords)
  ) {
    throw new GraphqlDirectExecutionException(
      'Invalid argument: "orderByForRecords" must be an array',
      GraphqlDirectExecutionExceptionCode.INVALID_QUERY_INPUT,
      { userFriendlyMessage: STANDARD_ERROR_MESSAGE },
    );
  }

  if ('viewId' in args && isDefined(args.viewId) && !isString(args.viewId)) {
    throw new GraphqlDirectExecutionException(
      'Invalid argument: "viewId" must be a string',
      GraphqlDirectExecutionExceptionCode.INVALID_QUERY_INPUT,
      { userFriendlyMessage: STANDARD_ERROR_MESSAGE },
    );
  }

  if (
    'includeRecords' in args &&
    isDefined(args.includeRecords) &&
    !isBoolean(args.includeRecords)
  ) {
    throw new GraphqlDirectExecutionException(
      'Invalid argument: "includeRecords" must be a boolean',
      GraphqlDirectExecutionExceptionCode.INVALID_QUERY_INPUT,
      { userFriendlyMessage: STANDARD_ERROR_MESSAGE },
    );
  }

  if ('limit' in args && isDefined(args.limit) && !isNumber(args.limit)) {
    throw new GraphqlDirectExecutionException(
      'Invalid argument: "limit" must be a number',
      GraphqlDirectExecutionExceptionCode.INVALID_QUERY_INPUT,
      { userFriendlyMessage: STANDARD_ERROR_MESSAGE },
    );
  }

  if (
    'offsetForRecords' in args &&
    isDefined(args.offsetForRecords) &&
    !isNumber(args.offsetForRecords)
  ) {
    throw new GraphqlDirectExecutionException(
      'Invalid argument: "offsetForRecords" must be a number',
      GraphqlDirectExecutionExceptionCode.INVALID_QUERY_INPUT,
      { userFriendlyMessage: STANDARD_ERROR_MESSAGE },
    );
  }
}