From 94b3485ff877e59e6bc6bc29d6db916e8cbeaf3b Mon Sep 17 00:00:00 2001 From: noah-brunate Date: Tue, 29 Oct 2024 14:49:59 +0300 Subject: [PATCH 1/5] simplifying gateways' return values --- packages/gateway/src/constants.ts | 2 +- .../bookmarkAdvancedSearch/root-resolvers.ts | 24 +-- .../src/features/metrics/root-resolvers.ts | 4 +- .../features/registration/root-resolvers.ts | 89 +++-------- .../src/features/search/root-resolvers.ts | 16 +- packages/gateway/src/features/search/utils.ts | 4 +- .../src/features/systems/root-resolvers.ts | 14 +- .../src/features/user/root-resolvers.ts | 147 ++++++------------ 8 files changed, 89 insertions(+), 211 deletions(-) diff --git a/packages/gateway/src/constants.ts b/packages/gateway/src/constants.ts index 740cc44ad63..8f4f5b65c59 100644 --- a/packages/gateway/src/constants.ts +++ b/packages/gateway/src/constants.ts @@ -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 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 bad69765683..f91924f7fa0 100644 --- a/packages/gateway/src/features/registration/root-resolvers.ts +++ b/packages/gateway/src/features/registration/root-resolvers.ts @@ -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()) @@ -83,9 +84,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`, @@ -110,9 +109,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`, @@ -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> { @@ -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( @@ -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( @@ -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> { @@ -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( @@ -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( @@ -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) { @@ -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, @@ -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, @@ -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, @@ -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 }) { @@ -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( @@ -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( @@ -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 @@ -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, @@ -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( @@ -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) @@ -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( @@ -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' ) 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..5d33bd1a702 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,13 +357,10 @@ 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 }, async changePhone( _, @@ -385,19 +368,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,13 +388,10 @@ 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 }, async changeEmail( _, @@ -423,19 +399,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,13 +419,10 @@ 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 }, async changeAvatar(_, { userId, avatar }, { headers: authHeader }) { try { @@ -464,10 +433,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 +456,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 +468,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 +491,15 @@ 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 +515,15 @@ 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 +538,15 @@ 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,14 +561,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 } } } From 1f0b4f443c725585b1cb00251209ad9a6cef0c86 Mon Sep 17 00:00:00 2001 From: noah-brunate Date: Wed, 30 Oct 2024 12:10:39 +0300 Subject: [PATCH 2/5] odified the test file --- .../gateway/src/features/user/root-resolvers.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/gateway/src/features/user/root-resolvers.test.ts b/packages/gateway/src/features/user/root-resolvers.test.ts index 80e8e3c5216..989778dcd0b 100644 --- a/packages/gateway/src/features/user/root-resolvers.test.ts +++ b/packages/gateway/src/features/user/root-resolvers.test.ts @@ -649,7 +649,7 @@ describe('User root resolvers', () => { { headers: authHeaderValidUser } ) - expect(response).toEqual(true) + expect(response).toEqual(undefined) }) it('throws error if @user-mgnt/changeUserPassword sends anything but 200', async () => { fetch.mockResponseOnce(JSON.stringify({}), { status: 401 }) @@ -735,7 +735,7 @@ describe('User root resolvers', () => { { headers: authHeaderValidUser } ) - expect(response).toEqual(true) + expect(response).toEqual(undefined) }) it('throws error if @user-mgnt/changeUserPhone sends anything but 201', async () => { fetch.mockResponseOnce(JSON.stringify({}), { status: 401 }) @@ -1066,7 +1066,7 @@ describe('User root resolvers', () => { { headers: authHeaderSysAdmin } ) - expect(response).toEqual(true) + expect(response).toEqual(undefined) }) it('throws error for unauthorized user', async () => { @@ -1178,7 +1178,7 @@ describe('User root resolvers', () => { { headers: authHeaderSysAdmin } ) - expect(res).toBe(true) + expect(res).toBe(undefined) }) }) @@ -1256,7 +1256,7 @@ describe('User root resolvers', () => { { headers: authHeaderSysAdmin } ) - expect(res).toBe(true) + expect(res).toBe(undefined) }) }) @@ -1334,7 +1334,7 @@ describe('User root resolvers', () => { { headers: authHeaderSysAdmin } ) - expect(res).toBe(true) + expect(res).toBe(undefined) }) }) }) From 86ee8d3fbd47de38e17c1483de1c3c68c1d6333e Mon Sep 17 00:00:00 2001 From: noah-brunate Date: Wed, 30 Oct 2024 16:34:45 +0300 Subject: [PATCH 3/5] made some fixes --- packages/gateway/src/constants.ts | 2 +- packages/gateway/src/features/registration/root-resolvers.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gateway/src/constants.ts b/packages/gateway/src/constants.ts index 8f4f5b65c59..740cc44ad63 100644 --- a/packages/gateway/src/constants.ts +++ b/packages/gateway/src/constants.ts @@ -16,7 +16,7 @@ export const AVATAR_API = export const NATIVE_LANGUAGE = (() => { const languages = env.LANGUAGES.split(',') - return languages.find((language: string) => language !== 'en') + return languages.find((language) => language !== 'en') })() export const DEFAULT_TIMEOUT = 600000 diff --git a/packages/gateway/src/features/registration/root-resolvers.ts b/packages/gateway/src/features/registration/root-resolvers.ts index f91924f7fa0..6246da65bbd 100644 --- a/packages/gateway/src/features/registration/root-resolvers.ts +++ b/packages/gateway/src/features/registration/root-resolvers.ts @@ -596,7 +596,7 @@ export const resolvers: GQLResolver = { return taskEntry.resource.id }, async confirmRegistration(_, { id }, { headers: authHeader }) { - if (!inScope(authHeader, ['record.confirm-registration'] as unknown as Scope[])) { + if (!inScope(authHeader, ['record.confirm-registration'])) { throw new Error( 'User does not have a "record.confirm-registration" scope' ) From 7719f0da4245126f1aa761428b5f5cc32819f010 Mon Sep 17 00:00:00 2001 From: noah-brunate Date: Thu, 21 Nov 2024 09:56:46 +0300 Subject: [PATCH 4/5] rectified unused variable error --- packages/gateway/src/features/registration/root-resolvers.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/gateway/src/features/registration/root-resolvers.ts b/packages/gateway/src/features/registration/root-resolvers.ts index 39cf3961faf..7f2b4ce9b04 100644 --- a/packages/gateway/src/features/registration/root-resolvers.ts +++ b/packages/gateway/src/features/registration/root-resolvers.ts @@ -63,7 +63,6 @@ import { confirmRegistration } 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()) From 4ecd9f3d99bbe18705338b6bf78d97af7f9785d0 Mon Sep 17 00:00:00 2001 From: noah-brunate Date: Fri, 22 Nov 2024 10:34:56 +0300 Subject: [PATCH 5/5] made some fixes --- .../gateway/src/features/user/root-resolvers.test.ts | 12 ++++++------ packages/gateway/src/features/user/root-resolvers.ts | 7 +++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/gateway/src/features/user/root-resolvers.test.ts b/packages/gateway/src/features/user/root-resolvers.test.ts index 989778dcd0b..80e8e3c5216 100644 --- a/packages/gateway/src/features/user/root-resolvers.test.ts +++ b/packages/gateway/src/features/user/root-resolvers.test.ts @@ -649,7 +649,7 @@ describe('User root resolvers', () => { { headers: authHeaderValidUser } ) - expect(response).toEqual(undefined) + expect(response).toEqual(true) }) it('throws error if @user-mgnt/changeUserPassword sends anything but 200', async () => { fetch.mockResponseOnce(JSON.stringify({}), { status: 401 }) @@ -735,7 +735,7 @@ describe('User root resolvers', () => { { headers: authHeaderValidUser } ) - expect(response).toEqual(undefined) + expect(response).toEqual(true) }) it('throws error if @user-mgnt/changeUserPhone sends anything but 201', async () => { fetch.mockResponseOnce(JSON.stringify({}), { status: 401 }) @@ -1066,7 +1066,7 @@ describe('User root resolvers', () => { { headers: authHeaderSysAdmin } ) - expect(response).toEqual(undefined) + expect(response).toEqual(true) }) it('throws error for unauthorized user', async () => { @@ -1178,7 +1178,7 @@ describe('User root resolvers', () => { { headers: authHeaderSysAdmin } ) - expect(res).toBe(undefined) + expect(res).toBe(true) }) }) @@ -1256,7 +1256,7 @@ describe('User root resolvers', () => { { headers: authHeaderSysAdmin } ) - expect(res).toBe(undefined) + expect(res).toBe(true) }) }) @@ -1334,7 +1334,7 @@ describe('User root resolvers', () => { { headers: authHeaderSysAdmin } ) - expect(res).toBe(undefined) + expect(res).toBe(true) }) }) }) diff --git a/packages/gateway/src/features/user/root-resolvers.ts b/packages/gateway/src/features/user/root-resolvers.ts index 5d33bd1a702..7e9d616c08d 100644 --- a/packages/gateway/src/features/user/root-resolvers.ts +++ b/packages/gateway/src/features/user/root-resolvers.ts @@ -361,6 +361,7 @@ export const resolvers: GQLResolver = { "Something went wrong on user-mgnt service. Couldn't change user password" ) } + return true }, async changePhone( _, @@ -392,6 +393,7 @@ export const resolvers: GQLResolver = { "Something went wrong on user-mgnt service. Couldn't change user phone number" ) } + return true }, async changeEmail( _, @@ -423,6 +425,7 @@ export const resolvers: GQLResolver = { "Something went wrong on user-mgnt service. Couldn't change user email" ) } + return true }, async changeAvatar(_, { userId, avatar }, { headers: authHeader }) { try { @@ -495,6 +498,7 @@ export const resolvers: GQLResolver = { `Something went wrong on user-mgnt service. Couldn't audit user ${userId}` ) } + return true }, async resendInvite(_, { userId }, { headers: authHeader }) { if (!hasScope(authHeader, 'sysadmin')) { @@ -519,6 +523,7 @@ export const resolvers: GQLResolver = { `Something went wrong on user-mgnt service. Couldn't send sms to ${userId}` ) } + return true }, async usernameReminder(_, { userId }, { headers: authHeader }) { if (!hasScope(authHeader, 'sysadmin')) { @@ -542,6 +547,7 @@ export const resolvers: GQLResolver = { `Something went wrong on user-mgnt service. Couldn't send sms to ${userId}` ) } + return true }, async resetPasswordInvite(_, { userId }, { headers: authHeader }) { if (!hasScope(authHeader, 'sysadmin')) { @@ -565,6 +571,7 @@ export const resolvers: GQLResolver = { `Something went wrong on user-mgnt service. Couldn't reset password and send sms to ${userId}` ) } + return true } } }