Files: f9c8608d388933583c65f43ed6750ec4832159db / position / sync / chooseOneResults.js
1568 bytesRaw
1 | const isArray = require('isarray') |
2 | const positionChoiceError = require('../../errors/sync/positionChoiceError') |
3 | const positionLateError = require('../../errors/sync/positionLateError') |
4 | |
5 | // Expects `poll` and `position` objects passed in to be of shape: |
6 | // { |
7 | // key, |
8 | // value: { |
9 | // content: {...}, |
10 | // timestamp: ... |
11 | // author: ... |
12 | // } |
13 | // } |
14 | // |
15 | // postions must be of the correct type ie: type checked by the caller. |
16 | module.exports = function chooseOneResults ({positions, poll}) { |
17 | return positions.reduce(function (results, position) { |
18 | const { author, content } = position.value |
19 | const { choice } = content.positionDetails |
20 | |
21 | if (isInvalidChoice({position, poll})) { |
22 | results.errors.push(positionChoiceError({position})) |
23 | return results |
24 | } |
25 | |
26 | if (isPositionLate({position, poll})) { |
27 | results.errors.push(positionLateError({position})) |
28 | return results |
29 | } |
30 | |
31 | // TODO convert from Array to Object |
32 | // { |
33 | // 'kea': { |
34 | // @piet: 'because things' |
35 | // }, |
36 | // 'hermit crab': { |
37 | // @katie: 'scuttz..', |
38 | // @mix: 'what she said' |
39 | // } ] |
40 | // } |
41 | if (!isArray(results[choice])) { |
42 | results[choice] = [] |
43 | } |
44 | results[choice].push(author) |
45 | |
46 | return results |
47 | }, {errors: []}) |
48 | } |
49 | |
50 | function isInvalidChoice ({position, poll}) { |
51 | const { choice } = position.value.content.positionDetails |
52 | return choice >= poll.value.content.pollDetails.choices.length |
53 | } |
54 | |
55 | function isPositionLate ({position, poll}) { |
56 | return position.value.timestamp > poll.value.content.closesAt |
57 | } |
58 |
Built with git-ssb-web