mirror of
https://github.com/getAlby/lndhub.go.git
synced 2026-02-23 05:44:23 +01:00
Create user accounts on user create
Incoming: the account we debit on incoming transactions Current: the account that we credit on incoming transactions and debit on outgoing transactions (this is the user's balance) Outgoing: the acount we credit on outgoing transactions Fees: the account we credit for fees
This commit is contained in:
@@ -2,6 +2,7 @@ package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
"github.com/getAlby/lndhub.go/lib/security"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/gommon/random"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
const alphaNumBytes = random.Alphanumeric
|
||||
@@ -39,9 +41,25 @@ func (CreateUserController) CreateUser(c echo.Context) error {
|
||||
hashedPassword := security.HashPassword(password)
|
||||
user.Password = hashedPassword
|
||||
|
||||
if _, err := db.NewInsert().Model(&user).Exec(context.TODO()); err != nil {
|
||||
err := db.RunInTx(context.TODO(), &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error {
|
||||
if _, err := tx.NewInsert().Model(&user).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
accountTypes := []string{"incoming", "current", "outgoing", "fees"}
|
||||
for _, accountType := range accountTypes {
|
||||
account := models.Account{UserID: user.ID, Type: accountType}
|
||||
if _, err := db.NewInsert().Model(&account).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// Was the DB transaction successful?
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var ResponseBody struct {
|
||||
Login string `json:"login"`
|
||||
Password string `json:"password"`
|
||||
|
||||
Reference in New Issue
Block a user