2024-11-18 15:23:13 +00:00
|
|
|
// Copyright 2024 Patial Tech. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2024-11-15 16:12:15 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/mail"
|
|
|
|
"strings"
|
2024-11-17 16:58:29 +00:00
|
|
|
"time"
|
2024-11-15 16:12:15 +00:00
|
|
|
|
2024-11-17 16:58:29 +00:00
|
|
|
"gitserver.in/patialtech/rano/config"
|
2024-11-15 16:12:15 +00:00
|
|
|
"gitserver.in/patialtech/rano/db"
|
2024-11-17 16:58:29 +00:00
|
|
|
"gitserver.in/patialtech/rano/db/ent/user"
|
2024-11-15 16:12:15 +00:00
|
|
|
"gitserver.in/patialtech/rano/mailer"
|
|
|
|
"gitserver.in/patialtech/rano/mailer/message"
|
|
|
|
"gitserver.in/patialtech/rano/util/crypto"
|
|
|
|
"gitserver.in/patialtech/rano/util/validate"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreateInput struct {
|
|
|
|
Email string `validate:"email"`
|
|
|
|
Phone string
|
|
|
|
Pwd string `validate:"required"`
|
|
|
|
ConfirmPwd string `validate:"required"`
|
|
|
|
FirstName string `validate:"required"`
|
|
|
|
MiddleName string
|
|
|
|
LastName string
|
|
|
|
RoleID uint8 `validate:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrCreateInpNil = errors.New("user: create input is nil")
|
|
|
|
ErrWrongConfirmPwd = errors.New("user: confirm password does not match")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create user record in DB
|
|
|
|
//
|
|
|
|
// will return created userID on success
|
|
|
|
func Create(ctx context.Context, inp *CreateInput) (int64, error) {
|
2024-11-17 16:58:29 +00:00
|
|
|
// Check for nil input.
|
2024-11-15 16:12:15 +00:00
|
|
|
if inp == nil {
|
|
|
|
return 0, ErrCreateInpNil
|
|
|
|
}
|
|
|
|
|
2024-11-17 16:58:29 +00:00
|
|
|
// Validate struct.
|
2024-11-15 16:12:15 +00:00
|
|
|
if err := validate.Struct(inp); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-11-17 16:58:29 +00:00
|
|
|
// Compare pwd and comparePwd.
|
2024-11-15 16:12:15 +00:00
|
|
|
if inp.Pwd != inp.ConfirmPwd {
|
|
|
|
return 0, ErrWrongConfirmPwd
|
|
|
|
}
|
|
|
|
|
|
|
|
h, salt, err := crypto.PasswordHash(inp.Pwd)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-11-17 16:58:29 +00:00
|
|
|
// Begin a transaction.
|
|
|
|
tx, err := db.Client().BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save User to DB.
|
|
|
|
u, err := tx.User.Create().
|
2024-11-15 16:12:15 +00:00
|
|
|
SetEmail(inp.Email).
|
|
|
|
SetPwdHash(h).
|
|
|
|
SetPwdSalt(salt).
|
|
|
|
SetFirstName(inp.FirstName).
|
|
|
|
SetMiddleName(inp.MiddleName).
|
|
|
|
SetLastName(inp.LastName).
|
2024-11-17 16:58:29 +00:00
|
|
|
SetStatus(user.StatusActive).
|
2024-11-15 16:12:15 +00:00
|
|
|
Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("failed to create user")
|
|
|
|
}
|
|
|
|
|
2024-11-17 16:58:29 +00:00
|
|
|
// Get a new email-verification token
|
|
|
|
tokenDuration := time.Hour * 6
|
|
|
|
token, err := newTokenToVerifyEmail(u.ID, tokenDuration)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save token to DB
|
|
|
|
err = tx.VerifyToken.Create().
|
|
|
|
SetToken(token).
|
|
|
|
SetExpiresAt(time.Now().Add(tokenDuration).UTC()).
|
|
|
|
SetPurpose("VerifyEmail").
|
|
|
|
SetUserID(u.ID).Exec(ctx)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := fullName(inp.FirstName, inp.MiddleName, inp.LastName)
|
|
|
|
// Send a welcome email with a link to verigy email-address.
|
2024-11-15 16:12:15 +00:00
|
|
|
err = mailer.Send(
|
|
|
|
[]mail.Address{
|
2024-11-17 16:58:29 +00:00
|
|
|
{Name: name, Address: inp.Email},
|
2024-11-15 16:12:15 +00:00
|
|
|
},
|
|
|
|
&message.Welcome{
|
2024-11-17 16:58:29 +00:00
|
|
|
Name: name,
|
|
|
|
VerifyURL: config.VerifyEmailURL(token),
|
2024-11-15 16:12:15 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-11-17 16:58:29 +00:00
|
|
|
_ = tx.Rollback()
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2024-11-15 16:12:15 +00:00
|
|
|
}
|
|
|
|
|
2024-11-17 16:58:29 +00:00
|
|
|
// ALL Done!
|
|
|
|
// Created a new user in system.
|
2024-11-15 16:12:15 +00:00
|
|
|
return u.ID, nil
|
|
|
|
}
|
|
|
|
|
2024-11-17 16:58:29 +00:00
|
|
|
func fullName(fName, mName, lName string) string {
|
|
|
|
name := fmt.Sprintf("%s %s %s", fName, mName, lName)
|
2024-11-15 16:12:15 +00:00
|
|
|
return strings.Join(strings.Fields(name), " ")
|
|
|
|
}
|