git ssb

0+

Piet / ssb-loomio



Tree: 1370996e72773275a544e0bdd26ee0f6db0e49d8

Files: 1370996e72773275a544e0bdd26ee0f6db0e49d8 / position / sync / chooseOneResults.js

1499 bytesRaw
1var getMsgContent = require('../../lib/getMsgContent')
2var ChooseOne = require('../../poll/sync/chooseOne')
3const PositionChoiceError = require('../../errors/sync/positionChoiceError')
4const 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.
17module.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
48function isInvalidChoice ({position, poll}) {
49 const { choice } = position.value.content.positionDetails
50 return choice >= poll.value.content.pollDetails.choices.length
51}
52
53function isPositionLate ({position, poll}) {
54 return position.value.timestamp > poll.value.content.closesAt
55}
56

Built with git-ssb-web