From fc45e0a941ee509ba536e1193a6dc9893ce171cf Mon Sep 17 00:00:00 2001 From: emmdim Date: Thu, 31 Oct 2024 17:03:56 +0100 Subject: [PATCH] * Adds comments * Fix embedding subscriptions file * Minor fixes --- .../subscriptions.json | 0 db/helpers.go | 45 +++++++------- db/subscriptions.json | 59 ------------------- embed.go | 6 ++ stripe/stripe.go | 6 ++ subscriptions/subscriptions.go | 6 ++ 6 files changed, 38 insertions(+), 84 deletions(-) rename subscriptions.json => assets/subscriptions.json (100%) delete mode 100644 db/subscriptions.json create mode 100644 embed.go diff --git a/subscriptions.json b/assets/subscriptions.json similarity index 100% rename from subscriptions.json rename to assets/subscriptions.json diff --git a/db/helpers.go b/db/helpers.go index bb2c8c6..b64c123 100644 --- a/db/helpers.go +++ b/db/helpers.go @@ -4,10 +4,10 @@ import ( "context" "encoding/json" "fmt" - "os" "reflect" "time" + root "github.com/vocdoni/saas-backend" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" @@ -56,14 +56,6 @@ func (ms *MongoStorage) initCollections(database string) error { if _, err := ms.client.Database(database).Collection(name).DeleteMany(ctx, bson.D{}); err != nil { return nil, err } - var subscriptions []interface{} - for _, sub := range loadedSubscriptions { - subscriptions = append(subscriptions, sub) - } - count, err := ms.client.Database(database).Collection(name).InsertMany(ctx, subscriptions) - if err != nil || len(count.InsertedIDs) != len(loadedSubscriptions) { - return nil, fmt.Errorf("failed to insert subscriptions: %w", err) - } } } else { // if the collection has a validator create it with it @@ -75,16 +67,15 @@ func (ms *MongoStorage) initCollections(database string) error { if err := ms.client.Database(database).CreateCollection(ctx, name, opts); err != nil { return nil, err } - - if name == "subscriptions" { - var subscriptions []interface{} - for _, sub := range loadedSubscriptions { - subscriptions = append(subscriptions, sub) - } - count, err := ms.client.Database(database).Collection(name).InsertMany(ctx, subscriptions) - if err != nil || len(count.InsertedIDs) != len(loadedSubscriptions) { - return nil, fmt.Errorf("failed to insert subscriptions: %w", err) - } + } + if name == "subscriptions" { + var subscriptions []interface{} + for _, sub := range loadedSubscriptions { + subscriptions = append(subscriptions, sub) + } + count, err := ms.client.Database(database).Collection(name).InsertMany(ctx, subscriptions) + if err != nil || len(count.InsertedIDs) != len(loadedSubscriptions) { + return nil, fmt.Errorf("failed to insert subscriptions: %w", err) } } // return the collection @@ -222,15 +213,19 @@ func dynamicUpdateDocument(item interface{}, alwaysUpdateTags []string) (bson.M, // and return it as a Subscription array func readSubscriptionJSON(subscriptionsFile string) ([]*Subscription, error) { log.Warnf("Reading subscriptions from %s", subscriptionsFile) - file, err := os.Open(subscriptionsFile) + file, err := root.Assets.Open(fmt.Sprintf("assets/%s", subscriptionsFile)) if err != nil { return nil, err } - defer func() { - if err := file.Close(); err != nil { - log.Warnw("failed to close subscriptions file", "error", err) - } - }() + // file, err := os.Open(subscriptionsFile) + // if err != nil { + // return nil, err + // } + // defer func() { + // if err := file.Close(); err != nil { + // log.Warnw("failed to close subscriptions file", "error", err) + // } + // }() // Create a JSON decoder decoder := json.NewDecoder(file) diff --git a/db/subscriptions.json b/db/subscriptions.json deleted file mode 100644 index a46e1e1..0000000 --- a/db/subscriptions.json +++ /dev/null @@ -1,59 +0,0 @@ -[ - { - "ID": 1, - "Name": "Basic", - "StripeID": "stripe_123", - "Organization": { - "Memberships": 1, - "SubOrgs": 1 - }, - "VotingTypes": { - "Approval": true, - "Ranked": true, - "Weighted": true - }, - "Features": { - "Personalization": false, - "EmailReminder": false, - "SmsNotification": false - } - }, - { - "ID": 2, - "Name": "Pro", - "StripeID": "stripe_456", - "Organization": { - "Memberships": 10, - "SubOrgs": 5 - }, - "VotingTypes": { - "Approval": true, - "Ranked": false, - "Weighted": true - }, - "Features": { - "Personalization": false, - "EmailReminder": false, - "SmsNotification": false - } - }, - { - "ID": 3, - "Name": "Ulimited", - "StripeID": "stripe_789", - "Organization": { - "Memberships": 10, - "SubOrgs": 5 - }, - "VotingTypes": { - "Approval": true, - "Ranked": true, - "Weighted": true - }, - "Features": { - "Personalization": true, - "EmailReminder": true, - "SmsNotification": true - } - } -] \ No newline at end of file diff --git a/embed.go b/embed.go new file mode 100644 index 0000000..f644d19 --- /dev/null +++ b/embed.go @@ -0,0 +1,6 @@ +package root + +import "embed" + +//go:embed all:assets +var Assets embed.FS diff --git a/stripe/stripe.go b/stripe/stripe.go index bbcbfc5..2c0ebbd 100644 --- a/stripe/stripe.go +++ b/stripe/stripe.go @@ -9,10 +9,14 @@ import ( "go.vocdoni.io/dvote/log" ) +// StripeClient is a client for interacting with the Stripe API. +// It holds the necessary configuration such as the webhook secret. type StripeClient struct { webhookSecret string } +// New creates a new instance of StripeClient with the provided API secret and webhook secret. +// It sets the Stripe API key to the provided apiSecret. func New(apiSecret, webhookSecret string) *StripeClient { stripe.Key = apiSecret return &StripeClient{ @@ -20,6 +24,7 @@ func New(apiSecret, webhookSecret string) *StripeClient { } } +// DecodeEvent decodes a Stripe webhook event from the given payload and signature header. func (s *StripeClient) DecodeEvent(payload []byte, signatureHeader string) (*stripe.Event, error) { event := stripe.Event{} @@ -36,6 +41,7 @@ func (s *StripeClient) DecodeEvent(payload []byte, signatureHeader string) (*str return &event, nil } +// GetInfoFromEvent processes a Stripe event to extract customer and subscription information. func (s *StripeClient) GetInfoFromEvent(event stripe.Event) (*stripe.Customer, *stripe.Subscription, error) { var subscription stripe.Subscription err := json.Unmarshal(event.Data.Raw, &subscription) diff --git a/subscriptions/subscriptions.go b/subscriptions/subscriptions.go index 5d75d61..9efe6f5 100644 --- a/subscriptions/subscriptions.go +++ b/subscriptions/subscriptions.go @@ -7,14 +7,19 @@ import ( "go.vocdoni.io/proto/build/go/models" ) +// SubscriptionsConfig holds the configuration for the subscriptions service. +// It includes a reference to the MongoDB storage used by the service. type SubscriptionsConfig struct { DB *db.MongoStorage } +// Subscriptions is the service that manages the organization permissions based on +// the subscription plans. type Subscriptions struct { db *db.MongoStorage } +// New creates a new Subscriptions service with the given configuration. func New(conf *SubscriptionsConfig) *Subscriptions { if conf == nil { return nil @@ -24,6 +29,7 @@ func New(conf *SubscriptionsConfig) *Subscriptions { } } +// HasPermission checks if the organization has permission to perform the given transaction. func (p *Subscriptions) HasPermission( tx *models.Tx, txType models.TxType,