diff --git a/backend/src/graphql/resolver/ContributionMessageResolver.test.ts b/backend/src/graphql/resolver/ContributionMessageResolver.test.ts index 436830c2c..36d78c382 100644 --- a/backend/src/graphql/resolver/ContributionMessageResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionMessageResolver.test.ts @@ -88,6 +88,7 @@ describe('ContributionMessageResolver', () => { describe('input not valid', () => { it('throws error when contribution does not exist', async () => { + jest.clearAllMocks() await expect( mutate({ mutation: adminCreateContributionMessage, @@ -98,16 +99,20 @@ describe('ContributionMessageResolver', () => { }), ).resolves.toEqual( expect.objectContaining({ - errors: [ - new GraphQLError( - 'ContributionMessage was not successful: Error: Contribution not found', - ), - ], + errors: [new GraphQLError('ContributionMessage was not sent successfully')], }), ) }) + it('logs the error thrown', () => { + expect(logger.error).toBeCalledWith( + 'ContributionMessage was not sent successfully', + new Error('Contribution not found'), + ) + }) + it('throws error when contribution.userId equals user.id', async () => { + jest.clearAllMocks() await mutate({ mutation: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, @@ -130,14 +135,17 @@ describe('ContributionMessageResolver', () => { }), ).resolves.toEqual( expect.objectContaining({ - errors: [ - new GraphQLError( - 'ContributionMessage was not successful: Error: Admin can not answer on own contribution', - ), - ], + errors: [new GraphQLError('ContributionMessage was not sent successfully')], }), ) }) + + it('logs the error thrown', () => { + expect(logger.error).toBeCalledWith( + 'ContributionMessage was not sent successfully', + new Error('Admin can not answer on his own contribution'), + ) + }) }) describe('valid input', () => { @@ -210,6 +218,7 @@ describe('ContributionMessageResolver', () => { describe('input not valid', () => { it('throws error when contribution does not exist', async () => { + jest.clearAllMocks() await expect( mutate({ mutation: createContributionMessage, @@ -220,16 +229,20 @@ describe('ContributionMessageResolver', () => { }), ).resolves.toEqual( expect.objectContaining({ - errors: [ - new GraphQLError( - 'ContributionMessage was not successful: Error: Contribution not found', - ), - ], + errors: [new GraphQLError('ContributionMessage was not sent successfully')], }), ) }) + it('logs the error thrown', () => { + expect(logger.error).toBeCalledWith( + 'ContributionMessage was not sent successfully', + new Error('Contribution not found'), + ) + }) + it('throws error when other user tries to send createContributionMessage', async () => { + jest.clearAllMocks() await mutate({ mutation: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, @@ -244,14 +257,17 @@ describe('ContributionMessageResolver', () => { }), ).resolves.toEqual( expect.objectContaining({ - errors: [ - new GraphQLError( - 'ContributionMessage was not successful: Error: Can not send message to contribution of another user', - ), - ], + errors: [new GraphQLError('ContributionMessage was not sent successfully')], }), ) }) + + it('logs the error thrown', () => { + expect(logger.error).toBeCalledWith( + 'ContributionMessage was not sent successfully', + new Error('Can not send message to contribution of another user'), + ) + }) }) describe('valid input', () => { diff --git a/backend/src/graphql/resolver/ContributionMessageResolver.ts b/backend/src/graphql/resolver/ContributionMessageResolver.ts index 38bea804e..9544f81c4 100644 --- a/backend/src/graphql/resolver/ContributionMessageResolver.ts +++ b/backend/src/graphql/resolver/ContributionMessageResolver.ts @@ -16,6 +16,7 @@ import { backendLogger as logger } from '@/server/logger' import { RIGHTS } from '@/auth/RIGHTS' import { Context, getUser } from '@/server/context' import { sendAddedContributionMessageEmail } from '@/emails/sendEmailVariants' +import LogError from '@/server/LogError' @Resolver() export class ContributionMessageResolver { @@ -54,8 +55,7 @@ export class ContributionMessageResolver { await queryRunner.commitTransaction() } catch (e) { await queryRunner.rollbackTransaction() - logger.error(`ContributionMessage was not successful: ${e}`) - throw new Error(`ContributionMessage was not successful: ${e}`) + throw new LogError('ContributionMessage was not sent successfully', e) } finally { await queryRunner.release() } @@ -95,9 +95,7 @@ export class ContributionMessageResolver { @Ctx() context: Context, ): Promise { const user = getUser(context) - if (!user.emailContact) { - user.emailContact = await UserContact.findOneOrFail({ where: { id: user.emailId } }) - } + const queryRunner = getConnection().createQueryRunner() await queryRunner.connect() await queryRunner.startTransaction('REPEATABLE READ') @@ -108,12 +106,10 @@ export class ContributionMessageResolver { relations: ['user'], }) if (!contribution) { - logger.error('Contribution not found') - throw new Error('Contribution not found') + throw new LogError('Contribution not found', contributionId) } if (contribution.userId === user.id) { - logger.error('Admin can not answer on own contribution') - throw new Error('Admin can not answer on own contribution') + throw new LogError('Admin can not answer on his own contribution', contributionId) } if (!contribution.user.emailContact) { contribution.user.emailContact = await UserContact.findOneOrFail({ @@ -149,8 +145,7 @@ export class ContributionMessageResolver { await queryRunner.commitTransaction() } catch (e) { await queryRunner.rollbackTransaction() - logger.error(`ContributionMessage was not successful: ${e}`) - throw new Error(`ContributionMessage was not successful: ${e}`) + throw new LogError('ContributionMessage was not sent successfully', e) } finally { await queryRunner.release() }