Skip to content

Commit

Permalink
simplifying gateways' return values
Browse files Browse the repository at this point in the history
  • Loading branch information
noah-brunate committed Oct 29, 2024
1 parent d17cc56 commit 94b3485
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 211 deletions.
2 changes: 1 addition & 1 deletion packages/gateway/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const AVATAR_API =

export const NATIVE_LANGUAGE = (() => {
const languages = env.LANGUAGES.split(',')
return languages.find((language) => language !== 'en')
return languages.find((language: string) => language !== 'en')
})()

export const DEFAULT_TIMEOUT = 600000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ export const resolvers: GQLResolver = {
) {
// Only registrar or registration agent should be able to search user
if (!inScope(authHeader, ['register', 'validate'])) {
return await Promise.reject(
new Error(
'Advanced search is only allowed for registrar or registration agent'
)
throw new Error(
'Advanced search is only allowed for registrar or registration agent'
)
}

Expand All @@ -39,10 +37,8 @@ export const resolvers: GQLResolver = {
})

if (res.status !== 201) {
return await Promise.reject(
new Error(
`Something went wrong on user management service. Couldn't bookmark advanced search.`
)
throw new Error(
`Something went wrong on user management service. Couldn't bookmark advanced search.`
)
}
const response = await res.json()
Expand All @@ -55,10 +51,8 @@ export const resolvers: GQLResolver = {
) {
// Only registrar or registration agent should be able to search user
if (!inScope(authHeader, ['register', 'validate'])) {
return await Promise.reject(
new Error(
'Advanced search is only allowed for registrar or registration agent'
)
throw new Error(
'Advanced search is only allowed for registrar or registration agent'
)
}

Expand All @@ -77,10 +71,8 @@ export const resolvers: GQLResolver = {
})

if (res.status !== 200) {
return await Promise.reject(
new Error(
`Something went wrong on user management service. Couldn't unbookmarked advanced search.`
)
throw new Error(
`Something went wrong on user management service. Couldn't unbookmarked advanced search.`
)
}
const response = res.json()
Expand Down
4 changes: 1 addition & 3 deletions packages/gateway/src/features/metrics/root-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ export const resolvers: GQLResolver = {
results
}
} else {
return await Promise.reject(
new Error('User does not have the scope required for this resource')
)
throw new Error('User does not have the scope required for this resource')
}
},
async getTotalPayments(
Expand Down
89 changes: 24 additions & 65 deletions packages/gateway/src/features/registration/root-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
markNotADuplicate
} from '@gateway/workflow/index'
import { getRecordById } from '@gateway/records'
import { Scope } from '@opencrvs/commons/authentication'

async function getAnonymousToken() {
const res = await fetch(new URL('/anonymous-token', AUTH_URL).toString())
Expand All @@ -83,9 +84,7 @@ export const resolvers: GQLResolver = {
{ headers: authHeader }
): Promise<Saved<Bundle>[]> {
if (!hasScope(authHeader, 'sysadmin')) {
return await Promise.reject(
new Error('User does not have a sysadmin scope')
)
throw new Error('User does not have a sysadmin scope')
}
const res = await fetchFHIR<Saved<Bundle<Saved<Composition>>>>(
`/Composition?date=gt${fromDate.toISOString()}&date=lte${toDate.toISOString()}&_count=0`,
Expand All @@ -110,9 +109,7 @@ export const resolvers: GQLResolver = {
{ headers: authHeader }
): Promise<Saved<Bundle>[]> {
if (!hasScope(authHeader, 'sysadmin')) {
return await Promise.reject(
new Error('User does not have a sysadmin scope')
)
throw new Error('User does not have a sysadmin scope')
}
const res = await fetchFHIR<Saved<Bundle<Saved<Composition>>>>(
`/Composition?date=gt${fromDate.toISOString()}&date=lte${toDate.toISOString()}&_count=0`,
Expand Down Expand Up @@ -143,9 +140,7 @@ export const resolvers: GQLResolver = {
)
return context.record
} else {
return await Promise.reject(
new Error('User does not have a register or validate scope')
)
throw new Error('User does not have a register or validate scope')
}
},
async fetchDeathRegistration(_, { id }, context): Promise<Saved<Bundle>> {
Expand All @@ -160,9 +155,7 @@ export const resolvers: GQLResolver = {
)
return context.record
} else {
return await Promise.reject(
new Error('User does not have a register or validate scope')
)
throw new Error('User does not have a register or validate scope')
}
},
async fetchMarriageRegistration(
Expand All @@ -181,9 +174,7 @@ export const resolvers: GQLResolver = {
)
return context.record
} else {
return await Promise.reject(
new Error('User does not have a register or validate scope')
)
throw new Error('User does not have a register or validate scope')
}
},
async queryRegistrationByIdentifier(
Expand Down Expand Up @@ -211,9 +202,7 @@ export const resolvers: GQLResolver = {

return await fetchFHIR(`/${task.focus.reference}`, authHeader)
} else {
return await Promise.reject(
new Error('User does not have a register or validate scope')
)
throw new Error('User does not have a register or validate scope')
}
},
async fetchRegistration(_, { id }, context): Promise<Saved<Bundle>> {
Expand Down Expand Up @@ -246,9 +235,7 @@ export const resolvers: GQLResolver = {

return person
} else {
return await Promise.reject(
new Error('User does not have enough scope')
)
throw new Error('User does not have enough scope')
}
},
async queryPersonByNidIdentifier(
Expand Down Expand Up @@ -279,9 +266,7 @@ export const resolvers: GQLResolver = {
return response.data
}
} else {
return await Promise.reject(
new Error('User does not have enough scope')
)
throw new Error('User does not have enough scope')
}
},
async fetchRegistrationCountByStatus(
Expand Down Expand Up @@ -334,9 +319,7 @@ export const resolvers: GQLResolver = {
total
}
} else {
return await Promise.reject(
new Error('User does not have enough scope')
)
throw new Error('User does not have enough scope')
}
},
async fetchRecordDetailsForVerification(_, { id }, context) {
Expand Down Expand Up @@ -382,9 +365,7 @@ export const resolvers: GQLResolver = {
throw new UnassignError('User has been unassigned')
}
if (!hasScope(authHeader, 'validate')) {
return await Promise.reject(
new Error('User does not have a validate scope')
)
throw new Error('User does not have a validate scope')
} else {
return await markEventAsValidated(
id,
Expand All @@ -400,9 +381,7 @@ export const resolvers: GQLResolver = {
throw new UnassignError('User has been unassigned')
}
if (!hasScope(authHeader, 'validate')) {
return await Promise.reject(
new Error('User does not have a validate scope')
)
throw new Error('User does not have a validate scope')
}
return await markEventAsValidated(
id,
Expand All @@ -417,9 +396,7 @@ export const resolvers: GQLResolver = {
throw new UnassignError('User has been unassigned')
}
if (!hasScope(authHeader, 'validate')) {
return await Promise.reject(
new Error('User does not have a validate scope')
)
throw new Error('User does not have a validate scope')
} else {
return await markEventAsValidated(
id,
Expand All @@ -437,9 +414,7 @@ export const resolvers: GQLResolver = {
if (hasScope(authHeader, 'register')) {
return markEventAsRegistered(id, authHeader, EVENT_TYPE.BIRTH, details)
} else {
return await Promise.reject(
new Error('User does not have a register scope')
)
throw new Error('User does not have a register scope')
}
},
async markDeathAsRegistered(_, { id, details }, { headers: authHeader }) {
Expand All @@ -450,9 +425,7 @@ export const resolvers: GQLResolver = {
if (hasScope(authHeader, 'register')) {
return markEventAsRegistered(id, authHeader, EVENT_TYPE.DEATH, details)
} else {
return await Promise.reject(
new Error('User does not have a register scope')
)
throw new Error('User does not have a register scope')
}
},
async markMarriageAsRegistered(
Expand All @@ -472,9 +445,7 @@ export const resolvers: GQLResolver = {
details
)
} else {
return await Promise.reject(
new Error('User does not have a register scope')
)
throw new Error('User does not have a register scope')
}
},
async markEventAsVoided(
Expand All @@ -487,13 +458,11 @@ export const resolvers: GQLResolver = {
throw new UnassignError('User has been unassigned')
}
if (!inScope(authHeader, ['register', 'validate'])) {
return await Promise.reject(
new Error('User does not have a register or validate scope')
)
throw new Error('User does not have a register or validate scope')
}
const taskEntry = await rejectDeclaration(id, authHeader, reason, comment)
if (!taskEntry) {
return await Promise.reject(new Error('Task not found'))
throw new Error('Task not found')
}

// return the taskId
Expand All @@ -509,9 +478,7 @@ export const resolvers: GQLResolver = {
throw new UnassignError('User has been unassigned')
}
if (!inScope(authHeader, ['register', 'validate'])) {
return await Promise.reject(
new Error('User does not have a register or validate scope')
)
throw new Error('User does not have a register or validate scope')
}
const taskEntry = await archiveRegistration(
id,
Expand All @@ -529,9 +496,7 @@ export const resolvers: GQLResolver = {
throw new UnassignError('User has been unassigned')
}
if (!inScope(authHeader, ['register', 'validate'])) {
return await Promise.reject(
new Error('User does not have a register or validate scope')
)
throw new Error('User does not have a register or validate scope')
}

const { taskId, prevRegStatus } = await reinstateRegistration(
Expand Down Expand Up @@ -593,16 +558,12 @@ export const resolvers: GQLResolver = {

return composition.id
} else {
return await Promise.reject(
new Error('User does not have a register scope')
)
throw new Error('User does not have a register scope')
}
},
async markEventAsUnassigned(_, { id }, { headers: authHeader }) {
if (!inScope(authHeader, ['register', 'validate'])) {
return await Promise.reject(
new Error('User does not have a register or validate scope')
)
throw new Error('User does not have a register or validate scope')
}
const task = getTaskFromSavedBundle(
await unassignRegistration(id, authHeader)
Expand All @@ -621,9 +582,7 @@ export const resolvers: GQLResolver = {
throw new UnassignError('User has been unassigned')
}
if (!inScope(authHeader, ['register', 'validate'])) {
return await Promise.reject(
new Error('User does not have a register or validate scope')
)
throw new Error('User does not have a register or validate scope')
}

const taskEntry = await duplicateRegistration(
Expand All @@ -637,7 +596,7 @@ export const resolvers: GQLResolver = {
return taskEntry.resource.id
},
async confirmRegistration(_, { id }, { headers: authHeader }) {
if (!inScope(authHeader, ['record.confirm-registration'])) {
if (!inScope(authHeader, ['record.confirm-registration'] as unknown as Scope[])) {
throw new Error(
'User does not have a "record.confirm-registration" scope'
)
Expand Down
16 changes: 6 additions & 10 deletions packages/gateway/src/features/search/root-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ export const resolvers: GQLResolver = {
'recordsearch'
])
) {
return await Promise.reject(
new Error(
'Advanced search is only allowed for registrar, registration agent & field agent'
)
throw new Error(
'Advanced search is only allowed for registrar, registration agent & field agent'
)
}

Expand Down Expand Up @@ -146,7 +144,7 @@ export const resolvers: GQLResolver = {

if ((searchResult?.statusCode ?? 0) >= 400) {
const errMsg = searchResult as Options<string>
return await Promise.reject(new Error(errMsg.message))
throw new Error(errMsg.message)
}

;(searchResult.body.hits.hits || []).forEach(async (hit) => {
Expand Down Expand Up @@ -175,7 +173,7 @@ export const resolvers: GQLResolver = {
)

if (!hasAtLeastOneParam) {
return await Promise.reject(new Error('There is no param to search '))
throw new Error('There is no param to search ')
}

searchCriteria.parameters = { ...advancedSearchParameters }
Expand All @@ -201,10 +199,8 @@ export const resolvers: GQLResolver = {
{ headers: authHeader }
) {
if (!inScope(authHeader, ['sysadmin', 'register', 'validate'])) {
return await Promise.reject(
new Error(
'User does not have a sysadmin or register or validate scope'
)
throw new Error(
'User does not have a sysadmin or register or validate scope'
)
}

Expand Down
4 changes: 1 addition & 3 deletions packages/gateway/src/features/search/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export const postAdvancedSearch = async (
})
return await response.json()
} catch (error) {
return await Promise.reject(
new Error(`Search request failed: ${error.message}`)
)
throw new Error(`Search request failed: ${error.message}`)
}
}
Loading

0 comments on commit 94b3485

Please sign in to comment.