2022-05-15 18:10:49 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017-2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2022-05-21 12:27:46 +00:00
|
|
|
"context"
|
2022-05-21 11:51:17 +00:00
|
|
|
"database/sql"
|
2022-05-22 19:47:27 +00:00
|
|
|
"database/sql/driver"
|
2022-05-21 11:51:17 +00:00
|
|
|
"errors"
|
2022-05-15 18:10:49 +00:00
|
|
|
"fmt"
|
2022-05-22 19:15:54 +00:00
|
|
|
"strings"
|
2022-05-15 18:10:49 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
)
|
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
type VoteType struct {
|
|
|
|
label string
|
|
|
|
id uint8
|
|
|
|
}
|
2022-05-15 18:10:49 +00:00
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
var (
|
|
|
|
VoteTypeMotion = &VoteType{label: "motion", id: 0}
|
|
|
|
VoteTypeVeto = &VoteType{label: "veto", id: 1}
|
2022-05-15 18:10:49 +00:00
|
|
|
)
|
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
func (v *VoteType) String() string {
|
|
|
|
return v.label
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
func VoteTypeFromString(label string) (*VoteType, error) {
|
|
|
|
for _, vt := range []*VoteType{VoteTypeMotion, VoteTypeVeto} {
|
|
|
|
if strings.EqualFold(vt.label, label) {
|
|
|
|
return vt, nil
|
|
|
|
}
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
return nil, fmt.Errorf("unknown vote type %s", label)
|
|
|
|
}
|
|
|
|
|
2022-05-22 19:47:27 +00:00
|
|
|
func VoteTypeFromInt(id int64) (*VoteType, error) {
|
2022-05-22 19:15:54 +00:00
|
|
|
for _, vt := range []*VoteType{VoteTypeMotion, VoteTypeVeto} {
|
2022-05-22 19:47:27 +00:00
|
|
|
if int64(vt.id) == id {
|
2022-05-22 19:15:54 +00:00
|
|
|
return vt, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unknown vote type id %d", id)
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 19:47:27 +00:00
|
|
|
func (v *VoteType) Scan(src any) error {
|
|
|
|
value, ok := src.(int64)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("could not cast %v of %T to uint8", src, src)
|
|
|
|
}
|
|
|
|
|
|
|
|
vt, err := VoteTypeFromInt(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = *vt
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VoteType) Value() (driver.Value, error) {
|
|
|
|
return int64(v.id), nil
|
|
|
|
}
|
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
func (v *VoteType) QuorumAndMajority() (int, float32) {
|
2022-05-15 18:10:49 +00:00
|
|
|
const (
|
|
|
|
majorityDefault = 0.99
|
|
|
|
majorityMotion = 0.50
|
|
|
|
quorumDefault = 1
|
|
|
|
quorumMotion = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
if v == VoteTypeMotion {
|
|
|
|
return quorumMotion, majorityMotion
|
|
|
|
}
|
|
|
|
|
|
|
|
return quorumDefault, majorityDefault
|
|
|
|
}
|
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
type VoteStatus struct {
|
|
|
|
Label string
|
2022-05-27 15:39:54 +00:00
|
|
|
ID int8
|
2022-05-27 12:42:39 +00:00
|
|
|
}
|
2022-05-15 18:10:49 +00:00
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
var (
|
2022-05-27 15:39:54 +00:00
|
|
|
voteStatusDeclined = &VoteStatus{Label: "declined", ID: -1}
|
|
|
|
voteStatusPending = &VoteStatus{Label: "pending", ID: 0}
|
|
|
|
voteStatusApproved = &VoteStatus{Label: "approved", ID: 1}
|
2022-05-29 13:36:27 +00:00
|
|
|
voteStatusWithdrawn = &VoteStatus{Label: "withdrawn", ID: -2}
|
2022-05-15 18:10:49 +00:00
|
|
|
)
|
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
func VoteStatusFromInt(id int64) (*VoteStatus, error) {
|
2022-05-29 13:36:27 +00:00
|
|
|
for _, vs := range []*VoteStatus{voteStatusPending, voteStatusApproved, voteStatusWithdrawn, voteStatusDeclined} {
|
2022-05-27 15:39:54 +00:00
|
|
|
if int64(vs.ID) == id {
|
2022-05-27 12:42:39 +00:00
|
|
|
return vs, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unknown vote status id %d", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VoteStatus) String() string {
|
|
|
|
return v.Label
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
func (v *VoteStatus) Scan(src any) error {
|
|
|
|
value, ok := src.(int64)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("could not cast %v of %T to uint8", src, src)
|
|
|
|
}
|
|
|
|
|
|
|
|
vs, err := VoteStatusFromInt(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
*v = *vs
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VoteStatus) Value() (driver.Value, error) {
|
2022-05-27 15:39:54 +00:00
|
|
|
return int64(v.ID), nil
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
type VoteChoice struct {
|
2022-05-27 15:39:54 +00:00
|
|
|
Label string
|
|
|
|
ID int8
|
2022-05-27 12:42:39 +00:00
|
|
|
}
|
2022-05-15 18:10:49 +00:00
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
var (
|
2022-05-27 15:39:54 +00:00
|
|
|
VoteAye = &VoteChoice{Label: "aye", ID: 1}
|
|
|
|
VoteNaye = &VoteChoice{Label: "naye", ID: -1}
|
|
|
|
VoteAbstain = &VoteChoice{Label: "abstain", ID: 0}
|
2022-05-15 18:10:49 +00:00
|
|
|
)
|
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
func VoteChoiceFromString(label string) (*VoteChoice, error) {
|
|
|
|
for _, vc := range []*VoteChoice{VoteAye, VoteNaye, VoteAbstain} {
|
2022-05-27 15:39:54 +00:00
|
|
|
if strings.EqualFold(vc.Label, label) {
|
2022-05-27 12:42:39 +00:00
|
|
|
return vc, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unknown vote choice %s", label)
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
func VoteChoiceFromInt(id int64) (*VoteChoice, error) {
|
|
|
|
for _, vc := range []*VoteChoice{VoteAye, VoteNaye, VoteAbstain} {
|
2022-05-27 15:39:54 +00:00
|
|
|
if int64(vc.ID) == id {
|
2022-05-27 12:42:39 +00:00
|
|
|
return vc, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unknown vote type id %d", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VoteChoice) String() string {
|
2022-05-27 15:39:54 +00:00
|
|
|
return v.Label
|
2022-05-27 12:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VoteChoice) Scan(src any) error {
|
|
|
|
value, ok := src.(int64)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("could not cast %v of %T to uint8", src, src)
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
vc, err := VoteChoiceFromInt(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = *vc
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VoteChoice) Value() (driver.Value, error) {
|
2022-05-27 15:39:54 +00:00
|
|
|
return int64(v.ID), nil
|
2022-05-27 12:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VoteChoice) Equal(other *VoteChoice) bool {
|
2022-05-27 15:39:54 +00:00
|
|
|
return v.ID == other.ID
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 11:51:17 +00:00
|
|
|
type VoteSums struct {
|
|
|
|
Ayes, Nayes, Abstains int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VoteSums) VoteCount() int {
|
|
|
|
return v.Ayes + v.Nayes + v.Abstains
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VoteSums) TotalVotes() int {
|
|
|
|
return v.Ayes + v.Nayes
|
|
|
|
}
|
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
func (v *VoteSums) Percent() int {
|
|
|
|
totalVotes := v.TotalVotes()
|
|
|
|
if totalVotes == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.Ayes * 100 / totalVotes
|
|
|
|
}
|
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
func (v *VoteSums) CalculateResult(quorum int, majority float32) (*VoteStatus, string) {
|
2022-05-21 11:51:17 +00:00
|
|
|
if v.VoteCount() < quorum {
|
|
|
|
return voteStatusDeclined, fmt.Sprintf("Needed quorum of %d has not been reached.", quorum)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (float32(v.Ayes) / float32(v.TotalVotes())) < majority {
|
|
|
|
return voteStatusDeclined, fmt.Sprintf("Needed majority of %0.2f%% has not been reached.", majority)
|
|
|
|
}
|
|
|
|
|
|
|
|
return voteStatusApproved, "Quorum and majority have been reached"
|
|
|
|
}
|
|
|
|
|
2022-05-21 17:18:17 +00:00
|
|
|
type Motion struct {
|
2022-05-29 13:36:27 +00:00
|
|
|
ID int64 `db:"id"`
|
|
|
|
Proposed time.Time `db:"proposed"`
|
|
|
|
Proponent int64 `db:"proponent"`
|
|
|
|
Proposer string `db:"proposer"`
|
|
|
|
Title string `db:"title"`
|
|
|
|
Content string `db:"content"`
|
|
|
|
Status *VoteStatus `db:"status"`
|
|
|
|
Due time.Time `db:"due"`
|
|
|
|
Modified time.Time `db:"modified"`
|
|
|
|
Tag string `db:"tag"`
|
|
|
|
Type *VoteType `db:"votetype"`
|
|
|
|
Sums *VoteSums `db:"-"`
|
|
|
|
Votes []*Vote `db:"-"`
|
|
|
|
Reasoning string `db:"-"`
|
2022-05-26 19:04:47 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 17:18:17 +00:00
|
|
|
type MotionModel struct {
|
2022-05-29 13:36:27 +00:00
|
|
|
DB *sqlx.DB
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new decision.
|
2022-05-21 17:18:17 +00:00
|
|
|
func (m *MotionModel) Create(
|
2022-05-21 12:27:46 +00:00
|
|
|
ctx context.Context,
|
2022-05-26 13:27:25 +00:00
|
|
|
proponent *User,
|
2022-05-22 19:47:27 +00:00
|
|
|
voteType *VoteType,
|
2022-05-15 18:10:49 +00:00
|
|
|
title, content string,
|
|
|
|
proposed, due time.Time,
|
|
|
|
) (int64, error) {
|
2022-05-21 17:18:17 +00:00
|
|
|
d := &Motion{
|
2022-05-15 18:10:49 +00:00
|
|
|
Proposed: proposed.UTC(),
|
|
|
|
Proponent: proponent.ID,
|
|
|
|
Title: title,
|
|
|
|
Content: content,
|
|
|
|
Due: due.UTC(),
|
2022-05-26 13:27:25 +00:00
|
|
|
Type: voteType,
|
2022-05-27 15:39:54 +00:00
|
|
|
Status: voteStatusPending,
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 12:27:46 +00:00
|
|
|
result, err := m.DB.NamedExecContext(
|
|
|
|
ctx,
|
|
|
|
`INSERT INTO decisions
|
2022-05-15 18:10:49 +00:00
|
|
|
(proposed, proponent, title, content, votetype, status, due, modified, tag)
|
|
|
|
VALUES (:proposed, :proponent, :title, :content, :votetype, :status, :due, :proposed,
|
2022-05-21 17:18:17 +00:00
|
|
|
'm' || STRFTIME('%Y%m%d', :proposed) || '.' || (
|
2022-05-15 18:10:49 +00:00
|
|
|
SELECT COUNT(*)+1 AS num
|
|
|
|
FROM decisions
|
2022-05-21 17:18:17 +00:00
|
|
|
WHERE proposed BETWEEN DATE(:proposed) AND DATE(:proposed, '1 day')
|
2022-05-21 12:27:46 +00:00
|
|
|
))`,
|
|
|
|
d,
|
|
|
|
)
|
2022-05-15 18:10:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("creating motion failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := result.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not get inserted decision id: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
func (m *MotionModel) CloseDecisions(ctx context.Context) ([]*Motion, error) {
|
2022-05-21 12:27:46 +00:00
|
|
|
tx, err := m.DB.BeginTxx(ctx, nil)
|
2022-05-21 11:51:17 +00:00
|
|
|
if err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return nil, fmt.Errorf(errCouldNotStartTransaction, err)
|
2022-05-21 11:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func(tx *sqlx.Tx) {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}(tx)
|
|
|
|
|
|
|
|
rows, err := tx.NamedQuery(`
|
2022-05-15 18:10:49 +00:00
|
|
|
SELECT decisions.id, decisions.tag, decisions.proponent, decisions.proposed, decisions.title, decisions.content,
|
|
|
|
decisions.votetype, decisions.status, decisions.due, decisions.modified
|
|
|
|
FROM decisions
|
|
|
|
WHERE decisions.status=0 AND :now > due`, struct{ Now time.Time }{Now: time.Now().UTC()})
|
|
|
|
if err != nil {
|
2022-05-21 11:51:17 +00:00
|
|
|
return nil, fmt.Errorf("fetching closable decisions failed: %w", err)
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func(rows *sqlx.Rows) {
|
|
|
|
_ = rows.Close()
|
|
|
|
}(rows)
|
|
|
|
|
2022-05-21 17:18:17 +00:00
|
|
|
decisions := make([]*Motion, 0)
|
2022-05-15 18:10:49 +00:00
|
|
|
|
|
|
|
for rows.Next() {
|
2022-05-21 17:18:17 +00:00
|
|
|
decision := &Motion{}
|
2022-05-15 18:10:49 +00:00
|
|
|
if err = rows.StructScan(decision); err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return nil, fmt.Errorf(errCouldNotScanResult, err)
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if rows.Err() != nil {
|
2022-05-21 11:51:17 +00:00
|
|
|
return nil, fmt.Errorf("row error: %w", err)
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decisions = append(decisions, decision)
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
results := make([]*Motion, 0, len(decisions))
|
2022-05-21 11:51:17 +00:00
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
var decisionResult *Motion
|
2022-05-21 11:51:17 +00:00
|
|
|
|
2022-05-15 18:10:49 +00:00
|
|
|
for _, decision := range decisions {
|
2022-05-29 13:36:27 +00:00
|
|
|
if decisionResult, err = closeDecision(ctx, tx, decision); err != nil {
|
2022-05-21 11:51:17 +00:00
|
|
|
return nil, fmt.Errorf("closing decision %s failed: %w", decision.Tag, err)
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
2022-05-21 11:51:17 +00:00
|
|
|
|
|
|
|
results = append(results, decisionResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return nil, fmt.Errorf(errCouldNotCommitTransaction, err)
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 11:51:17 +00:00
|
|
|
return results, nil
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
func closeDecision(ctx context.Context, tx *sqlx.Tx, d *Motion) (*Motion, error) {
|
2022-05-26 13:27:25 +00:00
|
|
|
quorum, majority := d.Type.QuorumAndMajority()
|
2022-05-21 11:51:17 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
voteSums *VoteSums
|
|
|
|
err error
|
|
|
|
reasoning string
|
|
|
|
)
|
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
// TODO: implement prefetching in CloseDecisions
|
|
|
|
if voteSums, err = sumsForDecision(ctx, tx, d); err != nil {
|
2022-05-21 11:51:17 +00:00
|
|
|
return nil, fmt.Errorf("getting vote sums failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Status, reasoning = voteSums.CalculateResult(quorum, majority)
|
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
result, err := tx.NamedExecContext(
|
2022-05-21 12:27:46 +00:00
|
|
|
ctx,
|
2022-05-21 11:51:17 +00:00
|
|
|
`UPDATE decisions SET status=:status, modified=CURRENT_TIMESTAMP WHERE id=:id`,
|
|
|
|
d,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return nil, fmt.Errorf(errCouldNotExecuteQuery, err)
|
2022-05-21 11:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
affectedRows, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get affected rows count: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if affectedRows != 1 {
|
|
|
|
return nil, fmt.Errorf("unexpected number of rows %d instead of 1", affectedRows)
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
d.Sums = voteSums
|
|
|
|
d.Reasoning = reasoning
|
|
|
|
|
|
|
|
return d, nil
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
func (m *MotionModel) UnvotedForVoter(ctx context.Context, voter *User) ([]*Motion, error) {
|
|
|
|
// TODO: implement more efficient variant that fetches unvoted votes for a slice of voters
|
2022-05-29 10:01:58 +00:00
|
|
|
rows, err := m.DB.QueryxContext(
|
|
|
|
ctx,
|
|
|
|
`SELECT decisions.*
|
|
|
|
FROM decisions
|
2022-05-29 13:36:27 +00:00
|
|
|
WHERE due < ? AND status=? AND NOT EXISTS(SELECT * FROM votes WHERE decision = decisions.id AND voter = ?)`,
|
|
|
|
time.Now().UTC(),
|
|
|
|
voteStatusPending,
|
2022-05-29 10:01:58 +00:00
|
|
|
voter.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(errCouldNotExecuteQuery, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func(rows *sqlx.Rows) {
|
|
|
|
_ = rows.Close()
|
|
|
|
}(rows)
|
|
|
|
|
|
|
|
result := make([]*Motion, 0)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf(errCouldNotFetchRow, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var motion Motion
|
|
|
|
|
|
|
|
if err := rows.StructScan(&motion); err != nil {
|
|
|
|
return nil, fmt.Errorf(errCouldNotScanResult, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, &motion)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
2022-05-21 11:51:17 +00:00
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
func sumsForDecision(ctx context.Context, tx *sqlx.Tx, d *Motion) (*VoteSums, error) {
|
2022-05-21 12:27:46 +00:00
|
|
|
voteRows, err := tx.QueryxContext(
|
|
|
|
ctx,
|
2022-05-21 11:51:17 +00:00
|
|
|
`SELECT vote, COUNT(vote) FROM votes WHERE decision=$1 GROUP BY vote`,
|
|
|
|
d.ID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("fetching vote sums for motion %s failed: %w", d.Tag, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func(voteRows *sqlx.Rows) {
|
|
|
|
_ = voteRows.Close()
|
|
|
|
}(voteRows)
|
|
|
|
|
|
|
|
sums := &VoteSums{}
|
|
|
|
|
|
|
|
for voteRows.Next() {
|
|
|
|
var (
|
2022-05-27 12:42:39 +00:00
|
|
|
vote *VoteChoice
|
2022-05-21 11:51:17 +00:00
|
|
|
count int
|
|
|
|
)
|
|
|
|
|
|
|
|
if err = voteRows.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not fetch vote sums for motion %s: %w", d.Tag, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = voteRows.Scan(&vote, &count); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse row for vote sums of motion %s: %w", d.Tag, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch vote {
|
2022-05-27 12:42:39 +00:00
|
|
|
case VoteAye:
|
2022-05-21 11:51:17 +00:00
|
|
|
sums.Ayes = count
|
2022-05-27 12:42:39 +00:00
|
|
|
case VoteNaye:
|
2022-05-21 11:51:17 +00:00
|
|
|
sums.Nayes = count
|
2022-05-27 12:42:39 +00:00
|
|
|
case VoteAbstain:
|
2022-05-21 11:51:17 +00:00
|
|
|
sums.Abstains = count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sums, nil
|
|
|
|
}
|
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
func (m *MotionModel) NextPendingDue(ctx context.Context) (*time.Time, error) {
|
2022-05-21 12:27:46 +00:00
|
|
|
row := m.DB.QueryRowContext(
|
|
|
|
ctx,
|
|
|
|
`SELECT due FROM decisions WHERE status=0 ORDER BY due LIMIT 1`,
|
|
|
|
nil,
|
|
|
|
)
|
2022-05-21 11:51:17 +00:00
|
|
|
|
|
|
|
if row == nil {
|
|
|
|
return nil, errors.New("no row returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := row.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not retrieve row for next pending decision: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var due time.Time
|
|
|
|
|
|
|
|
if err := row.Scan(&due); err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("parsing result failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &due, nil
|
|
|
|
}
|
2022-05-21 17:18:17 +00:00
|
|
|
|
|
|
|
type MotionListOptions struct {
|
|
|
|
Limit int
|
2022-05-26 13:27:25 +00:00
|
|
|
UnvotedOnly bool
|
2022-05-21 17:18:17 +00:00
|
|
|
Before, After *time.Time
|
2022-05-26 14:47:57 +00:00
|
|
|
VoterID int64
|
2022-05-21 17:18:17 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 13:27:25 +00:00
|
|
|
func (m *MotionModel) TimestampRange(ctx context.Context, options *MotionListOptions) (*time.Time, *time.Time, error) {
|
|
|
|
var row *sqlx.Row
|
|
|
|
|
|
|
|
if options.UnvotedOnly {
|
|
|
|
row = m.DB.QueryRowxContext(
|
|
|
|
ctx,
|
|
|
|
`SELECT MIN(proposed), MAX(proposed)
|
|
|
|
FROM decisions
|
|
|
|
WHERE due >= ?
|
|
|
|
AND NOT EXISTS(SELECT * FROM votes WHERE decision = decisions.id AND voter = ?)`,
|
|
|
|
time.Now().UTC(),
|
2022-05-26 14:47:57 +00:00
|
|
|
options.VoterID,
|
2022-05-26 13:27:25 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
row = m.DB.QueryRowxContext(
|
|
|
|
ctx,
|
|
|
|
`SELECT MIN(proposed), MAX(proposed) FROM decisions`,
|
|
|
|
)
|
|
|
|
}
|
2022-05-21 17:18:17 +00:00
|
|
|
|
|
|
|
if err := row.Err(); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("could not query for motion timestamps: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
first, last sql.NullString
|
|
|
|
firstTs, lastTs *time.Time
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := row.Scan(&first, &last); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("could not scan timestamps: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !first.Valid || !last.Valid {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if firstTs, err = parseSqlite3TimeStamp(first.String); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastTs, err = parseSqlite3TimeStamp(last.String); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstTs, lastTs, nil
|
|
|
|
}
|
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
func (m *MotionModel) List(ctx context.Context, options *MotionListOptions) ([]*Motion, error) {
|
2022-05-21 18:49:35 +00:00
|
|
|
var (
|
|
|
|
rows *sqlx.Rows
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case options.Before != nil:
|
2022-05-29 13:36:27 +00:00
|
|
|
rows, err = m.rowsBefore(ctx, options)
|
2022-05-21 18:49:35 +00:00
|
|
|
case options.After != nil:
|
2022-05-29 13:36:27 +00:00
|
|
|
rows, err = m.rowsAfter(ctx, options)
|
2022-05-21 18:49:35 +00:00
|
|
|
default:
|
2022-05-29 13:36:27 +00:00
|
|
|
rows, err = m.rowsFirst(ctx, options)
|
2022-05-21 17:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-05-21 18:49:35 +00:00
|
|
|
return nil, err
|
2022-05-21 17:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func(rows *sqlx.Rows) {
|
|
|
|
_ = rows.Close()
|
|
|
|
}(rows)
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
motions := make([]*Motion, 0, options.Limit)
|
2022-05-21 17:18:17 +00:00
|
|
|
|
|
|
|
for rows.Next() {
|
2022-05-27 15:39:54 +00:00
|
|
|
var decision Motion
|
2022-05-21 17:18:17 +00:00
|
|
|
|
|
|
|
if err = rows.Err(); err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return nil, fmt.Errorf(errCouldNotFetchRow, err)
|
2022-05-21 17:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = rows.StructScan(&decision); err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return nil, fmt.Errorf(errCouldNotScanResult, err)
|
2022-05-21 17:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
motions = append(motions, &decision)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(motions) > 0 {
|
|
|
|
err = m.FillVoteSums(ctx, motions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return motions, nil
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
func (m *MotionModel) FillVoteSums(ctx context.Context, decisions []*Motion) error {
|
2022-05-21 17:18:17 +00:00
|
|
|
decisionIds := make([]int64, len(decisions))
|
2022-05-27 15:39:54 +00:00
|
|
|
decisionMap := make(map[int64]*Motion, len(decisions))
|
2022-05-21 17:18:17 +00:00
|
|
|
|
|
|
|
for idx, decision := range decisions {
|
2022-05-27 15:39:54 +00:00
|
|
|
decision.Sums = &VoteSums{}
|
2022-05-21 17:18:17 +00:00
|
|
|
decisionIds[idx] = decision.ID
|
|
|
|
decisionMap[decision.ID] = decision
|
|
|
|
}
|
|
|
|
|
|
|
|
query, args, err := sqlx.In(
|
2022-05-29 13:36:27 +00:00
|
|
|
`SELECT v.decision, v.vote, COUNT(*)
|
|
|
|
FROM votes v
|
|
|
|
WHERE v.decision IN (?)
|
|
|
|
GROUP BY v.decision, v.vote`,
|
2022-05-21 17:18:17 +00:00
|
|
|
decisionIds,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create IN query: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := m.DB.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return fmt.Errorf(errCouldNotExecuteQuery, err)
|
2022-05-21 17:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func(rows *sql.Rows) {
|
|
|
|
_ = rows.Close()
|
|
|
|
}(rows)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
if err = rows.Err(); err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return fmt.Errorf(errCouldNotFetchRow, err)
|
2022-05-21 17:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
decisionID int64
|
2022-05-27 12:42:39 +00:00
|
|
|
vote *VoteChoice
|
2022-05-21 17:18:17 +00:00
|
|
|
count int
|
|
|
|
)
|
|
|
|
|
|
|
|
err = rows.Scan(&decisionID, &vote, &count)
|
|
|
|
if err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return fmt.Errorf(errCouldNotScanResult, err)
|
2022-05-21 17:18:17 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:42:39 +00:00
|
|
|
switch {
|
|
|
|
case vote.Equal(VoteAye):
|
2022-05-21 17:18:17 +00:00
|
|
|
decisionMap[decisionID].Sums.Ayes = count
|
2022-05-27 12:42:39 +00:00
|
|
|
case vote.Equal(VoteNaye):
|
2022-05-21 17:18:17 +00:00
|
|
|
decisionMap[decisionID].Sums.Nayes = count
|
2022-05-27 12:42:39 +00:00
|
|
|
case vote.Equal(VoteAbstain):
|
2022-05-21 17:18:17 +00:00
|
|
|
decisionMap[decisionID].Sums.Abstains = count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-21 18:49:35 +00:00
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
func (m *MotionModel) rowsBefore(ctx context.Context, options *MotionListOptions) (*sqlx.Rows, error) {
|
|
|
|
// TODO: implement variant for options.UnvotedOnly
|
2022-05-21 18:49:35 +00:00
|
|
|
rows, err := m.DB.QueryxContext(
|
|
|
|
ctx,
|
|
|
|
`SELECT decisions.id,
|
|
|
|
decisions.tag,
|
|
|
|
decisions.proponent,
|
|
|
|
voters.name AS proposer,
|
|
|
|
decisions.proposed,
|
|
|
|
decisions.title,
|
|
|
|
decisions.content,
|
|
|
|
decisions.votetype,
|
|
|
|
decisions.status,
|
|
|
|
decisions.due,
|
|
|
|
decisions.modified
|
|
|
|
FROM decisions
|
|
|
|
JOIN voters ON decisions.proponent = voters.id
|
|
|
|
WHERE decisions.proposed < $1
|
|
|
|
ORDER BY proposed DESC
|
|
|
|
LIMIT $2`,
|
|
|
|
options.Before,
|
|
|
|
options.Limit,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not query motions before %s: %w", options.Before, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows, nil
|
|
|
|
}
|
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
func (m *MotionModel) rowsAfter(ctx context.Context, options *MotionListOptions) (*sqlx.Rows, error) {
|
|
|
|
// TODO: implement variant for options.UnvotedOnly
|
2022-05-21 18:49:35 +00:00
|
|
|
rows, err := m.DB.QueryxContext(
|
|
|
|
ctx,
|
|
|
|
`WITH display_decision AS (SELECT decisions.id,
|
|
|
|
decisions.tag,
|
|
|
|
decisions.proponent,
|
|
|
|
voters.name AS proposer,
|
|
|
|
decisions.proposed,
|
|
|
|
decisions.title,
|
|
|
|
decisions.content,
|
|
|
|
decisions.votetype,
|
|
|
|
decisions.status,
|
|
|
|
decisions.due,
|
|
|
|
decisions.modified
|
|
|
|
FROM decisions
|
|
|
|
JOIN voters ON decisions.proponent = voters.id
|
|
|
|
WHERE decisions.proposed > $1
|
|
|
|
ORDER BY proposed
|
|
|
|
LIMIT $2)
|
|
|
|
SELECT *
|
|
|
|
FROM display_decision
|
|
|
|
ORDER BY proposed DESC`,
|
|
|
|
options.After,
|
|
|
|
options.Limit,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not query motions after %s: %w", options.After, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows, nil
|
|
|
|
}
|
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
func (m *MotionModel) rowsFirst(ctx context.Context, options *MotionListOptions) (*sqlx.Rows, error) {
|
2022-05-26 13:27:25 +00:00
|
|
|
var (
|
|
|
|
rows *sqlx.Rows
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if options.UnvotedOnly {
|
|
|
|
rows, err = m.DB.QueryxContext(
|
|
|
|
ctx,
|
|
|
|
`SELECT decisions.id,
|
|
|
|
decisions.tag,
|
|
|
|
decisions.proponent,
|
|
|
|
voters.name AS proposer,
|
|
|
|
decisions.proposed,
|
|
|
|
decisions.title,
|
|
|
|
decisions.content,
|
|
|
|
decisions.votetype,
|
|
|
|
decisions.status,
|
|
|
|
decisions.due,
|
|
|
|
decisions.modified
|
|
|
|
FROM decisions
|
|
|
|
JOIN voters ON decisions.proponent = voters.id
|
2022-05-29 13:36:27 +00:00
|
|
|
WHERE status=? AND due >= ? AND NOT EXISTS(SELECT * FROM votes WHERE decision = decisions.id AND voter = ?)
|
2022-05-26 13:27:25 +00:00
|
|
|
ORDER BY decisions.proposed DESC
|
|
|
|
LIMIT ?`,
|
2022-05-29 13:36:27 +00:00
|
|
|
voteStatusPending,
|
2022-05-26 13:27:25 +00:00
|
|
|
time.Now().UTC(),
|
2022-05-29 13:36:27 +00:00
|
|
|
options.VoterID,
|
2022-05-26 13:27:25 +00:00
|
|
|
options.Limit,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
rows, err = m.DB.QueryxContext(
|
|
|
|
ctx,
|
|
|
|
`SELECT decisions.id,
|
2022-05-21 18:49:35 +00:00
|
|
|
decisions.tag,
|
|
|
|
decisions.proponent,
|
|
|
|
voters.name AS proposer,
|
|
|
|
decisions.proposed,
|
|
|
|
decisions.title,
|
|
|
|
decisions.content,
|
|
|
|
decisions.votetype,
|
|
|
|
decisions.status,
|
|
|
|
decisions.due,
|
|
|
|
decisions.modified
|
|
|
|
FROM decisions
|
|
|
|
JOIN voters ON decisions.proponent = voters.id
|
|
|
|
ORDER BY proposed DESC
|
2022-05-26 13:27:25 +00:00
|
|
|
LIMIT ?`,
|
|
|
|
options.Limit,
|
|
|
|
)
|
|
|
|
}
|
2022-05-26 14:47:57 +00:00
|
|
|
|
2022-05-21 18:49:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not query motions: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows, nil
|
|
|
|
}
|
2022-05-22 12:08:02 +00:00
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
func (m *MotionModel) ByTag(ctx context.Context, tag string, withVotes bool) (*Motion, error) {
|
2022-05-22 12:08:02 +00:00
|
|
|
row := m.DB.QueryRowxContext(
|
|
|
|
ctx,
|
|
|
|
`SELECT decisions.id,
|
|
|
|
decisions.tag,
|
|
|
|
decisions.proponent,
|
|
|
|
voters.name AS proposer,
|
|
|
|
decisions.proposed,
|
|
|
|
decisions.title,
|
|
|
|
decisions.content,
|
|
|
|
decisions.votetype,
|
|
|
|
decisions.status,
|
|
|
|
decisions.due,
|
|
|
|
decisions.modified
|
|
|
|
FROM decisions
|
|
|
|
JOIN voters ON decisions.proponent = voters.id
|
|
|
|
WHERE decisions.tag = ?`,
|
|
|
|
tag,
|
|
|
|
)
|
|
|
|
if err := row.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not query motion: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
var result Motion
|
2022-05-22 12:08:02 +00:00
|
|
|
|
|
|
|
if err := row.StructScan(&result); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not fill motion from query result: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
if err := m.FillVoteSums(ctx, []*Motion{&result}); err != nil {
|
2022-05-22 12:08:02 +00:00
|
|
|
return nil, fmt.Errorf("could not get vote sums: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-26 19:04:47 +00:00
|
|
|
if result.ID != 0 && withVotes {
|
2022-05-22 12:08:02 +00:00
|
|
|
if err := m.FillVotes(ctx, &result); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get votes for %s: %w", result.Tag, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
func (m *MotionModel) FillVotes(ctx context.Context, md *Motion) error {
|
2022-05-22 12:08:02 +00:00
|
|
|
rows, err := m.DB.QueryxContext(ctx,
|
|
|
|
`SELECT voters.name, votes.vote
|
|
|
|
FROM voters
|
|
|
|
JOIN votes ON votes.voter = voters.id
|
|
|
|
WHERE votes.decision = ?
|
|
|
|
ORDER BY voters.name`,
|
|
|
|
md.ID)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("could not fetch rows: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func(rows *sqlx.Rows) {
|
|
|
|
_ = rows.Close()
|
|
|
|
}(rows)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return fmt.Errorf("could not get row: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
var vote Vote
|
2022-05-22 12:08:02 +00:00
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
if err := rows.StructScan(&vote); err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return fmt.Errorf(errCouldNotScanResult, err)
|
2022-05-22 12:08:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
md.Votes = append(md.Votes, &vote)
|
2022-05-22 12:08:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-26 13:27:25 +00:00
|
|
|
|
2022-05-29 13:36:27 +00:00
|
|
|
func (m *MotionModel) ByID(ctx context.Context, id int64) (*Motion, error) {
|
2022-05-26 13:27:25 +00:00
|
|
|
row := m.DB.QueryRowxContext(ctx, `SELECT * FROM decisions WHERE id=?`, id)
|
|
|
|
|
|
|
|
if err := row.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not fetch tag for id %d: %w", id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var motion Motion
|
|
|
|
|
|
|
|
if err := row.StructScan(&motion); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get tag from row: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &motion, nil
|
|
|
|
}
|
2022-05-26 19:04:47 +00:00
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
func (m *MotionModel) Update(
|
|
|
|
ctx context.Context,
|
|
|
|
id int64,
|
2022-05-27 18:45:04 +00:00
|
|
|
updateFn func(*Motion),
|
2022-05-27 15:39:54 +00:00
|
|
|
) error {
|
2022-05-27 18:45:04 +00:00
|
|
|
tx, err := m.DB.BeginTxx(ctx, nil)
|
|
|
|
if err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return fmt.Errorf(errCouldNotStartTransaction, err)
|
2022-05-27 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func(tx *sqlx.Tx) {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}(tx)
|
|
|
|
|
|
|
|
row := tx.QueryRowxContext(ctx, `SELECT * FROM decisions WHERE id=?`, id)
|
|
|
|
if err := row.Err(); err != nil {
|
|
|
|
return fmt.Errorf("could not select motion: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var motion Motion
|
|
|
|
|
|
|
|
if err := row.StructScan(&motion); err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return fmt.Errorf(errCouldNotScanResult, err)
|
2022-05-27 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateFn(&motion)
|
|
|
|
|
|
|
|
motion.Modified = time.Now().UTC()
|
|
|
|
|
|
|
|
_, err = tx.NamedExecContext(
|
2022-05-26 19:04:47 +00:00
|
|
|
ctx,
|
2022-05-27 18:45:04 +00:00
|
|
|
`UPDATE decisions
|
|
|
|
SET title=:title,
|
|
|
|
content=:content,
|
|
|
|
votetype=:votetype,
|
|
|
|
due=:due,
|
2022-05-29 10:01:58 +00:00
|
|
|
modified=:modified,
|
|
|
|
status=:status
|
2022-05-27 18:45:04 +00:00
|
|
|
WHERE id = :id`,
|
|
|
|
motion,
|
2022-05-26 19:04:47 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not update decision: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-27 18:45:04 +00:00
|
|
|
if err := tx.Commit(); err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return fmt.Errorf(errCouldNotCommitTransaction, err)
|
2022-05-27 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 19:04:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-05-27 15:39:54 +00:00
|
|
|
|
|
|
|
type Vote struct {
|
|
|
|
UserID int64 `db:"voter"`
|
|
|
|
MotionID int64 `db:"decision"`
|
|
|
|
Vote *VoteChoice `db:"vote"`
|
|
|
|
Voted time.Time `db:"voted"`
|
|
|
|
Notes string `db:"notes"`
|
|
|
|
Name string `db:"name"`
|
|
|
|
}
|
|
|
|
|
2022-05-27 18:45:04 +00:00
|
|
|
func (m *MotionModel) UpdateVote(ctx context.Context, userID, motionID int64, performVoteFn func(v *Vote)) error {
|
2022-05-27 15:39:54 +00:00
|
|
|
tx, err := m.DB.BeginTxx(ctx, nil)
|
|
|
|
if err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return fmt.Errorf(errCouldNotStartTransaction, err)
|
2022-05-27 15:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func(tx *sqlx.Tx) {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}(tx)
|
|
|
|
|
|
|
|
row := tx.QueryRowxContext(ctx, `SELECT * FROM votes WHERE voter=? AND decision=?`, userID, motionID)
|
|
|
|
if err := row.Err(); err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return fmt.Errorf(errCouldNotExecuteQuery, err)
|
2022-05-27 15:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vote := Vote{UserID: userID, MotionID: motionID}
|
|
|
|
|
|
|
|
if err := row.StructScan(&vote); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return fmt.Errorf("could not scan vote structure: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
performVoteFn(&vote)
|
|
|
|
|
|
|
|
if _, err := tx.NamedExecContext(
|
|
|
|
ctx,
|
|
|
|
`INSERT INTO votes (decision, voter, vote, voted, notes)
|
|
|
|
VALUES (:decision, :voter, :vote, :voted, :notes)
|
|
|
|
ON CONFLICT (decision, voter)
|
|
|
|
DO UPDATE SET vote=:vote,
|
|
|
|
voted=:voted,
|
|
|
|
notes=:notes
|
|
|
|
WHERE decision = :decision
|
|
|
|
AND voter = :voter`,
|
|
|
|
vote,
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("could not insert or update vote: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2022-05-29 10:01:58 +00:00
|
|
|
return fmt.Errorf(errCouldNotCommitTransaction, err)
|
2022-05-27 15:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-29 13:36:27 +00:00
|
|
|
|
|
|
|
func (m *MotionModel) Withdraw(ctx context.Context, id int64) error {
|
|
|
|
return m.Update(ctx, id, func(m *Motion) {
|
|
|
|
m.Status = voteStatusWithdrawn
|
|
|
|
})
|
|
|
|
}
|