customer/internal/service/history/history.go

96 lines
2.6 KiB
Go
Raw Normal View History

2023-05-23 15:33:23 +00:00
package history
2023-05-23 10:52:27 +00:00
import (
"context"
2023-11-05 06:37:57 +00:00
"go.mongodb.org/mongo-driver/bson"
2023-05-23 10:52:27 +00:00
"go.uber.org/zap"
2024-03-11 17:21:32 +00:00
"log"
2024-11-18 07:23:41 +00:00
"gitea.pena/PenaSide/customer/internal/errors"
"gitea.pena/PenaSide/customer/internal/fields"
"gitea.pena/PenaSide/customer/internal/models"
2023-05-23 10:52:27 +00:00
)
2023-11-05 06:37:57 +00:00
type GetHistories struct {
Pagination *models.Pagination
Type *string
2023-11-05 06:37:57 +00:00
UserID string
2023-11-05 06:37:57 +00:00
}
func (receiver *GetHistories) BSON() bson.M {
query := bson.M{
fields.History.IsDeleted: false,
fields.History.Type: *receiver.Type,
2024-02-04 20:20:52 +00:00
fields.History.UserID: receiver.UserID,
2023-11-05 06:37:57 +00:00
}
return query
}
2023-05-23 10:52:27 +00:00
type historyRepository interface {
2023-11-05 06:37:57 +00:00
CountAll(context.Context, *GetHistories) (int64, errors.Error)
FindMany(context.Context, *GetHistories) ([]models.History, errors.Error)
2023-05-23 10:52:27 +00:00
Insert(context.Context, *models.History) (*models.History, errors.Error)
GetRecentTariffs(context.Context, string) ([]models.TariffID, errors.Error) // new
2023-12-01 11:52:06 +00:00
GetHistoryByID(context.Context, string) (*models.ReportHistory, errors.Error)
2023-11-29 19:01:14 +00:00
GetDocNumber(context.Context, string) (map[string]int, errors.Error)
2023-12-22 15:12:43 +00:00
CalculateCustomerLTV(ctx context.Context, from, to int64) (int64, errors.Error)
2023-05-23 10:52:27 +00:00
}
type authClient interface {
GetUser(ctx context.Context, userID string) (*models.User, errors.Error)
}
type verificationClient interface {
2024-02-04 20:20:52 +00:00
GetVerification(ctx context.Context, userID string) (*models.Verification, errors.Error)
}
type temlategenClient interface {
2023-11-29 19:01:14 +00:00
SendData(ctx context.Context, data models.RespGeneratorService, fileContents []byte, email string) errors.Error
2023-05-23 10:52:27 +00:00
}
type Deps struct {
Logger *zap.Logger
Repository historyRepository
AuthClient authClient
VerificationClient verificationClient
TemlategenClient temlategenClient
2023-05-23 10:52:27 +00:00
}
type Service struct {
logger *zap.Logger
repository historyRepository
AuthClient authClient
VerificationClient verificationClient
TemlategenClient temlategenClient
2023-05-23 10:52:27 +00:00
}
2023-06-13 19:20:11 +00:00
func New(deps Deps) *Service {
2023-05-23 10:52:27 +00:00
if deps.Logger == nil {
log.Panicln("logger is nil on <New (history service)>")
}
if deps.Repository == nil {
log.Panicln("repository is nil on <New (history service)>")
}
if deps.AuthClient == nil {
log.Panicln("auth client is nil on <New (account service)>")
}
2023-05-23 10:52:27 +00:00
return &Service{
logger: deps.Logger,
repository: deps.Repository,
AuthClient: deps.AuthClient,
2023-05-23 10:52:27 +00:00
}
}
func (receiver *Service) CreateHistory(ctx context.Context, history *models.History) (*models.History, errors.Error) {
createdHistory, err := receiver.repository.Insert(ctx, history)
if err != nil {
receiver.logger.Error("failed to create history on <CreateHistory> of <HistoryService>", zap.Error(err))
return nil, err
}
return createdHistory, nil
}