Skip to content

Commit

Permalink
Added amountRemaining for Campaigns and DACs
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechsimetka committed Oct 7, 2018
1 parent 3236427 commit 022aa39
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/services/donations/updateEntityCounters.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ const updateEntity = async (context, donation) => {
.service('donations')
.find({ paginate: false, query: donationQuery });

const totalDonated = donations
.reduce(
(accumulator, d) =>
accumulator.add(
// use amountRemaining for milestones b/c excess will be sent back in case of over donation
toBN(donation.ownerType === AdminTypes.MILESTONE ? d.amountRemaining : d.amount),
),
toBN(0),
)
.toString();
const { totalDonated, amountRemaining } = donations.reduce(
(accumulator, d) => ({
totalDonated: accumulator.totalDonated.add(toBN(d.amount)),
amountRemaining: accumulator.amountRemaining.add(toBN(d.amountRemaining)),
}),
{
totalDonated: toBN(0),
amountRemaining: toBN(0),
},
);

// NOTE: Using === to compare as both of these are strings and amounts in wei
const fullyFunded =
Expand All @@ -90,7 +90,13 @@ const updateEntity = async (context, donation) => {
d => ![DonationStatus.PAYING, DonationStatus.PAID].includes(d.status),
).length;

await service.patch(entity._id, { donationCount, totalDonated, peopleCount, fullyFunded });
await service.patch(entity._id, {
donationCount,
totalDonated: totalDonated.toString(),
amountRemaining: amountRemaining.toString(),
peopleCount,
fullyFunded,
});
} catch (error) {
logger.error(error);
}
Expand Down

0 comments on commit 022aa39

Please sign in to comment.