| 1 | package ownermode |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 6 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 7 | "errors" |
| 8 | "gorm.io/gorm" |
| 9 | ) |
| 10 | |
| 11 | func SelectBinding(tx *gorm.DB, b runnerproto.CoordinatorBinding, memberRev, connectionRev int64, memberRevision func(*gorm.DB, string) (int64, error), decode func(*planning.ConnectionRecord) error, eligible func(*gorm.DB, planning.ConnectionRecord, runnerproto.ConnectionReport) (bool, error), snapshot func(*gorm.DB, planning.ConnectionRecord, runnerproto.ConnectionReport) (team.IsolationSnapshot, error), forbidden, waiting error, storeError func(error) error) (runnerproto.Selection, error) { |
| 12 | m, err := memberRevision(tx, b.Person) |
| 13 | if err != nil { |
| 14 | return runnerproto.Selection{}, err |
| 15 | } |
| 16 | if m != memberRev { |
| 17 | return runnerproto.Selection{}, forbidden |
| 18 | } |
| 19 | var c planning.ConnectionRecord |
| 20 | if err = tx.First(&c, "id=?", b.ConnectionID).Error; err != nil { |
| 21 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 22 | return runnerproto.Selection{}, waiting |
| 23 | } |
| 24 | return runnerproto.Selection{}, storeError(err) |
| 25 | } |
| 26 | if c.Person != b.Person || c.RunnerID != b.RunnerID || c.Revision != connectionRev { |
| 27 | return runnerproto.Selection{}, forbidden |
| 28 | } |
| 29 | if err = decode(&c); err != nil { |
| 30 | return runnerproto.Selection{}, err |
| 31 | } |
| 32 | for _, r := range c.Reports { |
| 33 | ok, err := eligible(tx, c, r) |
| 34 | if err != nil { |
| 35 | return runnerproto.Selection{}, err |
| 36 | } |
| 37 | if !ok { |
| 38 | continue |
| 39 | } |
| 40 | // Task 5 supports the subscription lane; metered calls need explicit prices. |
| 41 | if r.Kind != "subscription" { |
| 42 | continue |
| 43 | } |
| 44 | iso, err := snapshot(tx, c, r) |
| 45 | if err != nil { |
| 46 | return runnerproto.Selection{}, err |
| 47 | } |
| 48 | return runnerproto.Selection{ConnectionID: c.ID, RunnerID: c.RunnerID, Person: c.Person, Provider: r.Provider, Family: r.Family, Model: r.Model, AccountID: r.Routing.AccountID, AccountLabel: r.AccountLabel, Revision: c.Revision, Isolation: iso}, nil |
| 49 | } |
| 50 | return runnerproto.Selection{}, waiting |
| 51 | } |
| 52 | |