|
| 1 | +package repositories |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/NdoleStudio/httpsms/pkg/entities" |
| 9 | + "github.com/NdoleStudio/httpsms/pkg/telemetry" |
| 10 | + "github.com/cockroachdb/cockroach-go/v2/crdb/crdbgorm" |
| 11 | + "github.com/google/uuid" |
| 12 | + "github.com/jinzhu/now" |
| 13 | + "github.com/palantir/stacktrace" |
| 14 | + "gorm.io/gorm" |
| 15 | +) |
| 16 | + |
| 17 | +// gormBillingUsageRepository is responsible for persisting entities.BillingUsage |
| 18 | +type gormBillingUsageRepository struct { |
| 19 | + logger telemetry.Logger |
| 20 | + tracer telemetry.Tracer |
| 21 | + db *gorm.DB |
| 22 | +} |
| 23 | + |
| 24 | +// NewGormBillingUsageRepository creates the GORM version of the BillingUsageRepository |
| 25 | +func NewGormBillingUsageRepository( |
| 26 | + logger telemetry.Logger, |
| 27 | + tracer telemetry.Tracer, |
| 28 | + db *gorm.DB, |
| 29 | +) BillingUsageRepository { |
| 30 | + return &gormBillingUsageRepository{ |
| 31 | + logger: logger.WithService(fmt.Sprintf("%T", &gormBillingUsageRepository{})), |
| 32 | + tracer: tracer, |
| 33 | + db: db, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// RegisterSentMessage registers a message as sent |
| 38 | +func (repository *gormBillingUsageRepository) RegisterSentMessage(ctx context.Context, userID entities.UserID) error { |
| 39 | + ctx, span := repository.tracer.Start(ctx) |
| 40 | + defer span.End() |
| 41 | + |
| 42 | + return crdbgorm.ExecuteTx(ctx, repository.db, nil, |
| 43 | + func(tx *gorm.DB) error { |
| 44 | + result := tx.WithContext(ctx). |
| 45 | + Model(&entities.BillingUsage{}). |
| 46 | + Where("start_timestamp = ?", now.BeginningOfMonth()). |
| 47 | + Where("user_id = ?", userID). |
| 48 | + UpdateColumn("sent_messages", gorm.Expr("sent_messages + ?", 1)) |
| 49 | + |
| 50 | + if result.RowsAffected == 0 { |
| 51 | + return tx.Create(repository.createBillingUsage(userID, 1, 0)).Error |
| 52 | + } |
| 53 | + return result.Error |
| 54 | + }, |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +// RegisterReceivedMessage registers a message as received |
| 59 | +func (repository *gormBillingUsageRepository) RegisterReceivedMessage(ctx context.Context, userID entities.UserID) error { |
| 60 | + ctx, span := repository.tracer.Start(ctx) |
| 61 | + defer span.End() |
| 62 | + |
| 63 | + return crdbgorm.ExecuteTx(ctx, repository.db, nil, |
| 64 | + func(tx *gorm.DB) error { |
| 65 | + result := tx.WithContext(ctx). |
| 66 | + Model(&entities.BillingUsage{}). |
| 67 | + Where("start_timestamp = ?", now.BeginningOfMonth()). |
| 68 | + Where("user_id = ?", userID). |
| 69 | + UpdateColumn("sent_messages", gorm.Expr("received_messages + ?", 1)) |
| 70 | + |
| 71 | + if result.RowsAffected == 0 { |
| 72 | + return tx.Create(repository.createBillingUsage(userID, 0, 1)).Error |
| 73 | + } |
| 74 | + return result.Error |
| 75 | + }, |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +// GetCurrent returns the current billing usage by entities.UserID |
| 80 | +func (repository *gormBillingUsageRepository) GetCurrent(ctx context.Context, userID entities.UserID) (*entities.BillingUsage, error) { |
| 81 | + ctx, span := repository.tracer.Start(ctx) |
| 82 | + defer span.End() |
| 83 | + |
| 84 | + usage := repository.createBillingUsage(userID, 0, 0) |
| 85 | + |
| 86 | + err := crdbgorm.ExecuteTx(ctx, repository.db, nil, |
| 87 | + func(tx *gorm.DB) error { |
| 88 | + result := tx.WithContext(ctx). |
| 89 | + Where("start_timestamp = ?", now.BeginningOfMonth()). |
| 90 | + First(usage) |
| 91 | + |
| 92 | + if errors.Is(result.Error, gorm.ErrRecordNotFound) { |
| 93 | + return tx.WithContext(ctx).Create(usage).Error |
| 94 | + } |
| 95 | + return result.Error |
| 96 | + }, |
| 97 | + ) |
| 98 | + if err != nil { |
| 99 | + return usage, stacktrace.Propagate(err, fmt.Sprintf("cannot load billing usage for user [%s]", userID)) |
| 100 | + } |
| 101 | + |
| 102 | + return usage, err |
| 103 | +} |
| 104 | + |
| 105 | +// GetHistory returns past billing usage by entities.UserID |
| 106 | +func (repository *gormBillingUsageRepository) GetHistory(ctx context.Context, userID entities.UserID, params IndexParams) (*[]entities.BillingUsage, error) { |
| 107 | + ctx, span := repository.tracer.Start(ctx) |
| 108 | + defer span.End() |
| 109 | + |
| 110 | + usages := new([]entities.BillingUsage) |
| 111 | + |
| 112 | + err := repository.db.WithContext(ctx). |
| 113 | + Where("user_id = ?", userID). |
| 114 | + Where("start_timestamp != ?", now.BeginningOfMonth()). |
| 115 | + Order("start_timestamp DESC"). |
| 116 | + Limit(params.Limit). |
| 117 | + Offset(params.Skip). |
| 118 | + Find(&usages). |
| 119 | + Error |
| 120 | + if err != nil { |
| 121 | + msg := fmt.Sprintf("cannot fetch billing usage history for userID [%s] and params [%+#v]", userID, params) |
| 122 | + return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 123 | + } |
| 124 | + |
| 125 | + return usages, err |
| 126 | +} |
| 127 | + |
| 128 | +func (repository *gormBillingUsageRepository) createBillingUsage(userID entities.UserID, sent uint, received uint) *entities.BillingUsage { |
| 129 | + return &entities.BillingUsage{ |
| 130 | + ID: uuid.New(), |
| 131 | + UserID: userID, |
| 132 | + SentMessages: sent, |
| 133 | + ReceivedMessages: received, |
| 134 | + StartTimestamp: now.BeginningOfMonth(), |
| 135 | + EndTimestamp: now.EndOfMonth(), |
| 136 | + } |
| 137 | +} |
0 commit comments