git ssb

0+

Piet / ssb-loomio



Tree: 306a27bb28f6bbbcdede609146d7259018493d5d

Files: 306a27bb28f6bbbcdede609146d7259018493d5d / position / sync / chooseOneResults.js

1128 bytesRaw
1const isArray = require('isarray')
2const 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.
14module.exports = function ({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
33function isInvalidChoice ({position, poll}) {
34 const { positionDetails: {choice} } = Position(position)
35 return choice >= poll.pollDetails.choices.length
36}
37
38function isPositionAfterClose ({position, poll}) {
39 return position.value.timestamp > poll.closesAt
40}
41

Built with git-ssb-web