reconcile and rep ids sorted

This commit is contained in:
Miloš Jovanović 2026-09-10 12:39:36 +02:00
parent ede7884b31
commit e0911df455
5 changed files with 112 additions and 12 deletions

View file

@ -10,6 +10,7 @@ import (
"net/url"
"strconv"
"time"
"bytes"
"golang.org/x/time/rate"
)
@ -123,7 +124,7 @@ func departmentOf(props map[string]json.RawMessage) string {
//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.
// same label appears more than one time
func optionLabels(props map[string]json.RawMessage, key string) []string {
raw, ok := props[key]
@ -163,6 +164,61 @@ func (c *Client) GetUser(ctx context.Context, id int64) (*User, error) {
return u.toUser(), nil
}
// ActiveAgent returns the user ID of the person's current agent, or 0 if
// they have none. The is_active query param is ignored by the API, so
// rows are filtered here.
func (c *Client) ActiveAgent(ctx context.Context, userID int64) (int64, error) {
q := url.Values{}
q.Set("user_id", strconv.FormatInt(userID, 10))
q.Set("_limit", "100")
raw, err := c.do(ctx, http.MethodGet, "/agent_assignments?"+q.Encode(), nil)
if err != nil {
return 0, err
}
var env struct {
Data []struct {
AgentUserID int64 `json:"agent_user_id"`
IsActive bool `json:"is_active"`
} `json:"data"`
}
if err := json.Unmarshal(raw, &env); err != nil {
return 0, err
}
for _, a := range env.Data {
if a.IsActive {
return a.AgentUserID, nil
}
}
return 0, nil
}
// AssignAgent creates an agent assignment. ST deactivates any prior active
// assignment automatically. Not idempotent: a repeat call creates a new row.
func (c *Client) AssignAgent(ctx context.Context, userID, agentID int64) error {
if c.dryRun {
log.Printf("DRY RUN: would assign user %d to agent %d", userID, agentID)
return nil
}
body, err := json.Marshal(map[string]int64{
"user_id": userID,
"agent_user_id": agentID,
})
if err != nil {
return err
}
_, err = c.do(ctx, http.MethodPost, "/agent_assignments", bytes.NewReader(body))
if err != nil {
return err
}
log.Printf("assigned user %d to agent %d", userID, agentID)
return 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) {