Skip to content

Commit 74b9850

Browse files
authored
Fix: Query String Generated by Service returns Error (#99)
* updated query builder * updated query tests
1 parent eeaf61e commit 74b9850

File tree

2 files changed

+105
-105
lines changed

2 files changed

+105
-105
lines changed

src/api/fusionauth/query-generator/query-generator.service.spec.ts

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { QueryGeneratorService } from './query-generator.service';
33

44
describe('QueryGeneratorService', () => {
55
let service: QueryGeneratorService;
6-
const applicationId = "1234-1234-1234-1234";
7-
const applicationIds = ["1234-1234-1234-1234", "1234-1234-1234-1234"];
8-
const queryString = "test";
9-
6+
const applicationId = '1234-1234-1234-1234';
7+
const applicationIds = ['1234-1234-1234-1234', '1234-1234-1234-1234'];
8+
const queryString = 'test';
9+
1010
beforeEach(async () => {
1111
const module: TestingModule = await Test.createTestingModule({
1212
providers: [QueryGeneratorService],
@@ -25,28 +25,27 @@ describe('QueryGeneratorService', () => {
2525
must: [
2626
{
2727
nested: {
28-
path: "registrations",
28+
path: 'registrations',
2929
query: {
3030
bool: {
3131
must: [
3232
{
3333
match: {
34-
"registrations.applicationId": applicationId
35-
}
36-
}
37-
]
38-
}
39-
}
40-
}
41-
}
42-
]
43-
}
44-
})
34+
'registrations.applicationId': applicationId,
35+
},
36+
},
37+
],
38+
},
39+
},
40+
},
41+
},
42+
],
43+
},
44+
});
4545

4646
jest.spyOn(service, 'queryUsersByApplicationId');
4747
expect(service.queryUsersByApplicationId(applicationId)).toBe(result);
48-
49-
})
48+
});
5049

5150
it('should create queryUsersByApplicationIdAndQueryString query', () => {
5251
const result = JSON.stringify({
@@ -55,32 +54,34 @@ describe('QueryGeneratorService', () => {
5554
{
5655
bool: {
5756
must: [
58-
[
59-
{
60-
nested: {
61-
path: "registrations",
62-
query: {
63-
bool: {
64-
should: service.createMatchTags(applicationIds)
65-
}
66-
}
67-
}
68-
}
69-
]
70-
]
71-
}
57+
{
58+
nested: {
59+
path: 'registrations',
60+
query: {
61+
bool: {
62+
should: service.createMatchTags(applicationIds),
63+
},
64+
},
65+
},
66+
},
67+
],
68+
},
7269
},
7370
{
7471
query_string: {
75-
query: queryString
76-
}
77-
}
78-
]
79-
}
80-
})
72+
query: queryString,
73+
},
74+
},
75+
],
76+
},
77+
});
8178

8279
jest.spyOn(service, 'queryUsersByApplicationIdAndQueryString');
83-
expect(service.queryUsersByApplicationIdAndQueryString(applicationIds, queryString)).toBe(result);
84-
85-
})
80+
expect(
81+
service.queryUsersByApplicationIdAndQueryString(
82+
applicationIds,
83+
queryString,
84+
),
85+
).toBe(result);
86+
});
8687
});

src/api/fusionauth/query-generator/query-generator.service.ts

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,77 @@ import { Injectable } from '@nestjs/common';
22

33
@Injectable()
44
export class QueryGeneratorService {
5-
constructor(){}
5+
constructor() {}
66

7-
queryUsersByApplicationId(applicationId: string): string{
8-
const query = {
7+
queryUsersByApplicationId(applicationId: string): string {
8+
const query = {
9+
bool: {
10+
must: [
11+
{
12+
nested: {
13+
path: 'registrations',
14+
query: {
15+
bool: {
16+
must: [
17+
{
18+
match: {
19+
'registrations.applicationId': applicationId,
20+
},
21+
},
22+
],
23+
},
24+
},
25+
},
26+
},
27+
],
28+
},
29+
};
30+
return JSON.stringify(query);
31+
}
32+
33+
queryUsersByApplicationIdAndQueryString(
34+
applicationId: string[],
35+
queryString: string,
36+
): string {
37+
const query = {
38+
bool: {
39+
must: [
40+
{
941
bool: {
1042
must: [
1143
{
1244
nested: {
13-
path: "registrations",
45+
path: 'registrations',
1446
query: {
1547
bool: {
16-
must: [
17-
{
18-
match: {
19-
"registrations.applicationId": applicationId
20-
}
21-
}
22-
]
23-
}
24-
}
25-
}
26-
}
27-
]
28-
}
29-
}
30-
return JSON.stringify(query);
31-
}
32-
33-
queryUsersByApplicationIdAndQueryString(applicationId: string[], queryString: string): string{
34-
const query = {
35-
bool: {
36-
must: [
37-
{
38-
bool: {
39-
must: [
40-
[
41-
{
42-
nested: {
43-
path: "registrations",
44-
query: {
45-
bool: {
46-
should: this.createMatchTags(applicationId)
47-
}
48-
}
49-
}
50-
}
51-
]
52-
]
53-
}
48+
should: this.createMatchTags(applicationId),
49+
},
50+
},
51+
},
5452
},
55-
{
56-
query_string: {
57-
query: queryString
58-
}
59-
}
60-
]
61-
}
62-
}
63-
return JSON.stringify(query)
64-
}
53+
],
54+
},
55+
},
56+
{
57+
query_string: {
58+
query: queryString,
59+
},
60+
},
61+
],
62+
},
63+
};
64+
return JSON.stringify(query);
65+
}
6566

66-
createMatchTags(arr: string[]): Array<{match: any}>{
67-
let tags: Array<{match: any}> = [];
68-
for(let x of arr){
69-
tags.push(
70-
{
71-
match: {
72-
"registrations.applicationId": x
73-
}
74-
}
75-
)
76-
}
77-
return tags;
67+
createMatchTags(arr: string[]): Array<{ match: any }> {
68+
const tags: Array<{ match: any }> = [];
69+
for (const x of arr) {
70+
tags.push({
71+
match: {
72+
'registrations.applicationId': x,
73+
},
74+
});
7875
}
76+
return tags;
77+
}
7978
}

0 commit comments

Comments
 (0)