Skip to content

Commit

Permalink
updates delegations in cache for users that got impact by third parti…
Browse files Browse the repository at this point in the history
…es in other sessions
  • Loading branch information
santisiri committed Mar 30, 2018
1 parent 247848a commit a63db7d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
38 changes: 37 additions & 1 deletion imports/api/transactions/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion imports/ui/templates/components/decision/liquid/liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
14 changes: 3 additions & 11 deletions imports/ui/templates/components/identity/card/card.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a63db7d

Please sign in to comment.