| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 8 | "amadan.net/oficina/equipo/internal/taskplan" |
| 9 | "gorm.io/gorm" |
| 10 | ) |
| 11 | |
| 12 | var errPlanApproval = &refusal{copyText("plan.awaiting_approval"), ErrConflict} |
| 13 | |
| 14 | var errPlanSelectionAsk = &refusal{copyText("connections.ask"), ErrUnavailable} |
| 15 | var errPlanSecondFamily = &refusal{copyText("connections.second_family"), ErrUnavailable} |
| 16 | |
| 17 | func selectionWaitCode(err error) string { |
| 18 | if errors.Is(err, errPlanApproval) { |
| 19 | return "plan.awaiting_approval" |
| 20 | } |
| 21 | if errors.Is(err, errPlanSelectionAsk) { |
| 22 | return "connections.ask" |
| 23 | } |
| 24 | if errors.Is(err, errPlanSecondFamily) { |
| 25 | return "connections.second_family" |
| 26 | } |
| 27 | return "task.wait.setup" |
| 28 | } |
| 29 | |
| 30 | func (s *Store) SelectPlanStep(ctx context.Context, actor Actor, taskID, stepKey string, m Mutation) (Selection, error) { |
| 31 | return s.planSelection(ctx, actor, taskID, stepKey, "", "", false, m) |
| 32 | } |
| 33 | func (s *Store) ConfirmPlanSelection(ctx context.Context, actor Actor, taskID, stepKey, connectionID, model string, m Mutation) (Selection, error) { |
| 34 | return s.planSelection(ctx, actor, taskID, stepKey, connectionID, model, true, m) |
| 35 | } |
| 36 | func (s *Store) planSelection(ctx context.Context, actor Actor, taskID, key, connection, model string, confirm bool, m Mutation) (out Selection, err error) { |
| 37 | actor.Address = normalizeAddress(actor.Address) |
| 38 | err = s.write(ctx, func(tx *gorm.DB) error { |
| 39 | out, err = s.planningAt(tx).Select(actor, taskID, key, connection, model, confirm, m) |
| 40 | return err |
| 41 | }) |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | func (s *Store) selectionPolicy() planning.SelectionPolicy { |
| 46 | return planning.SelectionPolicy{ |
| 47 | Select: s.selectStepConnectionConfirmed, Current: s.selectionCurrent, Subscription: subscriptionSelection, |
| 48 | ChoiceRevision: func(tx *gorm.DB, person string, project int64) (int64, error) { |
| 49 | c, _, err := choiceAt(tx, person, project) |
| 50 | return c.Revision, err |
| 51 | }, |
| 52 | SpecialistRevision: func(tx *gorm.DB, key string) (int64, error) { |
| 53 | sp, err := specialistAt(tx, key) |
| 54 | return sp.Revision, err |
| 55 | }, |
| 56 | Unavailable: ErrUnavailable, Conflict: ErrConflict, SecondFamily: errPlanSecondFamily, |
| 57 | } |
| 58 | } |
| 59 | func (s *Store) choosePlanSelection(tx *gorm.DB, t Task, p taskplan.Plan, st taskplan.Step, confirm bool) (Selection, error) { |
| 60 | return s.selectionPolicy().Choose(tx, t, p, st, confirm) |
| 61 | } |
| 62 | func (s *Store) selectPlanConnection(tx *gorm.DB, t Task, st taskplan.Step, excluded, runner string, availability bool) (Selection, error) { |
| 63 | return s.selectionPolicy().ForStep(tx, t, st, excluded, runner, availability) |
| 64 | } |
| 65 | |