From bf24d15d96628615bcb6d3bb230c0cd722fc92e2 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Mon, 6 Feb 2023 22:37:38 +0100 Subject: [PATCH] unify checks on adminCreateContribution and adminUpdateContribution --- .../resolver/ContributionResolver.test.ts | 4 ++-- .../graphql/resolver/ContributionResolver.ts | 18 +++++++----------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/backend/src/graphql/resolver/ContributionResolver.test.ts b/backend/src/graphql/resolver/ContributionResolver.test.ts index c3df9bd1d..1e0930d91 100644 --- a/backend/src/graphql/resolver/ContributionResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionResolver.test.ts @@ -1890,13 +1890,13 @@ describe('ContributionResolver', () => { }), ).resolves.toEqual( expect.objectContaining({ - errors: [new GraphQLError('Could not find UserContact')], + errors: [new GraphQLError('Could not find User')], }), ) }) it('logs the error thrown', () => { - expect(logger.error).toBeCalledWith('Could not find UserContact', 'bob@baumeister.de') + expect(logger.error).toBeCalledWith('Could not find User', 'bob@baumeister.de') }) }) diff --git a/backend/src/graphql/resolver/ContributionResolver.ts b/backend/src/graphql/resolver/ContributionResolver.ts index 340d99524..a845af022 100644 --- a/backend/src/graphql/resolver/ContributionResolver.ts +++ b/backend/src/graphql/resolver/ContributionResolver.ts @@ -304,7 +304,7 @@ export class ContributionResolver { withDeleted: true, relations: ['user'], }) - if (!emailContact) { + if (!emailContact || !emailContact.user) { throw new LogError('Could not find user', email) } if (emailContact.deletedAt || emailContact.user.deletedAt) { @@ -389,14 +389,10 @@ export class ContributionResolver { withDeleted: true, relations: ['user'], }) - if (!emailContact) { - throw new LogError('Could not find UserContact', email) - } - const user = emailContact.user - if (!user) { + if (!emailContact || !emailContact.user) { throw new LogError('Could not find User', email) } - if (user.deletedAt) { + if (emailContact.deletedAt || emailContact.user.deletedAt) { throw new LogError('User was deleted', email) } @@ -409,7 +405,7 @@ export class ContributionResolver { throw new LogError('Contribution not found', id) } - if (contributionToUpdate.userId !== user.id) { + if (contributionToUpdate.userId !== emailContact.user.id) { throw new LogError('User of the pending contribution and send user does not correspond') } @@ -418,7 +414,7 @@ export class ContributionResolver { } const creationDateObj = new Date(creationDate) - let creations = await getUserCreation(user.id, clientTimezoneOffset) + let creations = await getUserCreation(emailContact.user.id, clientTimezoneOffset) if (contributionToUpdate.contributionDate.getMonth() === creationDateObj.getMonth()) { creations = updateCreations(creations, contributionToUpdate, clientTimezoneOffset) @@ -441,11 +437,11 @@ export class ContributionResolver { result.memo = contributionToUpdate.memo result.date = contributionToUpdate.contributionDate - result.creation = await getUserCreation(user.id, clientTimezoneOffset) + result.creation = await getUserCreation(emailContact.user.id, clientTimezoneOffset) const event = new Event() const eventAdminContributionUpdate = new EventAdminContributionUpdate() - eventAdminContributionUpdate.userId = user.id + eventAdminContributionUpdate.userId = emailContact.user.id eventAdminContributionUpdate.amount = amount eventAdminContributionUpdate.contributionId = contributionToUpdate.id await eventProtocol.writeEvent(