fca39d317f
Fixes https://github.com/twentyhq/core-team-issues/issues/255?issue=twentyhq%7Ccore-team-issues%7C1214. Until then, in the endpoints of our dynamic schema, we were querying all columns and then formatting the result by removing the non-requested fields (fields not mentioned in the graphql Query) from the result. This is not compatible with field-level permissions that we are about to introduce because users would see their request denied if they have restricted rights on any of the fields of the objects they are querying, even if they did not query it in the first place. To prepare for this change, we are restricting the list of queried columns to those made necessary by the graphql query. I only made the changes in the dynamic schema for now. We will potentially need to do updates to other part of the app that use createQueryBuilder directly or not (for instance, when calling repository methods such as .findOne()), but they mostly regard system objects that are not subject to permissions or are executed by entities that bypass permission such as jobs creating People and Companies from their email sync. No changes have been brought to existingRecords related logic in the dynamic schema because @Weiko is currently working on it, so I may need to adapt the new logic after he is done. No feature flag have been added so far as this should not change anything at the moment.
176 lines
6.4 KiB
TypeScript
176 lines
6.4 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
|
|
import isEmpty from 'lodash.isempty';
|
|
import { In } from 'typeorm';
|
|
|
|
import {
|
|
GraphqlQueryBaseResolverService,
|
|
GraphqlQueryResolverExecutionArgs,
|
|
} from 'src/engine/api/graphql/graphql-query-runner/interfaces/base-resolver-service';
|
|
import {
|
|
ObjectRecord,
|
|
OrderByDirection,
|
|
} from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
|
|
import { IConnection } from 'src/engine/api/graphql/workspace-query-runner/interfaces/connection.interface';
|
|
import { WorkspaceQueryRunnerOptions } from 'src/engine/api/graphql/workspace-query-runner/interfaces/query-runner-option.interface';
|
|
import { FindDuplicatesResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';
|
|
|
|
import {
|
|
GraphqlQueryRunnerException,
|
|
GraphqlQueryRunnerExceptionCode,
|
|
} from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
|
|
import { GraphqlQueryParser } from 'src/engine/api/graphql/graphql-query-runner/graphql-query-parsers/graphql-query.parser';
|
|
import { ObjectRecordsToGraphqlConnectionHelper } from 'src/engine/api/graphql/graphql-query-runner/helpers/object-records-to-graphql-connection.helper';
|
|
import { buildColumnsToSelect } from 'src/engine/api/graphql/graphql-query-runner/utils/build-columns-to-select';
|
|
import { buildDuplicateConditions } from 'src/engine/api/utils/build-duplicate-conditions.utils';
|
|
import { getObjectMetadataMapItemByNameSingular } from 'src/engine/metadata-modules/utils/get-object-metadata-map-item-by-name-singular.util';
|
|
import { formatData } from 'src/engine/twenty-orm/utils/format-data.util';
|
|
import { formatResult } from 'src/engine/twenty-orm/utils/format-result.util';
|
|
|
|
@Injectable()
|
|
export class GraphqlQueryFindDuplicatesResolverService extends GraphqlQueryBaseResolverService<
|
|
FindDuplicatesResolverArgs,
|
|
IConnection<ObjectRecord>[]
|
|
> {
|
|
async resolve(
|
|
executionArgs: GraphqlQueryResolverExecutionArgs<FindDuplicatesResolverArgs>,
|
|
): Promise<IConnection<ObjectRecord>[]> {
|
|
const { objectMetadataItemWithFieldMaps, objectMetadataMaps } =
|
|
executionArgs.options;
|
|
|
|
const existingRecordsQueryBuilder =
|
|
executionArgs.repository.createQueryBuilder(
|
|
objectMetadataItemWithFieldMaps.nameSingular,
|
|
);
|
|
|
|
const objectMetadataItemWithFieldsMaps =
|
|
getObjectMetadataMapItemByNameSingular(
|
|
objectMetadataMaps,
|
|
objectMetadataItemWithFieldMaps.nameSingular,
|
|
);
|
|
|
|
if (!objectMetadataItemWithFieldsMaps) {
|
|
throw new GraphqlQueryRunnerException(
|
|
`Object ${objectMetadataItemWithFieldMaps.nameSingular} not found`,
|
|
GraphqlQueryRunnerExceptionCode.OBJECT_METADATA_NOT_FOUND,
|
|
);
|
|
}
|
|
|
|
const graphqlQueryParser = new GraphqlQueryParser(
|
|
objectMetadataItemWithFieldMaps,
|
|
objectMetadataMaps,
|
|
);
|
|
|
|
const typeORMObjectRecordsParser =
|
|
new ObjectRecordsToGraphqlConnectionHelper(objectMetadataMaps);
|
|
|
|
let objectRecords: Partial<ObjectRecord>[] = [];
|
|
|
|
if (executionArgs.args.ids) {
|
|
const nonFormattedObjectRecords = (await existingRecordsQueryBuilder
|
|
.where({ id: In(executionArgs.args.ids) })
|
|
.getMany()) as ObjectRecord[];
|
|
|
|
objectRecords = formatResult(
|
|
nonFormattedObjectRecords,
|
|
objectMetadataItemWithFieldMaps,
|
|
objectMetadataMaps,
|
|
);
|
|
} else if (executionArgs.args.data && !isEmpty(executionArgs.args.data)) {
|
|
objectRecords = formatData(
|
|
executionArgs.args.data,
|
|
objectMetadataItemWithFieldMaps,
|
|
);
|
|
}
|
|
|
|
const duplicateConnections: IConnection<ObjectRecord>[] = await Promise.all(
|
|
objectRecords.map(async (record) => {
|
|
const duplicateConditions = buildDuplicateConditions(
|
|
objectMetadataItemWithFieldMaps,
|
|
[record],
|
|
record.id,
|
|
);
|
|
|
|
if (isEmpty(duplicateConditions)) {
|
|
return typeORMObjectRecordsParser.createConnection({
|
|
objectRecords: [],
|
|
objectName: objectMetadataItemWithFieldMaps.nameSingular,
|
|
take: 0,
|
|
totalCount: 0,
|
|
order: [{ id: OrderByDirection.AscNullsFirst }],
|
|
hasNextPage: false,
|
|
hasPreviousPage: false,
|
|
});
|
|
}
|
|
|
|
const columnsToSelect = buildColumnsToSelect({
|
|
select: executionArgs.graphqlQuerySelectedFieldsResult.select,
|
|
relations: executionArgs.graphqlQuerySelectedFieldsResult.relations,
|
|
objectMetadataItemWithFieldMaps,
|
|
});
|
|
|
|
const duplicateRecordsQueryBuilder =
|
|
executionArgs.repository.createQueryBuilder(
|
|
objectMetadataItemWithFieldMaps.nameSingular,
|
|
);
|
|
|
|
graphqlQueryParser.applyFilterToBuilder(
|
|
duplicateRecordsQueryBuilder,
|
|
objectMetadataItemWithFieldMaps.nameSingular,
|
|
duplicateConditions,
|
|
);
|
|
|
|
const nonFormattedDuplicates = (await duplicateRecordsQueryBuilder
|
|
.setFindOptions({
|
|
select: columnsToSelect,
|
|
})
|
|
.getMany()) as ObjectRecord[];
|
|
|
|
const duplicates = formatResult<ObjectRecord[]>(
|
|
nonFormattedDuplicates,
|
|
objectMetadataItemWithFieldMaps,
|
|
objectMetadataMaps,
|
|
);
|
|
|
|
return typeORMObjectRecordsParser.createConnection({
|
|
objectRecords: duplicates,
|
|
objectName: objectMetadataItemWithFieldMaps.nameSingular,
|
|
take: duplicates.length,
|
|
totalCount: duplicates.length,
|
|
order: [{ id: OrderByDirection.AscNullsFirst }],
|
|
hasNextPage: false,
|
|
hasPreviousPage: false,
|
|
});
|
|
}),
|
|
);
|
|
|
|
return duplicateConnections;
|
|
}
|
|
|
|
async validate(
|
|
args: FindDuplicatesResolverArgs,
|
|
_options: WorkspaceQueryRunnerOptions,
|
|
): Promise<void> {
|
|
if (!args.data && !args.ids) {
|
|
throw new GraphqlQueryRunnerException(
|
|
'You have to provide either "data" or "ids" argument',
|
|
GraphqlQueryRunnerExceptionCode.INVALID_QUERY_INPUT,
|
|
);
|
|
}
|
|
|
|
if (args.data && args.ids) {
|
|
throw new GraphqlQueryRunnerException(
|
|
'You cannot provide both "data" and "ids" arguments',
|
|
GraphqlQueryRunnerExceptionCode.INVALID_QUERY_INPUT,
|
|
);
|
|
}
|
|
|
|
if (!args.ids && isEmpty(args.data)) {
|
|
throw new GraphqlQueryRunnerException(
|
|
'The "data" condition can not be empty when "ids" input not provided',
|
|
GraphqlQueryRunnerExceptionCode.INVALID_QUERY_INPUT,
|
|
);
|
|
}
|
|
}
|
|
}
|