Skip to content

Commit

Permalink
chore: amend gateway search communication payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil20 committed Dec 10, 2024
1 parent 2db4f53 commit 35937ac
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 147 deletions.
3 changes: 1 addition & 2 deletions packages/gateway/src/features/search/root-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ export const resolvers: GQLResolver = {
const filteredSearchParamsWithScopes = filterSearchParamsWithScope(
authHeader.Authorization,
advancedSearchParameters,
user,
advancedSearchParameters.event
user
)

const searchCriteria: ISearchCriteria = {
Expand Down
82 changes: 31 additions & 51 deletions packages/gateway/src/features/search/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { IUserModelData } from '@gateway/features/user/type-resolvers'

export interface ISearchCriteria {
parameters: GQLAdvancedSearchParametersInput
parameters: SearchParamsType
sort?: string
sortColumn?: string
sortBy?: Array<Record<string, string>>
Expand All @@ -51,37 +51,39 @@ export const postAdvancedSearch = async (
throw new Error(`Search request failed: ${error.message}`)
}
}
type SearchParamsType = Omit<GQLAdvancedSearchParametersInput, 'event'> & {
event?: {
eventName: string
jurisdictionId?: string
}[]
}

const addSearchParamForScope = (
requiredScopes: Scope[],
tokenScopes: Scope[],
advancedSearchParameters: GQLAdvancedSearchParametersInput & {
birthJurisdictionId?: string
deathJurisdictionId?: string
marriageJurisdictionId?: string
advancedSearchParameters: Omit<GQLAdvancedSearchParametersInput, 'event'> & {
event?: {
eventName: string
jurisdictionId?: string
}[]
},
user: IUserModelData,
event?: GQLEventType
event: GQLEventType | undefined
) => {
const { declarationJurisdictionId, declarationLocationId } =
advancedSearchParameters
if (!tokenScopes.some((scope) => requiredScopes.includes(scope))) return

const scopeMappings = {
[GQLEventType.birth]: {
jurisdictionScope: SCOPES.SEARCH_BIRTH_MY_JURISDICTION,
generalScope: SCOPES.SEARCH_BIRTH,
jurisdictionIdField: 'birthJurisdictionId' as const
generalScope: SCOPES.SEARCH_BIRTH
},
[GQLEventType.death]: {
jurisdictionScope: SCOPES.SEARCH_DEATH_MY_JURISDICTION,
generalScope: SCOPES.SEARCH_DEATH,
jurisdictionIdField: 'deathJurisdictionId' as const
generalScope: SCOPES.SEARCH_DEATH
},
[GQLEventType.marriage]: {
jurisdictionScope: SCOPES.SEARCH_MARRIAGE_MY_JURISDICTION,
generalScope: SCOPES.SEARCH_MARRIAGE,
jurisdictionIdField: 'marriageJurisdictionId' as const
generalScope: SCOPES.SEARCH_MARRIAGE
}
}

Expand All @@ -98,53 +100,31 @@ const addSearchParamForScope = (
}

if (event) {
const { jurisdictionScope, generalScope, jurisdictionIdField } =
scopeMappings[event]
const { jurisdictionScope, generalScope } = scopeMappings[event]

if (tokenScopes.includes(jurisdictionScope)) {
advancedSearchParameters[jurisdictionIdField] = user.primaryOfficeId
advancedSearchParameters.event ??= []
advancedSearchParameters.event.push({
eventName: event,
jurisdictionId: user.primaryOfficeId
})
} else if (tokenScopes.includes(generalScope)) {
advancedSearchParameters.event = event
}
advancedSearchParameters.event ??= []
advancedSearchParameters.event.push({ eventName: event })
} else throw new Error('Not enough scope to search')
}
}

export const filterSearchParamsWithScope = (
token: string,
advancedSearchParameters: GQLAdvancedSearchParametersInput,
user: IUserModelData,
eventType?: GQLEventType
): Omit<
GQLAdvancedSearchParametersInput,
'event' & {
event: GQLEventType[]
}
> => {
user: IUserModelData
): SearchParamsType => {
const currentUserScopes = getTokenPayload(token).scope
const { event: eventType, ...restOfParams } = advancedSearchParameters
const newParams: SearchParamsType = restOfParams

addSearchParamForScope(
[SCOPES.SEARCH_BIRTH, SCOPES.SEARCH_BIRTH_MY_JURISDICTION],
currentUserScopes,
advancedSearchParameters,
user,
eventType
)

addSearchParamForScope(
[SCOPES.SEARCH_DEATH, SCOPES.SEARCH_DEATH_MY_JURISDICTION],
currentUserScopes,
advancedSearchParameters,
user,
eventType
)

addSearchParamForScope(
[SCOPES.SEARCH_MARRIAGE, SCOPES.SEARCH_MARRIAGE_MY_JURISDICTION],
currentUserScopes,
advancedSearchParameters,
user,
eventType
)
addSearchParamForScope(currentUserScopes, newParams, user, eventType)

return advancedSearchParameters
return newParams
}
11 changes: 7 additions & 4 deletions packages/search/src/features/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ export enum SortOrder {
ASC = 'asc',
DESC = 'desc'
}

type EventPayload = {
eventName: string
jurisdictionId?: string
}

export interface IAdvancedSearchParam {
event?: string
event?: EventPayload[]
name?: string
registrationStatuses?: string[]
dateOfEvent?: string
Expand Down Expand Up @@ -92,9 +98,6 @@ export interface IAdvancedSearchParam {
brideDoBEnd?: string
brideIdentifier?: string
dateOfMarriage?: string
birthJurisdictionId?: UUID
deathJurisdictionId?: UUID
marriageJurisdictionId?: UUID
}

export interface ISearchCriteria {
Expand Down
118 changes: 28 additions & 90 deletions packages/search/src/features/search/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,99 +61,37 @@ export async function advancedQueryBuilder(
})
}

if (
params.birthJurisdictionId ||
params.deathJurisdictionId ||
params.marriageJurisdictionId ||
params.event
) {
let leafLevelJurisdictionIds: UUID[] = []

const jurisdictionId: UUID | null =
params.birthJurisdictionId ??
params.deathJurisdictionId ??
params.marriageJurisdictionId ??
null

if (jurisdictionId) {
leafLevelJurisdictionIds = await resolveLocationChildren(jurisdictionId)
}

if (params.event && params.event.length > 0) {
const shouldConditions: any[] = []

if (params.birthJurisdictionId) {
shouldConditions.push({
bool: {
must: [
{
terms: {
'event.keyword': ['birth']
}
},
{
terms: {
'declarationJurisdictionIds.keyword': leafLevelJurisdictionIds
}
}
]
}
})
} else if (params.event === 'birth') {
shouldConditions.push({
terms: {
'event.keyword': ['birth']
}
})
}

if (params.deathJurisdictionId) {
shouldConditions.push({
bool: {
must: [
{
terms: {
'event.keyword': ['death']
}
},
{
terms: {
'declarationJurisdictionIds.keyword': leafLevelJurisdictionIds
for (const { eventName, jurisdictionId } of params.event) {
if (jurisdictionId) {
const leafLevelJurisdictionIds = await resolveLocationChildren(
jurisdictionId as UUID
)
shouldConditions.push({
bool: {
must: [
{
terms: {
'event.keyword': [eventName]
}
},
{
terms: {
'declarationJurisdictionIds.keyword': leafLevelJurisdictionIds
}
}
}
]
}
})
} else if (params.event === 'death') {
shouldConditions.push({
terms: {
'event.keyword': ['death']
}
})
}

if (params.marriageJurisdictionId) {
shouldConditions.push({
bool: {
must: [
{
terms: {
'event.keyword': ['marriage']
}
},
{
terms: {
'declarationJurisdictionIds.keyword': leafLevelJurisdictionIds
}
}
]
}
})
} else if (params.event === 'marriage') {
shouldConditions.push({
terms: {
'event.keyword': ['marriage']
}
})
]
}
})
} else if (eventName) {
shouldConditions.push({
terms: {
'event.keyword': [eventName]
}
})
}
}

must.push({
Expand Down

0 comments on commit 35937ac

Please sign in to comment.