package equipo import ( "context" "errors" "amadan.net/oficina/equipo/internal/equipo/planning" "amadan.net/oficina/equipo/internal/taskplan" "gorm.io/gorm" ) var errPlanApproval = &refusal{copyText("plan.awaiting_approval"), ErrConflict} var errPlanSelectionAsk = &refusal{copyText("connections.ask"), ErrUnavailable} var errPlanSecondFamily = &refusal{copyText("connections.second_family"), ErrUnavailable} func selectionWaitCode(err error) string { if errors.Is(err, errPlanApproval) { return "plan.awaiting_approval" } if errors.Is(err, errPlanSelectionAsk) { return "connections.ask" } if errors.Is(err, errPlanSecondFamily) { return "connections.second_family" } return "task.wait.setup" } func (s *Store) SelectPlanStep(ctx context.Context, actor Actor, taskID, stepKey string, m Mutation) (Selection, error) { return s.planSelection(ctx, actor, taskID, stepKey, "", "", false, m) } func (s *Store) ConfirmPlanSelection(ctx context.Context, actor Actor, taskID, stepKey, connectionID, model string, m Mutation) (Selection, error) { return s.planSelection(ctx, actor, taskID, stepKey, connectionID, model, true, m) } func (s *Store) planSelection(ctx context.Context, actor Actor, taskID, key, connection, model string, confirm bool, m Mutation) (out Selection, err error) { actor.Address = normalizeAddress(actor.Address) err = s.write(ctx, func(tx *gorm.DB) error { out, err = s.planningAt(tx).Select(actor, taskID, key, connection, model, confirm, m) return err }) return } func (s *Store) selectionPolicy() planning.SelectionPolicy { return planning.SelectionPolicy{ Select: s.selectStepConnectionConfirmed, Current: s.selectionCurrent, Subscription: subscriptionSelection, ChoiceRevision: func(tx *gorm.DB, person string, project int64) (int64, error) { c, _, err := choiceAt(tx, person, project) return c.Revision, err }, SpecialistRevision: func(tx *gorm.DB, key string) (int64, error) { sp, err := specialistAt(tx, key) return sp.Revision, err }, Unavailable: ErrUnavailable, Conflict: ErrConflict, SecondFamily: errPlanSecondFamily, } } func (s *Store) choosePlanSelection(tx *gorm.DB, t Task, p taskplan.Plan, st taskplan.Step, confirm bool) (Selection, error) { return s.selectionPolicy().Choose(tx, t, p, st, confirm) } func (s *Store) selectPlanConnection(tx *gorm.DB, t Task, st taskplan.Step, excluded, runner string, availability bool) (Selection, error) { return s.selectionPolicy().ForStep(tx, t, st, excluded, runner, availability) }