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

@ -2,13 +2,14 @@ package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"time"
"encoding/json"
"golang.org/x/time/rate"
)
@ -102,6 +103,7 @@ func (a apiUser) toUser() *User {
Email: a.Email,
ChapterID: a.ChapterID,
Department: departmentOf(a.CustomProps),
AppointmentTypes: optionLabels(a.CustomProps, "appointment-type"),
}
}
@ -118,27 +120,36 @@ func departmentOf(props map[string]json.RawMessage) string {
return s
}
//optionLabels returne the labels of an option-valued custom-property
// ST serialises select and checkbox fields as [{label, value}], and the
// same label can appear more than once.
func optionLabels(props map[string]json.RawMessage, key string) []string {
raw, ok := props[key]
if !ok || len(raw) == 0 {
return nil
}
var arr []struct {
Label string `json:"label"`
Value string `json:"value"`
}
if err := json.Unmarshal(raw, &arr); err != nil {
return nil
}
out := make([]string, 0, len(arr))
for _, o := range arr {
out = append(out, o.Label)
}
return out
}
// isRep tolerates all three shapes "department-rep" has been seen in:
// array of options, bare string, bare bool.
func isRep(props map[string]json.RawMessage) bool {
raw, ok := props["department-rep"]
if !ok || len(raw) == 0 {
return false
}
var arr []struct{ Value string }
if err := json.Unmarshal(raw, &arr); err == nil {
return len(arr) > 0
}
var s string
if err := json.Unmarshal(raw, &s); err == nil {
return s != "" && s != "false"
}
var b bool
if err := json.Unmarshal(raw, &b); err == nil {
return b
}
return false
return len(optionLabels(props, "department-rep")) > 0
}
func (c *Client) GetUser(ctx context.Context, id int64) (*User, error) {
raw, err := c.do(ctx, http.MethodGet, "/users/"+strconv.FormatInt(id, 10), nil)
if err != nil {
@ -150,3 +161,42 @@ func (c *Client) GetUser(ctx context.Context, id int64) (*User, error) {
}
return u.toUser(), nil
}
// ListUsers pages the full user list. If since is non-zero, only users
// updated after that Unix timestamp are returned.
func (c *Client) ListUsers(ctx context.Context, since int64) ([]User, error) {
var out []User
offset := 0
for {
q := url.Values{}
q.Set("_limit", "100")
q.Set("_offset", strconv.Itoa(offset))
if since > 0 {
q.Set("_since", strconv.FormatInt(since, 10))
}
raw, err := c.do(ctx, http.MethodGet, "/users?"+q.Encode(), nil)
if err != nil {
return nil, fmt.Errorf("listing users at offset %d: %w", offset, err)
}
var env struct {
Data []apiUser `json:"data"`
}
if err := json.Unmarshal(raw, &env); err != nil {
return nil, fmt.Errorf("parsing users at offset %d: %w", offset, err)
}
for _, a := range env.Data {
out = append(out, *a.toUser())
}
if len(env.Data) < 100 {
return out, nil
}
offset += 100
log.Printf("listed %d users so far", len(out))
}
}