git ssb

0+

Piet / ssb-loomio



Tree: f368010bd5cd7dd957193a674e18bd6e6cad8233

Files: f368010bd5cd7dd957193a674e18bd6e6cad8233 / position / sync / chooseOneResults.js

1647 bytesRaw
1const isArray = require('isarray')
2const {ERROR_POSITION_CHOICE, ERROR_POSITION_TYPE, ERROR_POSITION_LATE} = require('../../types')
3
4// Expects `poll` and `position` objects passed in to be of shape:
5// {
6// key,
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.
15module.exports = function chooseOneResults ({positions, poll}) {
16 return positions.reduce(function (results, position) {
17 const { author, content } = position.value
18 const { choice } = content.positionDetails
19
20 if (isInvalidChoice({position, poll})) {
21 // TODO change this to push errors into poll.errors
22 results.errors.push({type: ERROR_POSITION_CHOICE, position})
23 return results
24 }
25
26 if (isPositionLate({position, poll})) {
27 // TODO change this to push errors into poll.errors
28 results.errors.push({type: ERROR_POSITION_LATE, position})
29 return results
30 }
31
32 // TODO convert from Array to Object
33 // {
34 // 'kea': {
35 // @piet: 'because things'
36 // },
37 // 'hermit crab': {
38 // @katie: 'scuttz..',
39 // @mix: 'what she said'
40 // } ]
41 // }
42 if (!isArray(results[choice])) {
43 results[choice] = []
44 }
45 results[choice].push(author)
46
47 return results
48 }, {errors: []})
49}
50
51function isInvalidChoice ({position, poll}) {
52 const { choice } = position.value.content.positionDetails
53 return choice >= poll.value.content.pollDetails.choices.length
54}
55
56function isPositionLate ({position, poll}) {
57 return position.value.timestamp > poll.value.content.closesAt
58}
59

Built with git-ssb-web