Skip to content

Commit

Permalink
Begin refacto duplicate(ids)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixMalfait committed Jun 24, 2024
1 parent 5f155bb commit 584f7d4
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('useFindDuplicateRecords', () => {
const { result } = renderHook(
() =>
useFindDuplicateRecords({
objectRecordId,
objectRecordIds: [objectRecordId],
objectNameSingular,
}),
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { logError } from '~/utils/logError';

export const useFindDuplicateRecords = <T extends ObjectRecord = ObjectRecord>({
objectRecordId = '',
objectRecordIds = [],
objectNameSingular,
onCompleted,
}: ObjectMetadataItemIdentifier & {
objectRecordId: string | undefined;
objectRecordIds: string[] | undefined;
onCompleted?: (data: RecordGqlConnection) => void;
skip?: boolean;
}) => {
Expand All @@ -42,7 +42,7 @@ export const useFindDuplicateRecords = <T extends ObjectRecord = ObjectRecord>({
findDuplicateRecordsQuery,
{
variables: {
id: objectRecordId,
ids: objectRecordIds,
},
onCompleted: (data) => {
onCompleted?.(data[queryResponseField]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export const useFindDuplicateRecordsQuery = ({
const findDuplicateRecordsQuery = gql`
query FindDuplicate${capitalize(
objectMetadataItem.nameSingular,
)}($id: ID!) {
)}($ids: [ID]!) {
${getFindDuplicateRecordsQueryResponseField(
objectMetadataItem.nameSingular,
)}(id: $id) {
)}(ids: $ids) {
edges {
node ${mapObjectMetadataToGraphQLQuery({
objectMetadataItems,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const RecordDetailDuplicatesSection = ({
objectNameSingular: string;
}) => {
const { records: duplicateRecords } = useFindDuplicateRecords({
objectRecordId,
objectRecordIds: [objectRecordId],
objectNameSingular,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ export class FindDuplicatesQueryFactory {
async create(
args: FindDuplicatesResolverArgs,
options: WorkspaceQueryBuilderOptions,
currentRecord?: Record,
existingRecords?: Record[],
) {
const fieldsString = await this.fieldsStringFactory.create(
options.info,
options.fieldMetadataCollection,
options.objectMetadataCollection,
);

if (currentRecord) {
if (existingRecords) {
const query = existingRecords.reduce((acc, record, index) => {
return (
acc + this.buildQuery(fieldsString, options, undefined, record, index)
);
}, '');

return `query {
${this.buildQuery(fieldsString, options, undefined, currentRecord)}
${query}
}`;
}

Expand Down Expand Up @@ -67,14 +73,14 @@ export class FindDuplicatesQueryFactory {
fieldsString: string,
options: WorkspaceQueryBuilderOptions,
data?: Record,
currentRecord?: Record,
existingRecord?: Record,
index?: number,
) {
const duplicateCondition =
this.duplicateService.buildDuplicateConditionForGraphQL(
options.objectMetadataItem,
data ?? currentRecord,
currentRecord?.id,
data ?? existingRecord,
existingRecord?.id,
);

const filters = stringifyWithoutKeyQuote(duplicateCondition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ export class WorkspaceQueryBuilderFactory {
findDuplicates(
args: FindDuplicatesResolverArgs,
options: WorkspaceQueryBuilderOptions,
existingRecord?: IRecord,
existingRecords?: IRecord[],
): Promise<string> {
return this.findDuplicatesQueryFactory.create(
args,
options,
existingRecord,
existingRecords,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ export class QueryRunnerArgsFactory {
case ResolverArgsType.FindDuplicates:
return {
...args,
id: await this.overrideValueByFieldMetadata(
'id',
(args as FindDuplicatesResolverArgs).id,
fieldMetadataMap,
ids: await Promise.all(
(args as FindDuplicatesResolverArgs).ids?.map((id) =>
this.overrideValueByFieldMetadata('id', id, fieldMetadataMap),
) ?? [],
),
data: await Promise.all(
(args as FindDuplicatesResolverArgs).data?.map((arg, index) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ export class WorkspaceQueryRunnerService {
args: FindDuplicatesResolverArgs<TRecord>,
options: WorkspaceQueryRunnerOptions,
): Promise<IConnection<TRecord> | undefined> {
if (!args.data && !args.id) {
if (!args.data && !args.ids) {
throw new BadRequestException(
'You have to provide either "data" or "id" argument',
);
}

if (!args.id && isEmpty(args.data)) {
if (!args.ids && isEmpty(args.data)) {
throw new BadRequestException(
'The "data" condition can not be empty when ID input not provided',
);
Expand All @@ -192,24 +192,24 @@ export class WorkspaceQueryRunnerService {
ResolverArgsType.FindDuplicates,
)) as FindDuplicatesResolverArgs<TRecord>;

let existingRecord: Record<string, any> | null = null;
let existingRecords: IRecord[] | undefined = undefined;

if (computedArgs.id) {
existingRecord = await this.duplicateService.findExistingRecords(
[computedArgs.id],
if (computedArgs.ids && computedArgs.ids.length > 0) {
existingRecords = await this.duplicateService.findExistingRecords(
computedArgs.ids,
objectMetadataItem,
workspaceId,
);

if (existingRecord && existingRecord.length != 1) {
throw new NotFoundError(`Object with id ${args.id} not found`);
if (!existingRecords || existingRecords.length === 0) {
throw new NotFoundError(`Object with id ${args.ids} not found`);
}
}

const query = await this.workspaceQueryBuilderFactory.findDuplicates(
computedArgs,
options,
existingRecord ? existingRecord[0] : null,
existingRecords,
);

await this.workspacePreQueryHookService.executePreHooks(
Expand Down Expand Up @@ -672,9 +672,6 @@ export class WorkspaceQueryRunnerService {
[],
)[0],
};

console.log('result', result);

const errors = graphqlResult?.[0]?.resolve?.errors;

if (
Expand Down Expand Up @@ -705,8 +702,6 @@ export class WorkspaceQueryRunnerService {
objectMetadataItem,
);

console.log(resultWithGetters);

return parseResult(resultWithGetters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface FindOneResolverArgs<Filter = any> {
}

export interface FindDuplicatesResolverArgs<Data extends Record = Record> {
id?: string;
ids?: string[];
data?: Data[];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ export const getResolverArgs = (
};
case 'findDuplicates':
return {
id: {
ids: {
type: GraphQLID,
isNullable: true,
isArray: true,
},
data: {
kind: InputTypeDefinitionKind.Create,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';

import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
import { Record as IRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/record.interface';

import { settings } from 'src/engine/constants/settings';
import { DUPLICATE_CRITERIA_COLLECTION } from 'src/engine/core-modules/duplicate/constants/duplicate-criteria.constants';
Expand Down Expand Up @@ -38,7 +39,7 @@ export class DuplicateService {
workspaceId,
);

return results as Record<string, any>[] | null;
return results as IRecord[];
}

async findDuplicate(
Expand Down

0 comments on commit 584f7d4

Please sign in to comment.