Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading