chore: build
ci-api / test (push) Failing after 8m7s
ci-web / test (push) Failing after 5m5s

This commit is contained in:
Xor290
2026-09-20 12:18:33 +02:00
parent 8f4c7fa47a
commit 919c807004
174 changed files with 9669 additions and 1308 deletions
+54 -10
View File
@@ -2,19 +2,34 @@ package users
import (
"context"
"errors"
"fmt"
"github.com/google/uuid"
"backend/internal/platform/config"
"backend/internal/platform/security"
)
type Service struct {
repo Repository
// ErrCustomerAccountsDisabled is returned by Create when trying to create a
// customer-role user while neither customer login nor registration is
// enabled -- such an account would have no way to sign in.
var ErrCustomerAccountsDisabled = errors.New("customer accounts are disabled")
// AccountsGate lets the users module check the site-wide customer-accounts
// toggles without importing the site module directly.
type AccountsGate interface {
CustomerAccountsAvailable(ctx context.Context) (bool, error)
}
func NewService(repo Repository) *Service {
return &Service{repo: repo}
type Service struct {
repo Repository
gate AccountsGate
cfg config.SeedConfig
}
func NewService(repo Repository, gate AccountsGate, cfg config.SeedConfig) *Service {
return &Service{repo: repo, gate: gate, cfg: cfg}
}
func (s *Service) List(ctx context.Context) ([]*User, error) {
@@ -25,14 +40,41 @@ func (s *Service) FindByID(ctx context.Context, id uuid.UUID) (*User, error) {
return s.repo.FindByID(ctx, id)
}
func (s *Service) FindByEmail(ctx context.Context, email string) (*User, error) {
return s.repo.FindByEmail(ctx, email)
func (s *Service) CountByRole(ctx context.Context, role string) (int64, error) {
return s.repo.CountByRole(ctx, role)
}
func (s *Service) Create(ctx context.Context, email, password, role string) (*User, error) {
func (s *Service) FindByUsername(ctx context.Context, username string) (*User, error) {
return s.repo.FindByUsername(ctx, username)
}
func (s *Service) Create(ctx context.Context, username, password, role string) (*User, error) {
if role != RoleAdmin && role != RoleCustomer {
return nil, fmt.Errorf("invalid role %q", role)
}
if role == RoleAdmin {
count, err := s.repo.CountByRole(ctx, RoleAdmin)
if err != nil {
return nil, err
}
if count >= s.cfg.AdminNumber {
return nil, ErrAdminLimitReached
}
}
if role == RoleCustomer {
available, err := s.gate.CustomerAccountsAvailable(ctx)
if err != nil {
return nil, err
}
if !available {
return nil, ErrCustomerAccountsDisabled
}
}
hash, err := security.HashPassword(password)
if err != nil {
return nil, fmt.Errorf("hash password: %w", err)
@@ -40,24 +82,26 @@ func (s *Service) Create(ctx context.Context, email, password, role string) (*Us
user := &User{
ID: uuid.New(),
Email: email,
Username: username,
PasswordHash: hash,
Role: role,
IsActive: true,
}
if err := s.repo.Create(ctx, user); err != nil {
return nil, err
}
return user, nil
}
// UpdateProfile updates the mutable, non-security fields of a user.
func (s *Service) UpdateProfile(ctx context.Context, id uuid.UUID, email string, isActive bool) (*User, error) {
func (s *Service) UpdateProfile(ctx context.Context, id uuid.UUID, username string, isActive bool) (*User, error) {
user, err := s.repo.FindByID(ctx, id)
if err != nil {
return nil, err
}
user.Email = email
user.Username = username
user.IsActive = isActive
if err := s.repo.Update(ctx, user); err != nil {
return nil, err