| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "sort" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/release" |
| 10 | ) |
| 11 | |
| 12 | type AccountReviewGroup struct { |
| 13 | Account Account |
| 14 | Hosts []string |
| 15 | } |
| 16 | |
| 17 | type AccountReview struct { |
| 18 | Groups []AccountReviewGroup |
| 19 | Count int |
| 20 | NoInstances bool |
| 21 | Owned int |
| 22 | blocked bool |
| 23 | } |
| 24 | |
| 25 | func (a AccountReview) SummaryText() string { |
| 26 | return strings.ReplaceAll(approvedMismatchCopy.Summary, "{count}", strconv.Itoa(a.Count)) |
| 27 | } |
| 28 | |
| 29 | // appAccountReview returns only candidates whose source account the viewer |
| 30 | // can access. A source with its own same-named app is a legitimate sibling, |
| 31 | // not evidence that the viewed app is half-migrated. |
| 32 | func (s *Server) appAccountReview(address string, viewed *Account, app string) (AccountReview, bool, error) { |
| 33 | reg, err := openRegistry(s.cfg.Registry) |
| 34 | if err != nil { |
| 35 | return AccountReview{}, false, err |
| 36 | } |
| 37 | routes, err := reg.List() |
| 38 | if err != nil { |
| 39 | return AccountReview{}, false, err |
| 40 | } |
| 41 | accounts, err := s.store.AccountsFor(address) |
| 42 | if err != nil { |
| 43 | return AccountReview{}, false, err |
| 44 | } |
| 45 | accessible := make(map[string]Account, len(accounts)) |
| 46 | for _, account := range accounts { |
| 47 | accessible[account.Sqid] = account |
| 48 | } |
| 49 | groups := make(map[string][]string) |
| 50 | owned, hidden, sibling := 0, false, false |
| 51 | for _, rt := range routes { |
| 52 | if rt.App != app || rt.Platform { |
| 53 | continue |
| 54 | } |
| 55 | source := release.RouteAccount(rt) |
| 56 | if source == viewed.Sqid { |
| 57 | owned++ |
| 58 | continue |
| 59 | } |
| 60 | account, ok := accessible[source] |
| 61 | if !ok { |
| 62 | hidden = true |
| 63 | continue |
| 64 | } |
| 65 | has, err := s.store.HasAppIn(account.ID, app) |
| 66 | if err != nil { |
| 67 | return AccountReview{}, false, err |
| 68 | } |
| 69 | if !has { |
| 70 | has, err = s.store.HasTrashedAppIn(account.ID, app) |
| 71 | if err != nil { |
| 72 | return AccountReview{}, false, err |
| 73 | } |
| 74 | } |
| 75 | if has { |
| 76 | sibling = true |
| 77 | continue |
| 78 | } |
| 79 | groups[source] = append(groups[source], rt.Host) |
| 80 | } |
| 81 | var review AccountReview |
| 82 | review.Owned = owned |
| 83 | review.blocked = hidden || sibling |
| 84 | for source, hosts := range groups { |
| 85 | sort.Strings(hosts) |
| 86 | review.Groups = append(review.Groups, AccountReviewGroup{Account: accessible[source], Hosts: hosts}) |
| 87 | review.Count += len(hosts) |
| 88 | } |
| 89 | sort.Slice(review.Groups, func(i, j int) bool { return review.Groups[i].Account.Sqid < review.Groups[j].Account.Sqid }) |
| 90 | return review, review.Count > 0 && owned == 0 && !hidden && !sibling && len(review.Groups) == 1, nil |
| 91 | } |
| 92 | |
| 93 | func (s *Server) handleAppAccountReview(w http.ResponseWriter, r *http.Request) { |
| 94 | p, _, ok := s.appPage(w, r, "overview") |
| 95 | if !ok { |
| 96 | return |
| 97 | } |
| 98 | s.render(w, http.StatusOK, "app_account_review", p) |
| 99 | } |
| 100 | |