From 269de2cf563e54acb080975fa2a24af097562f86 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 26 Jun 2025 16:00:32 +0200 Subject: [PATCH] emails resolver does not have logger in context, why? --- backend/src/graphql/resolvers/badges.ts | 12 +++++++---- backend/src/graphql/resolvers/emails.spec.ts | 6 ++++++ backend/src/graphql/resolvers/emails.ts | 21 ++++++++++++++------ backend/src/graphql/resolvers/users.ts | 18 ++++++++--------- 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/backend/src/graphql/resolvers/badges.ts b/backend/src/graphql/resolvers/badges.ts index 2d8c08f06..f3faf1fe4 100644 --- a/backend/src/graphql/resolvers/badges.ts +++ b/backend/src/graphql/resolvers/badges.ts @@ -62,7 +62,9 @@ export default { try { const { relation, user } = await writeTxResultPromise if (!relation) { - context.logger.error('Could not reward badge! Ensure the user and the badge exist and the badge is of the correct type.') + context.logger.error( + 'Could not reward badge! Ensure the user and the badge exist and the badge is of the correct type.', + ) throw new Error( 'Could not reward badge! Ensure the user and the badge exist and the badge is of the correct type.', ) @@ -72,7 +74,7 @@ export default { context.logger.error('setVerificationBadge', error) throw new Error(error) } finally { - session.close() + await session.close() } }, @@ -127,7 +129,9 @@ export default { ).records.map((record) => record.get('user')) if (users.length !== 1) { - context.logger.error('Could not reward badge! Ensure the user and the badge exist and the badge is of the correct type.') + context.logger.error( + 'Could not reward badge! Ensure the user and the badge exist and the badge is of the correct type.', + ) throw new Error( 'Could not reward badge! Ensure the user and the badge exist and the badge is of the correct type.', ) @@ -160,7 +164,7 @@ export default { try { return await writeTxResultPromise } catch (error) { - context.logger.error('revokeBadge', error) + context.logger.error('revokeBadge', error) throw new Error(error) } finally { session.close() diff --git a/backend/src/graphql/resolvers/emails.spec.ts b/backend/src/graphql/resolvers/emails.spec.ts index f77602463..367e1102a 100644 --- a/backend/src/graphql/resolvers/emails.spec.ts +++ b/backend/src/graphql/resolvers/emails.spec.ts @@ -18,6 +18,12 @@ let user let variables const driver = getDriver() +const loggerErrorMock: (e) => void = jest.fn() + +jest.mock('@src/logger', () => ({ + error: (e) => loggerErrorMock(e), +})) + beforeAll(async () => { await cleanDatabase() diff --git a/backend/src/graphql/resolvers/emails.ts b/backend/src/graphql/resolvers/emails.ts index f5c4eb0ad..7b1f78181 100644 --- a/backend/src/graphql/resolvers/emails.ts +++ b/backend/src/graphql/resolvers/emails.ts @@ -7,6 +7,8 @@ import { UserInputError } from 'apollo-server' // eslint-disable-next-line import/extensions import Validator from 'neode/build/Services/Validator.js' +import { Context } from '@src/server' + import existingEmailAddress from './helpers/existingEmailAddress' import generateNonce from './helpers/generateNonce' import normalizeEmail from './helpers/normalizeEmail' @@ -36,13 +38,14 @@ export default { }, }, Mutation: { - AddEmailAddress: async (_parent, args, context, _resolveInfo) => { + AddEmailAddress: async (_parent, args, context: Context, _resolveInfo) => { let response args.email = normalizeEmail(args.email) try { const { neode } = context await new Validator(neode, neode.model('UnverifiedEmailAddress'), args) } catch (e) { + // context.logger.error('must be a valid email') throw new UserInputError('must be a valid email') } @@ -77,11 +80,11 @@ export default { const txResult = await writeTxResultPromise response = txResult[0] } finally { - session.close() + await session.close() } return response }, - VerifyEmailAddress: async (_parent, args, context, _resolveInfo) => { + VerifyEmailAddress: async (_parent, args, context: Context, _resolveInfo) => { let response const { user: { id: userId }, @@ -111,13 +114,19 @@ export default { const txResult = await writeTxResultPromise response = txResult[0] } catch (e) { - if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed') + if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed') { + // context.logger.error('A user account with this email already exists.') throw new UserInputError('A user account with this email already exists.') + } + context.logger.error('VerifyEmailAddress', e) throw new Error(e) } finally { - session.close() + await session.close() + } + if (!response) { + // context.logger.error('Invalid nonce or no email address found.') + throw new UserInputError('Invalid nonce or no email address found.') } - if (!response) throw new UserInputError('Invalid nonce or no email address found.') return response }, }, diff --git a/backend/src/graphql/resolvers/users.ts b/backend/src/graphql/resolvers/users.ts index 93189cf73..891fe3934 100644 --- a/backend/src/graphql/resolvers/users.ts +++ b/backend/src/graphql/resolvers/users.ts @@ -60,7 +60,7 @@ export default { throw new UserInputError(e.message) } }, - blockedUsers: async (_object, _args, context, _resolveInfo) => { + blockedUsers: async (_object, _args, context: Context, _resolveInfo) => { try { return getBlockedUsers(context) } catch (e) { @@ -122,7 +122,7 @@ export default { const unmutedUser = await neode.find('User', params.id) return unmutedUser.toJson() }, - blockUser: async (_object, args, context, _resolveInfo) => { + blockUser: async (_object, args, context: Context, _resolveInfo) => { const { user: currentUser } = context if (currentUser.id === args.id) return null @@ -147,10 +147,10 @@ export default { context.logger.error('blockUser mutation', error.message) throw new UserInputError(error.message) } finally { - session.close() + await session.close() } }, - unblockUser: async (_object, args, context, _resolveInfo) => { + unblockUser: async (_object, args, context: Context, _resolveInfo) => { const { user: currentUser } = context if (currentUser.id === args.id) return null @@ -172,10 +172,10 @@ export default { context.logger.error('unblockUser mutation', error.message) throw new UserInputError(error.message) } finally { - session.close() + await session.close() } }, - UpdateUser: async (_parent, params, context, _resolveInfo) => { + UpdateUser: async (_parent, params, context: Context, _resolveInfo) => { const { avatar: avatarInput } = params delete params.avatar params.locationName = params.locationName === '' ? null : params.locationName @@ -228,7 +228,7 @@ export default { context.logger.error('UpdateUser mutation', error.message) throw new UserInputError(error.message) } finally { - session.close() + await session.close() } }, DeleteUser: async (_object, params, context, _resolveInfo) => { @@ -297,7 +297,7 @@ export default { session.close() } }, - switchUserRole: async (_object, args, context, _resolveInfo) => { + switchUserRole: async (_object, args, context: Context, _resolveInfo) => { const { role, id } = args if (context.user.id === id) { @@ -322,7 +322,7 @@ export default { const user = await writeTxResultPromise return user } finally { - session.close() + await session.close() } }, saveCategorySettings: async (_object, args, context, _resolveInfo) => {