@@ -3,7 +3,7 @@ import { Injectable, Logger } from '@nestjs/common';
3
3
import isEmpty from 'lodash.isempty' ;
4
4
5
5
import { WorkspaceQueryBuilderOptions } from 'src/engine/api/graphql/workspace-query-builder/interfaces/workspace-query-builder-options.interface' ;
6
- import { RecordFilter } from 'src/engine/api/graphql/workspace-query-builder/interfaces/record.interface' ;
6
+ import { Record } from 'src/engine/api/graphql/workspace-query-builder/interfaces/record.interface' ;
7
7
import { FindDuplicatesResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface' ;
8
8
9
9
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util' ;
@@ -23,57 +23,71 @@ export class FindDuplicatesQueryFactory {
23
23
private readonly duplicateService : DuplicateService ,
24
24
) { }
25
25
26
- async create < Filter extends RecordFilter = RecordFilter > (
27
- args : FindDuplicatesResolverArgs < Filter > ,
26
+ async create (
27
+ args : FindDuplicatesResolverArgs ,
28
28
options : WorkspaceQueryBuilderOptions ,
29
- currentRecord ?: Record < string , unknown > ,
29
+ currentRecord ?: Record ,
30
30
) {
31
31
const fieldsString = await this . fieldsStringFactory . create (
32
32
options . info ,
33
33
options . fieldMetadataCollection ,
34
34
options . objectMetadataCollection ,
35
35
) ;
36
36
37
- const argsData = this . getFindDuplicateBy < Filter > (
38
- args ,
39
- options ,
40
- currentRecord ,
41
- ) ;
37
+ if ( currentRecord ) {
38
+ return `query {
39
+ ${ this . buildQuery ( fieldsString , options , undefined , currentRecord ) }
40
+ }` ;
41
+ }
42
42
43
- const duplicateCondition =
44
- this . duplicateService . buildDuplicateConditionForGraphQL (
45
- options . objectMetadataItem ,
46
- argsData ,
47
- args . id ,
43
+ const query = args . data ?. reduce ( ( acc , dataItem , index ) => {
44
+ const argsData = this . argsAliasFactory . create (
45
+ dataItem ?? { } ,
46
+ options . fieldMetadataCollection ,
48
47
) ;
49
48
50
- const filters = stringifyWithoutKeyQuote ( duplicateCondition ) ;
49
+ return (
50
+ acc +
51
+ this . buildQuery (
52
+ fieldsString ,
53
+ options ,
54
+ argsData as Record ,
55
+ undefined ,
56
+ index ,
57
+ )
58
+ ) ;
59
+ } , '' ) ;
51
60
52
- return `
53
- query {
54
- ${ computeObjectTargetTable ( options . objectMetadataItem ) } Collection${
55
- isEmpty ( duplicateCondition ?. or )
56
- ? '(first: 0)'
57
- : `(filter: ${ filters } )`
58
- } {
59
- ${ fieldsString }
60
- }
61
- }
62
- ` ;
61
+ return `query {
62
+ ${ query }
63
+ }` ;
63
64
}
64
65
65
- getFindDuplicateBy < Filter extends RecordFilter = RecordFilter > (
66
- args : FindDuplicatesResolverArgs < Filter > ,
66
+ buildQuery (
67
+ fieldsString : string ,
67
68
options : WorkspaceQueryBuilderOptions ,
68
- currentRecord ?: Record < string , unknown > ,
69
+ data ?: Record ,
70
+ currentRecord ?: Record ,
71
+ index ?: number ,
69
72
) {
70
- if ( currentRecord ) {
71
- return currentRecord ;
72
- }
73
+ const duplicateCondition =
74
+ this . duplicateService . buildDuplicateConditionForGraphQL (
75
+ options . objectMetadataItem ,
76
+ data ?? currentRecord ,
77
+ currentRecord ?. id ,
78
+ ) ;
73
79
74
- return this . argsAliasFactory . create (
75
- args . data ?? { } ,
76
- options . fieldMetadataCollection ,
77
- ) ;
80
+ const filters = stringifyWithoutKeyQuote ( duplicateCondition ) ;
81
+
82
+ return `${ computeObjectTargetTable (
83
+ options . objectMetadataItem ,
84
+ ) } Collection${ index } : ${ computeObjectTargetTable (
85
+ options . objectMetadataItem ,
86
+ ) } Collection${
87
+ isEmpty ( duplicateCondition ?. or ) ? '(first: 0)' : `(filter: ${ filters } )`
88
+ } {
89
+ ${ fieldsString }
90
+ }
91
+ ` ;
78
92
}
79
93
}
0 commit comments