oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package setup
2
3import (
4 "amadan.net/oficina/equipo/internal/runnerproto"
5 "context"
6 "encoding/json"
7 "errors"
8 "slices"
9 "time"
10
11 "gorm.io/gorm"
12 "gorm.io/gorm/clause"
13)
14
15type OnboardingState struct {
16 Revision int64
17 CurrentStep, ProviderChoice, RunnerID, AccountSetupID string
18 Completed map[string]bool
19}
20type OnboardingRecord struct {
21 Person string `gorm:"primaryKey"`
22 Revision int64
23 ProviderChoice, RunnerID, AccountSetupID string
24 Completed map[string]bool `gorm:"serializer:json"`
25}
26
27func (OnboardingRecord) TableName() string { return "onboarding_states" }
28
29var onboardingSteps = []string{"welcome", "account", "runner", "specialists", "first_task"}
30
31func (r OnboardingRecord) view() OnboardingState {
32 out := OnboardingState{Revision: r.Revision, ProviderChoice: r.ProviderChoice, RunnerID: r.RunnerID, AccountSetupID: r.AccountSetupID, Completed: r.Completed, CurrentStep: "complete"}
33 for _, step := range onboardingSteps {
34 if !r.Completed[step] {
35 out.CurrentStep = step
36 break
37 }
38 }
39 return out
40}
41func (s *Store) onboardingMember(a Actor) error {
42 m, e := s.deps.Member(s.tx, a.Address)
43 if e != nil {
44 return e
45 }
46 if !m.Active || (m.Role != RoleOwner && m.Role != RoleMember) {
47 return s.deps.Forbidden
48 }
49 return nil
50}
51func (s *Store) initializeOnboarding(a Actor) error {
52 r := OnboardingRecord{Person: a.Address, Revision: 1, Completed: map[string]bool{}}
53 return s.tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&r).Error
54}
55func (s *Store) onboarding(a Actor) (OnboardingRecord, error) {
56 var r OnboardingRecord
57 if e := s.onboardingMember(a); e != nil {
58 return r, e
59 }
60 e := s.tx.Where("person=?", a.Address).First(&r).Error
61 if errors.Is(e, gorm.ErrRecordNotFound) {
62 return OnboardingRecord{Person: a.Address, Revision: 1, Completed: map[string]bool{}}, nil
63 }
64 return r, e
65}
66func (s *Application) Onboarding(ctx context.Context, a Actor) (out OnboardingState, err error) {
67 a.Address = s.Normalize(a.Address)
68 err = s.Read(ctx, func(tx *gorm.DB) error { r, e := s.At(tx).onboarding(a); out = r.view(); return e })
69 if err != nil {
70 out = OnboardingState{}
71 }
72 return
73}
74func (s *Application) AdvanceOnboarding(ctx context.Context, a Actor, step string, m Mutation) (OnboardingState, error) {
75 return s.changeOnboarding(ctx, a, step, "", m)
76}
77func (s *Application) ChooseOnboardingProvider(ctx context.Context, a Actor, choice string, m Mutation) (OnboardingState, error) {
78 return s.changeOnboarding(ctx, a, "choose", choice, m)
79}
80func (s *Application) changeOnboarding(ctx context.Context, a Actor, step, choice string, m Mutation) (out OnboardingState, err error) {
81 a.Address = s.Normalize(a.Address)
82 err = s.Write(ctx, func(tx *gorm.DB) error {
83 var e error
84 out, e = s.At(tx).advanceOnboarding(a, step, choice, m)
85 return e
86 })
87 if err != nil {
88 out = OnboardingState{}
89 }
90 return
91}
92func (s *Store) advanceOnboarding(a Actor, step, choice string, m Mutation) (OnboardingState, error) {
93 r, e := s.onboarding(a)
94 if e != nil {
95 return OnboardingState{}, e
96 }
97 if step != "choose" && !slices.Contains(onboardingSteps, step) {
98 return OnboardingState{}, s.deps.Invalid
99 }
100 if step == "choose" && choice != "codex" && choice != "claude" {
101 return OnboardingState{}, s.deps.Invalid
102 }
103 input := struct {
104 Step, Choice string
105 Revision int64
106 }{step, choice, m.Revision}
107 var out OnboardingState
108 if ok, e := s.replay(a, "onboarding", a.Address, m, input, &out); ok || e != nil {
109 return out, e
110 }
111 if r.Revision != m.Revision {
112 return out, s.deps.Conflict
113 }
114 if step == "choose" {
115 if r.Completed["account"] || r.view().CurrentStep != "account" {
116 return out, s.deps.Conflict
117 }
118 r.ProviderChoice = choice
119 } else {
120 if r.view().CurrentStep != step {
121 return out, s.deps.Conflict
122 }
123 if e = s.onboardingPrerequisite(a, &r, step); e != nil {
124 return out, e
125 }
126 r.Completed[step] = true
127 }
128 if e = s.initializeOnboarding(a); e != nil {
129 return out, e
130 }
131 r.Revision++
132 q := s.tx.Model(&OnboardingRecord{}).Where("person=? AND revision=?", a.Address, m.Revision).Select("Revision", "ProviderChoice", "RunnerID", "AccountSetupID", "Completed").Updates(&r)
133 if q.Error != nil {
134 return out, q.Error
135 }
136 if q.RowsAffected != 1 {
137 return out, s.deps.Conflict
138 }
139 if e = s.deps.Audit(s.tx, a, "onboarding."+step, a.Address); e != nil {
140 return out, e
141 }
142 out = r.view()
143 return out, s.save(a, "onboarding", a.Address, m, input, out)
144}
145func (s *Store) onboardingPrerequisite(a Actor, r *OnboardingRecord, step string) error {
146 switch step {
147 case "account":
148 if r.ProviderChoice == "" {
149 return s.deps.Conflict
150 }
151 case "runner":
152 // A server-observed credential exchange and connection are prerequisites.
153 // Approval alone is not evidence of a paired, signed-in runner.
154 var rows []struct{ ID, ReportsJSON string }
155 q := s.tx.Raw(`SELECT runners.id, c.reports_json FROM runners JOIN runner_enrolments e ON e.runner_id=runners.id JOIN connections c ON c.runner_id=runners.id WHERE runners.person=? AND runners.revoked_at IS NULL AND e.consumed_at IS NOT NULL AND e.revoked_at IS NULL AND c.person=? AND c.last_seen>? ORDER BY runners.id`, a.Address, a.Address, s.deps.Now.Add(-2*time.Minute)).Scan(&rows)
156 if q.Error != nil {
157 return q.Error
158 }
159 for _, row := range rows {
160 var reports []runnerproto.ConnectionReport
161 if e := json.Unmarshal([]byte(row.ReportsJSON), &reports); e != nil {
162 return e
163 }
164 for _, report := range reports {
165 if report.Provider == r.ProviderChoice && report.Available && report.ReadIsolation {
166 r.RunnerID = row.ID
167 return nil
168 }
169 }
170 }
171 return s.deps.Conflict
172 case "specialists":
173 var count int64
174 if e := s.tx.Table("specialists").Where("archived=? AND key IN ?", false, []string{"product", "design", "copy", "engineering", "review"}).Count(&count).Error; e != nil {
175 return e
176 }
177 if count != 5 {
178 return s.deps.Conflict
179 }
180 case "first_task":
181 var count int64
182 if e := s.tx.Table("tasks").Where("requester=? AND kind=?", a.Address, "recommendation").Count(&count).Error; e != nil {
183 return e
184 }
185 if count == 0 {
186 return s.deps.Conflict
187 }
188 }
189 return nil
190}
191