diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index 14551d7a5..416838c1c 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -212,95 +212,80 @@ 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('SUM(transaction_creations.amount)', 'sum') + .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`, { + targetDate: Raw((alias) => `${alias} >= :date and ${alias} < :endDate`, { date: dateBeforeLastMonth, - enddate: dateLastMonth, + 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 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() + .groupBy('target_month') + .orderBy('target_month', 'ASC') + .getRawMany() const loginPendingTasksAdminRepository = getCustomRepository(LoginPendingTasksAdminRepository) - const pendingAmountMounth = await loginPendingTasksAdminRepository + const pendingAmountsQuery = await loginPendingTasksAdminRepository .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`, { - date: dateMonth, - enddate: dateNextMonth, - }), - }) - .getRawOne() - - const pendingAmountLastMounth = await loginPendingTasksAdminRepository - .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 loginPendingTasksAdminRepository - .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: Raw((alias) => `${alias} >= :date and ${alias} < :endDate`, { date: dateBeforeLastMonth, - enddate: dateLastMonth, + endDate: dateNextMonth, }), }) - .getRawOne() + .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)) { + 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 usedCreationCurrentMonth = map.get(Number(currentMonthNumber)) + ? Number(map.get(Number(currentMonthNumber))) / 10000 + : 0 - // 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, + 1000 - usedCreationCurrentMonth, ] }