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