From 4d959470cef6a81db1d2ce06f446da5ba3a36695 Mon Sep 17 00:00:00 2001 From: elweyn Date: Wed, 8 Dec 2021 12:35:27 +0100 Subject: [PATCH 1/7] WIP: Change the getUserCreations function. --- backend/src/graphql/resolver/AdminResolver.ts | 143 +++++++++--------- 1 file changed, 73 insertions(+), 70 deletions(-) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index 3c03227d5..29c30e3de 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -218,84 +218,87 @@ async function getUserCreations(id: number): Promise { .andWhere({ targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { date: dateBeforeLastMonth, - enddate: dateLastMonth, - }), - }) - .getRawOne() - - const createdAmountLastMonth = await transactionCreationRepository - .createQueryBuilder('transaction_creations') - .select('SUM(transaction_creations.amount)', 'sum') - .where('transaction_creations.state_user_id = :id', { id }) - .andWhere({ - targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { - date: dateLastMonth, - enddate: dateMonth, - }), - }) - .getRawOne() - - const createdAmountMonth = await transactionCreationRepository - .createQueryBuilder('transaction_creations') - .select('SUM(transaction_creations.amount)', 'sum') - .where('transaction_creations.state_user_id = :id', { id }) - .andWhere({ - targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { - date: dateMonth, enddate: dateNextMonth, }), }) + .groupBy('EXTRACT(MONTH FROM transaction_creations.target_date)') .getRawOne() + console.log('createdAmountBeforeLastMonth', createdAmountBeforeLastMonth) - const pendingCreationRepository = getCustomRepository(PendingCreationRepository) - const pendingAmountMounth = await pendingCreationRepository - .createQueryBuilder('login_pending_tasks_admin') - .select('SUM(login_pending_tasks_admin.amount)', 'sum') - .where('login_pending_tasks_admin.userId = :id', { id }) - .andWhere({ - date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { - date: dateMonth, - enddate: dateNextMonth, - }), - }) - .getRawOne() + // const createdAmountLastMonth = await transactionCreationRepository + // .createQueryBuilder('transaction_creations') + // .select('SUM(transaction_creations.amount)', 'sum') + // .where('transaction_creations.state_user_id = :id', { id }) + // .andWhere({ + // targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { + // date: dateLastMonth, + // enddate: dateMonth, + // }), + // }) + // .getRawOne() - const pendingAmountLastMounth = await pendingCreationRepository - .createQueryBuilder('login_pending_tasks_admin') - .select('SUM(login_pending_tasks_admin.amount)', 'sum') - .where('login_pending_tasks_admin.userId = :id', { id }) - .andWhere({ - date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { - date: dateLastMonth, - enddate: dateMonth, - }), - }) - .getRawOne() + // const createdAmountMonth = await transactionCreationRepository + // .createQueryBuilder('transaction_creations') + // .select('SUM(transaction_creations.amount)', 'sum') + // .where('transaction_creations.state_user_id = :id', { id }) + // .andWhere({ + // targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { + // date: dateMonth, + // enddate: dateNextMonth, + // }), + // }) + // .getRawOne() - const pendingAmountBeforeLastMounth = await pendingCreationRepository - .createQueryBuilder('login_pending_tasks_admin') - .select('SUM(login_pending_tasks_admin.amount)', 'sum') - .where('login_pending_tasks_admin.userId = :id', { id }) - .andWhere({ - date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { - date: dateBeforeLastMonth, - enddate: dateLastMonth, - }), - }) - .getRawOne() + // const pendingCreationRepository = getCustomRepository(PendingCreationRepository) + // const pendingAmountMounth = await pendingCreationRepository + // .createQueryBuilder('login_pending_tasks_admin') + // .select('SUM(login_pending_tasks_admin.amount)', 'sum') + // .where('login_pending_tasks_admin.userId = :id', { id }) + // .andWhere({ + // date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { + // date: dateMonth, + // enddate: dateNextMonth, + // }), + // }) + // .getRawOne() - // COUNT amount from 2 tables - const usedCreationBeforeLastMonth = - (Number(createdAmountBeforeLastMonth.sum) + Number(pendingAmountBeforeLastMounth.sum)) / 10000 - const usedCreationLastMonth = - (Number(createdAmountLastMonth.sum) + Number(pendingAmountLastMounth.sum)) / 10000 - const usedCreationMonth = - (Number(createdAmountMonth.sum) + Number(pendingAmountMounth.sum)) / 10000 - return [ - 1000 - usedCreationBeforeLastMonth, - 1000 - usedCreationLastMonth, - 1000 - usedCreationMonth, - ] + // const pendingAmountLastMounth = await pendingCreationRepository + // .createQueryBuilder('login_pending_tasks_admin') + // .select('SUM(login_pending_tasks_admin.amount)', 'sum') + // .where('login_pending_tasks_admin.userId = :id', { id }) + // .andWhere({ + // date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { + // date: dateLastMonth, + // enddate: dateMonth, + // }), + // }) + // .getRawOne() + + // const pendingAmountBeforeLastMounth = await pendingCreationRepository + // .createQueryBuilder('login_pending_tasks_admin') + // .select('SUM(login_pending_tasks_admin.amount)', 'sum') + // .where('login_pending_tasks_admin.userId = :id', { id }) + // .andWhere({ + // date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { + // date: dateBeforeLastMonth, + // enddate: dateLastMonth, + // }), + // }) + // .getRawOne() + + // // COUNT amount from 2 tables + // const usedCreationBeforeLastMonth = + // (Number(createdAmountBeforeLastMonth.sum) + Number(pendingAmountBeforeLastMounth.sum)) / 10000 + // const usedCreationLastMonth = + // (Number(createdAmountLastMonth.sum) + Number(pendingAmountLastMounth.sum)) / 10000 + // const usedCreationMonth = + // (Number(createdAmountMonth.sum) + Number(pendingAmountMounth.sum)) / 10000 + // return [ + // 1000 - usedCreationBeforeLastMonth, + // 1000 - usedCreationLastMonth, + // 1000 - usedCreationMonth, + // ] + throw new Error('Not implemented yet') } function isCreationValid(creations: number[], amount: number, creationDate: Date) { From 77754760d531066e8ad1c4b856f20805ec570203 Mon Sep 17 00:00:00 2001 From: elweyn Date: Wed, 8 Dec 2021 12:40:15 +0100 Subject: [PATCH 2/7] WIP: Adding login_pending_tasks_admin query. --- backend/src/graphql/resolver/AdminResolver.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index 29c30e3de..71a6562f2 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -216,15 +216,29 @@ async function getUserCreations(id: number): Promise { .select('SUM(transaction_creations.amount)', 'sum') .where('transaction_creations.state_user_id = :id', { id }) .andWhere({ - targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { + targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :endDate`, { date: dateBeforeLastMonth, - enddate: dateNextMonth, + endDate: dateNextMonth, }), }) .groupBy('EXTRACT(MONTH FROM transaction_creations.target_date)') .getRawOne() console.log('createdAmountBeforeLastMonth', createdAmountBeforeLastMonth) + const pendingCreationRepository = getCustomRepository(PendingCreationRepository) + const pendingAmountBeforeLastMonth = await pendingCreationRepository + .createQueryBuilder('login_pending_tasks_admin') + .select('SUM(login_pending_tasks_admin.amount)', 'sum') + .where('login_pending_tasks_admin.userId = :id', { id }) + .andWhere({ + date: Raw((alias) => `${alias} >= :date and ${alias} < :endDate`, { + date: dateBeforeLastMonth, + endDate: dateNextMonth, + }), + }) + .orderBy('EXTRACT(MONTH FROM login_pending_tasks_admin.date)') + .getRawOne() + console.log(pendingAmountBeforeLastMonth) // const createdAmountLastMonth = await transactionCreationRepository // .createQueryBuilder('transaction_creations') // .select('SUM(transaction_creations.amount)', 'sum') From a6443317a94aedeef4b71d405c389db12a3a7f0e Mon Sep 17 00:00:00 2001 From: elweyn Date: Wed, 8 Dec 2021 13:16:17 +0100 Subject: [PATCH 3/7] Getting sum of last 3 month, not ordered by month. --- backend/src/graphql/resolver/AdminResolver.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index 71a6562f2..d8c6a28b8 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -221,9 +221,9 @@ async function getUserCreations(id: number): Promise { endDate: dateNextMonth, }), }) - .groupBy('EXTRACT(MONTH FROM transaction_creations.target_date)') + .orderBy('MONTH(transaction_creations.target_date)', 'ASC') .getRawOne() - console.log('createdAmountBeforeLastMonth', createdAmountBeforeLastMonth) + console.log('createdAmountBeforeLastMonth', id, createdAmountBeforeLastMonth) const pendingCreationRepository = getCustomRepository(PendingCreationRepository) const pendingAmountBeforeLastMonth = await pendingCreationRepository @@ -236,9 +236,15 @@ async function getUserCreations(id: number): Promise { endDate: dateNextMonth, }), }) - .orderBy('EXTRACT(MONTH FROM login_pending_tasks_admin.date)') + .orderBy('MONTH(login_pending_tasks_admin.date)', 'ASC') .getRawOne() - console.log(pendingAmountBeforeLastMonth) + console.log('pendingAmountBeforeLastMonth', id, pendingAmountBeforeLastMonth) + throw new Error('Not implemented yet') + // return [ + // 1000 - usedCreationBeforeLastMonth, + // 1000 - usedCreationLastMonth, + // 1000 - usedCreationMonth, + // ] // const createdAmountLastMonth = await transactionCreationRepository // .createQueryBuilder('transaction_creations') // .select('SUM(transaction_creations.amount)', 'sum') @@ -312,7 +318,6 @@ async function getUserCreations(id: number): Promise { // 1000 - usedCreationLastMonth, // 1000 - usedCreationMonth, // ] - throw new Error('Not implemented yet') } function isCreationValid(creations: number[], amount: number, creationDate: Date) { From f9c8a6861c7a5db369c159d5f31fd5406df75e28 Mon Sep 17 00:00:00 2001 From: elweyn Date: Wed, 8 Dec 2021 13:40:07 +0100 Subject: [PATCH 4/7] Get's every creation of last 3 month. --- backend/src/graphql/resolver/AdminResolver.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index d8c6a28b8..27230c7a5 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -213,7 +213,9 @@ async function getUserCreations(id: number): Promise { const transactionCreationRepository = getCustomRepository(TransactionCreationRepository) const createdAmountBeforeLastMonth = await transactionCreationRepository .createQueryBuilder('transaction_creations') - .select('SUM(transaction_creations.amount)', 'sum') + .select('transaction_creations.target_date') + .addSelect('MONTH(transaction_creations.target_date)', 'target_month') + .addSelect('transaction_creations.amount') .where('transaction_creations.state_user_id = :id', { id }) .andWhere({ targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :endDate`, { @@ -221,8 +223,8 @@ async function getUserCreations(id: number): Promise { endDate: dateNextMonth, }), }) - .orderBy('MONTH(transaction_creations.target_date)', 'ASC') - .getRawOne() + .orderBy('target_month', 'ASC') + .getRawMany() console.log('createdAmountBeforeLastMonth', id, createdAmountBeforeLastMonth) const pendingCreationRepository = getCustomRepository(PendingCreationRepository) From 6451d75a6e4c2aed0d0c804316b556cd07977b40 Mon Sep 17 00:00:00 2001 From: elweyn Date: Wed, 8 Dec 2021 14:02:56 +0100 Subject: [PATCH 5/7] Changed queries so that we get groupBy sum and orderBy date. --- backend/src/graphql/resolver/AdminResolver.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index 27230c7a5..90c7cba2f 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -213,9 +213,8 @@ async function getUserCreations(id: number): Promise { const transactionCreationRepository = getCustomRepository(TransactionCreationRepository) const createdAmountBeforeLastMonth = await transactionCreationRepository .createQueryBuilder('transaction_creations') - .select('transaction_creations.target_date') - .addSelect('MONTH(transaction_creations.target_date)', 'target_month') - .addSelect('transaction_creations.amount') + .select('MONTH(transaction_creations.target_date)', 'target_month') + .addSelect('SUM(transaction_creations.amount)', 'sum') .where('transaction_creations.state_user_id = :id', { id }) .andWhere({ targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :endDate`, { @@ -223,14 +222,17 @@ async function getUserCreations(id: number): Promise { endDate: dateNextMonth, }), }) + .groupBy('target_month') .orderBy('target_month', 'ASC') .getRawMany() console.log('createdAmountBeforeLastMonth', id, createdAmountBeforeLastMonth) + const pendingCreationRepository = getCustomRepository(PendingCreationRepository) const pendingAmountBeforeLastMonth = await pendingCreationRepository .createQueryBuilder('login_pending_tasks_admin') - .select('SUM(login_pending_tasks_admin.amount)', 'sum') + .select('MONTH(login_pending_tasks_admin.date)', 'target_month') + .addSelect('SUM(login_pending_tasks_admin.amount)', 'sum') .where('login_pending_tasks_admin.userId = :id', { id }) .andWhere({ date: Raw((alias) => `${alias} >= :date and ${alias} < :endDate`, { @@ -238,8 +240,13 @@ async function getUserCreations(id: number): Promise { endDate: dateNextMonth, }), }) - .orderBy('MONTH(login_pending_tasks_admin.date)', 'ASC') - .getRawOne() + .groupBy('target_month') + .orderBy('target_month', 'ASC') + .getRawMany() + pendingAmountBeforeLastMonth.reduce((total, pendingAmount) => { + total[pendingAmount.target_month] = pendingAmount + return total + }) console.log('pendingAmountBeforeLastMonth', id, pendingAmountBeforeLastMonth) throw new Error('Not implemented yet') // return [ From b77766b04df47338c4a4ba1a5d24721afd35f220 Mon Sep 17 00:00:00 2001 From: elweyn Date: Wed, 8 Dec 2021 14:51:10 +0100 Subject: [PATCH 6/7] Adding the pending and confirmed creation in a map and return old array. --- backend/src/graphql/resolver/AdminResolver.ts | 131 ++++++------------ 1 file changed, 41 insertions(+), 90 deletions(-) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index 90c7cba2f..174f38802 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -206,12 +206,12 @@ export class AdminResolver { async function getUserCreations(id: number): Promise { const dateNextMonth = moment().add(1, 'month').format('YYYY-MM') + '-01' - 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' - + const beforeLastMonthNumber = moment().subtract(2, 'month').format('M') + const lastMonthNumber = moment().subtract(1, 'month').format('M') + const currentMonthNumber = moment().format('M') const transactionCreationRepository = getCustomRepository(TransactionCreationRepository) - const createdAmountBeforeLastMonth = await transactionCreationRepository + const createdAmountsQuery = await transactionCreationRepository .createQueryBuilder('transaction_creations') .select('MONTH(transaction_creations.target_date)', 'target_month') .addSelect('SUM(transaction_creations.amount)', 'sum') @@ -225,11 +225,20 @@ async function getUserCreations(id: number): Promise { .groupBy('target_month') .orderBy('target_month', 'ASC') .getRawMany() - console.log('createdAmountBeforeLastMonth', id, createdAmountBeforeLastMonth) - + const map = new Map() + if (Array.isArray(createdAmountsQuery) && createdAmountsQuery.length > 0) { + createdAmountsQuery.forEach((createdAmount) => { + if (!map.has(createdAmount.target_month)) { + map.set(createdAmount.target_month, createdAmount.sum) + } else { + const store = map.get(createdAmount.target_month) + map.set(createdAmount.target_month, Number(store) + Number(createdAmount.sum)) + } + }) + } const pendingCreationRepository = getCustomRepository(PendingCreationRepository) - const pendingAmountBeforeLastMonth = await pendingCreationRepository + const pendingAmountsQuery = await pendingCreationRepository .createQueryBuilder('login_pending_tasks_admin') .select('MONTH(login_pending_tasks_admin.date)', 'target_month') .addSelect('SUM(login_pending_tasks_admin.amount)', 'sum') @@ -243,90 +252,32 @@ async function getUserCreations(id: number): Promise { .groupBy('target_month') .orderBy('target_month', 'ASC') .getRawMany() - pendingAmountBeforeLastMonth.reduce((total, pendingAmount) => { - total[pendingAmount.target_month] = pendingAmount - return total - }) - console.log('pendingAmountBeforeLastMonth', id, pendingAmountBeforeLastMonth) - throw new Error('Not implemented yet') - // return [ - // 1000 - usedCreationBeforeLastMonth, - // 1000 - usedCreationLastMonth, - // 1000 - usedCreationMonth, - // ] - // const createdAmountLastMonth = await transactionCreationRepository - // .createQueryBuilder('transaction_creations') - // .select('SUM(transaction_creations.amount)', 'sum') - // .where('transaction_creations.state_user_id = :id', { id }) - // .andWhere({ - // targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { - // date: dateLastMonth, - // enddate: dateMonth, - // }), - // }) - // .getRawOne() + if (Array.isArray(pendingAmountsQuery) && pendingAmountsQuery.length > 0) { + pendingAmountsQuery.forEach((pendingAmount) => { + if (!map.has(pendingAmount.target_month)) { + map.set(pendingAmount.target_month, pendingAmount.sum) + } else { + const store = map.get(pendingAmount.target_month) + map.set(pendingAmount.target_month, Number(store) + Number(pendingAmount.sum)) + } + }) + } + const usedCreationBeforeLastMonth = map.get(Number(beforeLastMonthNumber)) + ? Number(map.get(Number(beforeLastMonthNumber))) / 10000 + : 0 + const usedCreationLastMonth = map.get(Number(lastMonthNumber)) + ? Number(map.get(Number(lastMonthNumber))) / 10000 + : 0 - // const createdAmountMonth = await transactionCreationRepository - // .createQueryBuilder('transaction_creations') - // .select('SUM(transaction_creations.amount)', 'sum') - // .where('transaction_creations.state_user_id = :id', { id }) - // .andWhere({ - // targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { - // date: dateMonth, - // enddate: dateNextMonth, - // }), - // }) - // .getRawOne() - - // const pendingCreationRepository = getCustomRepository(PendingCreationRepository) - // const pendingAmountMounth = await pendingCreationRepository - // .createQueryBuilder('login_pending_tasks_admin') - // .select('SUM(login_pending_tasks_admin.amount)', 'sum') - // .where('login_pending_tasks_admin.userId = :id', { id }) - // .andWhere({ - // date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { - // date: dateMonth, - // enddate: dateNextMonth, - // }), - // }) - // .getRawOne() - - // const pendingAmountLastMounth = await pendingCreationRepository - // .createQueryBuilder('login_pending_tasks_admin') - // .select('SUM(login_pending_tasks_admin.amount)', 'sum') - // .where('login_pending_tasks_admin.userId = :id', { id }) - // .andWhere({ - // date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { - // date: dateLastMonth, - // enddate: dateMonth, - // }), - // }) - // .getRawOne() - - // const pendingAmountBeforeLastMounth = await pendingCreationRepository - // .createQueryBuilder('login_pending_tasks_admin') - // .select('SUM(login_pending_tasks_admin.amount)', 'sum') - // .where('login_pending_tasks_admin.userId = :id', { id }) - // .andWhere({ - // date: Raw((alias) => `${alias} >= :date and ${alias} < :enddate`, { - // date: dateBeforeLastMonth, - // enddate: dateLastMonth, - // }), - // }) - // .getRawOne() - - // // COUNT amount from 2 tables - // const usedCreationBeforeLastMonth = - // (Number(createdAmountBeforeLastMonth.sum) + Number(pendingAmountBeforeLastMounth.sum)) / 10000 - // const usedCreationLastMonth = - // (Number(createdAmountLastMonth.sum) + Number(pendingAmountLastMounth.sum)) / 10000 - // const usedCreationMonth = - // (Number(createdAmountMonth.sum) + Number(pendingAmountMounth.sum)) / 10000 - // return [ - // 1000 - usedCreationBeforeLastMonth, - // 1000 - usedCreationLastMonth, - // 1000 - usedCreationMonth, - // ] + const usedCreationCurrentMonth = map.get(Number(currentMonthNumber)) + ? Number(map.get(Number(currentMonthNumber))) / 10000 + : 0 + console.log(id, usedCreationBeforeLastMonth, usedCreationLastMonth, usedCreationCurrentMonth) + return [ + 1000 - usedCreationBeforeLastMonth, + 1000 - usedCreationLastMonth, + 1000 - usedCreationCurrentMonth, + ] } function isCreationValid(creations: number[], amount: number, creationDate: Date) { From bb6a260ab20e3cd5c0b80d0063d44452f54b21c9 Mon Sep 17 00:00:00 2001 From: elweyn Date: Wed, 8 Dec 2021 14:56:01 +0100 Subject: [PATCH 7/7] Clean-up code. --- backend/src/graphql/resolver/AdminResolver.ts | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index 174f38802..e901bb81a 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -210,6 +210,7 @@ async function getUserCreations(id: number): Promise { const beforeLastMonthNumber = moment().subtract(2, 'month').format('M') const lastMonthNumber = moment().subtract(1, 'month').format('M') const currentMonthNumber = moment().format('M') + const transactionCreationRepository = getCustomRepository(TransactionCreationRepository) const createdAmountsQuery = await transactionCreationRepository .createQueryBuilder('transaction_creations') @@ -225,17 +226,6 @@ async function getUserCreations(id: number): Promise { .groupBy('target_month') .orderBy('target_month', 'ASC') .getRawMany() - const map = new Map() - if (Array.isArray(createdAmountsQuery) && createdAmountsQuery.length > 0) { - createdAmountsQuery.forEach((createdAmount) => { - if (!map.has(createdAmount.target_month)) { - map.set(createdAmount.target_month, createdAmount.sum) - } else { - const store = map.get(createdAmount.target_month) - map.set(createdAmount.target_month, Number(store) + Number(createdAmount.sum)) - } - }) - } const pendingCreationRepository = getCustomRepository(PendingCreationRepository) const pendingAmountsQuery = await pendingCreationRepository @@ -252,6 +242,19 @@ async function getUserCreations(id: number): Promise { .groupBy('target_month') .orderBy('target_month', 'ASC') .getRawMany() + + const map = new Map() + if (Array.isArray(createdAmountsQuery) && createdAmountsQuery.length > 0) { + createdAmountsQuery.forEach((createdAmount) => { + if (!map.has(createdAmount.target_month)) { + map.set(createdAmount.target_month, createdAmount.sum) + } else { + const store = map.get(createdAmount.target_month) + map.set(createdAmount.target_month, Number(store) + Number(createdAmount.sum)) + } + }) + } + if (Array.isArray(pendingAmountsQuery) && pendingAmountsQuery.length > 0) { pendingAmountsQuery.forEach((pendingAmount) => { if (!map.has(pendingAmount.target_month)) { @@ -272,7 +275,7 @@ async function getUserCreations(id: number): Promise { const usedCreationCurrentMonth = map.get(Number(currentMonthNumber)) ? Number(map.get(Number(currentMonthNumber))) / 10000 : 0 - console.log(id, usedCreationBeforeLastMonth, usedCreationLastMonth, usedCreationCurrentMonth) + return [ 1000 - usedCreationBeforeLastMonth, 1000 - usedCreationLastMonth,