Skip to content

Commit

Permalink
feat: CreateInternalUser method returns email_verification entry
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaanqui committed Apr 22, 2024
1 parent 3d31dbc commit c7a8819
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
3 changes: 2 additions & 1 deletion graph/resolver/user.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions services/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ import (
"google.golang.org/api/option"
)

func (service Service) CreateInternalUser(ctx context.Context, input gmodel.CreateAccountInput) (gmodel.User, error) {
func (service Service) CreateInternalUser(ctx context.Context, input gmodel.CreateAccountInput) (user gmodel.User, email_verification model.EmailVerification, err error) {
if service.UserEmailExists(ctx, input.Email) {
return gmodel.User{}, fmt.Errorf("email already exists")
return gmodel.User{}, model.EmailVerification{}, fmt.Errorf("email already exists")
}
hashed_password, hash_err := service.HashPassword(input.Password)
if hash_err != nil {
return gmodel.User{}, hash_err
return gmodel.User{}, model.EmailVerification{}, hash_err
}

// Create transaction
tx, tx_err := service.DB.BeginTx(ctx, nil)
if tx_err != nil {
return gmodel.User{}, tx_err
return gmodel.User{}, model.EmailVerification{}, tx_err
}

var user gmodel.User
qb := table.User.
INSERT(
table.User.Email,
Expand All @@ -49,20 +48,20 @@ func (service Service) CreateInternalUser(ctx context.Context, input gmodel.Crea
RETURNING(table.User.AllColumns)
if err := qb.QueryContext(ctx, tx, &user); err != nil {
tx.Rollback()
return gmodel.User{}, fmt.Errorf("user entry could not be created. %s", err.Error())
return gmodel.User{}, model.EmailVerification{}, fmt.Errorf("user entry could not be created. %s", err.Error())
}
service.TX = tx
if _, err := service.CreateEmailVerification(ctx, user); err != nil {
if email_verification, err = service.CreateEmailVerification(ctx, user); err != nil {
tx.Rollback()
return gmodel.User{}, err
return gmodel.User{}, model.EmailVerification{}, err
}

// Commit changes from transaction
if err := tx.Commit(); err != nil {
tx.Rollback()
return gmodel.User{}, err
return gmodel.User{}, model.EmailVerification{}, err
}
return user, nil
return user, email_verification, nil
}

func (service Service) CreateOauthUser(ctx context.Context, input gmodel.CreateAccountInput, oauth_type model.UserAuthPlatformType) (gmodel.User, error) {
Expand Down
2 changes: 1 addition & 1 deletion tests/scheduling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func init_location_instances(count int) []*gmodel.CreateLocationInstance {
}

func TestLocation(t *testing.T) {
user, err := service.CreateInternalUser(ctx, gmodel.CreateAccountInput{
user, _, err := service.CreateInternalUser(ctx, gmodel.CreateAccountInput{
Email: "[email protected]",
Name: "Location Test User",
Password: "location_test_password123",
Expand Down
15 changes: 3 additions & 12 deletions tests/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func TestUser(t *testing.T) {

t.Run("create user", func(t *testing.T) {
var err error
user1, err = service.CreateInternalUser(ctx, user1_input)
var email_verification model.EmailVerification
user1, email_verification, err = service.CreateInternalUser(ctx, user1_input)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -61,16 +62,6 @@ func TestUser(t *testing.T) {
})

t.Run("should create email verification entry", func(t *testing.T) {
qb := table.EmailVerification.
SELECT(table.EmailVerification.AllColumns).
FROM(table.EmailVerification).
WHERE(table.EmailVerification.UserID.EQ(postgres.Int(user1.ID))).
LIMIT(1)
var email_verification model.EmailVerification
if err := qb.QueryContext(ctx, db, &email_verification); err != nil {
t.Fatal("email verification entry was not created.", err.Error())
}

ev_check, err := service.FindEmailVerificationByCode(ctx, email_verification.Code)
if err != nil {
t.Fatal("should find verification entry using the code")
Expand Down Expand Up @@ -104,7 +95,7 @@ func TestUser(t *testing.T) {
})

t.Run("duplicate user", func(t *testing.T) {
_, err := service.CreateInternalUser(ctx, gmodel.CreateAccountInput{
_, _, err := service.CreateInternalUser(ctx, gmodel.CreateAccountInput{
Email: user1.Email,
Password: "abc123",
})
Expand Down

0 comments on commit c7a8819

Please sign in to comment.