package console import ( "strings" "github.com/carlosframework/platform/internal/domains" ) // matchAccounts resolves one path/flag segment against the accounts the // VIEWER belongs to, matching it as a sqid or a name — symmetrically, with // no precedence between the two spaces. Precedence is how a reference // silently changes meaning the day a colliding account appears; a multi- // match is returned whole so the caller can be loud instead (picker page, // CLI error). Names match case-insensitively; an account whose name equals // its own sqid still counts once. // // The returned pointers address accts, so each match carries whatever // AccountsFor filled in — the viewer's Role included. func matchAccounts(accts []Account, seg string) []*Account { var out []*Account for i := range accts { if accts[i].Sqid == seg || strings.EqualFold(accts[i].Name, seg) { out = append(out, &accts[i]) } } return out } // preferredSeg is the path segment the console GENERATES for an account: // its name, when the name is slug-shaped and resolves uniquely among the // viewer's own accounts; the sqid otherwise. Links and redirects use it — // forms always post to the sqid path (page.Base), because a mutation must // name its target exactly and prettiness is for links. func preferredSeg(accts []Account, a *Account) string { if !domains.ValidAccountName(a.Name) { return a.Sqid } for i := range accts { if accts[i].ID == a.ID { continue } if strings.EqualFold(accts[i].Name, a.Name) || accts[i].Sqid == a.Name { return a.Sqid } } return a.Name }