ducky/subscriptions

Paddy 2015-06-11 Child:f1a22fc2321d

0:56a2bef197cd Go to Latest

ducky/subscriptions/subscription.go

First implementation of datastore interface. Define Subscriptions, and the SubscriptionChange type that can modify Subscriptions. Define the subscriptionStore, which describes how Subscriptions are to be created, retrieved, updated, and deleted in/from the storage backend they're stored in. Create a Memstore implementation of the subscriptionStore, which stores all our data in-memory, for use in testing. Write tests to cover the subscriptionStore interface, testing the entire Memstore. We'll plug future storage backends into these tests, to make sure they all behave the same, and to exercise each storage backend without requiring a suite of tests for each. At this point, we have 100% test coverage and no complaints from golint or go vet. I expect it's all downhill from here.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/subscription.go	Thu Jun 11 23:15:01 2015 -0400
     1.3 @@ -0,0 +1,149 @@
     1.4 +package subscriptions
     1.5 +
     1.6 +import (
     1.7 +	"errors"
     1.8 +	"time"
     1.9 +
    1.10 +	"code.secondbit.org/uuid.hg"
    1.11 +)
    1.12 +
    1.13 +const (
    1.14 +	// MonthlyPeriod represents a period of once a month.
    1.15 +	MonthlyPeriod period = "monthly"
    1.16 +)
    1.17 +
    1.18 +var (
    1.19 +	// ErrSubscriptionAlreadyExists is returned when a Subscription
    1.20 +	// with an identical ID already exists in the subscriptionStore.
    1.21 +	ErrSubscriptionAlreadyExists = errors.New("Subscription already exists")
    1.22 +	// ErrSubscriptionNotFound is returned when a single Subscription
    1.23 +	// is acted upon or requested, but cannot be found.
    1.24 +	ErrSubscriptionNotFound = errors.New("Subscription not found")
    1.25 +	// ErrStripeCustomerAlreadyExists is returned when a Subscription
    1.26 +	// is created or updates its StripeCustomer property, but that
    1.27 +	// StripeCustomer is already associated with another Subscription.
    1.28 +	ErrStripeCustomerAlreadyExists = errors.New("Stripe customer already assigned to another Subscription")
    1.29 +	// ErrSubscriptionChangeEmpty is returned when a SubscriptionChange
    1.30 +	// is empty but is passed to subscriptionStore.UpdateSubscription
    1.31 +	// anyways.
    1.32 +	ErrSubscriptionChangeEmpty = errors.New("SubscriptionChange is empty")
    1.33 +	// ErrNoSubscriptionID is returned when one or more Subscription IDs
    1.34 +	// are required, but none are provided.
    1.35 +	ErrNoSubscriptionID = errors.New("no Subscription ID provided")
    1.36 +)
    1.37 +
    1.38 +type period string
    1.39 +
    1.40 +// Subscription represents the state of a user's payments. It holds
    1.41 +// metadata about the last time a user was charged, how much a user
    1.42 +// should be charged, how to charge a user and how much to charge
    1.43 +// the user.
    1.44 +type Subscription struct {
    1.45 +	ID             uuid.ID
    1.46 +	UserID         uuid.ID
    1.47 +	StripeCustomer string
    1.48 +	Amount         int
    1.49 +	Period         period
    1.50 +	Created        time.Time
    1.51 +	BeginCharging  time.Time
    1.52 +	LastCharged    time.Time
    1.53 +	LastNotified   time.Time
    1.54 +	InLockout      bool
    1.55 +}
    1.56 +
    1.57 +// SubscriptionChange represents desired changes to a Subscription
    1.58 +// object. A nil value means that property should remain unchanged.
    1.59 +type SubscriptionChange struct {
    1.60 +	StripeCustomer *string
    1.61 +	Amount         *int
    1.62 +	Period         *period
    1.63 +	BeginCharging  *time.Time
    1.64 +	LastCharged    *time.Time
    1.65 +	LastNotified   *time.Time
    1.66 +	InLockout      *bool
    1.67 +}
    1.68 +
    1.69 +// IsEmpty returns true if the SubscriptionChange doesn't request
    1.70 +// a change to any property of the Subscription.
    1.71 +func (change SubscriptionChange) IsEmpty() bool {
    1.72 +	if change.StripeCustomer != nil {
    1.73 +		return false
    1.74 +	}
    1.75 +	if change.Amount != nil {
    1.76 +		return false
    1.77 +	}
    1.78 +	if change.Period != nil {
    1.79 +		return false
    1.80 +	}
    1.81 +	if change.BeginCharging != nil {
    1.82 +		return false
    1.83 +	}
    1.84 +	if change.LastCharged != nil {
    1.85 +		return false
    1.86 +	}
    1.87 +	if change.LastNotified != nil {
    1.88 +		return false
    1.89 +	}
    1.90 +	if change.InLockout != nil {
    1.91 +		return false
    1.92 +	}
    1.93 +	return true
    1.94 +}
    1.95 +
    1.96 +// ApplyChange updates a Subscription based on the changes requested
    1.97 +// by a SubscriptionChange.
    1.98 +func (s *Subscription) ApplyChange(change SubscriptionChange) {
    1.99 +	if change.StripeCustomer != nil {
   1.100 +		s.StripeCustomer = *change.StripeCustomer
   1.101 +	}
   1.102 +	if change.Amount != nil {
   1.103 +		s.Amount = *change.Amount
   1.104 +	}
   1.105 +	if change.Period != nil {
   1.106 +		s.Period = *change.Period
   1.107 +	}
   1.108 +	if change.BeginCharging != nil {
   1.109 +		s.BeginCharging = *change.BeginCharging
   1.110 +	}
   1.111 +	if change.LastCharged != nil {
   1.112 +		s.LastCharged = *change.LastCharged
   1.113 +	}
   1.114 +	if change.LastNotified != nil {
   1.115 +		s.LastNotified = *change.LastNotified
   1.116 +	}
   1.117 +	if change.InLockout != nil {
   1.118 +		s.InLockout = *change.InLockout
   1.119 +	}
   1.120 +}
   1.121 +
   1.122 +// ByLastChargeDate allows us to sort a []Subscription by the LastCharged
   1.123 +// property, with the lowest LastCharged date first.
   1.124 +type ByLastChargeDate []Subscription
   1.125 +
   1.126 +// Len returns the length the SubscriptionsByLastChargeDate. It fulfills
   1.127 +// the sort.Interface interface.
   1.128 +func (s ByLastChargeDate) Len() int {
   1.129 +	return len(s)
   1.130 +}
   1.131 +
   1.132 +// Swap puts the item in position i in position j, and the item in position
   1.133 +// j in position i. It fulfills the sort.Interface interface.
   1.134 +func (s ByLastChargeDate) Swap(i, j int) {
   1.135 +	s[i], s[j] = s[j], s[i]
   1.136 +}
   1.137 +
   1.138 +// Less returns true if the item in position i should be sorted before the
   1.139 +// item in position j.
   1.140 +func (s ByLastChargeDate) Less(i, j int) bool {
   1.141 +	return s[i].LastCharged.Before(s[j].LastCharged)
   1.142 +}
   1.143 +
   1.144 +type subscriptionStore interface {
   1.145 +	reset() error
   1.146 +	createSubscription(sub Subscription) error
   1.147 +	updateSubscription(id uuid.ID, change SubscriptionChange) error
   1.148 +	deleteSubscription(id uuid.ID) error
   1.149 +	listSubscriptionsLastChargedBefore(time.Time) ([]Subscription, error)
   1.150 +	getSubscriptions(ids []uuid.ID) (map[string]Subscription, error)
   1.151 +	getSubscriptionByUser(id uuid.ID) (Subscription, error)
   1.152 +}