Files
smart-search-back/internal/service/user.go
vallyenfail ff08bb2254
All checks were successful
Deploy Smart Search Backend Test / deploy (push) Successful in 2m25s
add service
2026-01-19 19:14:51 +03:00

104 lines
2.5 KiB
Go

package service
import (
"context"
"git.techease.ru/Smart-search/smart-search-back/internal/model"
"git.techease.ru/Smart-search/smart-search-back/internal/repository"
"git.techease.ru/Smart-search/smart-search-back/pkg/crypto"
)
type userService struct {
userRepo repository.UserRepository
requestRepo repository.RequestRepository
tokenUsageRepo repository.TokenUsageRepository
cryptoHelper *crypto.Crypto
}
type UserInfo struct {
Email string
Name string
Phone string
CompanyName string
PaymentStatus string
}
type Statistics struct {
RequestsCount int
SuppliersCount int
CreatedTZ int
}
type BalanceStatistics struct {
AverageCost float64
WriteOffHistory []*model.WriteOffHistory
}
func NewUserService(userRepo repository.UserRepository, requestRepo repository.RequestRepository, tokenUsageRepo repository.TokenUsageRepository, cryptoSecret string) UserService {
return &userService{
userRepo: userRepo,
requestRepo: requestRepo,
tokenUsageRepo: tokenUsageRepo,
cryptoHelper: crypto.NewCrypto(cryptoSecret),
}
}
func (s *userService) GetInfo(ctx context.Context, userID int) (*UserInfo, error) {
user, err := s.userRepo.FindByID(ctx, userID)
if err != nil {
return nil, err
}
email, err := s.cryptoHelper.Decrypt(user.Email)
if err != nil {
return nil, err
}
phone, err := s.cryptoHelper.Decrypt(user.Phone)
if err != nil {
return nil, err
}
userName, err := s.cryptoHelper.Decrypt(user.UserName)
if err != nil {
return nil, err
}
return &UserInfo{
Email: email,
Name: userName,
Phone: phone,
CompanyName: user.CompanyName,
PaymentStatus: user.PaymentStatus,
}, nil
}
func (s *userService) GetBalance(ctx context.Context, userID int) (float64, error) {
return s.userRepo.GetBalance(ctx, userID)
}
func (s *userService) GetStatistics(ctx context.Context, userID int) (*Statistics, error) {
requestsCount, suppliersCount, createdTZ, err := s.requestRepo.GetUserStatistics(ctx, userID)
if err != nil {
return nil, err
}
return &Statistics{
RequestsCount: requestsCount,
SuppliersCount: suppliersCount,
CreatedTZ: createdTZ,
}, nil
}
func (s *userService) GetBalanceStatistics(ctx context.Context, userID int) (*BalanceStatistics, error) {
averageCost, history, err := s.tokenUsageRepo.GetBalanceStatistics(ctx, userID)
if err != nil {
return nil, err
}
return &BalanceStatistics{
AverageCost: averageCost,
WriteOffHistory: history,
}, nil
}