| 1 | package planning |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 5 | "amadan.net/oficina/equipo/internal/taskplan" |
| 6 | "context" |
| 7 | "gorm.io/gorm" |
| 8 | ) |
| 9 | |
| 10 | type Application struct { |
| 11 | Write func(context.Context, func(*gorm.DB) error) error |
| 12 | At func(*gorm.DB) *Store |
| 13 | Normalize func(string) string |
| 14 | } |
| 15 | |
| 16 | func (s *Application) ProposePlan(ctx context.Context, a kernel.Actor, p taskplan.Plan, m kernel.Mutation) (out taskplan.Plan, err error) { |
| 17 | a.Address = s.Normalize(a.Address) |
| 18 | err = s.Write(ctx, func(tx *gorm.DB) error { out, err = s.At(tx).Propose(a, p, m); return err }) |
| 19 | return |
| 20 | } |
| 21 | func (s *Application) EditPlan(ctx context.Context, a kernel.Actor, p taskplan.Plan, m kernel.Mutation) (out taskplan.Plan, err error) { |
| 22 | a.Address = s.Normalize(a.Address) |
| 23 | err = s.Write(ctx, func(tx *gorm.DB) error { out, err = s.At(tx).Edit(a, p, m); return err }) |
| 24 | return |
| 25 | } |
| 26 | func (s *Application) ApprovePlan(ctx context.Context, a kernel.Actor, id string, m kernel.Mutation) (out Task, err error) { |
| 27 | a.Address = s.Normalize(a.Address) |
| 28 | err = s.Write(ctx, func(tx *gorm.DB) error { out, err = s.At(tx).Approve(a, id, m); return err }) |
| 29 | return |
| 30 | } |
| 31 | func (s *Application) CorrectTaskProject(ctx context.Context, a kernel.Actor, id string, project int64, m kernel.Mutation) (out Task, err error) { |
| 32 | a.Address = s.Normalize(a.Address) |
| 33 | err = s.Write(ctx, func(tx *gorm.DB) error { out, err = s.At(tx).Correct(a, id, project, m); return err }) |
| 34 | return |
| 35 | } |
| 36 | func (s *Application) SetProjectApproval(ctx context.Context, a kernel.Actor, project int64, mode ApprovalMode, m kernel.Mutation) error { |
| 37 | a.Address = s.Normalize(a.Address) |
| 38 | return s.Write(ctx, func(tx *gorm.DB) error { return s.At(tx).SetApproval(a, project, &mode, m, true) }) |
| 39 | } |
| 40 | func (s *Application) SetApprovalOverride(ctx context.Context, a kernel.Actor, project int64, mode *ApprovalMode, m kernel.Mutation) error { |
| 41 | a.Address = s.Normalize(a.Address) |
| 42 | return s.Write(ctx, func(tx *gorm.DB) error { return s.At(tx).SetApproval(a, project, mode, m, false) }) |
| 43 | } |
| 44 | func (s *Application) PlanApproval(ctx context.Context, a kernel.Actor, project int64) (out ApprovalMode, err error) { |
| 45 | a.Address = s.Normalize(a.Address) |
| 46 | err = s.Write(ctx, func(tx *gorm.DB) error { |
| 47 | st := s.At(tx) |
| 48 | if err := st.deps.Member(tx, a, false); err != nil { |
| 49 | return err |
| 50 | } |
| 51 | out, err = st.Approval(a.Address, project) |
| 52 | return err |
| 53 | }) |
| 54 | return |
| 55 | } |
| 56 | func (s *Application) CheckPlanDispatch(ctx context.Context, a kernel.Actor, id string) error { |
| 57 | a.Address = s.Normalize(a.Address) |
| 58 | return s.Write(ctx, func(tx *gorm.DB) error { return s.At(tx).CheckDispatch(a, id) }) |
| 59 | } |
| 60 | func (s *Application) TaskPlan(ctx context.Context, a kernel.Actor, id string) (out taskplan.Plan, err error) { |
| 61 | a.Address = s.Normalize(a.Address) |
| 62 | err = s.Write(ctx, func(tx *gorm.DB) error { |
| 63 | st := s.At(tx) |
| 64 | t, e := st.visibleTask(a, id) |
| 65 | if e != nil { |
| 66 | return e |
| 67 | } |
| 68 | out, err = st.Plan(t) |
| 69 | return err |
| 70 | }) |
| 71 | return |
| 72 | } |
| 73 | func (s *Application) Page(ctx context.Context, a kernel.Actor, id string) (out Page, err error) { |
| 74 | a.Address = s.Normalize(a.Address) |
| 75 | err = s.Write(ctx, func(tx *gorm.DB) error { out, err = s.At(tx).Page(a, id); return err }) |
| 76 | return |
| 77 | } |
| 78 | |