-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds subscription types and db setters/getters
- Loading branch information
Showing
9 changed files
with
382 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
func (ms *MongoStorage) SetSubscription(subscription *Subscription) error { | ||
ms.keysLock.Lock() | ||
defer ms.keysLock.Unlock() | ||
// create a context with a timeout | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
// prepare the document to be updated in the database modifying only the | ||
// fields that have changed | ||
// define 'active' parameter to be updated always to update it even its new | ||
// value is false | ||
updateDoc, err := dynamicUpdateDocument(subscription, []string{"active"}) | ||
if err != nil { | ||
return err | ||
} | ||
// set upsert to true to create the document if it doesn't exist | ||
opts := options.Update().SetUpsert(true) | ||
if _, err := ms.organizations.UpdateOne(ctx, bson.M{"_id": subscription.ID}, updateDoc, opts); err != nil { | ||
return err | ||
} | ||
if err != nil { | ||
log.Printf("Failed to add subscription: %v", err) | ||
return errors.New("failed to add subscription") | ||
} | ||
return nil | ||
} | ||
|
||
func (ms *MongoStorage) Subscription(subscriptionID int) (*Subscription, error) { | ||
ms.keysLock.RLock() | ||
defer ms.keysLock.RUnlock() | ||
// create a context with a timeout | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
// find the subscription in the database | ||
filter := bson.M{"_id": subscriptionID} | ||
subscription := &Subscription{} | ||
err := ms.subscriptions.FindOne(ctx, filter).Decode(subscription) | ||
if err != nil { | ||
if err == mongo.ErrNoDocuments { | ||
return nil, ErrNotFound // Subscription not found | ||
} | ||
return nil, errors.New("failed to get subscription") | ||
} | ||
return subscription, nil | ||
} | ||
|
||
func (ms *MongoStorage) DelSubscription(subscription *Subscription) error { | ||
ms.keysLock.Lock() | ||
defer ms.keysLock.Unlock() | ||
// create a context with a timeout | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
// delete the organization from the database | ||
_, err := ms.organizations.DeleteOne(ctx, bson.M{"_id": subscription.ID}) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// FILEPATH: /home/user/Projects/vocdoni/saas-backend/db/subscriptions_test.go | ||
|
||
package db | ||
|
||
import ( | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
) | ||
|
||
func TestSetSubscription(t *testing.T) { | ||
defer func() { | ||
if err := db.Reset(); err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
c := qt.New(t) // Create a new quicktest instance | ||
|
||
ms := &MongoStorage{} // Create a mock MongoStorage instance | ||
|
||
subscription := &Subscription{ | ||
ID: 123, | ||
Name: "Test Subscription", | ||
Organization: OrganizationLimits{}, | ||
VotingTypes: VotingTypes{}, | ||
Features: Features{}, | ||
} | ||
|
||
err := ms.SetSubscription(subscription) | ||
c.Assert(err, qt.IsNil) | ||
} | ||
|
||
func TestSubscription(t *testing.T) { | ||
c := qt.New(t) // Create a new quicktest instance | ||
|
||
ms := &MongoStorage{} // Create a mock MongoStorage instance | ||
|
||
subscriptionID := 123 | ||
|
||
subscription, err := ms.Subscription(subscriptionID) | ||
c.Assert(err, qt.IsNil) | ||
c.Assert(subscription, qt.Not(qt.IsNil)) | ||
c.Assert(subscription.ID, qt.Equals, subscriptionID) | ||
} | ||
|
||
func TestDelSubscription(t *testing.T) { | ||
c := qt.New(t) // Create a new quicktest instance | ||
|
||
ms := &MongoStorage{} // Create a mock MongoStorage instance | ||
|
||
subscription := &Subscription{ | ||
ID: 123, | ||
Name: "Test Subscription", | ||
Organization: OrganizationLimits{}, | ||
VotingTypes: VotingTypes{}, | ||
Features: Features{}, | ||
} | ||
|
||
err := ms.DelSubscription(subscription) | ||
c.Assert(err, qt.IsNil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.