package console import ( "net/http" "sort" "strconv" "strings" "github.com/carlosframework/platform/internal/release" ) type AccountReviewGroup struct { Account Account Hosts []string } type AccountReview struct { Groups []AccountReviewGroup Count int NoInstances bool Owned int blocked bool } func (a AccountReview) SummaryText() string { return strings.ReplaceAll(approvedMismatchCopy.Summary, "{count}", strconv.Itoa(a.Count)) } // appAccountReview returns only candidates whose source account the viewer // can access. A source with its own same-named app is a legitimate sibling, // not evidence that the viewed app is half-migrated. func (s *Server) appAccountReview(address string, viewed *Account, app string) (AccountReview, bool, error) { reg, err := openRegistry(s.cfg.Registry) if err != nil { return AccountReview{}, false, err } routes, err := reg.List() if err != nil { return AccountReview{}, false, err } accounts, err := s.store.AccountsFor(address) if err != nil { return AccountReview{}, false, err } accessible := make(map[string]Account, len(accounts)) for _, account := range accounts { accessible[account.Sqid] = account } groups := make(map[string][]string) owned, hidden, sibling := 0, false, false for _, rt := range routes { if rt.App != app || rt.Platform { continue } source := release.RouteAccount(rt) if source == viewed.Sqid { owned++ continue } account, ok := accessible[source] if !ok { hidden = true continue } has, err := s.store.HasAppIn(account.ID, app) if err != nil { return AccountReview{}, false, err } if !has { has, err = s.store.HasTrashedAppIn(account.ID, app) if err != nil { return AccountReview{}, false, err } } if has { sibling = true continue } groups[source] = append(groups[source], rt.Host) } var review AccountReview review.Owned = owned review.blocked = hidden || sibling for source, hosts := range groups { sort.Strings(hosts) review.Groups = append(review.Groups, AccountReviewGroup{Account: accessible[source], Hosts: hosts}) review.Count += len(hosts) } sort.Slice(review.Groups, func(i, j int) bool { return review.Groups[i].Account.Sqid < review.Groups[j].Account.Sqid }) return review, review.Count > 0 && owned == 0 && !hidden && !sibling && len(review.Groups) == 1, nil } func (s *Server) handleAppAccountReview(w http.ResponseWriter, r *http.Request) { p, _, ok := s.appPage(w, r, "overview") if !ok { return } s.render(w, http.StatusOK, "app_account_review", p) }