From 4f61af06aa52e6666fec8bb28fafaadde2e9832e Mon Sep 17 00:00:00 2001 From: Hannes Heine Date: Thu, 25 Nov 2021 10:22:42 +0100 Subject: [PATCH] Withdrew console.logs and changed creationDate type from any to Date. --- backend/src/graphql/resolver/AdminResolver.ts | 30 ++++--------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index ad8bd813e..9358dd0d2 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -31,15 +31,12 @@ export class AdminResolver { async createPendingCreation( @Args() { email, amount, note, creationDate, moderator }: CreatePendingCreationArgs, ): Promise { - // TODO: Check user validity const userRepository = getCustomRepository(UserRepository) const user = await userRepository.findByEmail(email) - // TODO: Check user open creation state (Open creation) + const creations = await getUserCreations(user.id) - console.log('creations', creations) + if (isCreationValid(creations, amount, creationDate)) { - // UserAdmin.creations() - // TODO: Write pending creation to DB const pendingCreationRepository = getCustomRepository(PendingCreationRepository) const loginPendingTaskAdmin = pendingCreationRepository.create() loginPendingTaskAdmin.userId = user.id @@ -60,13 +57,6 @@ async function getUserCreations(id: number): Promise { const dateMonth = moment().format('YYYY-MM') + '-01' const dateLastMonth = moment().subtract(1, 'month').format('YYYY-MM') + '-01' const dateBeforeLastMonth = moment().subtract(2, 'month').format('YYYY-MM') + '-01' - console.log( - 'Searching creation amount for: ', - dateNextMonth, - dateMonth, - dateLastMonth, - dateBeforeLastMonth, - ) const transactionCreationRepository = getCustomRepository(TransactionCreationRepository) const createdAmountBeforeLastMonth = await transactionCreationRepository @@ -80,7 +70,6 @@ async function getUserCreations(id: number): Promise { }), }) .getRawOne() - console.log('createdAmountBeforeLastMonth.sum', Number(createdAmountBeforeLastMonth.sum)) const createdAmountLastMonth = await transactionCreationRepository .createQueryBuilder('transaction_creations') @@ -93,7 +82,6 @@ async function getUserCreations(id: number): Promise { }), }) .getRawOne() - console.log('createdAmountLastMonth.sum', Number(createdAmountLastMonth.sum)) const createdAmountMonth = await transactionCreationRepository .createQueryBuilder('transaction_creations') @@ -106,7 +94,6 @@ async function getUserCreations(id: number): Promise { }), }) .getRawOne() - console.log('createdAmountMonth.sum', Number(createdAmountMonth.sum)) const pendingCreationRepository = getCustomRepository(PendingCreationRepository) const pendingAmountMounth = await pendingCreationRepository @@ -120,7 +107,6 @@ async function getUserCreations(id: number): Promise { }), }) .getRawOne() - console.log('pendingAmountMounth', Number(pendingAmountMounth.sum)) const pendingAmountLastMounth = await pendingCreationRepository .createQueryBuilder('login_pending_tasks_admin') @@ -133,7 +119,6 @@ async function getUserCreations(id: number): Promise { }), }) .getRawOne() - console.log('pendingAmountLastMounth', Number(pendingAmountLastMounth.sum)) const pendingAmountBeforeLastMounth = await pendingCreationRepository .createQueryBuilder('login_pending_tasks_admin') @@ -146,7 +131,6 @@ async function getUserCreations(id: number): Promise { }), }) .getRawOne() - console.log('pendingAmountBeforeLastMounth', Number(pendingAmountBeforeLastMounth.sum)) // COUNT amount from 2 tables const usedCreationBeforeLastMonth = @@ -155,9 +139,6 @@ async function getUserCreations(id: number): Promise { (Number(createdAmountLastMonth.sum) + Number(pendingAmountLastMounth.sum)) / 10000 const usedCreationMonth = (Number(createdAmountMonth.sum) + Number(pendingAmountMounth.sum)) / 10000 - console.log('1000 - usedCreationBeforeLastMonth', 1000 - usedCreationBeforeLastMonth) - console.log('1000 - usedCreationLastMonth', 1000 - usedCreationLastMonth) - console.log('1000 - usedCreationMonth', 1000 - usedCreationMonth) return [ 1000 - usedCreationBeforeLastMonth, 1000 - usedCreationLastMonth, @@ -165,13 +146,12 @@ async function getUserCreations(id: number): Promise { ] } -function isCreationValid(creations: number[], amount: number, creationDate: any) { +function isCreationValid(creations: number[], amount: number, creationDate: Date) { const dateMonth = moment().format('YYYY-MM') const dateLastMonth = moment().subtract(1, 'month').format('YYYY-MM') const dateBeforeLastMonth = moment().subtract(2, 'month').format('YYYY-MM') - // moment() const creationDateMonth = moment(creationDate).format('YYYY-MM') - console.log('CHECK: ', creationDateMonth, dateMonth, dateLastMonth, dateBeforeLastMonth) + let openCreation switch (creationDateMonth) { case dateMonth: @@ -186,7 +166,7 @@ function isCreationValid(creations: number[], amount: number, creationDate: any) default: throw new Error('CreationDate is not in last three months') } - console.log('creation >= amount', openCreation >= amount, openCreation, amount) + if (openCreation < amount) { throw new Error(`Open creation (${openCreation}) is less than amount (${amount})`) }