From 8eaacc669931be7dcf2debd76a16607da64b5bf2 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Feb 2023 12:50:29 +0100 Subject: [PATCH] implement LogError on creations and fix all corressponding tests --- .../resolver/ContributionLinkResolver.test.ts | 18 ++++------ .../resolver/ContributionResolver.test.ts | 35 ++++++++++--------- .../resolver/TransactionLinkResolver.test.ts | 7 +++- .../src/graphql/resolver/util/creations.ts | 25 +++++-------- 4 files changed, 40 insertions(+), 45 deletions(-) diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts index 46296e009..2a17f0556 100644 --- a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts @@ -257,17 +257,13 @@ describe('Contribution Links', () => { }), ).resolves.toEqual( expect.objectContaining({ - errors: [ - new GraphQLError('Start-Date is not initialized. A Start-Date must be set!'), - ], + errors: [new GraphQLError('A Start-Date must be set')], }), ) }) it('logs the error thrown', () => { - expect(logger.error).toBeCalledWith( - 'Start-Date is not initialized. A Start-Date must be set!', - ) + expect(logger.error).toBeCalledWith('A Start-Date must be set') }) it('returns an error if missing endDate', async () => { @@ -282,15 +278,13 @@ describe('Contribution Links', () => { }), ).resolves.toEqual( expect.objectContaining({ - errors: [new GraphQLError('End-Date is not initialized. An End-Date must be set!')], + errors: [new GraphQLError('An End-Date must be set')], }), ) }) it('logs the error thrown', () => { - expect(logger.error).toBeCalledWith( - 'End-Date is not initialized. An End-Date must be set!', - ) + expect(logger.error).toBeCalledWith('An End-Date must be set') }) it('returns an error if endDate is before startDate', async () => { @@ -307,7 +301,7 @@ describe('Contribution Links', () => { ).resolves.toEqual( expect.objectContaining({ errors: [ - new GraphQLError(`The value of validFrom must before or equals the validTo!`), + new GraphQLError(`The value of validFrom must before or equals the validTo`), ], }), ) @@ -315,7 +309,7 @@ describe('Contribution Links', () => { it('logs the error thrown', () => { expect(logger.error).toBeCalledWith( - `The value of validFrom must before or equals the validTo!`, + `The value of validFrom must before or equals the validTo`, ) }) diff --git a/backend/src/graphql/resolver/ContributionResolver.test.ts b/backend/src/graphql/resolver/ContributionResolver.test.ts index 1e0930d91..7f0231860 100644 --- a/backend/src/graphql/resolver/ContributionResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionResolver.test.ts @@ -187,8 +187,8 @@ describe('ContributionResolver', () => { it('logs the error found', () => { expect(logger.error).toBeCalledWith( - 'No information for available creations with the given creationDate=', - 'Invalid Date', + 'No information for available creations for the given date', + expect.any(Date), ) }) @@ -215,8 +215,8 @@ describe('ContributionResolver', () => { it('logs the error found', () => { expect(logger.error).toBeCalledWith( - 'No information for available creations with the given creationDate=', - 'Invalid Date', + 'No information for available creations for the given date', + expect.any(Date), ) }) }) @@ -637,7 +637,7 @@ describe('ContributionResolver', () => { expect.objectContaining({ errors: [ new GraphQLError( - 'The amount (1019 GDD) to be created exceeds the amount (1000 GDD) still available for this month.', + 'The amount to be created exceeds the amount still available for this month', ), ], }), @@ -646,7 +646,9 @@ describe('ContributionResolver', () => { it('logs the error found', () => { expect(logger.error).toBeCalledWith( - 'The amount (1019 GDD) to be created exceeds the amount (1000 GDD) still available for this month.', + 'The amount to be created exceeds the amount still available for this month', + new Decimal(1019), + new Decimal(1000), ) }) }) @@ -1717,8 +1719,8 @@ describe('ContributionResolver', () => { it('logs the error thrown', () => { expect(logger.error).toBeCalledWith( - 'No information for available creations with the given creationDate=', - new Date(variables.creationDate).toString(), + 'No information for available creations for the given date', + new Date(variables.creationDate), ) }) }) @@ -1742,8 +1744,8 @@ describe('ContributionResolver', () => { it('logs the error thrown', () => { expect(logger.error).toBeCalledWith( - 'No information for available creations with the given creationDate=', - new Date(variables.creationDate).toString(), + 'No information for available creations for the given date', + new Date(variables.creationDate), ) }) }) @@ -1758,7 +1760,7 @@ describe('ContributionResolver', () => { expect.objectContaining({ errors: [ new GraphQLError( - 'The amount (2000 GDD) to be created exceeds the amount (1000 GDD) still available for this month.', + 'The amount to be created exceeds the amount still available for this month', ), ], }), @@ -1767,7 +1769,7 @@ describe('ContributionResolver', () => { it('logs the error thrown', () => { expect(logger.error).toBeCalledWith( - 'The amount (2000 GDD) to be created exceeds the amount (1000 GDD) still available for this month.', + 'The amount to be created exceeds the amount still available for this month', new Decimal(2000), new Decimal(1000) ) }) }) @@ -1798,6 +1800,7 @@ describe('ContributionResolver', () => { describe('second creation surpasses the available amount ', () => { it('returns an array of the open creations for the last three months', async () => { + jest.clearAllMocks() variables.amount = new Decimal(1000) await expect( mutate({ mutation: adminCreateContribution, variables }), @@ -1805,7 +1808,7 @@ describe('ContributionResolver', () => { expect.objectContaining({ errors: [ new GraphQLError( - 'The amount (1000 GDD) to be created exceeds the amount (800 GDD) still available for this month.', + 'The amount to be created exceeds the amount still available for this month', ), ], }), @@ -1814,7 +1817,7 @@ describe('ContributionResolver', () => { it('logs the error thrown', () => { expect(logger.error).toBeCalledWith( - 'The amount (1000 GDD) to be created exceeds the amount (800 GDD) still available for this month.', + 'The amount to be created exceeds the amount still available for this month', new Decimal(1000), new Decimal(800) ) }) }) @@ -2007,7 +2010,7 @@ describe('ContributionResolver', () => { expect.objectContaining({ errors: [ new GraphQLError( - 'The amount (1900 GDD) to be created exceeds the amount (1000 GDD) still available for this month.', + 'The amount to be created exceeds the amount still available for this month', ), ], }), @@ -2016,7 +2019,7 @@ describe('ContributionResolver', () => { it('logs the error thrown', () => { expect(logger.error).toBeCalledWith( - 'The amount (1900 GDD) to be created exceeds the amount (1000 GDD) still available for this month.', + 'The amount to be created exceeds the amount still available for this month', new Decimal(1900), new Decimal(1000) ) }) }) diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.test.ts b/backend/src/graphql/resolver/TransactionLinkResolver.test.ts index af2e4fd59..0666efc8e 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.test.ts @@ -354,10 +354,15 @@ describe('TransactionLinkResolver', () => { }) it('logs the error thrown', () => { + /* expect(logger.error).toBeCalledWith( + 'The amount to be created exceeds the amount still available for this month', + new Decimal(5), + new Decimal(0), + ) */ expect(logger.error).toBeCalledWith( 'Creation from contribution link was not successful', new Error( - 'The amount (5 GDD) to be created exceeds the amount (0 GDD) still available for this month.', + 'The amount to be created exceeds the amount still available for this month', ), ) }) diff --git a/backend/src/graphql/resolver/util/creations.ts b/backend/src/graphql/resolver/util/creations.ts index 00137eaa1..6a47915b1 100644 --- a/backend/src/graphql/resolver/util/creations.ts +++ b/backend/src/graphql/resolver/util/creations.ts @@ -1,3 +1,4 @@ +import LogError from '@/server/LogError' import { backendLogger as logger } from '@/server/logger' import { getConnection } from '@dbTools/typeorm' import { Contribution } from '@entity/Contribution' @@ -19,19 +20,14 @@ export const validateContribution = ( const index = getCreationIndex(creationDate.getMonth(), timezoneOffset) if (index < 0) { - logger.error( - 'No information for available creations with the given creationDate=', - creationDate.toString(), - ) - throw new Error('No information for available creations for the given date') + throw new LogError('No information for available creations for the given date', creationDate) } if (amount.greaterThan(creations[index].toString())) { - logger.error( - `The amount (${amount} GDD) to be created exceeds the amount (${creations[index]} GDD) still available for this month.`, - ) - throw new Error( - `The amount (${amount} GDD) to be created exceeds the amount (${creations[index]} GDD) still available for this month.`, + throw new LogError( + 'The amount to be created exceeds the amount still available for this month', + amount, + creations[index], ) } } @@ -126,19 +122,16 @@ export const isStartEndDateValid = ( endDate: string | null | undefined, ): void => { if (!startDate) { - logger.error('Start-Date is not initialized. A Start-Date must be set!') - throw new Error('Start-Date is not initialized. A Start-Date must be set!') + throw new LogError('A Start-Date must be set') } if (!endDate) { - logger.error('End-Date is not initialized. An End-Date must be set!') - throw new Error('End-Date is not initialized. An End-Date must be set!') + throw new LogError('An End-Date must be set') } // check if endDate is before startDate if (new Date(endDate).getTime() - new Date(startDate).getTime() < 0) { - logger.error(`The value of validFrom must before or equals the validTo!`) - throw new Error(`The value of validFrom must before or equals the validTo!`) + throw new LogError(`The value of validFrom must before or equals the validTo`) } }