Skip to content

Commit

Permalink
Improves naming differentiating subscriptions and plans
Browse files Browse the repository at this point in the history
  • Loading branch information
emmdim committed Nov 7, 2024
1 parent 94df5d6 commit 72cd1c1
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 131 deletions.
12 changes: 6 additions & 6 deletions api/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ func (a *API) createOrganizationHandler(w http.ResponseWriter, r *http.Request)
parentOrg = orgInfo.Parent.Address
}
// find default plan
defaultPlan, err := a.db.DefaultSubscription()
defaultPlan, err := a.db.DefaultPlan()
if err != nil || defaultPlan == nil {
ErrNoDefaultPLan.WithErr((err)).Write(w)
return
}
subscription := &db.OrganizationSubscription{
SubscriptionID: defaultPlan.ID,
StartDate: time.Now(),
Active: true,
MaxCensusSize: defaultPlan.Organization.CensusSize,
PlanID: defaultPlan.ID,
StartDate: time.Now(),
Active: true,
MaxCensusSize: defaultPlan.Organization.CensusSize,
}
// create the organization
if err := a.db.SetOrganization(&db.Organization{
Expand Down Expand Up @@ -485,7 +485,7 @@ func (a *API) getOrganizationSubscriptionHandler(w http.ResponseWriter, r *http.
return
}
// get the subscription from the database
plan, err := a.db.Subscription(org.Subscription.SubscriptionID)
plan, err := a.db.Plan(org.Subscription.PlanID)
if err != nil {
ErrGenericInternalServerError.Withf("could not get subscription: %v", err).Write(w)
return
Expand Down
14 changes: 7 additions & 7 deletions api/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (a *API) handleWebhook(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
return
}
dbSubscription, err := a.db.SubscriptionByStripeId(subscription.Items.Data[0].Plan.Product.ID)
dbSubscription, err := a.db.PlanByStripeId(subscription.Items.Data[0].Plan.Product.ID)
if err != nil || dbSubscription == nil {
log.Warnf("Could not update subscription %s, a corresponding subscription was not found.",
subscription.ID)
Expand All @@ -62,12 +62,12 @@ func (a *API) handleWebhook(w http.ResponseWriter, r *http.Request) {
renewalDate := time.Unix(subscription.BillingCycleAnchor, 0)

organizationSubscription := &db.OrganizationSubscription{
SubscriptionID: dbSubscription.ID,
StartDate: startDate,
EndDate: endDate,
RenewalDate: renewalDate,
Active: subscription.Status == "active",
MaxCensusSize: int(subscription.Items.Data[0].Quantity),
PlanID: dbSubscription.ID,
StartDate: startDate,
EndDate: endDate,
RenewalDate: renewalDate,
Active: subscription.Status == "active",
MaxCensusSize: int(subscription.Items.Data[0].Quantity),
}

// TODO will only worked for new subscriptions
Expand Down
2 changes: 1 addition & 1 deletion api/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// It returns the list of subscriptions with their information.
func (a *API) getSubscriptionsHandler(w http.ResponseWriter, r *http.Request) {
// get the subscritions from the database
subscriptions, err := a.db.Subscriptions()
subscriptions, err := a.db.Plans()
if err != nil {
ErrGenericInternalServerError.Withf("could not get subscriptions: %v", err).Write(w)
return
Expand Down
2 changes: 1 addition & 1 deletion api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,5 @@ func organizationFromDB(dbOrg, parent *db.Organization) *OrganizationInfo {
type OrganizationSubscriptionInfo struct {
SubcriptionDetails *db.OrganizationSubscription `json:"subscriptionDetails"`
Usage *db.OrganizationCounters `json:"usage"`
Plan *db.Subscription `json:"plan"`
Plan *db.Plan `json:"plan"`
}
16 changes: 8 additions & 8 deletions db/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (ms *MongoStorage) initCollections(database string) error {
}
log.Infow("current collections", "collections", currentCollections)
log.Infow("reading subscriptions from file %s", ms.subscriptionsFile)
loadedSubscriptions, err := readSubscriptionJSON(ms.subscriptionsFile)
loadedPlans, err := readPlanJSON(ms.subscriptionsFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -70,11 +70,11 @@ func (ms *MongoStorage) initCollections(database string) error {
}
if name == "subscriptions" {
var subscriptions []interface{}
for _, sub := range loadedSubscriptions {
for _, sub := range loadedPlans {
subscriptions = append(subscriptions, sub)
}
count, err := ms.client.Database(database).Collection(name).InsertMany(ctx, subscriptions)
if err != nil || len(count.InsertedIDs) != len(loadedSubscriptions) {
if err != nil || len(count.InsertedIDs) != len(loadedPlans) {
return nil, fmt.Errorf("failed to insert subscriptions: %w", err)
}
}
Expand All @@ -98,7 +98,7 @@ func (ms *MongoStorage) initCollections(database string) error {
return err
}
// subscriptions collection
if ms.subscriptions, err = getCollection("subscriptions"); err != nil {
if ms.plans, err = getCollection("plans"); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -222,9 +222,9 @@ func dynamicUpdateDocument(item interface{}, alwaysUpdateTags []string) (bson.M,
return bson.M{"$set": update}, nil
}

// readSubscriptionJSON reads a JSON file with an array of subscritpions
// and return it as a Subscription array
func readSubscriptionJSON(subscriptionsFile string) ([]*Subscription, error) {
// readPlanJSON reads a JSON file with an array of subscritpions
// and return it as a Plan array
func readPlanJSON(subscriptionsFile string) ([]*Plan, error) {
log.Warnf("Reading subscriptions from %s", subscriptionsFile)
file, err := root.Assets.Open(fmt.Sprintf("assets/%s", subscriptionsFile))
if err != nil {
Expand All @@ -243,7 +243,7 @@ func readSubscriptionJSON(subscriptionsFile string) ([]*Subscription, error) {
// Create a JSON decoder
decoder := json.NewDecoder(file)

var subscriptions []*Subscription
var subscriptions []*Plan
err = decoder.Decode(&subscriptions)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type MongoStorage struct {
verifications *mongo.Collection
organizations *mongo.Collection
organizationInvites *mongo.Collection
subscriptions *mongo.Collection
plans *mongo.Collection
}

type Options struct {
Expand Down Expand Up @@ -116,7 +116,7 @@ func (ms *MongoStorage) Reset() error {
return err
}
// drop subscriptions collection
if err := ms.subscriptions.Drop(ctx); err != nil {
if err := ms.plans.Drop(ctx); err != nil {
return err
}
// init the collections
Expand Down
4 changes: 2 additions & 2 deletions db/mongo_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type OrganizationCollection struct {
Organizations []Organization `json:"organizations" bson:"organizations"`
}

type SubscriptionCollection struct {
Subscriptions []Subscription `json:"subscriptions" bson:"subscriptions"`
type PlanCollection struct {
Plans []Plan `json:"plans" bson:"plans"`
}

type OrganizationInvitesCollection struct {
Expand Down
2 changes: 1 addition & 1 deletion db/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (ms *MongoStorage) OrganizationsMembers(address string) ([]User, error) {
// the given address. If an error occurs, it returns the error. This method must
// be called with the keysLock held.
func (ms *MongoStorage) AddSubscriptionToOrganization(address string, orgSubscription *OrganizationSubscription) error {
if _, err := ms.Subscription(orgSubscription.SubscriptionID); err != nil {
if _, err := ms.Plan(orgSubscription.PlanID); err != nil {
return ErrInvalidData
}
ms.keysLock.Lock()
Expand Down
10 changes: 5 additions & 5 deletions db/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,20 @@ func TestOrganizationsMembers(t *testing.T) {
c.Assert(singleMember.Email, qt.Equals, testUserEmail)
}

func TestAddOrganizationSubscription(t *testing.T) {
func TestAddOrganizationPlan(t *testing.T) {
defer func() {
if err := db.Reset(); err != nil {
t.Error(err)
}
}()
c := qt.New(t)
// create a new organization
address := "orgToAddSubscription"
address := "orgToAddPlan"
c.Assert(db.SetOrganization(&Organization{
Address: address,
}), qt.IsNil)
// add a subscription to the organization
subscriptionName := "testSubscription"
subscriptionName := "testPlan"
startDate := time.Now()
endDate := startDate.AddDate(1, 0, 0)
active := true
Expand All @@ -206,14 +206,14 @@ func TestAddOrganizationSubscription(t *testing.T) {
}
// using a non existing subscription should fail
c.Assert(db.AddSubscriptionToOrganization(address, orgSubscription), qt.IsNotNil)
subscriptionID, err := db.SetSubscription(&Subscription{
subscriptionID, err := db.SetPlan(&Plan{
Name: subscriptionName,
StripeID: stripeID,
})
if err != nil {
t.Error(err)
}
orgSubscription.SubscriptionID = subscriptionID
orgSubscription.PlanID = subscriptionID
c.Assert(db.AddSubscriptionToOrganization(address, orgSubscription), qt.IsNil)
// retrieve the organization and check the subscription details
org, _, err := db.Organization(address, false)
Expand Down
Loading

0 comments on commit 72cd1c1

Please sign in to comment.