diff --git a/packages/gateway/src/features/bookmarkAdvancedSearch/root-resolvers.ts b/packages/gateway/src/features/bookmarkAdvancedSearch/root-resolvers.ts index 9b5dd4cb46e..33a4eca1806 100644 --- a/packages/gateway/src/features/bookmarkAdvancedSearch/root-resolvers.ts +++ b/packages/gateway/src/features/bookmarkAdvancedSearch/root-resolvers.ts @@ -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' ) } @@ -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() @@ -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' ) } @@ -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() diff --git a/packages/gateway/src/features/metrics/root-resolvers.ts b/packages/gateway/src/features/metrics/root-resolvers.ts index 3c030b2b7bb..fd0e41ecaf4 100644 --- a/packages/gateway/src/features/metrics/root-resolvers.ts +++ b/packages/gateway/src/features/metrics/root-resolvers.ts @@ -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( diff --git a/packages/gateway/src/features/registration/root-resolvers.ts b/packages/gateway/src/features/registration/root-resolvers.ts index d241a6289dc..7f2b4ce9b04 100644 --- a/packages/gateway/src/features/registration/root-resolvers.ts +++ b/packages/gateway/src/features/registration/root-resolvers.ts @@ -85,9 +85,7 @@ export const resolvers: GQLResolver = { { headers: authHeader } ): Promise[]> { 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>>>( `/Composition?date=gt${fromDate.toISOString()}&date=lte${toDate.toISOString()}&_count=0`, @@ -112,9 +110,7 @@ export const resolvers: GQLResolver = { { headers: authHeader } ): Promise[]> { 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>>>( `/Composition?date=gt${fromDate.toISOString()}&date=lte${toDate.toISOString()}&_count=0`, @@ -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> { @@ -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( @@ -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( @@ -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> { @@ -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( @@ -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( @@ -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) { @@ -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, @@ -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, @@ -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, @@ -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 }) { @@ -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( @@ -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( @@ -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 @@ -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, @@ -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( @@ -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) @@ -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( diff --git a/packages/gateway/src/features/search/root-resolvers.ts b/packages/gateway/src/features/search/root-resolvers.ts index 7a7e46f4999..740ec70b96c 100644 --- a/packages/gateway/src/features/search/root-resolvers.ts +++ b/packages/gateway/src/features/search/root-resolvers.ts @@ -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' ) } @@ -146,7 +144,7 @@ export const resolvers: GQLResolver = { if ((searchResult?.statusCode ?? 0) >= 400) { const errMsg = searchResult as Options - return await Promise.reject(new Error(errMsg.message)) + throw new Error(errMsg.message) } ;(searchResult.body.hits.hits || []).forEach(async (hit) => { @@ -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 } @@ -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' ) } diff --git a/packages/gateway/src/features/search/utils.ts b/packages/gateway/src/features/search/utils.ts index 620390ff475..04fd62dd7bb 100644 --- a/packages/gateway/src/features/search/utils.ts +++ b/packages/gateway/src/features/search/utils.ts @@ -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}`) } } diff --git a/packages/gateway/src/features/systems/root-resolvers.ts b/packages/gateway/src/features/systems/root-resolvers.ts index 6183555e1eb..cf93e326814 100644 --- a/packages/gateway/src/features/systems/root-resolvers.ts +++ b/packages/gateway/src/features/systems/root-resolvers.ts @@ -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', @@ -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() @@ -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) diff --git a/packages/gateway/src/features/user/root-resolvers.ts b/packages/gateway/src/features/user/root-resolvers.ts index 4b3e213f3cd..7e9d616c08d 100644 --- a/packages/gateway/src/features/user/root-resolvers.ts +++ b/packages/gateway/src/features/user/root-resolvers.ts @@ -80,10 +80,8 @@ export const resolvers: GQLResolver = { ) => { // Only sysadmin or registrar or registration agent should be able to search user if (!inScope(authHeader, ['sysadmin', 'register', 'validate'])) { - return await Promise.reject( - new Error( - 'Search user is only allowed for sysadmin or registrar or registration agent' - ) + throw new Error( + 'Search user is only allowed for sysadmin or registrar or registration agent' ) } @@ -142,10 +140,8 @@ export const resolvers: GQLResolver = { ) => { // Only sysadmin or registrar or registration agent should be able to search field agents if (!inScope(authHeader, ['sysadmin', 'register', 'validate'])) { - return await Promise.reject( - new Error( - 'Search field agents is only allowed for sysadmin or registrar or registration agent' - ) + throw new Error( + 'Search field agents is only allowed for sysadmin or registrar or registration agent' ) } @@ -252,9 +248,7 @@ export const resolvers: GQLResolver = { }) if (res.status !== 200) { - return await Promise.reject( - new Error('Unauthorized to verify password') - ) + throw new Error('Unauthorized to verify password') } return await res.json() @@ -266,9 +260,7 @@ export const resolvers: GQLResolver = { async createOrUpdateUser(_, { user }, { headers: authHeader }) { // Only sysadmin should be able to create user if (!hasScope(authHeader, 'sysadmin')) { - return await Promise.reject( - new Error('Create user is only allowed for sysadmin') - ) + throw new Error('Create user is only allowed for sysadmin') } try { @@ -312,10 +304,8 @@ export const resolvers: GQLResolver = { ] }) } else if (res.status !== 201) { - return await Promise.reject( - new Error( - `Something went wrong on user-mgnt service. Couldn't ${action} user` - ) + throw new Error( + `Something went wrong on user-mgnt service. Couldn't ${action} user` ) } return await res.json() @@ -337,10 +327,8 @@ export const resolvers: GQLResolver = { const response = await res.json() if (res.status !== 201) { - return await Promise.reject( - new Error( - "Something went wrong on user-mgnt service. Couldn't activate given user" - ) + throw new Error( + "Something went wrong on user-mgnt service. Couldn't activate given user" ) } return response.userId @@ -355,10 +343,8 @@ export const resolvers: GQLResolver = { !hasScope(authHeader, 'sysadmin') && !isTokenOwner(authHeader, userId) ) { - return await Promise.reject( - new Error( - `Change password is not allowed. ${userId} is not the owner of the token` - ) + throw new Error( + `Change password is not allowed. ${userId} is not the owner of the token` ) } const res = await fetch(`${USER_MANAGEMENT_URL}changeUserPassword`, { @@ -371,10 +357,8 @@ export const resolvers: GQLResolver = { }) if (res.status !== 200) { - return await Promise.reject( - new Error( - "Something went wrong on user-mgnt service. Couldn't change user password" - ) + throw new Error( + "Something went wrong on user-mgnt service. Couldn't change user password" ) } return true @@ -385,19 +369,15 @@ export const resolvers: GQLResolver = { { headers: authHeader } ) { if (!isTokenOwner(authHeader, userId)) { - return await Promise.reject( - new Error( - `Change phone is not allowed. ${userId} is not the owner of the token` - ) + throw new Error( + `Change phone is not allowed. ${userId} is not the owner of the token` ) } try { await checkVerificationCode(nonce, verifyCode) } catch (err) { logger.error(err) - return await Promise.reject( - new Error(`Change phone is not allowed. Error: ${err}`) - ) + throw new Error(`Change phone is not allowed. Error: ${err}`) } const res = await fetch(`${USER_MANAGEMENT_URL}changeUserPhone`, { method: 'POST', @@ -409,10 +389,8 @@ export const resolvers: GQLResolver = { }) if (res.status !== 200) { - return await Promise.reject( - new Error( - "Something went wrong on user-mgnt service. Couldn't change user phone number" - ) + throw new Error( + "Something went wrong on user-mgnt service. Couldn't change user phone number" ) } return true @@ -423,19 +401,15 @@ export const resolvers: GQLResolver = { { headers: authHeader } ) { if (!isTokenOwner(authHeader, userId)) { - return await Promise.reject( - new Error( - `Change email is not allowed. ${userId} is not the owner of the token` - ) + throw new Error( + `Change email is not allowed. ${userId} is not the owner of the token` ) } try { await checkVerificationCode(nonce, verifyCode) } catch (err) { logger.error(err) - return await Promise.reject( - new Error(`Change email is not allowed. Error: ${err}`) - ) + throw new Error(`Change email is not allowed. Error: ${err}`) } const res = await fetch(`${USER_MANAGEMENT_URL}changeUserEmail`, { method: 'POST', @@ -447,10 +421,8 @@ export const resolvers: GQLResolver = { }) if (res.status !== 200) { - return await Promise.reject( - new Error( - "Something went wrong on user-mgnt service. Couldn't change user email" - ) + throw new Error( + "Something went wrong on user-mgnt service. Couldn't change user email" ) } return true @@ -464,10 +436,8 @@ export const resolvers: GQLResolver = { // Only token owner should be able to change their avatar if (!isTokenOwner(authHeader, userId)) { - return await Promise.reject( - new Error( - `Changing avatar is not allowed. ${userId} is not the owner of the token` - ) + throw new Error( + `Changing avatar is not allowed. ${userId} is not the owner of the token` ) } @@ -489,10 +459,8 @@ export const resolvers: GQLResolver = { }) if (res.status !== 200) { - return await Promise.reject( - new Error( - "Something went wrong on user-mgnt service. Couldn't change user avatar" - ) + throw new Error( + "Something went wrong on user-mgnt service. Couldn't change user avatar" ) } return avatar @@ -503,10 +471,8 @@ export const resolvers: GQLResolver = { { headers: authHeader } ) { if (!hasScope(authHeader, 'sysadmin')) { - return await Promise.reject( - new Error( - `User ${userId} is not allowed to audit for not having the sys admin scope` - ) + throw new Error( + `User ${userId} is not allowed to audit for not having the sys admin scope` ) } @@ -528,21 +494,16 @@ export const resolvers: GQLResolver = { }) if (res.status !== 200) { - return await Promise.reject( - new Error( - `Something went wrong on user-mgnt service. Couldn't audit user ${userId}` - ) + throw new Error( + `Something went wrong on user-mgnt service. Couldn't audit user ${userId}` ) } - return true }, async resendInvite(_, { userId }, { headers: authHeader }) { if (!hasScope(authHeader, 'sysadmin')) { - return await Promise.reject( - new Error( - 'SMS invite can only be resent by a user with sys admin scope' - ) + throw new Error( + 'SMS invite can only be resent by a user with sys admin scope' ) } @@ -558,21 +519,16 @@ export const resolvers: GQLResolver = { }) if (res.status !== 200) { - return await Promise.reject( - new Error( - `Something went wrong on user-mgnt service. Couldn't send sms to ${userId}` - ) + throw new Error( + `Something went wrong on user-mgnt service. Couldn't send sms to ${userId}` ) } - return true }, async usernameReminder(_, { userId }, { headers: authHeader }) { if (!hasScope(authHeader, 'sysadmin')) { - return await Promise.reject( - new Error( - 'Username reminder can only be resent by a user with sys admin scope' - ) + throw new Error( + 'Username reminder can only be resent by a user with sys admin scope' ) } const res = await fetch(`${USER_MANAGEMENT_URL}usernameReminder`, { @@ -587,21 +543,16 @@ export const resolvers: GQLResolver = { }) if (res.status !== 200) { - return await Promise.reject( - new Error( - `Something went wrong on user-mgnt service. Couldn't send sms to ${userId}` - ) + throw new Error( + `Something went wrong on user-mgnt service. Couldn't send sms to ${userId}` ) } - return true }, async resetPasswordInvite(_, { userId }, { headers: authHeader }) { if (!hasScope(authHeader, 'sysadmin')) { - return await Promise.reject( - new Error( - 'Reset password can only be sent by a user with sys admin scope' - ) + throw new Error( + 'Reset password can only be sent by a user with sys admin scope' ) } const res = await fetch(`${USER_MANAGEMENT_URL}resetPasswordInvite`, { @@ -616,13 +567,10 @@ export const resolvers: GQLResolver = { }) if (res.status !== 200) { - return await Promise.reject( - new Error( - `Something went wrong on user-mgnt service. Couldn't reset password and send sms to ${userId}` - ) + throw new Error( + `Something went wrong on user-mgnt service. Couldn't reset password and send sms to ${userId}` ) } - return true } }