-
Notifications
You must be signed in to change notification settings - Fork 126
/
user.go
116 lines (92 loc) · 2.78 KB
/
user.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package prisma
import (
"context"
"strings"
"github.com/google/uuid"
"github.com/hatchet-dev/hatchet/internal/repository"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
"github.com/hatchet-dev/hatchet/internal/validator"
"github.com/steebchen/prisma-client-go/runtime/transaction"
)
type userRepository struct {
client *db.PrismaClient
v validator.Validator
}
func NewUserRepository(client *db.PrismaClient, v validator.Validator) repository.UserRepository {
return &userRepository{
client: client,
v: v,
}
}
func (r *userRepository) GetUserByID(id string) (*db.UserModel, error) {
return r.client.User.FindUnique(
db.User.ID.Equals(id),
).Exec(context.Background())
}
func (r *userRepository) GetUserByEmail(email string) (*db.UserModel, error) {
emailLower := strings.ToLower(email)
return r.client.User.FindUnique(
db.User.Email.Equals(emailLower),
).Exec(context.Background())
}
func (r *userRepository) GetUserPassword(id string) (*db.UserPasswordModel, error) {
return r.client.UserPassword.FindUnique(
db.UserPassword.UserID.Equals(id),
).Exec(context.Background())
}
func (r *userRepository) CreateUser(opts *repository.CreateUserOpts) (*db.UserModel, error) {
if err := r.v.Validate(opts); err != nil {
return nil, err
}
userId := uuid.New().String()
params := []db.UserSetParam{
db.User.ID.Set(userId),
}
if opts.EmailVerified != nil {
params = append(params, db.User.EmailVerified.Set(*opts.EmailVerified))
}
if opts.Name != nil {
params = append(params, db.User.Name.Set(*opts.Name))
}
emailLower := strings.ToLower(opts.Email)
createTx := r.client.User.CreateOne(
db.User.Email.Set(emailLower),
params...,
).Tx()
txs := []transaction.Param{
createTx,
r.client.UserPassword.CreateOne(
db.UserPassword.Hash.Set(opts.Password),
db.UserPassword.User.Link(db.User.ID.Equals(userId)),
).Tx(),
}
if err := r.client.Prisma.Transaction(txs...).Exec(context.Background()); err != nil {
return nil, err
}
return createTx.Result(), nil
}
func (r *userRepository) UpdateUser(id string, opts *repository.UpdateUserOpts) (*db.UserModel, error) {
if err := r.v.Validate(opts); err != nil {
return nil, err
}
params := []db.UserSetParam{}
if opts.EmailVerified != nil {
params = append(params, db.User.EmailVerified.Set(*opts.EmailVerified))
}
if opts.Name != nil {
params = append(params, db.User.Name.Set(*opts.Name))
}
return r.client.User.FindUnique(
db.User.ID.Equals(id),
).Update(
params...,
).Exec(context.Background())
}
func (r *userRepository) ListTenantMemberships(userId string) ([]db.TenantMemberModel, error) {
return r.client.TenantMember.FindMany(
db.TenantMember.UserID.Equals(userId),
).With(
db.TenantMember.Tenant.Fetch(),
db.TenantMember.User.Fetch(),
).Exec(context.Background())
}