Skip to content

Commit

Permalink
Fix: Query String Generated by Service returns Error (#99)
Browse files Browse the repository at this point in the history
* updated query builder

* updated query tests
  • Loading branch information
KDwevedi authored Nov 6, 2024
1 parent eeaf61e commit 74b9850
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 105 deletions.
83 changes: 42 additions & 41 deletions src/api/fusionauth/query-generator/query-generator.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { QueryGeneratorService } from './query-generator.service';

describe('QueryGeneratorService', () => {
let service: QueryGeneratorService;
const applicationId = "1234-1234-1234-1234";
const applicationIds = ["1234-1234-1234-1234", "1234-1234-1234-1234"];
const queryString = "test";
const applicationId = '1234-1234-1234-1234';
const applicationIds = ['1234-1234-1234-1234', '1234-1234-1234-1234'];
const queryString = 'test';

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [QueryGeneratorService],
Expand All @@ -25,28 +25,27 @@ describe('QueryGeneratorService', () => {
must: [
{
nested: {
path: "registrations",
path: 'registrations',
query: {
bool: {
must: [
{
match: {
"registrations.applicationId": applicationId
}
}
]
}
}
}
}
]
}
})
'registrations.applicationId': applicationId,
},
},
],
},
},
},
},
],
},
});

jest.spyOn(service, 'queryUsersByApplicationId');
expect(service.queryUsersByApplicationId(applicationId)).toBe(result);

})
});

it('should create queryUsersByApplicationIdAndQueryString query', () => {
const result = JSON.stringify({
Expand All @@ -55,32 +54,34 @@ describe('QueryGeneratorService', () => {
{
bool: {
must: [
[
{
nested: {
path: "registrations",
query: {
bool: {
should: service.createMatchTags(applicationIds)
}
}
}
}
]
]
}
{
nested: {
path: 'registrations',
query: {
bool: {
should: service.createMatchTags(applicationIds),
},
},
},
},
],
},
},
{
query_string: {
query: queryString
}
}
]
}
})
query: queryString,
},
},
],
},
});

jest.spyOn(service, 'queryUsersByApplicationIdAndQueryString');
expect(service.queryUsersByApplicationIdAndQueryString(applicationIds, queryString)).toBe(result);

})
expect(
service.queryUsersByApplicationIdAndQueryString(
applicationIds,
queryString,
),
).toBe(result);
});
});
127 changes: 63 additions & 64 deletions src/api/fusionauth/query-generator/query-generator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,77 @@ import { Injectable } from '@nestjs/common';

@Injectable()
export class QueryGeneratorService {
constructor(){}
constructor() {}

queryUsersByApplicationId(applicationId: string): string{
const query = {
queryUsersByApplicationId(applicationId: string): string {
const query = {
bool: {
must: [
{
nested: {
path: 'registrations',
query: {
bool: {
must: [
{
match: {
'registrations.applicationId': applicationId,
},
},
],
},
},
},
},
],
},
};
return JSON.stringify(query);
}

queryUsersByApplicationIdAndQueryString(
applicationId: string[],
queryString: string,
): string {
const query = {
bool: {
must: [
{
bool: {
must: [
{
nested: {
path: "registrations",
path: 'registrations',
query: {
bool: {
must: [
{
match: {
"registrations.applicationId": applicationId
}
}
]
}
}
}
}
]
}
}
return JSON.stringify(query);
}

queryUsersByApplicationIdAndQueryString(applicationId: string[], queryString: string): string{
const query = {
bool: {
must: [
{
bool: {
must: [
[
{
nested: {
path: "registrations",
query: {
bool: {
should: this.createMatchTags(applicationId)
}
}
}
}
]
]
}
should: this.createMatchTags(applicationId),
},
},
},
},
{
query_string: {
query: queryString
}
}
]
}
}
return JSON.stringify(query)
}
],
},
},
{
query_string: {
query: queryString,
},
},
],
},
};
return JSON.stringify(query);
}

createMatchTags(arr: string[]): Array<{match: any}>{
let tags: Array<{match: any}> = [];
for(let x of arr){
tags.push(
{
match: {
"registrations.applicationId": x
}
}
)
}
return tags;
createMatchTags(arr: string[]): Array<{ match: any }> {
const tags: Array<{ match: any }> = [];
for (const x of arr) {
tags.push({
match: {
'registrations.applicationId': x,
},
});
}
return tags;
}
}

0 comments on commit 74b9850

Please sign in to comment.