aboutsummaryrefslogtreecommitdiffstats
path: root/external/user/repo/repository.go
blob: 68769848f93aaabb4b1f233b7af8f14f0f342b89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
}