8d78032357
Closes https://github.com/twentyhq/core-team-issues/issues/248?issue=twentyhq%7Ccore-team-issues%7C1543 In this PR - Implementation of groupBy resolver, without pagination, without viewId parameter - introduction of new GqlInputTypeDefinitionKind `OrderByWithGroupBy` which differs from `OrderBy` as it should use aggregations as order by conditions (e.g.: OrderByWithGroupBy should use avgEmployees and not employees which does not make sense) - Minor refacto of all orderBy expressions to use orderBy signature with `OrderByCondition` which is usable for both groupBy queries and non-groupBy queries. groupBy queries differ because their orderBy conditions are based on aggregatedFields (such as avgEmployees) for which we can't use the same orderBy signature <img width="728" height="216" alt="Capture d’écran 2025-09-24 à 15 07 14" src="https://github.com/user-attachments/assets/aa5c0875-cf38-4d4b-be70-f4d64da77a41" /> - Minor refacto to add table name in aggregates expressions `(AVG("employees") -> AVG("company"."employees")` to keep having the table names in the orderBy expression as we already had. Since we use common utils for orderBy and aggregates on findMany this change also impacts findMany queries with aggregates. It also impacts field permission check where we extract the name of the selected fields to check permission on, so I also updated that (see `extractColumnNamesFromAggregateExpression`)
422 lines
14 KiB
TypeScript
422 lines
14 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
|
|
import { FieldMetadataType } from 'twenty-shared/types';
|
|
import { type FindOptionsRelations, type ObjectLiteral } from 'typeorm';
|
|
|
|
import { type ObjectRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
|
|
import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface';
|
|
|
|
import {
|
|
GraphqlQueryRunnerException,
|
|
GraphqlQueryRunnerExceptionCode,
|
|
} from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
|
|
import { ProcessAggregateHelper } from 'src/engine/api/graphql/graphql-query-runner/helpers/process-aggregate.helper';
|
|
import { buildColumnsToSelect } from 'src/engine/api/graphql/graphql-query-runner/utils/build-columns-to-select';
|
|
import { getFieldMetadataFromGraphQLField } from 'src/engine/api/graphql/graphql-query-runner/utils/get-field-metadata-from-graphql-field.util';
|
|
import { getTargetObjectMetadataOrThrow } from 'src/engine/api/graphql/graphql-query-runner/utils/get-target-object-metadata.util';
|
|
import { type AggregationField } from 'src/engine/api/graphql/workspace-schema-builder/utils/get-available-aggregations-from-object-fields.util';
|
|
import { type AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
|
|
import { type ObjectMetadataItemWithFieldMaps } from 'src/engine/metadata-modules/types/object-metadata-item-with-field-maps';
|
|
import { type ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-metadata-maps';
|
|
import { getObjectMetadataMapItemByNameSingular } from 'src/engine/metadata-modules/utils/get-object-metadata-map-item-by-name-singular.util';
|
|
import { type WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
|
|
import { type WorkspaceSelectQueryBuilder } from 'src/engine/twenty-orm/repository/workspace-select-query-builder';
|
|
import { isFieldMetadataEntityOfType } from 'src/engine/utils/is-field-metadata-of-type.util';
|
|
|
|
@Injectable()
|
|
export class ProcessNestedRelationsV2Helper {
|
|
constructor() {}
|
|
|
|
public async processNestedRelations<T extends ObjectRecord = ObjectRecord>({
|
|
objectMetadataMaps,
|
|
parentObjectMetadataItem,
|
|
parentObjectRecords,
|
|
parentObjectRecordsAggregatedValues = {},
|
|
relations,
|
|
aggregate = {},
|
|
limit,
|
|
authContext,
|
|
workspaceDataSource,
|
|
roleId,
|
|
shouldBypassPermissionChecks,
|
|
selectedFields,
|
|
}: {
|
|
objectMetadataMaps: ObjectMetadataMaps;
|
|
parentObjectMetadataItem: ObjectMetadataItemWithFieldMaps;
|
|
parentObjectRecords: T[];
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
parentObjectRecordsAggregatedValues?: Record<string, any>;
|
|
relations: Record<string, FindOptionsRelations<ObjectLiteral>>;
|
|
aggregate?: Record<string, AggregationField>;
|
|
limit: number;
|
|
authContext: AuthContext;
|
|
workspaceDataSource: WorkspaceDataSource;
|
|
shouldBypassPermissionChecks: boolean;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
selectedFields: Record<string, any>;
|
|
roleId?: string;
|
|
}): Promise<void> {
|
|
const processRelationTasks = Object.entries(relations).map(
|
|
([sourceFieldName, nestedRelations]) =>
|
|
this.processRelation({
|
|
objectMetadataMaps,
|
|
parentObjectMetadataItem,
|
|
parentObjectRecords,
|
|
parentObjectRecordsAggregatedValues,
|
|
sourceFieldName,
|
|
nestedRelations,
|
|
aggregate,
|
|
limit,
|
|
authContext,
|
|
workspaceDataSource,
|
|
shouldBypassPermissionChecks,
|
|
roleId,
|
|
selectedFields:
|
|
selectedFields[sourceFieldName] instanceof Object
|
|
? selectedFields[sourceFieldName]
|
|
: undefined,
|
|
}),
|
|
);
|
|
|
|
await Promise.all(processRelationTasks);
|
|
}
|
|
|
|
private async processRelation<T extends ObjectRecord = ObjectRecord>({
|
|
objectMetadataMaps,
|
|
parentObjectMetadataItem,
|
|
parentObjectRecords,
|
|
parentObjectRecordsAggregatedValues,
|
|
sourceFieldName,
|
|
nestedRelations,
|
|
aggregate,
|
|
limit,
|
|
authContext,
|
|
workspaceDataSource,
|
|
shouldBypassPermissionChecks,
|
|
roleId,
|
|
selectedFields,
|
|
}: {
|
|
objectMetadataMaps: ObjectMetadataMaps;
|
|
parentObjectMetadataItem: ObjectMetadataItemWithFieldMaps;
|
|
parentObjectRecords: T[];
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
parentObjectRecordsAggregatedValues: Record<string, any>;
|
|
sourceFieldName: string;
|
|
nestedRelations: FindOptionsRelations<ObjectLiteral>;
|
|
aggregate: Record<string, AggregationField>;
|
|
limit: number;
|
|
authContext: AuthContext;
|
|
workspaceDataSource: WorkspaceDataSource;
|
|
shouldBypassPermissionChecks: boolean;
|
|
roleId?: string;
|
|
selectedFields: Record<string, unknown>;
|
|
}): Promise<void> {
|
|
const sourceFieldMetadata = getFieldMetadataFromGraphQLField({
|
|
objectMetadataItem: parentObjectMetadataItem,
|
|
graphQLField: sourceFieldName,
|
|
objectMetadataMaps,
|
|
});
|
|
|
|
if (
|
|
!isFieldMetadataEntityOfType(
|
|
sourceFieldMetadata,
|
|
FieldMetadataType.RELATION,
|
|
) &&
|
|
!isFieldMetadataEntityOfType(
|
|
sourceFieldMetadata,
|
|
FieldMetadataType.MORPH_RELATION,
|
|
)
|
|
) {
|
|
// TODO: Maybe we should throw an error here ?
|
|
return;
|
|
}
|
|
|
|
if (!sourceFieldMetadata.settings) {
|
|
throw new GraphqlQueryRunnerException(
|
|
`Relation settings not found for field ${sourceFieldName}`,
|
|
GraphqlQueryRunnerExceptionCode.RELATION_SETTINGS_NOT_FOUND,
|
|
);
|
|
}
|
|
|
|
const relationType = sourceFieldMetadata.settings?.relationType;
|
|
const { targetRelationName, targetObjectMetadata, targetRelation } =
|
|
this.getTargetObjectMetadata({
|
|
objectMetadataMaps,
|
|
parentObjectMetadataItem,
|
|
sourceFieldName,
|
|
});
|
|
|
|
const targetObjectRepository = workspaceDataSource.getRepository(
|
|
targetObjectMetadata.nameSingular,
|
|
shouldBypassPermissionChecks,
|
|
roleId,
|
|
);
|
|
|
|
const targetObjectNameSingular = targetObjectMetadata.nameSingular;
|
|
|
|
let targetObjectQueryBuilder = targetObjectRepository.createQueryBuilder(
|
|
targetObjectNameSingular,
|
|
);
|
|
|
|
const columnsToSelect = buildColumnsToSelect({
|
|
select: selectedFields,
|
|
relations: nestedRelations,
|
|
objectMetadataItemWithFieldMaps: targetObjectMetadata,
|
|
objectMetadataMaps,
|
|
});
|
|
|
|
targetObjectQueryBuilder = targetObjectQueryBuilder.setFindOptions({
|
|
select: columnsToSelect,
|
|
});
|
|
|
|
const relationIds = this.getUniqueIds({
|
|
records: parentObjectRecords,
|
|
idField:
|
|
relationType === RelationType.ONE_TO_MANY
|
|
? 'id'
|
|
: (sourceFieldMetadata.settings.joinColumnName ??
|
|
`${sourceFieldName}Id`),
|
|
});
|
|
|
|
const fieldMetadataTargetRelationColumnName =
|
|
targetRelation &&
|
|
isFieldMetadataEntityOfType(
|
|
targetRelation,
|
|
FieldMetadataType.MORPH_RELATION,
|
|
)
|
|
? `${targetRelation.settings?.joinColumnName}`
|
|
: `${targetRelationName}Id`;
|
|
|
|
const { relationResults, relationAggregatedFieldsResult } =
|
|
await this.findRelations({
|
|
referenceQueryBuilder: targetObjectQueryBuilder,
|
|
column:
|
|
relationType === RelationType.ONE_TO_MANY
|
|
? `"${fieldMetadataTargetRelationColumnName}"`
|
|
: 'id',
|
|
ids: relationIds,
|
|
limit: limit * parentObjectRecords.length,
|
|
aggregate,
|
|
sourceFieldName,
|
|
targetObjectNameSingular,
|
|
});
|
|
|
|
this.assignRelationResults({
|
|
parentRecords: parentObjectRecords,
|
|
parentObjectRecordsAggregatedValues,
|
|
relationResults,
|
|
relationAggregatedFieldsResult,
|
|
sourceFieldName,
|
|
joinField:
|
|
relationType === RelationType.ONE_TO_MANY
|
|
? `${fieldMetadataTargetRelationColumnName}`
|
|
: 'id',
|
|
relationType,
|
|
});
|
|
|
|
const targetObjectMetadataItemWithFieldsMaps =
|
|
getObjectMetadataMapItemByNameSingular(
|
|
objectMetadataMaps,
|
|
targetObjectMetadata.nameSingular,
|
|
);
|
|
|
|
if (!targetObjectMetadataItemWithFieldsMaps) {
|
|
throw new GraphqlQueryRunnerException(
|
|
`Object ${targetObjectMetadata.nameSingular} not found`,
|
|
GraphqlQueryRunnerExceptionCode.OBJECT_METADATA_NOT_FOUND,
|
|
);
|
|
}
|
|
|
|
if (Object.keys(nestedRelations).length > 0) {
|
|
await this.processNestedRelations({
|
|
objectMetadataMaps,
|
|
parentObjectMetadataItem: targetObjectMetadataItemWithFieldsMaps,
|
|
parentObjectRecords: relationResults as ObjectRecord[],
|
|
parentObjectRecordsAggregatedValues: relationAggregatedFieldsResult,
|
|
relations: nestedRelations as Record<
|
|
string,
|
|
FindOptionsRelations<ObjectLiteral>
|
|
>,
|
|
aggregate,
|
|
limit,
|
|
authContext,
|
|
workspaceDataSource,
|
|
shouldBypassPermissionChecks,
|
|
roleId,
|
|
selectedFields,
|
|
});
|
|
}
|
|
}
|
|
|
|
private getTargetObjectMetadata({
|
|
objectMetadataMaps,
|
|
parentObjectMetadataItem,
|
|
sourceFieldName,
|
|
}: {
|
|
objectMetadataMaps: ObjectMetadataMaps;
|
|
parentObjectMetadataItem: ObjectMetadataItemWithFieldMaps;
|
|
sourceFieldName: string;
|
|
}) {
|
|
const targetFieldMetadata = getFieldMetadataFromGraphQLField({
|
|
objectMetadataItem: parentObjectMetadataItem,
|
|
graphQLField: sourceFieldName,
|
|
objectMetadataMaps,
|
|
});
|
|
|
|
const targetObjectMetadata = getTargetObjectMetadataOrThrow(
|
|
targetFieldMetadata,
|
|
objectMetadataMaps,
|
|
);
|
|
|
|
if (
|
|
!targetFieldMetadata.relationTargetObjectMetadataId ||
|
|
!targetFieldMetadata.relationTargetFieldMetadataId
|
|
) {
|
|
throw new GraphqlQueryRunnerException(
|
|
`Relation target object metadata id or field metadata id not found for field ${sourceFieldName}`,
|
|
GraphqlQueryRunnerExceptionCode.RELATION_TARGET_OBJECT_METADATA_NOT_FOUND,
|
|
);
|
|
}
|
|
|
|
const targetRelation =
|
|
objectMetadataMaps.byId[
|
|
targetFieldMetadata.relationTargetObjectMetadataId
|
|
]?.fieldsById[targetFieldMetadata.relationTargetFieldMetadataId];
|
|
|
|
const targetRelationName =
|
|
objectMetadataMaps.byId[
|
|
targetFieldMetadata.relationTargetObjectMetadataId
|
|
]?.fieldsById[targetFieldMetadata.relationTargetFieldMetadataId]?.name;
|
|
|
|
return { targetRelationName, targetObjectMetadata, targetRelation };
|
|
}
|
|
|
|
private getUniqueIds({
|
|
records,
|
|
idField,
|
|
}: {
|
|
records: ObjectRecord[];
|
|
idField: string;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
}): any[] {
|
|
return [...new Set(records.map((item) => item[idField]))];
|
|
}
|
|
|
|
private async findRelations({
|
|
referenceQueryBuilder,
|
|
column,
|
|
ids,
|
|
limit,
|
|
aggregate,
|
|
sourceFieldName,
|
|
targetObjectNameSingular,
|
|
}: {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
referenceQueryBuilder: WorkspaceSelectQueryBuilder<any>;
|
|
column: string;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
ids: any[];
|
|
limit: number;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
aggregate: Record<string, any>;
|
|
sourceFieldName: string;
|
|
targetObjectNameSingular: string;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
}): Promise<{ relationResults: any[]; relationAggregatedFieldsResult: any }> {
|
|
if (ids.length === 0) {
|
|
return { relationResults: [], relationAggregatedFieldsResult: {} };
|
|
}
|
|
|
|
const aggregateForRelation = aggregate[sourceFieldName];
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
let relationAggregatedFieldsResult: Record<string, any> = {};
|
|
|
|
if (aggregateForRelation) {
|
|
const aggregateQueryBuilder = referenceQueryBuilder.clone();
|
|
|
|
ProcessAggregateHelper.addSelectedAggregatedFieldsQueriesToQueryBuilder({
|
|
selectedAggregatedFields: aggregateForRelation,
|
|
queryBuilder: aggregateQueryBuilder,
|
|
objectMetadataNameSingular: targetObjectNameSingular,
|
|
});
|
|
|
|
const aggregatedFieldsValues = await aggregateQueryBuilder
|
|
.addSelect(column)
|
|
.where(`${column} IN (:...ids)`, {
|
|
ids,
|
|
})
|
|
.groupBy(column)
|
|
.getRawMany();
|
|
|
|
relationAggregatedFieldsResult = aggregatedFieldsValues.reduce(
|
|
(acc, item) => {
|
|
const columnWithoutQuotes = column.replace(/["']/g, '');
|
|
const key = item[columnWithoutQuotes];
|
|
const { [column]: _, ...itemWithoutColumn } = item;
|
|
|
|
acc[key] = itemWithoutColumn;
|
|
|
|
return acc;
|
|
},
|
|
{},
|
|
);
|
|
}
|
|
|
|
const queryBuilderOptions = referenceQueryBuilder.getFindOptions();
|
|
const columnWithoutQuotes = column.replace(/["']/g, '');
|
|
|
|
const result = await referenceQueryBuilder
|
|
.setFindOptions({
|
|
...queryBuilderOptions,
|
|
select: { ...queryBuilderOptions.select, [columnWithoutQuotes]: true },
|
|
})
|
|
.where(`${column} IN (:...ids)`, {
|
|
ids,
|
|
})
|
|
.take(limit)
|
|
.getMany();
|
|
|
|
return { relationResults: result, relationAggregatedFieldsResult };
|
|
}
|
|
|
|
private assignRelationResults({
|
|
parentRecords,
|
|
parentObjectRecordsAggregatedValues,
|
|
relationResults,
|
|
relationAggregatedFieldsResult,
|
|
sourceFieldName,
|
|
joinField,
|
|
relationType,
|
|
}: {
|
|
parentRecords: ObjectRecord[];
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
parentObjectRecordsAggregatedValues: Record<string, any>;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
relationResults: any[];
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
relationAggregatedFieldsResult: Record<string, any>;
|
|
sourceFieldName: string;
|
|
joinField: string;
|
|
relationType: RelationType;
|
|
}): void {
|
|
parentRecords.forEach((item) => {
|
|
if (relationType === RelationType.ONE_TO_MANY) {
|
|
item[sourceFieldName] = relationResults.filter(
|
|
(rel) => rel[joinField] === item.id,
|
|
);
|
|
} else {
|
|
if (relationResults.length === 0) {
|
|
item[`${sourceFieldName}Id`] = null;
|
|
}
|
|
item[sourceFieldName] =
|
|
relationResults.find(
|
|
(rel) => rel.id === item[`${sourceFieldName}Id`],
|
|
) ?? null;
|
|
}
|
|
});
|
|
|
|
parentObjectRecordsAggregatedValues[sourceFieldName] =
|
|
relationAggregatedFieldsResult;
|
|
}
|
|
}
|