diff --git a/.env.example b/.env.example index 92f25840..2817b98e 100644 --- a/.env.example +++ b/.env.example @@ -22,7 +22,7 @@ HAPI_NETWORK_WALLET_URL=http://wallet:8888 HAPI_HASURA_URL=http://hasura:8080/v1/graphql HAPI_HASURA_ADMIN_SECRET=myadminsecretkey HAPI_EDEN_ELECTION_INTERVAL=7889400 -HAPI_EDEN_HISTORIC_ELECTION= [{"election": 0, "date_election": "2022-09-18"}, {"election": 1, "date_election": "2022-09-22"}, {"election": 2, "date_election": "2022-09-23T13:00:00.000"}, {"election": 3, "date_election": "2022-09-23T20:00:00.000"}, {"election": 4, "date_election": "2022-09-27"}, {"election": 5, "date_election": "2022-09-30"}] +HAPI_EDEN_HISTORIC_ELECTION= [{"election": 0, "date_election": "2022-09-18"}, {"election": 1, "date_election": "2022-09-22"}, {"election": 2, "date_election": "2022-09-23T13:00:00.000"}, {"election": 3, "date_election": "2022-09-23T20:00:00.000"}, {"election": 4, "date_election": "2022-09-27"}, {"election": 5, "date_election": "2022-09-30"}, {"election": 6, "date_election": "2022-12-26T13:05:00.000"}, {"election": 7, "date_election": "2022-12-26T19:05:00.000"}, {"election": 8, "date_election": "2022-12-28T19:05:00.000"}, {"election": 9, "date_election": "2023-01-03T19:00:00.000"}, {"election": 10, "date_election": "2023-02-13T17:30:00.000"}, {"election": 11, "date_election": "2023-02-15T17:30:00.000"}, {"election": 12, "date_election": "2023-02-17T20:30:00.000"}, {"election": 13, "date_election": "2023-02-23T17:30:00.000"}] HAPI_EDEN_CONTRACT=genesis.eden HAPI_RATE_HISTORY=https://api.coingecko.com/api/v3 HAPI_DFUSE_API_KEY= diff --git a/docker-compose.yaml b/docker-compose.yaml index fcccc6b3..e4bc0add 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -9,7 +9,7 @@ services: - ./tmp/wallet/:/opt/application/data/ postgres: container_name: 'eden-spending-postgres' - image: postgres:12.12-bullseye + image: postgres:12.14-bullseye ports: - '5432:5432' volumes: diff --git a/hapi/src/gql/eden-delegates.gql.js b/hapi/src/gql/eden-delegates.gql.js index 19b82470..c3f96d42 100644 --- a/hapi/src/gql/eden-delegates.gql.js +++ b/hapi/src/gql/eden-delegates.gql.js @@ -36,7 +36,7 @@ const get = async (where, getMany = false) => { return getMany ? edenDelegates : edenDelegates[0] } -const update = async (id, lastSyncedAt) => { +const update = async (id, data) => { const mutation = ` mutation ($id: uuid!, $payload: eden_delegates_set_input) { update_eden_delegates_by_pk(pk_columns: {id: $id}, _set: $payload) { @@ -48,9 +48,7 @@ const update = async (id, lastSyncedAt) => { await hasuraUtil.instance.request(mutation, { id, - payload: { - last_synced_at: lastSyncedAt - } + payload: data }) } diff --git a/hapi/src/gql/eden-transaction.gql.js b/hapi/src/gql/eden-transaction.gql.js index f628e360..692b2ec5 100644 --- a/hapi/src/gql/eden-transaction.gql.js +++ b/hapi/src/gql/eden-transaction.gql.js @@ -28,6 +28,7 @@ const get = async (where, getMany = false) => { txid type updated_at + eos_exchange digest } } @@ -73,13 +74,36 @@ const update = async ({ where, _set }) => { return data.update_eden_transaction } +const getHistoricIncome = async (where, getMany = false) => { + const query = ` + query ($where: historic_incomes_bool_exp) { + historic_incomes(where: $where) { + recipient + eos_claimed + usd_claimed + eos_unclaimed + usd_unclaimed + election + delegate_level + } + }` + + const { historic_incomes: historicIncomes } = + await hasuraUtil.instance.request(query, { + where + }) + + return getMany ? historicIncomes : historicIncomes[0] +} + const getAggregate = async where => { const query = ` query ($where: eden_transaction_bool_exp) { - eden_transaction_aggregate(where: $where) { + eden_transaction_aggregate(where: $where, distinct_on: txid) { aggregate { sum { amount + usd_total } } } @@ -90,7 +114,7 @@ const getAggregate = async where => { where }) - return edenTransactionAggregate?.aggregate?.sum?.amount || 0 + return edenTransactionAggregate?.aggregate?.sum || {} } module.exports = { @@ -98,5 +122,6 @@ module.exports = { get, deleteTx, update, - getAggregate + getAggregate, + getHistoricIncome } diff --git a/hapi/src/gql/eden_global_amount.gql.js b/hapi/src/gql/eden_global_amount.gql.js new file mode 100644 index 00000000..0600548f --- /dev/null +++ b/hapi/src/gql/eden_global_amount.gql.js @@ -0,0 +1,86 @@ +const { hasuraUtil } = require('../utils') + +const save = async payload => { + const mutation = ` + mutation ($payload: global_amount_insert_input!) { + insert_global_amount_one(object: $payload) { + id + eos_income + usd_income + usd_expense + eos_expense + election + account + } + } + ` + const data = await hasuraUtil.instance.request(mutation, { payload }) + + return data.insert_global_amount_one +} + +const get = async (where, getMany = false) => { + const query = ` + query ($where: global_amount_bool_exp) { + global_amount(where: $where) { + id + eos_income + usd_income + usd_expense + eos_expense + election + account + } + } + ` + const { global_amount } = await hasuraUtil.instance.request(query, { + where + }) + + return getMany ? global_amount : global_amount[0] +} + +const update = async ({ where, _set }) => { + const mutation = ` + mutation update($where: global_amount_bool_exp!, $_set: global_amount_set_input) { + update_global_amount(where: $where, _set: $_set) { + returning { + id + } + } + } + ` + const data = await hasuraUtil.instance.request(mutation, { + where, + _set + }) + + return data.update_eden_transaction +} + +const getAggregate = async where => { + const query = ` + query ($where: global_amount_bool_exp) { + global_amount_aggregate(where: $where) { + aggregate { + sum { + eos_income + usd_income + } + } + } + } + ` + const { global_amount } = await hasuraUtil.instance.request(query, { + where + }) + + return global_amount?.aggregate?.sum || {} +} + +module.exports = { + save, + get, + update, + getAggregate +} diff --git a/hapi/src/gql/eden_total_expense_by_delegate_and_election.js b/hapi/src/gql/eden_total_expense_by_delegate_and_election.js new file mode 100644 index 00000000..38775c7a --- /dev/null +++ b/hapi/src/gql/eden_total_expense_by_delegate_and_election.js @@ -0,0 +1,81 @@ +const { hasuraUtil } = require('../utils') + +const save = async payload => { + const mutation = ` + mutation ($payload: total_expense_by_delegate_and_election_insert_input!) { + insert_total_expense_by_delegate_and_election_one(object: $payload) { + id + } + } + ` + const data = await hasuraUtil.instance.request(mutation, { payload }) + + return data.insert_total_expense_by_delegate_and_election_one +} + +const get = async (where, getMany = false) => { + const query = ` + query ($where: total_expense_by_delegate_and_election_bool_exp) { + total_expense_by_delegate_and_election(where: $where) { + id + eos_amount + usd_amount + id_election + category + } + } + ` + const { total_expense_by_delegate_and_election } = + await hasuraUtil.instance.request(query, { + where + }) + + return getMany + ? total_expense_by_delegate_and_election + : total_expense_by_delegate_and_election[0] +} + +const update = async ({ where, _set }) => { + const mutation = ` + mutation update($where: total_expense_by_delegate_and_election_bool_exp!, $_set: total_expense_by_delegate_and_election_set_input) { + update_total_expense_by_delegate_and_election(where: $where, _set: $_set) { + returning { + id + } + } + } + ` + const data = await hasuraUtil.instance.request(mutation, { + where, + _set + }) + + return data.update_eden_transaction +} + +const getAggregate = async where => { + const query = ` + query ($where: total_expense_by_delegate_and_election_bool_exp) { + total_expense_by_delegate_and_election_aggregate(where: $where) { + aggregate { + sum { + eos_amount + } + } + } + } + ` + const { total_expense_by_delegate_and_election } = + await hasuraUtil.instance.request(query, { + where + }) + + return total_expense_by_delegate_and_election?.aggregate?.sum?.eos_amount || 0 +} + +module.exports = { + save, + get, + update, + getAggregate +} diff --git a/hapi/src/gql/index.js b/hapi/src/gql/index.js index 55bbe752..041c2abc 100644 --- a/hapi/src/gql/index.js +++ b/hapi/src/gql/index.js @@ -3,5 +3,7 @@ module.exports = { edenTreasury: require('./eden-treasury.gql'), edenDelegatesGql: require('./eden-delegates.gql'), edenElectionGql: require('./eden-election.gql'), - edenHistoricElectionGql: require('./eden-historic-election.gql') + edenHistoricElectionGql: require('./eden-historic-election.gql'), + edenTotalExpenseByDelegateAndElection: require('./eden_total_expense_by_delegate_and_election'), + edenGlobalAmountGql: require('./eden_global_amount.gql') } diff --git a/hapi/src/services/dfuse/index.js b/hapi/src/services/dfuse/index.js index 4a5bf5d4..a7526068 100644 --- a/hapi/src/services/dfuse/index.js +++ b/hapi/src/services/dfuse/index.js @@ -141,16 +141,37 @@ const getDelegateData = async () => { const delegate = delegatesList[index] let hasMore = true let actions = [] - let blockNumber = delegate.last_synced_at + let blockNumber = delegate.last_synced_income_at try { while (hasMore) { ;({ hasMore, actions, blockNumber } = await getActions({ - query: `account:${edenConfig.edenContract} data.from:${delegate.account} data.to:${delegate.account} OR account:eosio.token data.from:${delegate.account} receiver:eosio.token OR data.account:${delegate.account} receiver:edenexplorer`, + query: `account:${edenConfig.edenContract} data.from:${delegate.account} data.to:${delegate.account}`, lowBlockNum: blockNumber })) await runUpdaters(actions) - await edenDelegatesGql.update(delegate.id, blockNumber) + + await edenDelegatesGql.update(delegate.id, { + last_synced_income_at: blockNumber + }) + await sleepUtil(10) + } + + hasMore = true + blockNumber = delegate.last_synced_at + + while (hasMore) { + await runUpdaters(actions) + ;({ hasMore, actions, blockNumber } = await getActions({ + query: `account:eosio.token data.from:${delegate.account} receiver:eosio.token OR data.account:${delegate.account} receiver:edenexplorer`, + lowBlockNum: blockNumber + })) + + await runUpdaters(actions) + + await edenDelegatesGql.update(delegate.id, { + last_synced_at: blockNumber + }) await sleepUtil(10) } } catch (error) { diff --git a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js index 158dc954..f106c119 100644 --- a/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js +++ b/hapi/src/services/dfuse/updaters/edenexplorer-categorize.updater.js @@ -1,4 +1,9 @@ -const { edenTransactionGql, edenElectionGql } = require('../../../gql') +const { + edenTransactionGql, + edenElectionGql, + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql +} = require('../../../gql') const { updaterUtil } = require('../../../utils') module.exports = { @@ -12,6 +17,7 @@ module.exports = { const { category, description } = updaterUtil.memoSplit(memo) const transactionToEditQuery = { + type: { _eq: 'expense' }, txid: { _eq: tx_id }, ...(digest && { digest: { _eq: digest } }) } @@ -23,12 +29,7 @@ module.exports = { if (!transactionToEdit) return const { idElection: id_election } = - await updaterUtil.getElectionWithoutExpense( - account, - transactionToEdit.amount, - edenElectionGql, - edenTransactionGql - ) + await updaterUtil.getElectionWithoutExpense(account, edenElectionGql) const updateQuery = { where: { @@ -39,6 +40,31 @@ module.exports = { } await edenTransactionGql.update(updateQuery) + + const existExpense = await edenTotalExpenseByDelegateAndElection.get({ + tx_id: { _eq: transactionToEdit.digest } + }) + + if (transactionToEdit.category === 'uncategorized' || !existExpense) { + await updaterUtil.saveTotalByDelegateAndElection( + transactionToEdit.digest, + account, + transactionToEdit.amount, + transactionToEdit.eos_exchange, + category, + edenElectionGql, + edenTransactionGql, + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql + ) + } else { + await edenTotalExpenseByDelegateAndElection.update({ + where: { + tx_id: { _eq: transactionToEdit.digest } + }, + _set: { category } + }) + } } catch (error) { console.error(`categorize updater sync error: ${error.message}`) } diff --git a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js index e1da035e..7fbf1885 100644 --- a/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js +++ b/hapi/src/services/dfuse/updaters/eosiotoken-transfer.updater.js @@ -1,7 +1,12 @@ const moment = require('moment') const { sleepUtil, communityUtil, updaterUtil } = require('../../../utils') -const { edenTransactionGql, edenElectionGql } = require('../../../gql') +const { + edenTransactionGql, + edenElectionGql, + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql +} = require('../../../gql') const { transactionConstant } = require('../../../constants') let LASTEST_RATE_DATE_CONSULTED = null @@ -32,6 +37,19 @@ module.exports = { }, _set: { digest: action.action.receipt.digest } }) + + await updaterUtil.saveTotalByDelegateAndElection( + action.action.receipt.digest, + from, + amount, + registeredTxWithoutDigest.eos_exchange, + registeredTxWithoutDigest.category, + edenElectionGql, + edenTransactionGql, + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql + ) + return } @@ -47,13 +65,6 @@ module.exports = { } try { - const { idElection } = await updaterUtil.getElectionWithoutExpense( - from, - amount, - edenElectionGql, - edenTransactionGql - ) - const txDate = moment(action.timestamp).format('DD-MM-YYYY') if (LASTEST_RATE_DATE_CONSULTED !== txDate) { @@ -78,8 +89,7 @@ module.exports = { category, date: action.timestamp, description, - id_election: - category === 'uncategorized' ? action.election.id : idElection, + id_election: action.election.id, recipient: to, type: 'expense', eos_exchange: LASTEST_RATE_DATA_CONSULTED, @@ -88,6 +98,20 @@ module.exports = { } await edenTransactionGql.save(transactionData) + + if (category === 'uncategorized') return + + await updaterUtil.saveTotalByDelegateAndElection( + action.action.receipt.digest, + from, + amount, + LASTEST_RATE_DATA_CONSULTED, + category, + edenElectionGql, + edenTransactionGql, + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql + ) } catch (error) { console.error( `transfer sync error ${action.action.name}: ${error.message}` diff --git a/hapi/src/services/dfuse/updaters/genesiseden-fundtransfer.updater.js b/hapi/src/services/dfuse/updaters/genesiseden-fundtransfer.updater.js index f931da98..4298bc3a 100644 --- a/hapi/src/services/dfuse/updaters/genesiseden-fundtransfer.updater.js +++ b/hapi/src/services/dfuse/updaters/genesiseden-fundtransfer.updater.js @@ -69,7 +69,7 @@ module.exports = { eden_delegate: { account: { _eq: from } } }, description: { _eq: `distribution funds rank ${rank}` }, - type: { _eq: 'unclaimed' } + category: { _eq: 'unclaimed' } }) if (!existUnclaimedTx) return diff --git a/hapi/src/services/eden-unclaimed-funds.service.js b/hapi/src/services/eden-unclaimed-funds.service.js index 33319e95..c67255c5 100644 --- a/hapi/src/services/eden-unclaimed-funds.service.js +++ b/hapi/src/services/eden-unclaimed-funds.service.js @@ -104,7 +104,14 @@ const updateEdenUncleimedFundsWorker = () => { action: updateUnclaimedFunds } } +const updateEdenUncleimedFundsWorker2 = () => { + return { + name: servicesConstant.MESSAGES.uncleimedFunds, + action: updateUnclaimedFunds + } +} module.exports = { - updateEdenUncleimedFundsWorker + updateEdenUncleimedFundsWorker, + updateEdenUncleimedFundsWorker2 } diff --git a/hapi/src/services/worker.service.js b/hapi/src/services/worker.service.js index 15bda0ac..13904524 100644 --- a/hapi/src/services/worker.service.js +++ b/hapi/src/services/worker.service.js @@ -47,6 +47,7 @@ const run = async ({ name, action, interval }) => { const init = async () => { await hasuraUtil.hasuraAssembled() await run(edenHistoricDelegateService.updateHistoricDelegatesWorker()) + await run(edenUnclaimedFundsService.updateEdenUncleimedFundsWorker2()) run(edenDelegatesService.updateEdenTableWorker()) run(edenUnclaimedFundsService.updateEdenUncleimedFundsWorker()) run(dfuseService.syncWorker()) diff --git a/hapi/src/utils/community.util.js b/hapi/src/utils/community.util.js index a18733ad..d3344114 100644 --- a/hapi/src/utils/community.util.js +++ b/hapi/src/utils/community.util.js @@ -11,7 +11,7 @@ const nextElectionDate = async () => { const response = await loadTableData({ next_key: null }, 'elect.curr') const nextElection = response.rows[0] const now = moment(new Date()) - const end = moment(nextElection[1].start_time).add(1, 'day') + const end = moment(nextElection[1].start_time) const duration = moment.duration(end.diff(now)).asSeconds() return duration > 0 ? duration : servicesConstant.INTERVALS.oneHour @@ -29,7 +29,7 @@ const nextDistributionDate = async () => { const nextDistribution = response.rows[0] const now = moment(new Date()) - const end = moment(nextDistribution[1].distribution_time).add(1, 'day') + const end = moment(nextDistribution[1].distribution_time) const duration = moment.duration(end.diff(now)).asSeconds() return duration > 0 ? duration : servicesConstant.INTERVALS.oneHour diff --git a/hapi/src/utils/updater.util.js b/hapi/src/utils/updater.util.js index 00e2af6a..f6ef837b 100644 --- a/hapi/src/utils/updater.util.js +++ b/hapi/src/utils/updater.util.js @@ -10,62 +10,156 @@ const memoSplit = memoString => { const category = transactionConstant.CATEGORIES.includes( memoSplit[0].toLowerCase().trim() ) - ? memoSplit[0] + ? memoSplit[0].toLowerCase().trim() : 'uncategorized' const description = category !== 'uncategorized' ? memoSplit[1] : memoString return { category, description } } -const getElectionWithoutExpense = async ( +const getElectionWithoutExpense = async (delegateAccount, edenElectionGql) => { + try { + const electionsQuery = { + eden_delegate: { account: { _eq: delegateAccount } } + } + const elections = await edenElectionGql.get(electionsQuery, true) + + return { + election: elections.at(-1).election, + idElection: elections.at(-1).id + } + } catch (error) { + console.error('An error occurred in getElectionWithoutExpense: ', error) + } +} + +const saveTotalByDelegateAndElection = async ( + txId, delegateAccount, - amount, + eosAmount, + eosRate, + category, edenElectionGql, - edenTransactionGql + edenTransactionGql, + edenTotalExpenseByDelegateAndElection, + edenGlobalAmountGql ) => { try { + let tempAmount = eosAmount const electionsQuery = { eden_delegate: { account: { _eq: delegateAccount } } } const elections = await edenElectionGql.get(electionsQuery, true) for (const { id, election } of elections) { - const incomeQuery = { - eden_election: { - eden_delegate: { account: { _eq: delegateAccount } }, - election: { _eq: election } - }, - type: { _eq: 'income' } - } - const expenseQuery = { - eden_election: { - eden_delegate: { account: { _eq: delegateAccount } }, - election: { _eq: election } - }, - type: { _eq: 'expense' }, - category: { _neq: 'uncategorized' } + const globalAmountQuery = { + election: { _eq: election }, + account: { _eq: delegateAccount } } - const income = (await edenTransactionGql.getAggregate(incomeQuery)) || 0 + const { eos_claimed, eos_unclaimed, usd_claimed, usd_unclaimed } = + await edenTransactionGql.getHistoricIncome({ + election: { _eq: election }, + recipient: { _eq: delegateAccount } + }) - const expense = (await edenTransactionGql.getAggregate(expenseQuery)) || 0 + const amount = eos_claimed + eos_unclaimed || 0 + const usd_total = usd_claimed + usd_unclaimed || 0 - if (expense + amount <= income) { - return { election, idElection: id } + let globalAmount = await edenGlobalAmountGql.get(globalAmountQuery) + + if (!globalAmount) { + globalAmount = await edenGlobalAmountGql.save({ + account: delegateAccount, + election, + eos_income: Number(amount).toFixed(2), + usd_income: Number(usd_total).toFixed(2), + eos_expense: 0, + usd_expense: 0 + }) } - } - return { - election: elections.at(-1).election, - idElection: elections.at(-1).id + await edenGlobalAmountGql.update({ + where: globalAmountQuery, + _set: { + eos_income: Number(amount).toFixed(2), + usd_income: Number(usd_total).toFixed(2) + } + }) + + if (globalAmount.eos_expense >= globalAmount.eos_income) continue + + if (globalAmount.eos_expense + tempAmount > amount) { + const totalToSave = globalAmount.eos_income - globalAmount.eos_expense + + const totalByDelegateAndElectionData = { + eos_amount: totalToSave, + usd_amount: totalToSave * eosRate, + id_election: id, + category, + tx_id: txId, + election, + account: delegateAccount + } + + await edenGlobalAmountGql.update({ + where: globalAmountQuery, + _set: { + eos_expense: Number(globalAmount.eos_expense + totalToSave).toFixed( + 2 + ), + usd_expense: Number( + globalAmount.usd_expense + totalToSave * eosRate + ).toFixed(2) + } + }) + + await edenTotalExpenseByDelegateAndElection.save( + totalByDelegateAndElectionData + ) + + tempAmount = tempAmount - totalToSave + } else { + const totalByDelegateAndElectionData = { + eos_amount: tempAmount, + usd_amount: tempAmount * eosRate, + id_election: id, + category, + tx_id: txId, + election, + account: delegateAccount + } + + await edenGlobalAmountGql.update({ + where: globalAmountQuery, + _set: { + eos_expense: Number(globalAmount.eos_expense + tempAmount).toFixed( + 2 + ), + usd_expense: Number( + globalAmount.usd_expense + tempAmount * eosRate + ).toFixed(2) + } + }) + + await edenTotalExpenseByDelegateAndElection.save( + totalByDelegateAndElectionData + ) + + return + } } } catch (error) { - console.error('An error occurred in getElectionWithoutExpense: ', error) + console.error( + 'An error occurred in saveTotalByDelegateAndElection: ', + error + ) } } module.exports = { memoSplit, isEdenExpense, - getElectionWithoutExpense + getElectionWithoutExpense, + saveTotalByDelegateAndElection } diff --git a/hasura/metadata/databases/default/tables/public_eden_delegates.yaml b/hasura/metadata/databases/default/tables/public_eden_delegates.yaml index 6a8849c9..6205e654 100644 --- a/hasura/metadata/databases/default/tables/public_eden_delegates.yaml +++ b/hasura/metadata/databases/default/tables/public_eden_delegates.yaml @@ -17,6 +17,7 @@ select_permissions: - created_at - id - last_synced_at + - last_synced_income_at - updated_at filter: {} allow_aggregations: true @@ -25,5 +26,6 @@ update_permissions: permission: columns: - last_synced_at + - last_synced_income_at filter: {} check: null diff --git a/hasura/metadata/databases/default/tables/public_eden_election.yaml b/hasura/metadata/databases/default/tables/public_eden_election.yaml index 06767ab7..ff759b4a 100644 --- a/hasura/metadata/databases/default/tables/public_eden_election.yaml +++ b/hasura/metadata/databases/default/tables/public_eden_election.yaml @@ -13,6 +13,13 @@ array_relationships: table: name: eden_transaction schema: public + - name: total_expense_by_delegate_and_elections + using: + foreign_key_constraint_on: + column: id_election + table: + name: total_expense_by_delegate_and_election + schema: public select_permissions: - role: guest permission: diff --git a/hasura/metadata/databases/default/tables/public_election_summary.yaml b/hasura/metadata/databases/default/tables/public_election_summary.yaml new file mode 100644 index 00000000..0a95adb1 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_election_summary.yaml @@ -0,0 +1,3 @@ +table: + name: election_summary + schema: public diff --git a/hasura/metadata/databases/default/tables/public_expenses_by_category_and_election.yaml b/hasura/metadata/databases/default/tables/public_expenses_by_category_and_election.yaml new file mode 100644 index 00000000..fa0be86a --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_expenses_by_category_and_election.yaml @@ -0,0 +1,12 @@ +table: + name: expenses_by_category_and_election + schema: public +select_permissions: + - role: guest + permission: + columns: + - category + - election + - total_eos_amount + - total_usd_amount + filter: {} diff --git a/hasura/metadata/databases/default/tables/public_expenses_by_category_election_and_delegate.yaml b/hasura/metadata/databases/default/tables/public_expenses_by_category_election_and_delegate.yaml new file mode 100644 index 00000000..ed81e7f6 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_expenses_by_category_election_and_delegate.yaml @@ -0,0 +1,13 @@ +table: + name: expenses_by_category_election_and_delegate + schema: public +select_permissions: + - role: guest + permission: + columns: + - category + - election + - total_eos_amount + - total_usd_amount + - id_election + filter: {} diff --git a/hasura/metadata/databases/default/tables/public_global_amount.yaml b/hasura/metadata/databases/default/tables/public_global_amount.yaml new file mode 100644 index 00000000..3b49afb7 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_global_amount.yaml @@ -0,0 +1,40 @@ +table: + name: global_amount + schema: public +insert_permissions: + - role: guest + permission: + check: {} + columns: + - account + - election + - eos_income + - usd_income + - eos_expense + - usd_expense + - id +select_permissions: + - role: guest + permission: + columns: + - election + - eos_expense + - eos_income + - usd_expense + - usd_income + - account + - id + filter: {} +update_permissions: + - role: guest + permission: + columns: + - election + - eos_expense + - eos_income + - usd_expense + - usd_income + - account + - id + filter: {} + check: null diff --git a/hasura/metadata/databases/default/tables/public_total_by_delegate_and_election.yaml b/hasura/metadata/databases/default/tables/public_total_by_delegate_and_election.yaml new file mode 100644 index 00000000..e2024df8 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_total_by_delegate_and_election.yaml @@ -0,0 +1,15 @@ +table: + name: total_by_delegate_and_election + schema: public +select_permissions: + - role: guest + permission: + columns: + - account + - election + - eos_expense + - eos_income + - percent_claimed + - usd_expense + - usd_income + filter: {} diff --git a/hasura/metadata/databases/default/tables/public_total_expense_by_all_election.yaml b/hasura/metadata/databases/default/tables/public_total_expense_by_all_election.yaml index b5d9ba04..2fef4467 100644 --- a/hasura/metadata/databases/default/tables/public_total_expense_by_all_election.yaml +++ b/hasura/metadata/databases/default/tables/public_total_expense_by_all_election.yaml @@ -7,9 +7,8 @@ select_permissions: columns: - election - eos_categorized - - eos_uncategorized - - percent_categorized - - percent_uncategorized - usd_categorized + - eos_uncategorized - usd_uncategorized + - percent_categorized filter: {} diff --git a/hasura/metadata/databases/default/tables/public_total_expense_by_delegate_and_election.yaml b/hasura/metadata/databases/default/tables/public_total_expense_by_delegate_and_election.yaml new file mode 100644 index 00000000..9a46fe24 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_total_expense_by_delegate_and_election.yaml @@ -0,0 +1,37 @@ +table: + name: total_expense_by_delegate_and_election + schema: public +object_relationships: + - name: eden_election + using: + foreign_key_constraint_on: id_election +insert_permissions: + - role: guest + permission: + check: {} + columns: + - account + - category + - create_at + - election + - eos_amount + - id + - id_election + - tx_id + - updated_at + - usd_amount +select_permissions: + - role: guest + permission: + columns: + - account + - category + - create_at + - election + - eos_amount + - id + - id_election + - tx_id + - updated_at + - usd_amount + filter: {} diff --git a/hasura/metadata/databases/default/tables/public_total_expense_by_election_view.yaml b/hasura/metadata/databases/default/tables/public_total_expense_by_election_view.yaml new file mode 100644 index 00000000..83db3be5 --- /dev/null +++ b/hasura/metadata/databases/default/tables/public_total_expense_by_election_view.yaml @@ -0,0 +1,11 @@ +table: + name: total_expense_by_election_view + schema: public +select_permissions: + - role: guest + permission: + columns: + - total_eos_amount + - total_usd_amount + - id_election + filter: {} diff --git a/hasura/metadata/databases/default/tables/tables.yaml b/hasura/metadata/databases/default/tables/tables.yaml index 47d775bb..785ffc86 100644 --- a/hasura/metadata/databases/default/tables/tables.yaml +++ b/hasura/metadata/databases/default/tables/tables.yaml @@ -4,13 +4,17 @@ - "!include public_eden_transaction.yaml" - "!include public_eden_treasury.yaml" - "!include public_expenses_by_category_and_delegate.yaml" +- "!include public_expenses_by_category_and_election.yaml" +- "!include public_expenses_by_category_election_and_delegate.yaml" - "!include public_expenses_by_delegate.yaml" -- "!include public_historic_expenses.yaml" +- "!include public_global_amount.yaml" - "!include public_historic_incomes.yaml" - "!include public_incomes_by_delegate.yaml" - "!include public_total_by_category.yaml" - "!include public_total_by_category_and_election.yaml" +- "!include public_total_by_delegate_and_election.yaml" - "!include public_total_by_election.yaml" - "!include public_total_expense_by_all_election.yaml" +- "!include public_total_expense_by_delegate_and_election.yaml" - "!include public_total_income_by_all_elections.yaml" - "!include public_transaction_by_category_and_election.yaml" diff --git a/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/down.sql b/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/down.sql new file mode 100644 index 00000000..8bfd841a --- /dev/null +++ b/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/down.sql @@ -0,0 +1 @@ +DROP TABLE "public"."total_expense_by_delegate_and_election"; diff --git a/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/up.sql b/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/up.sql new file mode 100644 index 00000000..bbfe3299 --- /dev/null +++ b/hasura/migrations/default/1676257021815_create_table_public_total_expense_by_delegate_and_election/up.sql @@ -0,0 +1,2 @@ +CREATE TABLE "public"."total_expense_by_delegate_and_election" ("id" uuid NOT NULL DEFAULT gen_random_uuid(), "eos_amount" numeric NOT NULL DEFAULT 0, "usd_amount" numeric NOT NULL DEFAULT 0, "id_election" uuid NOT NULL, "category" varchar NOT NULL, "create_at" timestamptz NOT NULL DEFAULT now(), "updated_at" timestamptz NOT NULL DEFAULT now(), PRIMARY KEY ("id","id_election") , FOREIGN KEY ("id_election") REFERENCES "public"."eden_election"("id") ON UPDATE restrict ON DELETE restrict, UNIQUE ("id"), UNIQUE ("id_election")); +CREATE EXTENSION IF NOT EXISTS pgcrypto; diff --git a/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/down.sql b/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/down.sql new file mode 100644 index 00000000..ce4cbda1 --- /dev/null +++ b/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/down.sql @@ -0,0 +1 @@ +alter table "public"."total_expense_by_delegate_and_election" add constraint "total_expense_by_delegate_and_election_id_election_key" unique ("id_election"); diff --git a/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/up.sql b/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/up.sql new file mode 100644 index 00000000..fa1d79e4 --- /dev/null +++ b/hasura/migrations/default/1676259522486_alter_table_public_total_expense_by_delegate_and_election_drop_constraint_total_expense_by_delegate_and_election_id_election_key/up.sql @@ -0,0 +1 @@ +alter table "public"."total_expense_by_delegate_and_election" drop constraint "total_expense_by_delegate_and_election_id_election_key"; diff --git a/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/down.sql b/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/down.sql new file mode 100644 index 00000000..c40af06d --- /dev/null +++ b/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/down.sql @@ -0,0 +1,4 @@ +alter table "public"."total_expense_by_delegate_and_election" drop constraint "total_expense_by_delegate_and_election_pkey"; +alter table "public"."total_expense_by_delegate_and_election" + add constraint "total_expense_by_delegate_and_election_pkey" + primary key ("id", "id_election"); diff --git a/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/up.sql b/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/up.sql new file mode 100644 index 00000000..e58b6043 --- /dev/null +++ b/hasura/migrations/default/1676299667211_modify_primarykey_public_total_expense_by_delegate_and_election/up.sql @@ -0,0 +1,6 @@ +BEGIN TRANSACTION; +ALTER TABLE "public"."total_expense_by_delegate_and_election" DROP CONSTRAINT "total_expense_by_delegate_and_election_pkey"; + +ALTER TABLE "public"."total_expense_by_delegate_and_election" + ADD CONSTRAINT "total_expense_by_delegate_and_election_pkey" PRIMARY KEY ("id"); +COMMIT TRANSACTION; diff --git a/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/down.sql b/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/down.sql new file mode 100644 index 00000000..db189778 --- /dev/null +++ b/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."total_expense_by_delegate_and_election" add column "election" integer +-- not null default '0'; diff --git a/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/up.sql b/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/up.sql new file mode 100644 index 00000000..703f3596 --- /dev/null +++ b/hasura/migrations/default/1676325921458_alter_table_public_total_expense_by_delegate_and_election_add_column_election/up.sql @@ -0,0 +1,2 @@ +alter table "public"."total_expense_by_delegate_and_election" add column "election" integer + not null default '0'; diff --git a/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/down.sql b/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/down.sql new file mode 100644 index 00000000..2779ba67 --- /dev/null +++ b/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."total_expense_by_delegate_and_election" add column "tx_id" uuid +-- null; diff --git a/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/up.sql b/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/up.sql new file mode 100644 index 00000000..66fcbe59 --- /dev/null +++ b/hasura/migrations/default/1676327657273_alter_table_public_total_expense_by_delegate_and_election_add_column_tx_id/up.sql @@ -0,0 +1,2 @@ +alter table "public"."total_expense_by_delegate_and_election" add column "tx_id" uuid + null; diff --git a/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/down.sql b/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/down.sql new file mode 100644 index 00000000..8dfe99fc --- /dev/null +++ b/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."total_expense_by_delegate_and_election" ALTER COLUMN "tx_id" TYPE uuid; diff --git a/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/up.sql b/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/up.sql new file mode 100644 index 00000000..c50def10 --- /dev/null +++ b/hasura/migrations/default/1676328556378_alter_table_public_total_expense_by_delegate_and_election_alter_column_tx_id/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."total_expense_by_delegate_and_election" ALTER COLUMN "tx_id" TYPE text; diff --git a/hasura/migrations/default/1676352327360_run_sql_migration/down.sql b/hasura/migrations/default/1676352327360_run_sql_migration/down.sql new file mode 100644 index 00000000..09445d43 --- /dev/null +++ b/hasura/migrations/default/1676352327360_run_sql_migration/down.sql @@ -0,0 +1,6 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW total_expense_by_election_view AS +-- SELECT id_election, SUM(eos_amount) AS total_eos_amount, SUM(usd_amount) AS total_usd_amount +-- FROM total_expense_by_delegate_and_election +-- GROUP BY id_election; diff --git a/hasura/migrations/default/1676352327360_run_sql_migration/up.sql b/hasura/migrations/default/1676352327360_run_sql_migration/up.sql new file mode 100644 index 00000000..0dc43c67 --- /dev/null +++ b/hasura/migrations/default/1676352327360_run_sql_migration/up.sql @@ -0,0 +1,4 @@ +CREATE OR REPLACE VIEW total_expense_by_election_view AS +SELECT id_election, SUM(eos_amount) AS total_eos_amount, SUM(usd_amount) AS total_usd_amount +FROM total_expense_by_delegate_and_election +GROUP BY id_election; diff --git a/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/down.sql b/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/down.sql new file mode 100644 index 00000000..4c0f8967 --- /dev/null +++ b/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."total_expense_by_delegate_and_election" add column "account" varchar +-- null; diff --git a/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/up.sql b/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/up.sql new file mode 100644 index 00000000..99922db2 --- /dev/null +++ b/hasura/migrations/default/1676353223250_alter_table_public_total_expense_by_delegate_and_election_add_column_account/up.sql @@ -0,0 +1,2 @@ +alter table "public"."total_expense_by_delegate_and_election" add column "account" varchar + null; diff --git a/hasura/migrations/default/1676355867613_create_table_public_global_amount/down.sql b/hasura/migrations/default/1676355867613_create_table_public_global_amount/down.sql new file mode 100644 index 00000000..4516582b --- /dev/null +++ b/hasura/migrations/default/1676355867613_create_table_public_global_amount/down.sql @@ -0,0 +1 @@ +DROP TABLE "public"."global_amount"; diff --git a/hasura/migrations/default/1676355867613_create_table_public_global_amount/up.sql b/hasura/migrations/default/1676355867613_create_table_public_global_amount/up.sql new file mode 100644 index 00000000..ee12212a --- /dev/null +++ b/hasura/migrations/default/1676355867613_create_table_public_global_amount/up.sql @@ -0,0 +1 @@ +CREATE TABLE "public"."global_amount" ("account" text NOT NULL, "election" integer NOT NULL, "eos_income" integer NOT NULL, "usd_income" integer NOT NULL, "eos_expense" integer NOT NULL, "usd_expense" integer NOT NULL, "id" uuid NOT NULL, PRIMARY KEY ("id") ); diff --git a/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/down.sql b/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/down.sql new file mode 100644 index 00000000..375631cc --- /dev/null +++ b/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "election" TYPE integer; diff --git a/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/up.sql b/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/up.sql new file mode 100644 index 00000000..7a6e1ab2 --- /dev/null +++ b/hasura/migrations/default/1676359087677_alter_table_public_global_amount_alter_column_election/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "election" TYPE numeric; diff --git a/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/down.sql b/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/down.sql new file mode 100644 index 00000000..2d340730 --- /dev/null +++ b/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "eos_income" TYPE integer; diff --git a/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/up.sql b/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/up.sql new file mode 100644 index 00000000..7e1c8259 --- /dev/null +++ b/hasura/migrations/default/1676359101051_alter_table_public_global_amount_alter_column_eos_income/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "eos_income" TYPE numeric; diff --git a/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/down.sql b/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/down.sql new file mode 100644 index 00000000..5dd24b86 --- /dev/null +++ b/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "usd_income" TYPE integer; diff --git a/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/up.sql b/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/up.sql new file mode 100644 index 00000000..1c14a620 --- /dev/null +++ b/hasura/migrations/default/1676359107194_alter_table_public_global_amount_alter_column_usd_income/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "usd_income" TYPE numeric; diff --git a/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/down.sql b/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/down.sql new file mode 100644 index 00000000..49b0aa7f --- /dev/null +++ b/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "eos_expense" TYPE integer; diff --git a/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/up.sql b/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/up.sql new file mode 100644 index 00000000..28219d97 --- /dev/null +++ b/hasura/migrations/default/1676359113501_alter_table_public_global_amount_alter_column_eos_expense/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "eos_expense" TYPE numeric; diff --git a/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/down.sql b/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/down.sql new file mode 100644 index 00000000..17525e07 --- /dev/null +++ b/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/down.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "usd_expense" TYPE integer; diff --git a/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/up.sql b/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/up.sql new file mode 100644 index 00000000..ca4830c2 --- /dev/null +++ b/hasura/migrations/default/1676359119503_alter_table_public_global_amount_alter_column_usd_expense/up.sql @@ -0,0 +1 @@ +ALTER TABLE "public"."global_amount" ALTER COLUMN "usd_expense" TYPE numeric; diff --git a/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/down.sql b/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/down.sql new file mode 100644 index 00000000..621f303e --- /dev/null +++ b/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/down.sql @@ -0,0 +1,3 @@ +alter table "public"."global_amount" + add constraint "global_amount_pkey" + primary key ("id"); diff --git a/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/up.sql b/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/up.sql new file mode 100644 index 00000000..b39e56c6 --- /dev/null +++ b/hasura/migrations/default/1676359569675_modify_primarykey_public_global_amount/up.sql @@ -0,0 +1 @@ +alter table "public"."global_amount" drop constraint "global_amount_pkey"; diff --git a/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/down.sql b/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/down.sql new file mode 100644 index 00000000..c64338c4 --- /dev/null +++ b/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/down.sql @@ -0,0 +1,2 @@ +alter table "public"."global_amount" alter column "id" drop not null; +alter table "public"."global_amount" add column "id" uuid; diff --git a/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/up.sql b/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/up.sql new file mode 100644 index 00000000..1abd4b4f --- /dev/null +++ b/hasura/migrations/default/1676359717840_alter_table_public_global_amount_drop_column_id/up.sql @@ -0,0 +1 @@ +alter table "public"."global_amount" drop column "id" cascade; diff --git a/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/down.sql b/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/down.sql new file mode 100644 index 00000000..3bb509c9 --- /dev/null +++ b/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/down.sql @@ -0,0 +1,5 @@ +alter table "public"."global_amount" drop column "id" cascade +alter table "public"."global_amount" drop column "id"; +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE EXTENSION IF NOT EXISTS pgcrypto; diff --git a/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/up.sql b/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/up.sql new file mode 100644 index 00000000..03639e86 --- /dev/null +++ b/hasura/migrations/default/1676359745958_alter_table_public_global_amount_add_column_id/up.sql @@ -0,0 +1,3 @@ +CREATE EXTENSION IF NOT EXISTS pgcrypto; +alter table "public"."global_amount" add column "id" uuid + not null default gen_random_uuid(); diff --git a/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/down.sql b/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/down.sql new file mode 100644 index 00000000..b39e56c6 --- /dev/null +++ b/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/down.sql @@ -0,0 +1 @@ +alter table "public"."global_amount" drop constraint "global_amount_pkey"; diff --git a/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/up.sql b/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/up.sql new file mode 100644 index 00000000..621f303e --- /dev/null +++ b/hasura/migrations/default/1676359753172_modify_primarykey_public_global_amount/up.sql @@ -0,0 +1,3 @@ +alter table "public"."global_amount" + add constraint "global_amount_pkey" + primary key ("id"); diff --git a/hasura/migrations/default/1676413782853_run_sql_migration/down.sql b/hasura/migrations/default/1676413782853_run_sql_migration/down.sql new file mode 100644 index 00000000..4b67c47b --- /dev/null +++ b/hasura/migrations/default/1676413782853_run_sql_migration/down.sql @@ -0,0 +1,6 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW "public"."expenses_by_category_and_election" AS +-- SELECT id_election,category,election, SUM(eos_amount) AS total_eos_amount, SUM(usd_amount) AS total_usd_amount +-- FROM total_expense_by_delegate_and_election +-- GROUP BY id_election, category, election; diff --git a/hasura/migrations/default/1676413782853_run_sql_migration/up.sql b/hasura/migrations/default/1676413782853_run_sql_migration/up.sql new file mode 100644 index 00000000..3a9d23b1 --- /dev/null +++ b/hasura/migrations/default/1676413782853_run_sql_migration/up.sql @@ -0,0 +1,4 @@ +CREATE OR REPLACE VIEW "public"."expenses_by_category_and_election" AS +SELECT id_election,category,election, SUM(eos_amount) AS total_eos_amount, SUM(usd_amount) AS total_usd_amount +FROM total_expense_by_delegate_and_election +GROUP BY id_election, category, election; diff --git a/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/down.sql b/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/down.sql new file mode 100644 index 00000000..930490cb --- /dev/null +++ b/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/down.sql @@ -0,0 +1,3 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- DROP VIEW "public"."historic_expenses"; diff --git a/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/up.sql b/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/up.sql new file mode 100644 index 00000000..1303ce38 --- /dev/null +++ b/hasura/migrations/default/1676416649322_drop_view_public_historic_expenses/up.sql @@ -0,0 +1 @@ +DROP VIEW "public"."historic_expenses"; diff --git a/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/down.sql b/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/down.sql new file mode 100644 index 00000000..a5c53dba --- /dev/null +++ b/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/down.sql @@ -0,0 +1,3 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- DROP VIEW "public"."total_expense_by_election_view"; diff --git a/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/up.sql b/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/up.sql new file mode 100644 index 00000000..6629491b --- /dev/null +++ b/hasura/migrations/default/1676437036054_drop_view_public_total_expense_by_election_view/up.sql @@ -0,0 +1 @@ +DROP VIEW "public"."total_expense_by_election_view"; diff --git a/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/down.sql b/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/down.sql new file mode 100644 index 00000000..910d1733 --- /dev/null +++ b/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."eden_delegates" add column "last_synced_income_at" int8 +-- null default '209274902'; diff --git a/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/up.sql b/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/up.sql new file mode 100644 index 00000000..50058362 --- /dev/null +++ b/hasura/migrations/default/1676499950026_alter_table_public_eden_delegates_add_column_last_synced_income_at/up.sql @@ -0,0 +1,2 @@ +alter table "public"."eden_delegates" add column "last_synced_income_at" int8 + null default '209274902'; diff --git a/hasura/migrations/default/1676601295310_run_sql_migration/down.sql b/hasura/migrations/default/1676601295310_run_sql_migration/down.sql new file mode 100644 index 00000000..20f63ecc --- /dev/null +++ b/hasura/migrations/default/1676601295310_run_sql_migration/down.sql @@ -0,0 +1,3 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- Drop VIEW total_expense_by_all_election; diff --git a/hasura/migrations/default/1676601295310_run_sql_migration/up.sql b/hasura/migrations/default/1676601295310_run_sql_migration/up.sql new file mode 100644 index 00000000..9742283e --- /dev/null +++ b/hasura/migrations/default/1676601295310_run_sql_migration/up.sql @@ -0,0 +1 @@ +Drop VIEW total_expense_by_all_election; diff --git a/hasura/migrations/default/1676601302025_run_sql_migration/down.sql b/hasura/migrations/default/1676601302025_run_sql_migration/down.sql new file mode 100644 index 00000000..50e71caa --- /dev/null +++ b/hasura/migrations/default/1676601302025_run_sql_migration/down.sql @@ -0,0 +1,20 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW total_expense_by_all_election AS +-- WITH election_sums AS ( +-- SELECT election, +-- COALESCE(SUM(eos_claimed + eos_unclaimed), 0) AS eos_sum, +-- COALESCE(SUM(usd_claimed + usd_unclaimed), 0) AS usd_sum +-- FROM historic_incomes +-- GROUP BY election +-- ) +-- SELECT ga.election, +-- COALESCE(SUM(ga.eos_expense), 0) AS eos_categorized, +-- COALESCE(SUM(ga.usd_expense), 0) AS usd_categorized, +-- COALESCE(election_sums.eos_sum - SUM(ga.eos_expense), 0) AS eos_uncategorized, +-- COALESCE(election_sums.usd_sum - SUM(ga.usd_expense), 0) AS usd_uncategorized, +-- COALESCE(SUM(ga.eos_expense) / NULLIF(election_sums.eos_sum, 0) * 100, 0) AS percent_categorized +-- FROM global_amount ga +-- LEFT JOIN election_sums ON ga.election = election_sums.election +-- GROUP BY ga.election, election_sums.eos_sum, election_sums.usd_sum +-- ORDER BY ga.election; diff --git a/hasura/migrations/default/1676601302025_run_sql_migration/up.sql b/hasura/migrations/default/1676601302025_run_sql_migration/up.sql new file mode 100644 index 00000000..1c3b7022 --- /dev/null +++ b/hasura/migrations/default/1676601302025_run_sql_migration/up.sql @@ -0,0 +1,18 @@ +CREATE OR REPLACE VIEW total_expense_by_all_election AS +WITH election_sums AS ( + SELECT election, + COALESCE(SUM(eos_claimed + eos_unclaimed), 0) AS eos_sum, + COALESCE(SUM(usd_claimed + usd_unclaimed), 0) AS usd_sum + FROM historic_incomes + GROUP BY election +) +SELECT ga.election, + COALESCE(SUM(ga.eos_expense), 0) AS eos_categorized, + COALESCE(SUM(ga.usd_expense), 0) AS usd_categorized, + COALESCE(election_sums.eos_sum - SUM(ga.eos_expense), 0) AS eos_uncategorized, + COALESCE(election_sums.usd_sum - SUM(ga.usd_expense), 0) AS usd_uncategorized, + COALESCE(SUM(ga.eos_expense) / NULLIF(election_sums.eos_sum, 0) * 100, 0) AS percent_categorized +FROM global_amount ga +LEFT JOIN election_sums ON ga.election = election_sums.election +GROUP BY ga.election, election_sums.eos_sum, election_sums.usd_sum +ORDER BY ga.election; diff --git a/hasura/migrations/default/1676601336185_run_sql_migration/down.sql b/hasura/migrations/default/1676601336185_run_sql_migration/down.sql new file mode 100644 index 00000000..686ed6df --- /dev/null +++ b/hasura/migrations/default/1676601336185_run_sql_migration/down.sql @@ -0,0 +1,13 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW "public"."total_by_delegate_and_election" AS +-- SELECT hi.election, +-- hi.recipient AS account, +-- sum((hi.eos_claimed + hi.eos_unclaimed)) AS eos_income, +-- sum((hi.usd_unclaimed + hi.usd_claimed)) AS usd_income, +-- COALESCE(sum(ga.eos_expense), (0)::numeric) AS eos_expense, +-- COALESCE(sum(ga.usd_expense), (0)::numeric) AS usd_expense, +-- COALESCE(((sum(ga.eos_expense) / sum((hi.eos_claimed + hi.eos_unclaimed))) * (100)::numeric), (0)::numeric) AS percent_claimed +-- FROM (historic_incomes hi +-- LEFT JOIN global_amount ga ON (((hi.election = ga.election) AND ((hi.recipient)::text = ga.account)))) +-- GROUP BY hi.election, hi.recipient; diff --git a/hasura/migrations/default/1676601336185_run_sql_migration/up.sql b/hasura/migrations/default/1676601336185_run_sql_migration/up.sql new file mode 100644 index 00000000..5ef279d4 --- /dev/null +++ b/hasura/migrations/default/1676601336185_run_sql_migration/up.sql @@ -0,0 +1,11 @@ +CREATE OR REPLACE VIEW "public"."total_by_delegate_and_election" AS + SELECT hi.election, + hi.recipient AS account, + sum((hi.eos_claimed + hi.eos_unclaimed)) AS eos_income, + sum((hi.usd_unclaimed + hi.usd_claimed)) AS usd_income, + COALESCE(sum(ga.eos_expense), (0)::numeric) AS eos_expense, + COALESCE(sum(ga.usd_expense), (0)::numeric) AS usd_expense, + COALESCE(((sum(ga.eos_expense) / sum((hi.eos_claimed + hi.eos_unclaimed))) * (100)::numeric), (0)::numeric) AS percent_claimed + FROM (historic_incomes hi + LEFT JOIN global_amount ga ON (((hi.election = ga.election) AND ((hi.recipient)::text = ga.account)))) + GROUP BY hi.election, hi.recipient; diff --git a/hasura/migrations/default/1677714380669_run_sql_migration/down.sql b/hasura/migrations/default/1677714380669_run_sql_migration/down.sql new file mode 100644 index 00000000..d4cdc5b2 --- /dev/null +++ b/hasura/migrations/default/1677714380669_run_sql_migration/down.sql @@ -0,0 +1,3 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- DROP VIEW "public"."expenses_by_category_and_election"; diff --git a/hasura/migrations/default/1677714380669_run_sql_migration/up.sql b/hasura/migrations/default/1677714380669_run_sql_migration/up.sql new file mode 100644 index 00000000..5029bec0 --- /dev/null +++ b/hasura/migrations/default/1677714380669_run_sql_migration/up.sql @@ -0,0 +1 @@ +DROP VIEW "public"."expenses_by_category_and_election"; diff --git a/hasura/migrations/default/1677714387991_run_sql_migration/down.sql b/hasura/migrations/default/1677714387991_run_sql_migration/down.sql new file mode 100644 index 00000000..df8893f7 --- /dev/null +++ b/hasura/migrations/default/1677714387991_run_sql_migration/down.sql @@ -0,0 +1,10 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW "public"."expenses_by_category_and_election" AS +-- SELECT +-- total_expense_by_delegate_and_election.category, +-- total_expense_by_delegate_and_election.election, +-- sum(total_expense_by_delegate_and_election.eos_amount) AS total_eos_amount, +-- sum(total_expense_by_delegate_and_election.usd_amount) AS total_usd_amount +-- FROM total_expense_by_delegate_and_election +-- GROUP BY total_expense_by_delegate_and_election.category, total_expense_by_delegate_and_election.election; diff --git a/hasura/migrations/default/1677714387991_run_sql_migration/up.sql b/hasura/migrations/default/1677714387991_run_sql_migration/up.sql new file mode 100644 index 00000000..8b5cf0cf --- /dev/null +++ b/hasura/migrations/default/1677714387991_run_sql_migration/up.sql @@ -0,0 +1,8 @@ +CREATE OR REPLACE VIEW "public"."expenses_by_category_and_election" AS + SELECT + total_expense_by_delegate_and_election.category, + total_expense_by_delegate_and_election.election, + sum(total_expense_by_delegate_and_election.eos_amount) AS total_eos_amount, + sum(total_expense_by_delegate_and_election.usd_amount) AS total_usd_amount + FROM total_expense_by_delegate_and_election + GROUP BY total_expense_by_delegate_and_election.category, total_expense_by_delegate_and_election.election; diff --git a/hasura/migrations/default/1677733616588_run_sql_migration/down.sql b/hasura/migrations/default/1677733616588_run_sql_migration/down.sql new file mode 100644 index 00000000..cf9435b6 --- /dev/null +++ b/hasura/migrations/default/1677733616588_run_sql_migration/down.sql @@ -0,0 +1,14 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- CREATE OR REPLACE VIEW "public"."expenses_by_category_election_and_delegate" AS +-- +-- SELECT total_expense_by_delegate_and_election.id_election, +-- total_expense_by_delegate_and_election.category, +-- total_expense_by_delegate_and_election.election, +-- sum(total_expense_by_delegate_and_election.eos_amount) AS total_eos_amount, +-- sum(total_expense_by_delegate_and_election.usd_amount) AS total_usd_amount +-- FROM total_expense_by_delegate_and_election +-- GROUP BY +-- total_expense_by_delegate_and_election.category, +-- total_expense_by_delegate_and_election.election, +-- total_expense_by_delegate_and_election.id_election; diff --git a/hasura/migrations/default/1677733616588_run_sql_migration/up.sql b/hasura/migrations/default/1677733616588_run_sql_migration/up.sql new file mode 100644 index 00000000..b3a1ff84 --- /dev/null +++ b/hasura/migrations/default/1677733616588_run_sql_migration/up.sql @@ -0,0 +1,12 @@ +CREATE OR REPLACE VIEW "public"."expenses_by_category_election_and_delegate" AS + + SELECT total_expense_by_delegate_and_election.id_election, + total_expense_by_delegate_and_election.category, + total_expense_by_delegate_and_election.election, + sum(total_expense_by_delegate_and_election.eos_amount) AS total_eos_amount, + sum(total_expense_by_delegate_and_election.usd_amount) AS total_usd_amount + FROM total_expense_by_delegate_and_election + GROUP BY + total_expense_by_delegate_and_election.category, + total_expense_by_delegate_and_election.election, + total_expense_by_delegate_and_election.id_election; diff --git a/kubernetes/postgres-statefulset.yaml b/kubernetes/postgres-statefulset.yaml index a5911a90..f9d3dda1 100644 --- a/kubernetes/postgres-statefulset.yaml +++ b/kubernetes/postgres-statefulset.yaml @@ -21,7 +21,7 @@ spec: imagePullSecrets: - name: regcred containers: - - image: postgres:13.6-alpine + - image: postgres:13.10-alpine imagePullPolicy: 'Always' name: postgres envFrom: diff --git a/webapp/src/gql/eden-expense.gql.js b/webapp/src/gql/eden-expense.gql.js index baf3827a..517958ea 100644 --- a/webapp/src/gql/eden-expense.gql.js +++ b/webapp/src/gql/eden-expense.gql.js @@ -7,7 +7,6 @@ export const GET_TOTAL_EXPENSE_BY_ALL_ELECTIONS = gql` eos_categorized eos_uncategorized percent_categorized - percent_uncategorized usd_categorized usd_uncategorized } @@ -30,29 +29,25 @@ export const GET_TOTAL_EXPENSE_BY_DELEGATE = gql` export const GET_DELEGATES_EXPENSE_BY_ELECTION = gql` query getDelegatesByElection($election: Int) { - historic_expenses(where: { election: { _eq: $election } }) { - delegate_payer - usd_categorized - eos_claimed - eos_unclaimed - eos_categorized + total_by_delegate_and_election(where: { election: { _eq: $election } }) { + account + election + eos_income + usd_income + eos_expense + usd_expense + percent_claimed } } ` export const GET_TOTAL_EXPENSE_BY_CATEGORY_AND_ELECTION = gql` - query getTotalClaimedAndUnclaimedByElection($election: Int) { - total_by_category_and_election( - where: { - election: { _eq: $election } - category: { _neq: "uncategorized" } - type: { _eq: "expense" } - } - ) { - amount + query getExpensesByCategoryAndElection($election: Int) { + expenses_by_category_and_election(where: { election: { _eq: $election } }) { + total_usd_amount category + total_eos_amount election - usd_total } } ` @@ -92,17 +87,17 @@ export const GET_EXPENSES_BY_ACCOUNT = gql` } } ` + export const GET_EXPENSE_BY_CATEGORY = gql` - query getExpenseByCategory($delegate: String, $election: Int) { - expenses_by_category_and_delegate( - where: { - election: { _eq: $election } - delegate_payer: { _eq: $delegate } - } + query getExpensesByCategoryAndElection($id_election: uuid) { + expenses_by_category_election_and_delegate( + where: { id_election: { _eq: $id_election } } ) { + total_usd_amount category - amount - usd_total + total_eos_amount + election + id_election } } ` diff --git a/webapp/src/gql/eden-transaction.gql.js b/webapp/src/gql/eden-transaction.gql.js index 86526960..2f222456 100644 --- a/webapp/src/gql/eden-transaction.gql.js +++ b/webapp/src/gql/eden-transaction.gql.js @@ -1,21 +1,22 @@ import gql from 'graphql-tag' +export const GET_TOTAL_EXPENSE_BY_ELECTION = gql` + query getTotalExpenseByDelegateView($election: numeric, $account: String) { + global_amount( + where: { election: { _eq: $election }, account: { _eq: $account } } + ) { + election + eos_expense + usd_expense + } + } +` + export const GET_TRANSACTIONS_BY_DELEGATE_AND_ELECTION = gql` query getTransactionsByDelegateAndElection( $election: Int $delegate: String ) { - historic_expenses( - where: { - election: { _eq: $election } - delegate_payer: { _eq: $delegate } - } - ) { - eos_categorized - eos_claimed - usd_categorized - eos_unclaimed - } historic_incomes( where: { election: { _eq: $election }, recipient: { _eq: $delegate } } ) { diff --git a/webapp/src/hooks/customHooks/useDelegateReportState.js b/webapp/src/hooks/customHooks/useDelegateReportState.js index 99ab565d..84db92df 100644 --- a/webapp/src/hooks/customHooks/useDelegateReportState.js +++ b/webapp/src/hooks/customHooks/useDelegateReportState.js @@ -9,6 +9,7 @@ import { GET_MAX_DELEGATE_LEVEL, GET_HISTORIC_ELECTIONS, GET_EXPENSE_BY_CATEGORY, + GET_TOTAL_EXPENSE_BY_ELECTION, GET_TOTAL_DELEGATE_INCOME_BY_ELECTION, GET_TRANSACTIONS_BY_DELEGATE_AND_ELECTION } from '../../gql' @@ -37,6 +38,9 @@ const useDelegateReportState = () => { const loadDelegatesByElection = useImperativeQuery( GET_TOTAL_DELEGATE_INCOME_BY_ELECTION ) + const getTotalExpenseByDelegate = useImperativeQuery( + GET_TOTAL_EXPENSE_BY_ELECTION + ) useEffect(async () => { const { data: electionsData } = await loadElections() @@ -84,9 +88,15 @@ const useDelegateReportState = () => { useEffect(async () => { if (!delegateSelect) return - const responseCategory = await loadCategoryList({ - election: Number(electionRoundSelect), - delegate: delegateSelect + const { data: electionId } = await loadElections({ + where: { + eden_delegate: { account: { _eq: delegateSelect } }, + election: { _eq: electionRoundSelect } + } + }) + + const { data: responseCategory } = await loadCategoryList({ + id_election: electionId?.eden_election[0].id }) const responseTransaction = await loadTransactions({ @@ -94,13 +104,19 @@ const useDelegateReportState = () => { delegate: delegateSelect }) + const { data: responseTotalExpenseByElection } = + await getTotalExpenseByDelegate({ + election: electionRoundSelect, + account: delegateSelect + }) + const transactions = newDataFormatByTypeDelegate( responseTransaction.data.historic_incomes || [], - responseTransaction.data.historic_expenses || [] + responseTotalExpenseByElection.global_amount || [] ) + const categories = newDataFormatByCategoryDelegate( - responseCategory.data?.expenses_by_category_and_delegate || [], - transactions + responseCategory?.expenses_by_category_election_and_delegate || [] ) setCategoryList(categories) diff --git a/webapp/src/hooks/customHooks/useExpenseReportState.js b/webapp/src/hooks/customHooks/useExpenseReportState.js index bab1d8e2..66a261f4 100644 --- a/webapp/src/hooks/customHooks/useExpenseReportState.js +++ b/webapp/src/hooks/customHooks/useExpenseReportState.js @@ -12,6 +12,7 @@ import { newDataExpenseFormatByAllElections, newDataFormatByAllDelegatesExpense, newDataFormatByElectionAndDelegateExpense, + newDataFormatExpenseByCategoryAndElection, newDataFormatTotalByCategoryExpense } from '../../utils/new-format-objects' import { useImperativeQuery } from '../../utils' @@ -74,25 +75,25 @@ const useExpenseReportState = () => { setCategoryList([]) setDelegatesList([]) - const delegatesExpenseByElections = await loadDelegatesExpenseByElections( - { + const { data: delegatesExpenseByElections } = + await loadDelegatesExpenseByElections({ election: electionRoundSelect - } - ) + }) - const totalByCategoryAndElection = await loadTotalByCategoryAndElection({ - election: electionRoundSelect - }) + const { data: totalByCategoryAndElection } = + await loadTotalByCategoryAndElection({ + election: electionRoundSelect + }) setDelegatesList( newDataFormatByElectionAndDelegateExpense( - delegatesExpenseByElections?.data?.historic_expenses || [] + delegatesExpenseByElections?.total_by_delegate_and_election || [] ) ) setCategoryList( - newDataFormatTotalByCategoryExpense( - totalByCategoryAndElection?.data?.total_by_category_and_election || [] + newDataFormatExpenseByCategoryAndElection( + totalByCategoryAndElection?.expenses_by_category_and_election || [] ) ) } diff --git a/webapp/src/utils/new-format-objects.js b/webapp/src/utils/new-format-objects.js index e52a83bf..7719f88f 100644 --- a/webapp/src/utils/new-format-objects.js +++ b/webapp/src/utils/new-format-objects.js @@ -47,14 +47,14 @@ export const newDataFormatByTreasuryList = ( isValued: false }) } - const curDate = treasuryList.at(-1).date.split('T')[0] + const curDate = treasuryList.at(-1)?.date.split('T')[0] if (!newTreasuryList.find(element => element.date.split('T')[0] === curDate)) newTreasuryList.push({ - EOS_BALANCE: treasuryList.at(-1).balance, - USD_BALANCE: treasuryList.at(-1).usd_total, - EOS_VALUED: treasuryList.at(-1).balance, - USD_VALUED: treasuryList.at(-1).usd_total, - date: treasuryList.at(-1).date.split('T')[0], + EOS_BALANCE: treasuryList.at(-1)?.balance, + USD_BALANCE: treasuryList.at(-1)?.usd_total, + EOS_VALUED: treasuryList.at(-1)?.balance, + USD_VALUED: treasuryList.at(-1)?.usd_total, + date: treasuryList.at(-1)?.date.split('T')[0], isValued: false }) @@ -83,7 +83,7 @@ export const newDataExpenseFormatByAllElections = ({ EOS_TOTAL: Number(data.eos_categorized + data.eos_uncategorized), USD_TOTAL: Number(data.usd_categorized + data.usd_uncategorized), EOS_CATEGORIZED_PERCENT: data.percent_categorized, - EOS_UNCATEGORIZED_PERCENT: data.percent_uncategorized + EOS_UNCATEGORIZED_PERCENT: 100 - data.percent_categorized })) export const newDataIncomeFormatByAllElections = ({ @@ -113,32 +113,43 @@ export const newDataFormatByAllDelegatesExpense = transactionsList => export const newDataFormatByElectionAndDelegateExpense = transactionsList => transactionsList.map(data => ({ - name: data.delegate_payer, - EOS_CATEGORIZED: Number(data.eos_categorized), - USD_CATEGORIZED: Number(data.usd_categorized), - EOS_UNCATEGORIZED: Number( - data.eos_claimed + data.eos_unclaimed - data.eos_categorized - ), - USD_UNCATEGORIZED: Number(data.usd_uncategorized), - EOS_CATEGORIZED_PERCENT: Number( - (data.eos_categorized / (data.eos_claimed + data.eos_unclaimed || 1)) * - 100 - ), - EOS_UNCATEGORIZED_PERCENT: Number( - ((data.eos_claimed + data.eos_unclaimed - data.eos_categorized) / - (data.eos_claimed + data.eos_unclaimed) || 1) * 100 - ), + name: data.account, + EOS_CATEGORIZED: Number(data.eos_expense), + USD_CATEGORIZED: Number(data.usd_expense), + EOS_UNCATEGORIZED: Number(data.eos_income - data.eos_expense), + USD_UNCATEGORIZED: Number(data.usd_income - data.usd_expense), + EOS_CATEGORIZED_PERCENT: + data.percent_claimed > 100 + ? 100 + : data.percent_claimed < 0 + ? 0 + : data.percent_claimed, + EOS_UNCATEGORIZED_PERCENT: + 100 - data.percent_claimed > 100 + ? 100 + : 100 - data.percent_claimed < 0 + ? 0 + : 100 - data.percent_claimed, + color: generateColor() })) export const newDataFormatTotalByCategoryExpense = totalByCategory => totalByCategory.map(data => ({ - name: data.category ? data.category : `Election ${data.election + 1}`, + name: data.category, EOS_CATEGORIZED: Number(data.amount), USD_CATEGORIZED: Number(data.usd_total), color: generateColor() })) +export const newDataFormatExpenseByCategoryAndElection = totalByCategory => + totalByCategory.map(data => ({ + name: data.category, + EOS_CATEGORIZED: Number(data.total_eos_amount), + USD_CATEGORIZED: Number(data.total_usd_amount), + color: generateColor() + })) + export const generateDelegateData = ( type, category, @@ -164,11 +175,11 @@ export const newDataFormatByTypeDelegate = (incomeList, expenseList) => { const resultUncategorizedEOS = (incomeList[0]?.eos_claimed || 0) + (incomeList[0]?.eos_unclaimed || 0) - - (expenseList[0]?.eos_categorized || 0) + (expenseList[0]?.eos_expense || 0) const resultUncategorizedUSD = (incomeList[0]?.usd_claimed || 0) + (incomeList[0]?.usd_unclaimed || 0) - - (expenseList[0]?.usd_categorized || 0) + (expenseList[0]?.usd_expense || 0) transactions.push( generateDelegateData( @@ -185,8 +196,8 @@ export const newDataFormatByTypeDelegate = (incomeList, expenseList) => { generateDelegateData( 'expense', 'Categorized', - expenseList[0]?.eos_categorized || 0, - expenseList[0]?.usd_categorized || 0, + expenseList[0]?.eos_expense || 0, + expenseList[0]?.usd_expense || 0, resultUncategorizedEOS >= 0 ? resultUncategorizedEOS : 0 || 0, resultUncategorizedUSD >= 0 ? resultUncategorizedUSD : 0 || 0 ) @@ -195,26 +206,10 @@ export const newDataFormatByTypeDelegate = (incomeList, expenseList) => { return transactions } -export const newDataFormatByCategoryDelegate = (categoryList, transaction) => { - const newCategoryList = [] - - categoryList.forEach(data => { - if (data.category === 'uncategorized') return - - newCategoryList.push({ - category: data.category, - EOS: Number(data.amount), - USD: Number(data.usd_total), - color: generateColor() - }) - }) - - newCategoryList.push({ - category: 'uncategorized', - EOS: transaction[1].EOS_UN, - USD: transaction[1].USD_UN, +export const newDataFormatByCategoryDelegate = categoryList => + categoryList.map(data => ({ + category: data.category, + EOS: Number(data.total_eos_amount), + USD: Number(data.total_usd_amount), color: generateColor() - }) - - return newCategoryList -} + }))