diff --git a/constitutional.go b/constitutional.go index d9c98f6..9fec3ca 100644 --- a/constitutional.go +++ b/constitutional.go @@ -3,7 +3,6 @@ package main import ( "context" "fmt" - "math" "os" "strings" "time" @@ -99,10 +98,9 @@ func EvaluatePolls() { if poll.OpenedTime.AddDate(0, 0, 2).After(now) { continue } - //Two-Thirds Quorum - voterCount := len(poll.AllowedUsers) - //fuckass rounding - quorum := int(math.Ceil(float64(voterCount) * poll.QuorumType)) + + quorum := CalculateQuorum(*poll) + notVoted := make([]*OIDCUser, 0) votedCount := 0 // check all voters to see if they have voted diff --git a/database/poll.go b/database/poll.go index 485d648..dd534d8 100644 --- a/database/poll.go +++ b/database/poll.go @@ -86,7 +86,6 @@ func CreatePoll(ctx context.Context, poll *Poll) (string, error) { if err != nil { return "", err } - return result.InsertedID.(primitive.ObjectID).Hex(), nil } diff --git a/main.go b/main.go index c58400e..a5f1a2a 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "html/template" + "math" "net/http" "os" "slices" @@ -35,6 +36,17 @@ func inc(x int) string { return strconv.Itoa(x + 1) } +// Gets the number of people eligible to vote in a poll +func GetVoterCount(poll database.Poll) int { + return len(poll.AllowedUsers) +} + +// Calculates the number of votes required for quorum in a poll +func CalculateQuorum(poll database.Poll) int { + voterCount := GetVoterCount(poll) + return int(math.Ceil(float64(voterCount) * poll.QuorumType)) +} + func MakeLinks(s string) template.HTML { rx := xurls.Strict() s = template.HTMLEscapeString(s) @@ -68,11 +80,11 @@ func main() { oidcClient.setupOidcClient(os.Getenv("VOTE_OIDC_ID"), os.Getenv("VOTE_OIDC_SECRET")) InitConstitution() - if (DEV_DISABLE_ACTIVE_FILTERS) { + if DEV_DISABLE_ACTIVE_FILTERS { logging.Logger.WithFields(logrus.Fields{"method": "main init"}).Warning("Dev disable active filters is set!") } - if (DEV_FORCE_IS_EVALS) { + if DEV_FORCE_IS_EVALS { logging.Logger.WithFields(logrus.Fields{"method": "main init"}).Warning("Dev force evals is set!") } @@ -169,7 +181,7 @@ func main() { VoteType: database.POLL_TYPE_SIMPLE, OpenedTime: time.Now(), Open: true, - QuorumType: quorum, + QuorumType: float64(quorum), Gatekeep: c.PostForm("gatekeep") == "true", AllowWriteIns: c.PostForm("allowWriteIn") == "true", Hidden: c.PostForm("hidden") == "true", @@ -408,18 +420,22 @@ func main() { canModify := containsString(claims.UserInfo.Groups, "active_rtp") || containsString(claims.UserInfo.Groups, "eboard") || poll.CreatedBy == claims.UserInfo.Username + votesNeededForQuorum := int(poll.QuorumType * float64(len(poll.AllowedUsers))) c.HTML(200, "result.tmpl", gin.H{ - "Id": poll.Id, - "ShortDescription": poll.ShortDescription, - "LongDescription": poll.LongDescription, - "VoteType": poll.VoteType, - "Results": results, - "IsOpen": poll.Open, - "IsHidden": poll.Hidden, - "CanModify": canModify, - "Username": claims.UserInfo.Username, - "FullName": claims.UserInfo.FullName, - "Gatekeep": poll.Gatekeep, + "Id": poll.Id, + "ShortDescription": poll.ShortDescription, + "LongDescription": poll.LongDescription, + "VoteType": poll.VoteType, + "Results": results, + "IsOpen": poll.Open, + "IsHidden": poll.Hidden, + "CanModify": canModify, + "Username": claims.UserInfo.Username, + "FullName": claims.UserInfo.FullName, + "Gatekeep": poll.Gatekeep, + "Quorum": strconv.FormatFloat(poll.QuorumType*100.0, 'f', 0, 64), + "EligibleVoters": poll.AllowedUsers, + "VotesNeededForQuorum": votesNeededForQuorum, }) })) diff --git a/templates/result.tmpl b/templates/result.tmpl index f3542a6..890be5e 100644 --- a/templates/result.tmpl +++ b/templates/result.tmpl @@ -38,6 +38,21 @@