initial commit
This commit is contained in:
38
storage/incentives.go
Normal file
38
storage/incentives.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package storage
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
SubscriptionStatusAvailable = iota
|
||||
SubscriptionStatusInitiated
|
||||
SubscriptionStatusDeclined
|
||||
)
|
||||
|
||||
type Incentive struct {
|
||||
ID string
|
||||
Name string
|
||||
Start time.Time
|
||||
AcceptedProofLevels []string // A, B, C
|
||||
}
|
||||
|
||||
type IncentiveSubscription struct {
|
||||
ID string `bson:"_id"`
|
||||
IncentiveID string `bson:"incentive_id"`
|
||||
UserID string `bson:"user_id"`
|
||||
IdentityVerificationIDs []string `bson:"identity_verification_ids"` // identity checks to avoid multiple persons subscribing to similar incentives (OIDC subjects, passport/identity card numbers, ...)
|
||||
Declined bool `bson:"declined"`
|
||||
Data map[string]any `bson:"data"`
|
||||
SubscriptionDatetime time.Time `bson:"subscription_datetime"`
|
||||
}
|
||||
|
||||
func (i Incentive) SubscriptionStatus(incentiveSubscriptions []IncentiveSubscription) int {
|
||||
for _, subscription := range incentiveSubscriptions {
|
||||
if i.ID == subscription.IncentiveID {
|
||||
if subscription.Declined {
|
||||
return SubscriptionStatusDeclined
|
||||
}
|
||||
return SubscriptionStatusInitiated
|
||||
}
|
||||
}
|
||||
return SubscriptionStatusAvailable
|
||||
}
|
||||
82
storage/mongodb.go
Normal file
82
storage/mongodb.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type MongoDBStorage struct {
|
||||
*mongo.Client
|
||||
DbName string
|
||||
Collections map[string]string
|
||||
}
|
||||
|
||||
func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
|
||||
var (
|
||||
mongodb_host = cfg.GetString("storage.db.mongodb.host")
|
||||
mongodb_port = cfg.GetString("storage.db.mongodb.port")
|
||||
mongodb_dbname = cfg.GetString("storage.db.mongodb.db_name")
|
||||
mongodb_incentives_subscriptions = cfg.GetString("storage.db.mongodb.collections.incentives_subscriptions")
|
||||
mongodb_proofs = cfg.GetString("storage.db.mongodb.collections.proofs")
|
||||
)
|
||||
|
||||
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://" + mongodb_host + ":" + mongodb_port))
|
||||
if err != nil {
|
||||
return MongoDBStorage{}, err
|
||||
}
|
||||
|
||||
err = client.Connect(context.TODO())
|
||||
|
||||
if err != nil {
|
||||
return MongoDBStorage{}, err
|
||||
}
|
||||
|
||||
storage := MongoDBStorage{
|
||||
Client: client,
|
||||
DbName: mongodb_dbname,
|
||||
Collections: map[string]string{
|
||||
"incentives_subscriptions": mongodb_incentives_subscriptions,
|
||||
"proofs": mongodb_proofs,
|
||||
},
|
||||
}
|
||||
|
||||
return storage, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetUserSubscriptions(userid string) ([]IncentiveSubscription, error) {
|
||||
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["incentives_subscriptions"])
|
||||
|
||||
subscriptions := []IncentiveSubscription{}
|
||||
|
||||
findOptions := options.Find()
|
||||
cur, err := collection.Find(context.TODO(), bson.M{"user_id": userid}, findOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for cur.Next(context.TODO()) {
|
||||
var subscription IncentiveSubscription
|
||||
|
||||
if err := cur.Decode(&subscription); err != nil {
|
||||
return subscriptions, err
|
||||
}
|
||||
|
||||
subscriptions = append(subscriptions, subscription)
|
||||
}
|
||||
|
||||
return subscriptions, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) SubscribeIncentive(incentive_subscription IncentiveSubscription) error {
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["incentives_subscriptions"])
|
||||
if _, err := collection.InsertOne(context.TODO(), incentive_subscription); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
23
storage/proofs.go
Normal file
23
storage/proofs.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package storage
|
||||
|
||||
import "time"
|
||||
|
||||
type Proof struct {
|
||||
ID string `bson:"_id"`
|
||||
OperatorJourneyID string `bson:"operator_journey_id"`
|
||||
RPCStatus string `bson:"rpc_status"`
|
||||
ProofLevelDowngraded *string `bson:"proof_level_downgraded"`
|
||||
}
|
||||
|
||||
type RPCStatus struct {
|
||||
RPCJourneyID string `bson:"rpc_journey_id"`
|
||||
Submitted time.Time
|
||||
}
|
||||
|
||||
func (p Proof) ProofLevel() string {
|
||||
if p.ProofLevelDowngraded != nil {
|
||||
return *p.ProofLevelDowngraded
|
||||
}
|
||||
//TODO
|
||||
return ""
|
||||
}
|
||||
26
storage/storage.go
Normal file
26
storage/storage.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
GetUserSubscriptions(userid string) ([]IncentiveSubscription, error)
|
||||
SubscribeIncentive(incentive_subscription IncentiveSubscription) error
|
||||
}
|
||||
|
||||
func NewStorage(cfg *viper.Viper) (Storage, error) {
|
||||
var (
|
||||
storage_type = cfg.GetString("storage.db.type")
|
||||
)
|
||||
|
||||
switch storage_type {
|
||||
case "mongodb":
|
||||
s, err := NewMongoDBStorage(cfg)
|
||||
return s, err
|
||||
default:
|
||||
return nil, fmt.Errorf("storage type %v is not supported", storage_type)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user