add basic schema and sync function, sorting by appointment type

This commit is contained in:
Miloš Jovanović 2026-09-09 23:46:35 +02:00
parent b665bf63a0
commit 40f16346f5
4 changed files with 207 additions and 33 deletions

View file

@ -3,6 +3,7 @@ package main
import (
"context"
"crypto/subtle"
"fmt"
"errors"
"log"
"net/http"
@ -17,6 +18,7 @@ type User struct {
Email string
ChapterID int64
Department string
AppointmentTypes []string
}
// STClient is what the handler needs from the Solidarity Tech API.
@ -38,27 +40,68 @@ type Server struct {
st STClient
store Store
secret string
// runTimeout caps a single reconcile. At 2 req/sec, size this
// against your largest department.
eligible map[string]bool
// runTimeout caps a single reconcile.
runTimeout time.Duration
}
func NewServer(st STClient, store Store, secret string) *Server {
func NewServer(st STClient, store Store, secret string, eligible map[string]bool) *Server {
return &Server{
st: st,
store: store,
secret: secret,
eligible: eligible,
runTimeout: 2 * time.Hour,
}
}
// Eligible reports whether any of the user's appointment types is in set.
// Labels are matched, not option values, so a rename in the ST dashboard
// silently disqualifies everyone — log unknown labels during sync.
func (u User) Eligible(set map[string]bool) bool {
for _, label := range u.AppointmentTypes {
if set[strings.ToLower(strings.TrimSpace(label))] {
return true
}
}
return false
}
func (s *Server) Routes() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("POST /webhook", s.handleWebhook)
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
// TEMPORARY: remove before deploy.
mux.HandleFunc("GET /debug/users", func(w http.ResponseWriter, r *http.Request) {
client, ok := s.st.(*Client)
if !ok {
http.Error(w, "not a real client", 500)
return
}
users, err := client.ListUsers(r.Context(), 0)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
fmt.Fprintf(w, "%d users\n", len(users))
counts := map[string]int{}
eligible := 0
for _, u := range users {
if !u.Eligible(s.eligible) {
continue
}
eligible++
counts[fmt.Sprintf("%d|%s", u.ChapterID, normalizeDept(u.Department))]++
}
fmt.Fprintf(w, "%d users, %d eligible\n\n", len(users), eligible)
for k, n := range counts {
fmt.Fprintf(w, "%6d %s\n", n, k)
}
})
return mux
}