Skip to content

Commit

Permalink
* Adds comments
Browse files Browse the repository at this point in the history
* Fix embedding subscriptions file
* Minor fixes
  • Loading branch information
emmdim committed Oct 31, 2024
1 parent 7e7918e commit fc45e0a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 84 deletions.
File renamed without changes.
45 changes: 20 additions & 25 deletions db/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
59 changes: 0 additions & 59 deletions db/subscriptions.json

This file was deleted.

6 changes: 6 additions & 0 deletions embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package root

import "embed"

//go:embed all:assets
var Assets embed.FS
6 changes: 6 additions & 0 deletions stripe/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ 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{
webhookSecret: webhookSecret,
}
}

// 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{}

Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions subscriptions/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit fc45e0a

Please sign in to comment.