| 1 | package setup |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "gorm.io/gorm" |
| 6 | "net/http" |
| 7 | "slices" |
| 8 | "strconv" |
| 9 | ) |
| 10 | |
| 11 | type OnboardingProject struct { |
| 12 | ID int64 |
| 13 | Name string |
| 14 | } |
| 15 | |
| 16 | func (s *Application) OnboardingProjects(ctx context.Context, a Actor) (out []OnboardingProject, err error) { |
| 17 | err = s.Read(ctx, func(tx *gorm.DB) error { |
| 18 | if e := s.At(tx).onboardingMember(a); e != nil { |
| 19 | return e |
| 20 | } |
| 21 | return tx.Table("projects").Select("id", "name").Order("name, id").Scan(&out).Error |
| 22 | }) |
| 23 | if err != nil { |
| 24 | out = nil |
| 25 | } |
| 26 | return |
| 27 | } |
| 28 | func (h *OnboardingHandler) accountForm(w http.ResponseWriter, r *http.Request, fields ...string) (Actor, bool) { |
| 29 | privacy(w) |
| 30 | a, e := h.Actor(r) |
| 31 | if e != nil { |
| 32 | h.Failure(w, r, e) |
| 33 | return a, false |
| 34 | } |
| 35 | r.Body = http.MaxBytesReader(w, r.Body, 4096) |
| 36 | if r.ParseForm() != nil { |
| 37 | h.Failure(w, r, h.Invalid) |
| 38 | return a, false |
| 39 | } |
| 40 | for k, v := range r.PostForm { |
| 41 | if len(v) != 1 || !slices.Contains(fields, k) { |
| 42 | h.Failure(w, r, h.Invalid) |
| 43 | return a, false |
| 44 | } |
| 45 | } |
| 46 | return a, true |
| 47 | } |
| 48 | func (h *OnboardingHandler) Account(w http.ResponseWriter, r *http.Request) { |
| 49 | a, ok := h.accountForm(w, r, "runner_id", "provider", "label", "request_id") |
| 50 | if !ok { |
| 51 | return |
| 52 | } |
| 53 | _, e := h.Store.RequestAccountSetup(r.Context(), a, r.PostForm.Get("runner_id"), r.PostForm.Get("provider"), r.PostForm.Get("label"), Mutation{RequestID: r.PostForm.Get("request_id")}) |
| 54 | if e != nil { |
| 55 | h.Failure(w, r, e) |
| 56 | return |
| 57 | } |
| 58 | http.Redirect(w, r, "/onboarding", http.StatusSeeOther) |
| 59 | } |
| 60 | func (h *OnboardingHandler) CancelAccount(w http.ResponseWriter, r *http.Request) { |
| 61 | a, ok := h.accountForm(w, r, "id", "revision", "request_id") |
| 62 | if !ok { |
| 63 | return |
| 64 | } |
| 65 | rev, e := strconv.ParseInt(r.PostForm.Get("revision"), 10, 64) |
| 66 | if e != nil { |
| 67 | h.Failure(w, r, h.Invalid) |
| 68 | return |
| 69 | } |
| 70 | e = h.Store.CancelAccountSetup(r.Context(), a, r.PostForm.Get("id"), Mutation{Revision: rev, RequestID: r.PostForm.Get("request_id")}) |
| 71 | if e != nil { |
| 72 | h.Failure(w, r, e) |
| 73 | return |
| 74 | } |
| 75 | http.Redirect(w, r, "/onboarding", http.StatusSeeOther) |
| 76 | } |
| 77 | func (h *OnboardingHandler) FirstTask(w http.ResponseWriter, r *http.Request) { |
| 78 | a, ok := h.accountForm(w, r, "project_id", "request_id") |
| 79 | if !ok { |
| 80 | return |
| 81 | } |
| 82 | id, e := strconv.ParseInt(r.PostForm.Get("project_id"), 10, 64) |
| 83 | if e != nil { |
| 84 | h.Failure(w, r, h.Invalid) |
| 85 | return |
| 86 | } |
| 87 | p, e := h.Store.StartOnboardingTask(r.Context(), a, id, Mutation{RequestID: r.PostForm.Get("request_id")}) |
| 88 | if e != nil { |
| 89 | h.Failure(w, r, e) |
| 90 | return |
| 91 | } |
| 92 | http.Redirect(w, r, "/tasks/"+p.TaskID, http.StatusSeeOther) |
| 93 | } |
| 94 | |