diff --git a/src/api/fusionauth/query-generator/query-generator.service.spec.ts b/src/api/fusionauth/query-generator/query-generator.service.spec.ts index 46bfe9f..d7f0a7b 100644 --- a/src/api/fusionauth/query-generator/query-generator.service.spec.ts +++ b/src/api/fusionauth/query-generator/query-generator.service.spec.ts @@ -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], @@ -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({ @@ -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); + }); }); diff --git a/src/api/fusionauth/query-generator/query-generator.service.ts b/src/api/fusionauth/query-generator/query-generator.service.ts index d28cf12..3ac7574 100644 --- a/src/api/fusionauth/query-generator/query-generator.service.ts +++ b/src/api/fusionauth/query-generator/query-generator.service.ts @@ -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; + } }