From d386da4ac559fb59bfe57755304967941a597c18 Mon Sep 17 00:00:00 2001 From: amit-s19 Date: Thu, 28 Dec 2023 16:38:05 +0530 Subject: [PATCH] Search Query Fix --- src/admin/admin.controller.ts | 1 + .../query-generator.service.ts | 115 +++++++++--------- 2 files changed, 56 insertions(+), 60 deletions(-) diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index c85e737..3864cfa 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -87,6 +87,7 @@ export class AdminController { @Get('/searchUser') @Roles( 'Admin', + 'super_admin_department', 'school', 'State Admin', 'District Admin', diff --git a/src/api/fusionauth/query-generator/query-generator.service.ts b/src/api/fusionauth/query-generator/query-generator.service.ts index d28cf12..6fe18d5 100644 --- a/src/api/fusionauth/query-generator/query-generator.service.ts +++ b/src/api/fusionauth/query-generator/query-generator.service.ts @@ -2,78 +2,73 @@ import { Injectable } from '@nestjs/common'; @Injectable() export class QueryGeneratorService { - constructor(){} + constructor() { } - queryUsersByApplicationId(applicationId: string): string{ - const query = { - bool: { - must: [ - { - nested: { - path: "registrations", - query: { - bool: { - must: [ - { - match: { - "registrations.applicationId": applicationId - } - } - ] + queryUsersByApplicationId(applicationId: string): string { + const query = { + bool: { + must: [ + { + nested: { + path: "registrations", + query: { + bool: { + must: [ + { + match: { + "registrations.applicationId": applicationId } } - } + ] } - ] + } } } - return JSON.stringify(query); + ] + } } + 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) - } - } - } - } - ] - ] - } - }, - { - query_string: { - query: queryString - } - } - ] + queryUsersByApplicationIdAndQueryString(applicationId: string[], queryString: string): string { + const query: any = { + "bool": { + "must": [{ + "nested": { + "path": "registrations", + "query": { + "bool": { + "must": this.createMatchTags(applicationId) + } } } - 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 - } - } - ) + if (queryString) { + query.bool.must.push( + { + "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; + ) } + return tags; + } }