| 1 | package abono |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // Member is one holding and whose it is, for an owner's screen. |
| 10 | type Member struct { |
| 11 | Subject string |
| 12 | Holding |
| 13 | } |
| 14 | |
| 15 | // Members is every holding of every subject: subscriptions and comps, |
| 16 | // the ones that no longer grant included, ordered by subject and then |
| 17 | // newest first, as Holdings orders one subject's. Rows not tied to a |
| 18 | // subject (unclaimed payments) and subscriptions on a price no offer |
| 19 | // recognises are left out, as Holdings leaves them out; Unrecognised |
| 20 | // lists the second kind. |
| 21 | func (s *Service) Members(ctx context.Context) ([]Member, error) { |
| 22 | var subjects []string |
| 23 | if err := s.cfg.DB.WithContext(ctx).Raw(`SELECT subject FROM abono_subscriptions WHERE subject <> '' AND plan <> '' |
| 24 | UNION SELECT subject FROM abono_comps WHERE subject <> '' ORDER BY subject`).Scan(&subjects).Error; err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | var out []Member |
| 28 | for _, subject := range subjects { |
| 29 | hs, err := s.Holdings(ctx, subject) |
| 30 | if err != nil { |
| 31 | return nil, err |
| 32 | } |
| 33 | for _, h := range hs { |
| 34 | out = append(out, Member{Subject: subject, Holding: h}) |
| 35 | } |
| 36 | } |
| 37 | return out, nil |
| 38 | } |
| 39 | |
| 40 | // TestMode reports whether the service runs on a Stripe test key |
| 41 | // (sk_test_ or rk_test_), read from the key's prefix (ruling R8): a |
| 42 | // restricted key may not be allowed to ask Stripe. The sandbox is not |
| 43 | // test mode; Sandbox says so. |
| 44 | func (s *Service) TestMode() bool { |
| 45 | if s == nil { |
| 46 | return false |
| 47 | } |
| 48 | return strings.HasPrefix(s.cfg.Key, "sk_test_") || strings.HasPrefix(s.cfg.Key, "rk_test_") |
| 49 | } |
| 50 | |
| 51 | // Sandbox reports whether the sandbox stands in for Stripe. |
| 52 | func (s *Service) Sandbox() bool { return s != nil && s.sandbox != nil } |
| 53 | |
| 54 | // Origin is Config.Origin, the site's own address: the webhook's address |
| 55 | // is Origin plus wherever the app mounted ServeWebhook. |
| 56 | func (s *Service) Origin() string { return s.cfg.Origin } |
| 57 | |
| 58 | // Now is the moment holdings are judged at, the clock Config.Now gave, |
| 59 | // in UTC. A page that describes a Holding passes this as the status |
| 60 | // partial's Now, so its words agree with Holding.Grants. |
| 61 | func (s *Service) Now() time.Time { return s.now() } |
| 62 | |