-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
escrowed.js
25 lines (24 loc) · 886 Bytes
/
escrowed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module.exports = {
escrowed: (req, res, model) =>
model
.find(req.query, { pol_id: 1, donation: 1 })
.sort({ date: -1 })
.then((dbModel) => {
let b = req.query;
dbModel.forEach((el) => {
b[el.pol_id] = (b[el.pol_id] || 0) + 1;
});
let arr = Array.from(
dbModel.reduce(
(m, { pol_id, donation }) =>
m.set(pol_id, (m.get(pol_id) || 0) + (donation || 0)),
new Map()
),
([pol_id, donation]) => ({ pol_id, donation })
);
arr.forEach((element) => (element.count = b[element.pol_id]));
res.json(arr);
})
.catch((err) => res.status(422).json(err))
};
// duplicate this function into one that returns the donations for a single bill only. will need to bundle up the bill_id or something with the request for that to happen.