From a23426649374cafc8d73791c5301be8a0ee03a47 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 15 Mar 2022 19:28:46 +0100 Subject: [PATCH] seed simple confired creations --- .../src/seeds/creation/CreationInterface.ts | 7 ++++ backend/src/seeds/creation/index.ts | 29 ++++++++++++++ backend/src/seeds/factory/creation.ts | 38 +++++++++++++++++++ backend/src/seeds/index.ts | 17 ++++++--- 4 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 backend/src/seeds/creation/CreationInterface.ts create mode 100644 backend/src/seeds/creation/index.ts create mode 100644 backend/src/seeds/factory/creation.ts diff --git a/backend/src/seeds/creation/CreationInterface.ts b/backend/src/seeds/creation/CreationInterface.ts new file mode 100644 index 000000000..8723f441d --- /dev/null +++ b/backend/src/seeds/creation/CreationInterface.ts @@ -0,0 +1,7 @@ +export interface CreationInterface { + email: string + amount: number + memo: string + creationDate: string + confirmed?: boolean +} diff --git a/backend/src/seeds/creation/index.ts b/backend/src/seeds/creation/index.ts new file mode 100644 index 000000000..7396a7ec8 --- /dev/null +++ b/backend/src/seeds/creation/index.ts @@ -0,0 +1,29 @@ +import { CreationInterface } from './CreationInterface' + +const lastMonth = (date: Date): string => { + return new Date(date.getFullYear(), date.getMonth() - 1, 1).toISOString() +} + +export const creations: CreationInterface[] = [ + { + email: 'bibi@bloxberg.de', + amount: 1000, + memo: 'Herzlich Willkommen bei Gradido!', + creationDate: lastMonth(new Date()), + confirmed: true, + }, + { + email: 'bob@baumeister.de', + amount: 1000, + memo: 'Herzlich Willkommen bei Gradido!', + creationDate: lastMonth(new Date()), + confirmed: true, + }, + { + email: 'raeuber@hotzenplotz.de', + amount: 1000, + memo: 'Herzlich Willkommen bei Gradido!', + creationDate: lastMonth(new Date()), + confirmed: true, + }, +] diff --git a/backend/src/seeds/factory/creation.ts b/backend/src/seeds/factory/creation.ts new file mode 100644 index 000000000..52b63b193 --- /dev/null +++ b/backend/src/seeds/factory/creation.ts @@ -0,0 +1,38 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ + +import { createPendingCreation, confirmPendingCreation } from '@/seeds/graphql/mutations' +import { login } from '@/seeds/graphql/queries' +import { CreationInterface } from '@/seeds/creation/CreationInterface' +import { ApolloServerTestClient } from 'apollo-server-testing' +import { User } from '@entity/User' +import { AdminPendingCreation } from '@entity/AdminPendingCreation' +// import CONFIG from '@/config/index' + +export const creationFactory = async ( + client: ApolloServerTestClient, + creation: CreationInterface, +): Promise => { + const { mutate, query } = client + + // get Peter Lustig + const peterLustig = await User.findOneOrFail({ where: { email: 'peter@lustig.de' } }) + const variables = { ...creation, moderator: peterLustig.id } + + // login as Peter Lustig (admin) + await query({ query: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' } }) + + await mutate({ mutation: createPendingCreation, variables }) + + // get User + const user = await User.findOneOrFail({ where: { email: creation.email } }) + + if (creation.confirmed) { + const pendingCreation = await AdminPendingCreation.findOneOrFail({ + where: { userId: user.id }, + order: { created: 'DESC' }, + }) + + await mutate({ mutation: confirmPendingCreation, variables: { id: pendingCreation.id } }) + } +} diff --git a/backend/src/seeds/index.ts b/backend/src/seeds/index.ts index 28660912a..3943a63e0 100644 --- a/backend/src/seeds/index.ts +++ b/backend/src/seeds/index.ts @@ -7,14 +7,16 @@ import { createTestClient } from 'apollo-server-testing' import { name, internet, random } from 'faker' import { users } from './users/index' +import { creations } from './creation/index' import { userFactory } from './factory/user' +import { creationFactory } from './factory/creation' import { entities } from '@entity/index' const context = { token: '', setHeaders: { - push: (value: string): void => { - context.token = value + push: (value: { key: string; value: string }): void => { + context.token = value.value }, // eslint-disable-next-line @typescript-eslint/no-empty-function forEach: (): void => {}, @@ -38,18 +40,18 @@ const resetEntity = async (entity: any) => { const run = async () => { const server = await createServer(context) - const testClient = createTestClient(server.apollo) + const seedClient = createTestClient(server.apollo) const { con } = server await cleanDB() // seed the standard users for (let i = 0; i < users.length; i++) { - await userFactory(testClient, users[i]) + await userFactory(seedClient, users[i]) } // seed 100 random users for (let i = 0; i < 100; i++) { - await userFactory(testClient, { + await userFactory(seedClient, { firstName: name.firstName(), lastName: name.lastName(), email: internet.email(), @@ -57,6 +59,11 @@ const run = async () => { }) } + // create GDD + for (let i = 0; i < creations.length; i++) { + await creationFactory(seedClient, creations[i]) + } + await con.close() }