| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/taskplan" |
| 8 | ) |
| 9 | |
| 10 | type SourceRef = taskplan.SourceRef |
| 11 | type AnswerResult = taskplan.AnswerResult |
| 12 | type SourceReferenceValidator func(context.Context, *sql.Tx, Scope, SourceRef) error |
| 13 | |
| 14 | func (s *Store) validateAnswer(ctx context.Context, tx *sql.Tx, j jobRow, payload []byte) error { |
| 15 | var a AnswerResult |
| 16 | if j.Target.Kind != TargetProjectRead || j.Evidence.Predicate != "" || strictJSON(payload, &a) != nil || taskplan.ValidateAnswer(a) != nil || s.sourceReference == nil { |
| 17 | return ErrEvidence |
| 18 | } |
| 19 | for _, ref := range a.Sources { |
| 20 | if err := s.sourceReference(ctx, tx, Scope{Account: j.Account, Job: j.ID}, ref); err != nil { |
| 21 | return err |
| 22 | } |
| 23 | } |
| 24 | return nil |
| 25 | } |
| 26 | func (s *Store) CompleteReadWork(ctx context.Context, scope Scope, h Handle, answer ArtifactSpec) error { |
| 27 | if answer.Role != RoleAnswer { |
| 28 | return ErrScope |
| 29 | } |
| 30 | return s.Atomic(ctx, func(ctx context.Context, tx *sql.Tx) error { |
| 31 | if _, err := s.Register(ctx, scope, h, answer); err != nil { |
| 32 | return err |
| 33 | } |
| 34 | return s.Complete(ctx, scope, h, AttemptResult{Outcome: OutcomeSuccess}) |
| 35 | }) |
| 36 | } |
| 37 | func (s *Store) evaluateReadWork(ctx context.Context, tx *sql.Tx, j jobRow) (EvidenceReport, error) { |
| 38 | rep := EvidenceReport{} |
| 39 | if j.Target.Kind != TargetProjectRead { |
| 40 | return rep, ErrEvidence |
| 41 | } |
| 42 | a, err := latestArtifact(ctx, tx, j.ID, RoleAnswer) |
| 43 | if err != nil { |
| 44 | return rep, err |
| 45 | } |
| 46 | if a == nil { |
| 47 | rep.Reason = "answer_missing" |
| 48 | return rep, nil |
| 49 | } |
| 50 | if err = s.validateAnswer(ctx, tx, j, a.Payload); err != nil { |
| 51 | return rep, err |
| 52 | } |
| 53 | rep.Satisfied = true |
| 54 | return rep, nil |
| 55 | } |
| 56 | |