add basis for multiple reps

This commit is contained in:
Miloš Jovanović 2026-09-10 11:52:32 +02:00
parent 64ae38e11c
commit d666849759
2 changed files with 34 additions and 0 deletions

View file

@ -35,6 +35,8 @@ type Store interface {
UpsertPeople(users []User, eligible map[string]bool) error UpsertPeople(users []User, eligible map[string]bool) error
LastSync() (int64, error) LastSync() (int64, error)
SetLastSync(ts int64) error SetLastSync(ts int64) error
RepsFor(chapterID int64, dept string) ([]int64, error)
EligibleMembers(chapterID int64, dept string) ([]int64, error)
} }
// ErrLocked is returned by AcquireLock when a run is already in flight // ErrLocked is returned by AcquireLock when a run is already in flight

View file

@ -57,6 +57,38 @@ func OpenStore(path string) (*SQLStore, error) {
return &SQLStore{db: db}, nil return &SQLStore{db: db}, nil
} }
// RepsFor returns the user IDs of every rep for a department, ordered by
// ID so assignment is deterministic across runs.
func (s *SQLStore) RepsFor(chapterID int64, dept string) ([]int64, error) {
return s.userIDs(`SELECT id FROM people
WHERE is_rep = 1 AND chapter_id = ? AND department = ?
ORDER BY id`, chapterID, dept)
}
func (s *SQLStore) EligibleMembers(chapterID int64, dept string) ([]int64, error) {
return s.userIDs(`SELECT id FROM people
WHERE is_eligible = 1 AND chapter_id = ? AND department = ?
ORDER BY id`, chapterID, dept)
}
func (s *SQLStore) userIDs(query string, args ...any) ([]int64, error) {
rows, err := s.db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var out []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
return nil, err
}
out = append(out, id)
}
return out, rows.Err()
}
func (s *SQLStore) AcquireLock(chapterID int64, dept string, repID int64) (int64, error) { func (s *SQLStore) AcquireLock(chapterID int64, dept string, repID int64) (int64, error) {
res, err := s.db.Exec( res, err := s.db.Exec(
`INSERT INTO runs (chapter_id, department, rep_user_id, status) `INSERT INTO runs (chapter_id, department, rep_user_id, status)