Skip to content

Commit

Permalink
chore: update event search param of advance search based on scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil20 committed Nov 29, 2024
1 parent ee061c6 commit 70610aa
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 11 deletions.
22 changes: 16 additions & 6 deletions packages/gateway/src/features/search/root-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
} from '@gateway/features/user/utils'
import { GQLResolver } from '@gateway/graphql/schema'
import { Options } from '@hapi/boom'
import { ISearchCriteria, postAdvancedSearch } from './utils'
import {
filterSearchParamsWithScope,
ISearchCriteria,
postAdvancedSearch
} from './utils'
import { fetchRegistrationForDownloading } from '@gateway/workflow/index'
import { ApolloError } from 'apollo-server-hapi'
import { SCOPES } from '@opencrvs/commons/authentication'
Expand Down Expand Up @@ -89,10 +93,6 @@ export const resolvers: GQLResolver = {
},
{ headers: authHeader }
) {
const searchCriteria: ISearchCriteria = {
sort,
parameters: advancedSearchParameters
}
if (
!inScope(authHeader, [
SCOPES.SEARCH_BIRTH,
Expand All @@ -108,6 +108,16 @@ export const resolvers: GQLResolver = {
results: []
}

const filteredSearchParamsWithScopes = filterSearchParamsWithScope(
authHeader.Authorization,
advancedSearchParameters
)

const searchCriteria: ISearchCriteria = {
sort,
parameters: filteredSearchParamsWithScopes
}

if (count) {
searchCriteria.size = count
}
Expand Down Expand Up @@ -177,7 +187,7 @@ export const resolvers: GQLResolver = {
return await Promise.reject(new Error('There is no param to search '))
}

searchCriteria.parameters = { ...advancedSearchParameters }
searchCriteria.parameters = { ...filteredSearchParamsWithScopes }

const searchResult: ApiResponse<ISearchResponse<any>> =
await postAdvancedSearch(authHeader, searchCriteria)
Expand Down
67 changes: 66 additions & 1 deletion packages/gateway/src/features/search/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@
*/

import { SEARCH_URL } from '@gateway/constants'
import { GQLAdvancedSearchParametersInput } from '@gateway/graphql/schema'
import {
GQLAdvancedSearchParametersInput,
GQLEventType
} from '@gateway/graphql/schema'
import { IAuthHeader } from '@opencrvs/commons'
import fetch from '@gateway/fetch'
import {
getTokenPayload,
Scope,
SCOPES
} from '@opencrvs/commons/authentication'

export interface ISearchCriteria {
parameters: GQLAdvancedSearchParametersInput
Expand Down Expand Up @@ -44,3 +52,60 @@ export const postAdvancedSearch = async (
)
}
}

const addEventParamForScope = (
requiredScopes: Scope[],
event: GQLEventType,
tokenScopes: Scope[],
filteredParams: Omit<GQLAdvancedSearchParametersInput, 'event'> & {
event?: GQLEventType[]
},
searchParamEvent: GQLEventType | undefined
) => {
if (!tokenScopes.some((scope) => requiredScopes.includes(scope))) return

if (!searchParamEvent) {
filteredParams.event ??= []
filteredParams.event.push(event)
}
}

export const filterSearchParamsWithScope = (
token: string,
advancedSearchParameters: GQLAdvancedSearchParametersInput
): Omit<
GQLAdvancedSearchParametersInput,
'event' & {
event: GQLEventType[]
}
> => {
const { event: searchParamEvent, ...filteredParams } =
advancedSearchParameters
const currentUserScopes = getTokenPayload(token).scope

addEventParamForScope(
[SCOPES.SEARCH_BIRTH, SCOPES.SEARCH_BIRTH_MY_JURISDICTION],
GQLEventType.birth,
currentUserScopes,
filteredParams,
searchParamEvent
)

addEventParamForScope(
[SCOPES.SEARCH_DEATH, SCOPES.SEARCH_DEATH_MY_JURISDICTION],
GQLEventType.death,
currentUserScopes,
filteredParams,
searchParamEvent
)

addEventParamForScope(
[SCOPES.SEARCH_MARRIAGE, SCOPES.SEARCH_MARRIAGE_MY_JURISDICTION],
GQLEventType.marriage,
currentUserScopes,
filteredParams,
searchParamEvent
)

return filteredParams
}
2 changes: 1 addition & 1 deletion packages/search/src/features/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export enum SortOrder {
DESC = 'desc'
}
export interface IAdvancedSearchParam {
event?: string
event?: string[]
name?: string
registrationStatuses?: string[]
dateOfEvent?: string
Expand Down
6 changes: 3 additions & 3 deletions packages/search/src/features/search/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export async function advancedQueryBuilder(

const must: any[] = []

if (params.event) {
if (params.event && params.event.length > 0) {
must.push({
match: {
event: params.event
terms: {
'event.keyword': params.event
}
})
}
Expand Down

0 comments on commit 70610aa

Please sign in to comment.