From c13dd9addbf89f716e4ef5cfdf1d673139ffcb68 Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Sun, 30 Jun 2024 21:41:41 -0400 Subject: Move to Donetick Org, first commit --- external/user/model/model.go | 14 ++++++++++++ external/user/repo/repository.go | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 external/user/model/model.go create mode 100644 external/user/repo/repository.go (limited to 'external/user') diff --git a/external/user/model/model.go b/external/user/model/model.go new file mode 100644 index 0000000..d132e68 --- /dev/null +++ b/external/user/model/model.go @@ -0,0 +1,14 @@ +package model + +import ( + "time" + + uModel "donetick.com/core/internal/user/model" +) + +type UserExtended struct { + uModel.User + Credit int `gorm:"column:amount;->"` + SubscriptionStatus *string `gorm:"column:status;<-:false"` // read one column + ExpiredAt *time.Time `gorm:"column:expired_at;<-:false"` // read one column +} diff --git a/external/user/repo/repository.go b/external/user/repo/repository.go new file mode 100644 index 0000000..6876984 --- /dev/null +++ b/external/user/repo/repository.go @@ -0,0 +1,49 @@ +package user + +import ( + "context" + + exUser "donetick.com/core/external/user/model" + "donetick.com/core/logging" + "gorm.io/gorm" +) + +type ExtendedUserRepository struct { + db *gorm.DB +} + +func NewExtendedUserRepository(db *gorm.DB) *ExtendedUserRepository { + return &ExtendedUserRepository{db} +} + +func (a *ExtendedUserRepository) FindFullUserByEmail(ctx context.Context, email string) (*exUser.UserExtended, error) { + logger := logging.FromContext(ctx) + logger.Debugw("repository.user.FindFullUserByEmail", "email", email) + var acc exUser.UserExtended + if err := a.db.Table("users"). + Select("users.*, s.expired_at, s.status , s.customer_id"). + Joins("LEFT JOIN stripe_customers sc ON sc.user_id = users.id"). + Joins("LEFT JOIN stripe_subscriptions s ON s.customer_id = sc.customer_id AND s.expired_at > now() OR s.expired_at is null"). + Where("email = ?", email). + First(&acc).Error; err != nil { + logger.Error("repository.user.FindFullUserByEmail failed to find", "err", err) + return nil, err + } + return &acc, nil +} + +func (a *ExtendedUserRepository) FindFullUserByUsername(ctx context.Context, username string) (*exUser.UserExtended, error) { + logger := logging.FromContext(ctx) + logger.Debugw("repository.user.FindFullUserByUsername", "username", username) + var acc exUser.UserExtended + if err := a.db.Table("users"). + Select("users.*, s.expired_at, s.status , s.customer_id"). + Joins("LEFT JOIN stripe_customers sc ON sc.user_id = users.id"). + Joins("LEFT JOIN stripe_subscriptions s ON s.customer_id = sc.customer_id AND s.expired_at > now() OR s.expired_at is null"). + Where("username = ?", username). + First(&acc).Error; err != nil { + logger.Error("repository.user.FindFullUserByUsername failed to find", "err", err) + return nil, err + } + return &acc, nil +} -- cgit