| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 8 | "gorm.io/gorm" |
| 9 | ) |
| 10 | |
| 11 | func renderedDecisionOwner(tx *gorm.DB, actor Actor, projectID int64, decision string) (int64, error) { |
| 12 | if decision != "copy" && decision != "design" { |
| 13 | return 0, ErrInvalid |
| 14 | } |
| 15 | if _, err := currentRunnerMember(tx, actor.Address); err != nil { |
| 16 | return 0, err |
| 17 | } |
| 18 | var p Project |
| 19 | if err := tx.First(&p, projectID).Error; err != nil { |
| 20 | return 0, storeError(err) |
| 21 | } |
| 22 | var d DecisionOwner |
| 23 | if err := tx.First(&d, "project_id=? AND decision=?", projectID, decision).Error; err != nil { |
| 24 | return 0, storeError(err) |
| 25 | } |
| 26 | if d.Address != actor.Address { |
| 27 | return 0, ErrForbidden |
| 28 | } |
| 29 | return p.Revision, nil |
| 30 | } |
| 31 | func (s *Store) validateRenderedOwner(ctx context.Context, raw *sql.Tx, project team.ProjectID, a team.RenderedApproval) error { |
| 32 | if s.execution == nil { |
| 33 | return ErrUnavailable |
| 34 | } |
| 35 | revision, err := renderedDecisionOwner(s.execution.gormTx(ctx, raw), Actor{Address: a.Person}, int64(project), a.Decision) |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | if revision != a.DecisionRevision { |
| 40 | return ErrConflict |
| 41 | } |
| 42 | return nil |
| 43 | } |
| 44 | func (s *Store) ApproveRenderedChange(ctx context.Context, actor Actor, taskID, candidateHash, diffHash, renderHash, decision string, m Mutation) error { |
| 45 | actor.Address = normalizeAddress(actor.Address) |
| 46 | if s.execution == nil { |
| 47 | return ErrUnavailable |
| 48 | } |
| 49 | return s.write(ctx, func(tx *gorm.DB) error { |
| 50 | return s.planningAt(tx).ApproveRenderedChange(tx.Statement.Context, actor, taskID, candidateHash, diffHash, renderHash, decision, m) |
| 51 | }) |
| 52 | } |
| 53 | |