Files: cd3428fe2ad19111fa2a992ad2160dd3ce30ca85 / position / sync / chooseOneResults.js
1145 bytesRaw
1 | const isArray = require('isarray') |
2 | const Position = require('../../position/sync/position') |
3 | |
4 | // Expects `poll` and `position` objects passed in to be of shape: |
5 | // { |
6 | // value: { |
7 | // content: {...}, |
8 | // timestamp: ... |
9 | // author: ... |
10 | // } |
11 | // } |
12 | // |
13 | // postions must be of the correct type ie: type checked by the caller. |
14 | module.exports = function chooseOneResults ({positions, poll}) { |
15 | return positions.reduce(function (results, position) { |
16 | const { value: {author} } = position |
17 | const { positionDetails: {choice} } = Position(position) |
18 | |
19 | if (isInvalidChoice({position, poll}) || isPositionAfterClose({position, poll})) { |
20 | results.errors.invalidPositions.push(position) |
21 | return results |
22 | } |
23 | |
24 | if (!isArray(results[choice])) { |
25 | results[choice] = [] |
26 | } |
27 | results[choice].push(author) |
28 | |
29 | return results |
30 | }, {errors: {invalidPositions: []}}) |
31 | } |
32 | |
33 | function isInvalidChoice ({position, poll}) { |
34 | const { positionDetails: {choice} } = Position(position) |
35 | return choice >= poll.pollDetails.choices.length |
36 | } |
37 | |
38 | function isPositionAfterClose ({position, poll}) { |
39 | return position.value.timestamp > poll.closesAt |
40 | } |
41 |
Built with git-ssb-web