Files: 92693260135502dac86a07eef9c477fb0198568a / position / sync / chooseOneResults.js
1499 bytesRaw
1 | var getMsgContent = require('../../lib/getMsgContent') |
2 | var ChooseOne = require('../../poll/sync/chooseOne') |
3 | const PositionChoiceError = require('../../errors/sync/positionChoiceError') |
4 | const PositionLateError = require('../../errors/sync/positionLateError') |
5 | |
6 | // Expects `poll` and `position` objects passed in to be of shape: |
7 | // { |
8 | // key, |
9 | // value: { |
10 | // content: {...}, |
11 | // timestamp: ... |
12 | // author: ... |
13 | // } |
14 | // } |
15 | // |
16 | // postions must be of the correct type ie: type checked by the caller. |
17 | module.exports = function chooseOneResults ({positions, poll}) { |
18 | var results = getMsgContent(poll) |
19 | .pollDetails |
20 | .choices |
21 | .map(choice => { |
22 | return { |
23 | choice, |
24 | voters: {} |
25 | } |
26 | }) |
27 | |
28 | return positions.reduce(function (acc, position) { |
29 | const { author, content } = position.value |
30 | const { choice } = content.positionDetails |
31 | |
32 | if (isInvalidChoice({position, poll})) { |
33 | acc.errors.push(PositionChoiceError({position})) |
34 | return acc |
35 | } |
36 | |
37 | if (isPositionLate({position, poll})) { |
38 | acc.errors.push(PositionLateError({position})) |
39 | return acc |
40 | } |
41 | |
42 | acc.results[choice].voters[author] = position |
43 | |
44 | return acc |
45 | }, {errors: [], results}) |
46 | } |
47 | |
48 | function isInvalidChoice ({position, poll}) { |
49 | const { choice } = position.value.content.positionDetails |
50 | return choice >= poll.value.content.pollDetails.choices.length |
51 | } |
52 | |
53 | function isPositionLate ({position, poll}) { |
54 | return position.value.timestamp > poll.value.content.closesAt |
55 | } |
56 |
Built with git-ssb-web