diff --git a/imports/api/transactions/transaction.js b/imports/api/transactions/transaction.js index dafb46e1e..41cd06684 100644 --- a/imports/api/transactions/transaction.js +++ b/imports/api/transactions/transaction.js @@ -434,7 +434,6 @@ const _processedTx = (transactionId) => { * @param {object} transaction the transaction to find in the cache * @param {boolean} foreign the transaction comes from user in another session */ - const _updateWalletCache = (transaction, foreign) => { let counterPartyId; let delta; @@ -512,6 +511,42 @@ const _updateWalletCache = (transaction, foreign) => { } }; +/** +* @summary updates the cache of a foreign user balance with information from user collection +* @param {string} sessionId the transaction to find in the cache +* @param {string} userId the user being updated +* @param {object} wallet the wallet info to be used +*/ +const _updateUserCache = (sessionId, userId, wallet) => { + let delegation; + const cacheWallet = Session.get(sessionId); + const list = Session.get('voteList'); + + if (cacheWallet) { + if (cacheWallet.available !== wallet.available) { + cacheWallet.available = wallet.available; + cacheWallet.balance = wallet.balance; + cacheWallet.placed = wallet.placed; + Session.set(sessionId, cacheWallet); + + // check for delegations balances in cache + for (const item in list) { + delegation = Session.get(list[item]); + if (delegation.delegationContract) { + if (delegation.delegationContract.signatures[0]._id === userId || + delegation.delegationContract.signatures[1]._id === userId) { + delegation.minVotes = parseInt((delegation.inBallot - wallet.available) - 1, 10); + delegation.balance = wallet.balance; + delegation.placedPercentage = ((delegation.placed * 100) / delegation.balance); + Session.set(list[item], delegation); + break; + } + } + } + } + } +}; + /** * @summary create a new transaction between two parties * @param {string} senderId - user or collective allocating the funds @@ -608,6 +643,7 @@ const _genesisTransaction = (userId) => { }; export const processedTx = _processedTx; +export const updateUserCache = _updateUserCache; export const updateWalletCache = _updateWalletCache; export const processTransaction = _processTransaction; export const generateWalletAddress = _generateWalletAddress; diff --git a/imports/ui/templates/components/decision/liquid/liquid.js b/imports/ui/templates/components/decision/liquid/liquid.js index 2385a6d69..46d35c86c 100644 --- a/imports/ui/templates/components/decision/liquid/liquid.js +++ b/imports/ui/templates/components/decision/liquid/liquid.js @@ -196,7 +196,9 @@ Template.liquid.helpers({ return Template.instance().rightToVote.get(); }, minimumReached() { - return ((Session.get(this._id).allocateQuantity <= Session.get(this._id).minVotes) && Session.get(this._id).inBallot > 0); + return ((Session.get(this._id).allocateQuantity <= Session.get(this._id).minVotes) + && Session.get(this._id).inBallot > 0 + && Session.get(this._id).allocateQuantity !== 0); }, confirmationRequired() { if (Template.instance().contract.get().kind === 'DELEGATION') { diff --git a/imports/ui/templates/components/identity/card/card.js b/imports/ui/templates/components/identity/card/card.js index 016b9bb63..9ed9dc68c 100644 --- a/imports/ui/templates/components/identity/card/card.js +++ b/imports/ui/templates/components/identity/card/card.js @@ -1,9 +1,9 @@ import { Meteor } from 'meteor/meteor'; import { Template } from 'meteor/templating'; -import { Session } from 'meteor/session'; import { ReactiveVar } from 'meteor/reactive-var'; import { getDelegationContract } from '/imports/startup/both/modules/Contract'; +import { updateUserCache } from '/imports/api/transactions/transaction'; import '/imports/ui/templates/components/identity/card/card.html'; import '/imports/ui/templates/components/identity/avatar/avatar.js'; @@ -67,16 +67,8 @@ Template.card.helpers({ id = 'vote-user-balance'; } else { id = `vote-user-balance-${userId}`; - const cacheWallet = Session.get(id); - if (cacheWallet) { - console.log(Session.get(id)); - console.log(userWallet); - if (cacheWallet.available !== userWallet.available) { - cacheWallet.available = userWallet.available; - cacheWallet.balance = userWallet.balance; - cacheWallet.placed = userWallet.placed; - Session.set(id, cacheWallet); - } + if (userWallet) { + updateUserCache(id, userId, userWallet); } } return {