Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifying gateways' return values #7863

Merged
merged 10 commits into from
Nov 26, 2024
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
86 changes: 22 additions & 64 deletions packages/gateway/src/features/registration/root-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,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 @@ -112,9 +110,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 @@ -145,9 +141,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 @@ -162,9 +156,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 @@ -183,9 +175,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 @@ -213,9 +203,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 @@ -248,9 +236,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 @@ -281,9 +267,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 @@ -336,9 +320,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 @@ -384,9 +366,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 @@ -402,9 +382,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 @@ -419,9 +397,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 @@ -439,9 +415,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 @@ -452,9 +426,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 @@ -474,9 +446,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 @@ -489,13 +459,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 @@ -511,9 +479,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 @@ -531,9 +497,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 @@ -595,16 +559,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 @@ -623,9 +583,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 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}`)
}
}
14 changes: 4 additions & 10 deletions packages/gateway/src/features/systems/root-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ export const resolvers: GQLResolver = {
},
async deactivateSystem(_, { clientId }, { headers: authHeader }) {
if (!hasScope(authHeader, 'natlsysadmin')) {
return await Promise.reject(
new Error('Deactivate user is only allowed for natlsysadmin')
)
throw new Error('Deactivate user is only allowed for natlsysadmin')
}
const res = await fetch(`${USER_MANAGEMENT_URL}deactivateSystem`, {
method: 'POST',
Expand Down Expand Up @@ -78,10 +76,8 @@ export const resolvers: GQLResolver = {
})

if (res.status !== 201) {
return await Promise.reject(
new Error(
`Something went wrong on user management service. Couldn't register new system`
)
throw new Error(
`Something went wrong on user management service. Couldn't register new system`
)
}
return res.json()
Expand Down Expand Up @@ -154,9 +150,7 @@ export const resolvers: GQLResolver = {
Query: {
async fetchSystem(_, { clientId }, { headers: authHeader }) {
if (authHeader && !hasScope(authHeader, 'natlsysadmin')) {
return await Promise.reject(
new Error('Fetch integration is only allowed for natlsysadmin')
)
throw new Error('Fetch integration is only allowed for natlsysadmin')
}

return getSystem({ clientId }, authHeader)
Expand Down
Loading
Loading