diff --git a/modules/server/src/network/setup/constants.ts b/modules/server/src/network/setup/constants.ts index 0e7121240..4383c1fb1 100644 --- a/modules/server/src/network/setup/constants.ts +++ b/modules/server/src/network/setup/constants.ts @@ -3,6 +3,7 @@ import { ObjectValues } from '@/utils/types'; /** * Supported aggregations that can be used by network aggregation */ +// todo SUPPORTED_AGGREGATONS TO AGGREGATION export const SUPPORTED_AGGREGATIONS = { Aggregations: 'Aggregations', NumericAggregations: 'NumericAggregations', diff --git a/modules/server/src/network/setup/fields.ts b/modules/server/src/network/setup/fields.ts index 33e31a456..9d7d60293 100644 --- a/modules/server/src/network/setup/fields.ts +++ b/modules/server/src/network/setup/fields.ts @@ -1,19 +1,26 @@ -import { GQLFieldType } from '../queries'; -import { - NodeConfig, - SupportedAggregations, - SupportedNetworkFieldType, - UnsupportedAggregations, -} from '../types/types'; -import { SUPPORTED_AGGREGATIONS_LIST } from './constants'; +import { SupportedAggregation, SUPPORTED_AGGREGATIONS_LIST } from './constants'; +import { NodeConfig } from './query'; -export type NetworkFields = { name: string; fields: GQLFieldType[] }; +type NetworkFieldType = { + name: string; + type: T; +}; +type SupportedNetworkFieldType = NetworkFieldType; +type SupportedAggregations = SupportedNetworkFieldType[]; +type UnsupportedAggregations = NetworkFieldType[]; + +const isSupportedType = ( + fieldObject: NetworkFieldType, + supportedList: string[], +): fieldObject is SupportedNetworkFieldType => { + return supportedList.includes(fieldObject.type); +}; /** * Parse network fields into supported and unsupported * * @param fields - * @returns { supportedAggregations: [], unsupportedAggregations: [] } + * @returns An object containing supported types and unsupported types */ export const getFieldTypes = (fields: NodeConfig['aggregations']) => { const fieldTypes = fields.reduce( @@ -24,8 +31,7 @@ export const getFieldTypes = (fields: NodeConfig['aggregations']) => { }, field, ) => { - const isAggregationTypeSupported = SUPPORTED_AGGREGATIONS_LIST.includes(field.type); - if (isAggregationTypeSupported) { + if (isSupportedType(field, SUPPORTED_AGGREGATIONS_LIST)) { return { ...aggregations, supportedAggregations: aggregations.supportedAggregations.concat(field), @@ -47,18 +53,22 @@ export const getFieldTypes = (fields: NodeConfig['aggregations']) => { }; /** + * Takes in all node aggregation field types and returns a single array. + * - dedupes + * - ensures types are supported * - * @param nodeConfigs - * @param supportedTypes - * @returns unique fields + * @param nodeConfigs - + * @returns Unique and supported aggregation field types. */ -export const getAllFieldTypes = (nodeConfigs: NodeConfig[]): SupportedNetworkFieldType[] => { +export const normalizeFieldTypes = (nodeConfigs: NodeConfig[]): SupportedNetworkFieldType[] => { + // split into supported and unsupported types const nodeFieldTypes = nodeConfigs.map((config) => { const { supportedAggregations, unsupportedAggregations } = getFieldTypes(config.aggregations); return { supportedAggregations, unsupportedAggregations }; }); + // flatten to single object const allSupportedAggregations = nodeFieldTypes.flatMap( (fieldType) => fieldType.supportedAggregations, ); @@ -72,5 +82,6 @@ export const getAllFieldTypes = (nodeConfigs: NodeConfig[]): SupportedNetworkFie const uniqueSupportedAggregations = Array.from( new Set(allSupportedAggregations.map((nodeField) => JSON.stringify(nodeField))), ).map((nodeFieldString) => JSON.parse(nodeFieldString)); + return uniqueSupportedAggregations; };