2019-06-24 22:41:21 -08:00
|
|
|
package signup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-13 12:16:28 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web"
|
2019-06-24 22:41:21 -08:00
|
|
|
"time"
|
|
|
|
|
2019-07-13 12:16:28 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/account"
|
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/auth"
|
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/user"
|
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/user_account"
|
2019-06-24 22:41:21 -08:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
|
|
|
|
"gopkg.in/go-playground/validator.v9"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Signup performs the steps needed to create a new account, new user and then associate
|
|
|
|
// both records with a new user_account entry.
|
2019-06-26 20:21:00 -08:00
|
|
|
func Signup(ctx context.Context, claims auth.Claims, dbConn *sqlx.DB, req SignupRequest, now time.Time) (*SignupResult, error) {
|
2019-06-24 22:41:21 -08:00
|
|
|
span, ctx := tracer.StartSpanFromContext(ctx, "internal.signup.Signup")
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
// Validate the user email address is unique in the database.
|
|
|
|
uniqEmail, err := user.UniqueEmail(ctx, dbConn, req.User.Email, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the account name is unique in the database.
|
|
|
|
uniqName, err := account.UniqueName(ctx, dbConn, req.Account.Name, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f := func(fl validator.FieldLevel) bool {
|
|
|
|
if fl.Field().String() == "invalid" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var uniq bool
|
2019-06-24 23:02:43 -08:00
|
|
|
switch fl.FieldName() {
|
2019-06-26 20:21:00 -08:00
|
|
|
case "Name", "name":
|
2019-06-24 22:41:21 -08:00
|
|
|
uniq = uniqName
|
2019-06-26 20:21:00 -08:00
|
|
|
case "Email", "email":
|
2019-06-24 22:41:21 -08:00
|
|
|
uniq = uniqEmail
|
|
|
|
}
|
|
|
|
|
|
|
|
return uniq
|
|
|
|
}
|
2019-06-26 20:21:00 -08:00
|
|
|
|
|
|
|
v := web.NewValidator()
|
2019-06-24 22:41:21 -08:00
|
|
|
v.RegisterValidation("unique", f)
|
|
|
|
|
|
|
|
// Validate the request.
|
|
|
|
err = v.Struct(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:21:00 -08:00
|
|
|
var resp SignupResult
|
2019-06-24 22:41:21 -08:00
|
|
|
|
2019-06-25 02:40:29 -08:00
|
|
|
// UserCreateRequest contains information needed to create a new User.
|
|
|
|
userReq := user.UserCreateRequest{
|
|
|
|
Name: req.User.Name,
|
|
|
|
Email: req.User.Email,
|
|
|
|
Password: req.User.Password,
|
|
|
|
PasswordConfirm: req.User.PasswordConfirm,
|
|
|
|
Timezone: req.Account.Timezone,
|
|
|
|
}
|
|
|
|
|
2019-06-24 22:41:21 -08:00
|
|
|
// Execute user creation.
|
2019-06-25 02:40:29 -08:00
|
|
|
resp.User, err = user.Create(ctx, claims, dbConn, userReq, now)
|
2019-06-24 22:41:21 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-25 02:40:29 -08:00
|
|
|
accountStatus := account.AccountStatus_Active
|
|
|
|
accountReq := account.AccountCreateRequest{
|
|
|
|
Name: req.Account.Name,
|
|
|
|
Address1: req.Account.Address1,
|
|
|
|
Address2: req.Account.Address2,
|
|
|
|
City: req.Account.City,
|
|
|
|
Region: req.Account.Region,
|
|
|
|
Country: req.Account.Country,
|
|
|
|
Zipcode: req.Account.Zipcode,
|
|
|
|
Status: &accountStatus,
|
|
|
|
Timezone: req.Account.Timezone,
|
|
|
|
SignupUserID: &resp.User.ID,
|
|
|
|
BillingUserID: &resp.User.ID,
|
|
|
|
}
|
2019-06-24 22:41:21 -08:00
|
|
|
|
|
|
|
// Execute account creation.
|
2019-06-25 02:40:29 -08:00
|
|
|
resp.Account, err = account.Create(ctx, claims, dbConn, accountReq, now)
|
2019-06-24 22:41:21 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Associate the created user with the new account. The first user for the account will
|
|
|
|
// always have the role of admin.
|
2019-06-26 01:16:57 -08:00
|
|
|
ua := user_account.UserAccountCreateRequest{
|
2019-06-24 23:02:43 -08:00
|
|
|
UserID: resp.User.ID,
|
2019-06-24 22:41:21 -08:00
|
|
|
AccountID: resp.Account.ID,
|
2019-06-24 23:02:43 -08:00
|
|
|
Roles: []user_account.UserAccountRole{user_account.UserAccountRole_Admin},
|
2019-06-24 22:41:21 -08:00
|
|
|
//Status: Use default value
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = user_account.Create(ctx, claims, dbConn, ua, now)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
}
|