From 06b5fa429558a1e8f4c5dbe5d597a8b7b166fa01 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 12 Dec 2022 14:05:15 +0100 Subject: [PATCH 1/2] refactor(backend): seed contributions as user --- backend/src/seeds/factory/creation.ts | 49 +++++++-------------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/backend/src/seeds/factory/creation.ts b/backend/src/seeds/factory/creation.ts index 36e481519..09bf981bb 100644 --- a/backend/src/seeds/factory/creation.ts +++ b/backend/src/seeds/factory/creation.ts @@ -1,8 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ -import { backendLogger as logger } from '@/server/logger' -import { login, adminCreateContribution, confirmContribution } from '@/seeds/graphql/mutations' +import { login, createContribution, confirmContribution } from '@/seeds/graphql/mutations' import { CreationInterface } from '@/seeds/creation/CreationInterface' import { ApolloServerTestClient } from 'apollo-server-testing' import { Transaction } from '@entity/Transaction' @@ -19,43 +18,27 @@ export const creationFactory = async ( creation: CreationInterface, ): Promise => { const { mutate } = client - logger.trace('creationFactory...') - await mutate({ mutation: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' } }) - logger.trace('creationFactory... after login') - // TODO it would be nice to have this mutation return the id - await mutate({ mutation: adminCreateContribution, variables: { ...creation } }) - logger.trace('creationFactory... after adminCreateContribution') + await mutate({ mutation: login, variables: { email: creation.email, password: 'Aa12345_' } }) - const user = await findUserByEmail(creation.email) // userContact.user + const { + data: { createContribution: contribution }, + } = await mutate({ mutation: createContribution, variables: { ...creation } }) - const pendingCreation = await Contribution.findOneOrFail({ - where: { userId: user.id, amount: creation.amount }, - order: { createdAt: 'DESC' }, - }) - logger.trace( - 'creationFactory... after Contribution.findOneOrFail pendingCreation=', - pendingCreation, - ) if (creation.confirmed) { - logger.trace('creationFactory... creation.confirmed=', creation.confirmed) - await mutate({ mutation: confirmContribution, variables: { id: pendingCreation.id } }) - logger.trace('creationFactory... after confirmContribution') - const confirmedCreation = await Contribution.findOneOrFail({ id: pendingCreation.id }) - logger.trace( - 'creationFactory... after Contribution.findOneOrFail confirmedCreation=', - confirmedCreation, - ) + const user = await findUserByEmail(creation.email) // userContact.user + + await mutate({ mutation: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' } }) + await mutate({ mutation: confirmContribution, variables: { id: contribution.id } }) + const confirmedContribution = await Contribution.findOneOrFail({ id: contribution.id }) if (creation.moveCreationDate) { - logger.trace('creationFactory... creation.moveCreationDate=', creation.moveCreationDate) const transaction = await Transaction.findOneOrFail({ where: { userId: user.id, creationDate: new Date(creation.creationDate) }, order: { balanceDate: 'DESC' }, }) - logger.trace('creationFactory... after Transaction.findOneOrFail transaction=', transaction) if (transaction.decay.equals(0) && transaction.creationDate) { - confirmedCreation.contributionDate = new Date( + confirmedContribution.contributionDate = new Date( nMonthsBefore(transaction.creationDate, creation.moveCreationDate), ) transaction.creationDate = new Date( @@ -64,17 +47,11 @@ export const creationFactory = async ( transaction.balanceDate = new Date( nMonthsBefore(transaction.balanceDate, creation.moveCreationDate), ) - logger.trace('creationFactory... before transaction.save transaction=', transaction) await transaction.save() - logger.trace( - 'creationFactory... before confirmedCreation.save confirmedCreation=', - confirmedCreation, - ) - await confirmedCreation.save() + await confirmedContribution.save() } } } else { - logger.trace('creationFactory... pendingCreation=', pendingCreation) - return pendingCreation + return contribution } } From 35c392bbae05b692512169852e04be89e521f38e Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 12 Dec 2022 14:38:56 +0100 Subject: [PATCH 2/2] adopt unit tests to changes in factory --- .../graphql/resolver/AdminResolver.test.ts | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/backend/src/graphql/resolver/AdminResolver.test.ts b/backend/src/graphql/resolver/AdminResolver.test.ts index b83a9876f..02730f460 100644 --- a/backend/src/graphql/resolver/AdminResolver.test.ts +++ b/backend/src/graphql/resolver/AdminResolver.test.ts @@ -969,13 +969,21 @@ describe('AdminResolver', () => { const now = new Date() beforeAll(async () => { - creation = await creationFactory(testEnv, { - email: 'peter@lustig.de', - amount: 400, - memo: 'Herzlich Willkommen bei Gradido!', - creationDate: contributionDateFormatter( - new Date(now.getFullYear(), now.getMonth() - 1, 1), - ), + await mutate({ + mutation: adminCreateContribution, + variables: { + email: 'peter@lustig.de', + amount: 400, + memo: 'Herzlich Willkommen bei Gradido!', + creationDate: contributionDateFormatter( + new Date(now.getFullYear(), now.getMonth() - 1, 1), + ), + }, + }) + creation = await Contribution.findOneOrFail({ + where: { + memo: 'Herzlich Willkommen bei Gradido!', + }, }) }) @@ -1702,6 +1710,10 @@ describe('AdminResolver', () => { new Date(now.getFullYear(), now.getMonth() - 2, 1), ), }) + await query({ + query: login, + variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, + }) }) it('returns true', async () => { @@ -1782,6 +1794,10 @@ describe('AdminResolver', () => { new Date(now.getFullYear(), now.getMonth() - 2, 1), ), }) + await query({ + query: login, + variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, + }) }) // In the futrue this should not throw anymore