| 1 | package setup |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 7 | "gorm.io/gorm" |
| 8 | ) |
| 9 | |
| 10 | type Mutation = kernel.Mutation |
| 11 | type Application struct { |
| 12 | Write, Read func(context.Context, func(*gorm.DB) error) error |
| 13 | At func(*gorm.DB) *Store |
| 14 | Normalize func(string) string |
| 15 | ValidAddress func(string) bool |
| 16 | Invalid, Unavailable error |
| 17 | } |
| 18 | |
| 19 | func (s *Application) CreateInvitation(ctx context.Context, a Actor, address string, role Role, m Mutation) (out Invitation, err error) { |
| 20 | a.Address = s.Normalize(a.Address) |
| 21 | address = s.Normalize(address) |
| 22 | if !s.ValidAddress(address) || (role != RoleOwner && role != RoleMember) { |
| 23 | return out, s.Invalid |
| 24 | } |
| 25 | err = s.Write(ctx, func(tx *gorm.DB) error { out, err = s.At(tx).Create(a, address, role, m); return err }) |
| 26 | if err != nil { |
| 27 | out = Invitation{} |
| 28 | } |
| 29 | return |
| 30 | } |
| 31 | func (s *Application) AcceptInvitation(ctx context.Context, a Actor, id, secret string, m Mutation) (out Member, err error) { |
| 32 | a.Address = s.Normalize(a.Address) |
| 33 | err = s.Write(ctx, func(tx *gorm.DB) error { out, err = s.At(tx).Accept(a, id, secret, m); return err }) |
| 34 | if err != nil { |
| 35 | out = Member{} |
| 36 | } |
| 37 | return |
| 38 | } |
| 39 | func (s *Application) RevokeInvitation(ctx context.Context, a Actor, id string, m Mutation) error { |
| 40 | a.Address = s.Normalize(a.Address) |
| 41 | return s.Write(ctx, func(tx *gorm.DB) error { return s.At(tx).Revoke(a, id, m) }) |
| 42 | } |
| 43 | func (s *Application) Invitation(ctx context.Context, a Actor, id, secret string) (out Invitation, err error) { |
| 44 | if s.Read == nil { |
| 45 | return out, s.Unavailable |
| 46 | } |
| 47 | a.Address = s.Normalize(a.Address) |
| 48 | err = s.Read(ctx, func(tx *gorm.DB) error { out, err = s.At(tx).Get(a, id, secret); return err }) |
| 49 | return |
| 50 | } |
| 51 | func (s *Application) MayVerifyInvitation(ctx context.Context, address string) (bool, error) { |
| 52 | var ok bool |
| 53 | err := s.Read(ctx, func(tx *gorm.DB) error { |
| 54 | var err error |
| 55 | ok, err = s.At(tx).MayVerify(s.Normalize(address)) |
| 56 | return err |
| 57 | }) |
| 58 | return ok, err |
| 59 | } |
| 60 | |
| 61 | func (s *Application) ValidateInvitationToken(ctx context.Context, id, secret string) error { |
| 62 | return s.Read(ctx, func(tx *gorm.DB) error { return s.At(tx).ValidateToken(id, secret) }) |
| 63 | } |
| 64 | func (s *Application) PendingInvitations(ctx context.Context, a Actor) (out []Invitation, err error) { |
| 65 | a.Address = s.Normalize(a.Address) |
| 66 | err = s.Read(ctx, func(tx *gorm.DB) error { out, err = s.At(tx).Pending(a); return err }) |
| 67 | return |
| 68 | } |
| 69 | |
| 70 | func (s *Application) InvitationOwner(ctx context.Context, a Actor) error { |
| 71 | return s.Read(ctx, func(tx *gorm.DB) error { return s.At(tx).deps.Owner(tx, a) }) |
| 72 | } |
| 73 | |
| 74 | // InvitationSummary requires a verified caller. Pending recipients can inspect |
| 75 | // only their addressed invitation; accepting it still needs the invitation secret. |
| 76 | func (s *Application) InvitationSummary(ctx context.Context, a Actor, id string) (out Invitation, err error) { |
| 77 | a.Address = s.Normalize(a.Address) |
| 78 | err = s.Read(ctx, func(tx *gorm.DB) error { |
| 79 | store := s.At(tx) |
| 80 | row, e := store.record(id) |
| 81 | if e != nil { |
| 82 | return e |
| 83 | } |
| 84 | if a.Address == "" { |
| 85 | return store.deps.Forbidden |
| 86 | } |
| 87 | if a.Address != row.Address { |
| 88 | if e = store.deps.Owner(tx, a); e != nil { |
| 89 | return store.deps.NotFound |
| 90 | } |
| 91 | } else if e = store.inviter(row); e != nil { |
| 92 | return e |
| 93 | } |
| 94 | out = row.view() |
| 95 | return nil |
| 96 | }) |
| 97 | return |
| 98 | } |
| 99 | |