From 06dcb2380e19892587ac120e4e8420e0162c1be6 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 31 Mar 2022 13:48:15 +0200 Subject: [PATCH] seed creations months ago from now --- .../src/seeds/creation/CreationInterface.ts | 2 ++ backend/src/seeds/creation/index.ts | 13 ++++++------ backend/src/seeds/factory/creation.ts | 21 +++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/backend/src/seeds/creation/CreationInterface.ts b/backend/src/seeds/creation/CreationInterface.ts index 8723f441d..3e0f3cf6c 100644 --- a/backend/src/seeds/creation/CreationInterface.ts +++ b/backend/src/seeds/creation/CreationInterface.ts @@ -4,4 +4,6 @@ export interface CreationInterface { memo: string creationDate: string confirmed?: boolean + // number of months to move the confirmed creation to the past + moveCreationDate?: number } diff --git a/backend/src/seeds/creation/index.ts b/backend/src/seeds/creation/index.ts index 7396a7ec8..20d30d94c 100644 --- a/backend/src/seeds/creation/index.ts +++ b/backend/src/seeds/creation/index.ts @@ -1,29 +1,28 @@ import { CreationInterface } from './CreationInterface' - -const lastMonth = (date: Date): string => { - return new Date(date.getFullYear(), date.getMonth() - 1, 1).toISOString() -} +import { nMonthsBefore } from '../factory/creation' export const creations: CreationInterface[] = [ { email: 'bibi@bloxberg.de', amount: 1000, memo: 'Herzlich Willkommen bei Gradido!', - creationDate: lastMonth(new Date()), + creationDate: nMonthsBefore(new Date()), confirmed: true, + moveCreationDate: 12, }, { email: 'bob@baumeister.de', amount: 1000, memo: 'Herzlich Willkommen bei Gradido!', - creationDate: lastMonth(new Date()), + creationDate: nMonthsBefore(new Date()), confirmed: true, + moveCreationDate: 8, }, { email: 'raeuber@hotzenplotz.de', amount: 1000, memo: 'Herzlich Willkommen bei Gradido!', - creationDate: lastMonth(new Date()), + creationDate: nMonthsBefore(new Date()), confirmed: true, }, ] diff --git a/backend/src/seeds/factory/creation.ts b/backend/src/seeds/factory/creation.ts index d4d4c8101..64f693360 100644 --- a/backend/src/seeds/factory/creation.ts +++ b/backend/src/seeds/factory/creation.ts @@ -6,9 +6,14 @@ import { login } from '@/seeds/graphql/queries' import { CreationInterface } from '@/seeds/creation/CreationInterface' import { ApolloServerTestClient } from 'apollo-server-testing' import { User } from '@entity/User' +import { Transaction } from '@entity/Transaction' import { AdminPendingCreation } from '@entity/AdminPendingCreation' // import CONFIG from '@/config/index' +export const nMonthsBefore = (date: Date, months = 1): string => { + return new Date(date.getFullYear(), date.getMonth() - months, 1).toISOString() +} + export const creationFactory = async ( client: ApolloServerTestClient, creation: CreationInterface, @@ -34,5 +39,21 @@ export const creationFactory = async ( }) await mutate({ mutation: confirmPendingCreation, variables: { id: pendingCreation.id } }) + + if (creation.moveCreationDate) { + const transaction = await Transaction.findOneOrFail({ + where: { userId: user.id, creationDate: new Date(creation.creationDate) }, + order: { balanceDate: 'DESC' }, + }) + if (transaction.decay.equals(0) && transaction.creationDate) { + transaction.creationDate = new Date( + nMonthsBefore(transaction.creationDate, creation.moveCreationDate), + ) + transaction.balanceDate = new Date( + nMonthsBefore(transaction.balanceDate, creation.moveCreationDate), + ) + await transaction.save() + } + } } }