2024-05-24 17:44:24 +00:00
|
|
|
package users
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/emochka2007/block-accounting/internal/pkg/hdwallet"
|
|
|
|
"github.com/emochka2007/block-accounting/internal/pkg/models"
|
2024-05-25 13:00:21 +00:00
|
|
|
"github.com/emochka2007/block-accounting/internal/usecase/interactors/chain"
|
2024-05-24 17:44:24 +00:00
|
|
|
"github.com/emochka2007/block-accounting/internal/usecase/repository/users"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrorUsersNotFound = errors.New("users not found")
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreateParams struct {
|
|
|
|
Name string
|
|
|
|
Email string
|
|
|
|
Phone string
|
|
|
|
Tg string
|
|
|
|
Mnemonic string
|
|
|
|
Activate bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetParams struct {
|
|
|
|
Ids uuid.UUIDs
|
|
|
|
OrganizationId uuid.UUID
|
|
|
|
Mnemonic string
|
|
|
|
Seed []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteParams struct {
|
|
|
|
Id uuid.UUID
|
|
|
|
OrganizationId uuid.UUID
|
|
|
|
}
|
|
|
|
|
|
|
|
type ActivateParams struct {
|
|
|
|
Id uuid.UUID
|
|
|
|
OrganizationId uuid.UUID
|
|
|
|
}
|
|
|
|
|
|
|
|
type UsersInteractor interface {
|
|
|
|
Create(ctx context.Context, params CreateParams) (*models.User, error)
|
|
|
|
Update(ctx context.Context, newState models.User) error
|
|
|
|
Activate(ctx context.Context, params ActivateParams) error
|
|
|
|
Get(ctx context.Context, params GetParams) ([]*models.User, error)
|
|
|
|
Delete(ctx context.Context, params DeleteParams) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type usersInteractor struct {
|
2024-05-25 13:00:21 +00:00
|
|
|
log *slog.Logger
|
|
|
|
usersRepo users.Repository
|
|
|
|
chainInteractor chain.ChainInteractor
|
2024-05-24 17:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewUsersInteractor(
|
|
|
|
log *slog.Logger,
|
|
|
|
usersRepo users.Repository,
|
2024-05-25 13:00:21 +00:00
|
|
|
chainInteractor chain.ChainInteractor,
|
2024-05-24 17:44:24 +00:00
|
|
|
) UsersInteractor {
|
|
|
|
return &usersInteractor{
|
2024-05-25 13:00:21 +00:00
|
|
|
log: log,
|
|
|
|
usersRepo: usersRepo,
|
|
|
|
chainInteractor: chainInteractor,
|
2024-05-24 17:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *usersInteractor) Create(ctx context.Context, params CreateParams) (*models.User, error) {
|
|
|
|
seed, err := hdwallet.NewSeedFromMnemonic(params.Mnemonic)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error convert mnemonic into a seed. %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user := models.NewUser(
|
|
|
|
uuid.Must(uuid.NewV7()),
|
|
|
|
seed,
|
|
|
|
params.Activate,
|
|
|
|
time.Now(),
|
|
|
|
)
|
|
|
|
|
|
|
|
user.Name = params.Name
|
2024-05-24 23:39:32 +00:00
|
|
|
user.Mnemonic = params.Mnemonic
|
2024-05-24 17:44:24 +00:00
|
|
|
|
|
|
|
user.Credentails = &models.UserCredentials{
|
|
|
|
Email: params.Email,
|
|
|
|
Phone: params.Phone,
|
|
|
|
Telegram: params.Tg,
|
|
|
|
}
|
|
|
|
|
2024-05-25 13:00:21 +00:00
|
|
|
pk, err := i.chainInteractor.PubKey(ctx, user)
|
|
|
|
if err != nil {
|
|
|
|
// todo пока мокнуть
|
|
|
|
return nil, fmt.Errorf("error fetch user pub key. %w", err)
|
|
|
|
}
|
2024-05-24 22:45:56 +00:00
|
|
|
|
2024-05-25 13:00:21 +00:00
|
|
|
user.PK = pk
|
2024-05-24 22:45:56 +00:00
|
|
|
|
2024-05-24 17:44:24 +00:00
|
|
|
if err = i.usersRepo.Create(ctx, user); err != nil {
|
|
|
|
return nil, fmt.Errorf("error create new user. %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *usersInteractor) Update(ctx context.Context, newState models.User) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *usersInteractor) Activate(ctx context.Context, params ActivateParams) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *usersInteractor) Get(ctx context.Context, params GetParams) ([]*models.User, error) {
|
|
|
|
users, err := i.usersRepo.Get(ctx, users.GetParams{
|
|
|
|
Ids: params.Ids,
|
|
|
|
OrganizationId: params.OrganizationId,
|
|
|
|
Seed: params.Seed,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error fetch users from repository. %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(users) == 0 {
|
|
|
|
return nil, fmt.Errorf("error empty users set. %w", ErrorUsersNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *usersInteractor) Delete(ctx context.Context, params DeleteParams) error {
|
|
|
|
return nil
|
|
|
|
}
|