customer/internal/service/history/history.go

246 lines
6.7 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"
"fmt"
"log"
"math"
2023-11-29 19:01:14 +00:00
"os"
"time"
2023-05-23 10:52:27 +00:00
"go.uber.org/zap"
2023-09-14 10:02:32 +00:00
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/dto"
2023-05-23 10:52:27 +00:00
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
)
type historyRepository interface {
2023-09-14 23:01:53 +00:00
CountAll(context.Context, *dto.GetHistories) (int64, errors.Error)
2023-09-14 10:02:32 +00:00
FindMany(context.Context, *dto.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-11-25 18:28:26 +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-05-23 10:52:27 +00:00
}
type authClient interface {
GetUser(ctx context.Context, userID string) (*models.User, errors.Error)
}
type verificationClient interface {
GetUser(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
}
}
2023-09-14 10:02:32 +00:00
func (receiver *Service) GetHistoryList(ctx context.Context, dto *dto.GetHistories) (*models.PaginationResponse[models.History], errors.Error) {
if dto == nil {
2023-05-23 10:52:27 +00:00
return nil, errors.New(
fmt.Errorf("pagination is nil on <GetHistoryList> of <HistoryService>: %w", errors.ErrInternalError),
errors.ErrInternalError,
)
}
2023-09-14 23:01:53 +00:00
count, err := receiver.repository.CountAll(ctx, dto)
2023-05-23 10:52:27 +00:00
if err != nil {
receiver.logger.Error("failed to count histories on <GetHistoryList> of <HistoryService>",
2023-06-13 22:41:33 +00:00
zap.Error(err),
2023-05-23 10:52:27 +00:00
)
return nil, err
}
if count == 0 {
return &models.PaginationResponse[models.History]{TotalPages: 0, Records: []models.History{}}, nil
}
2023-09-14 10:02:32 +00:00
totalPages := int64(math.Ceil(float64(count) / float64(dto.Pagination.Limit)))
2023-05-23 10:52:27 +00:00
2023-09-14 10:02:32 +00:00
histories, err := receiver.repository.FindMany(ctx, dto)
2023-05-23 10:52:27 +00:00
if err != nil {
receiver.logger.Error("failed to get historiy list on <GetHistoryList> of <HistoryService>",
2023-06-13 22:41:33 +00:00
zap.Error(err),
2023-05-23 10:52:27 +00:00
)
return nil, err
}
return &models.PaginationResponse[models.History]{
TotalPages: totalPages,
Records: histories,
}, nil
}
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
}
2023-11-22 17:31:17 +00:00
// TODO:tests.
2023-11-23 18:55:22 +00:00
func (receiver *Service) GetRecentTariffs(ctx context.Context, userID string) ([]models.TariffID, errors.Error) {
2023-11-22 17:31:17 +00:00
if userID == "" {
receiver.logger.Error("user id is missing in <GetRecentTariffs> of <HistoryService>")
return nil, errors.New(
fmt.Errorf("user id is missing: %w", errors.ErrInvalidArgs),
errors.ErrInvalidArgs,
)
}
tariffs, err := receiver.repository.GetRecentTariffs(ctx, userID)
if err != nil {
receiver.logger.Error(
"failed to get recent tariffs in <GetRecentTariffs> of <HistoryService>",
zap.String("userId", userID),
zap.Error(err),
)
return nil, err
}
return tariffs, nil
}
2023-11-25 18:28:26 +00:00
2023-12-01 11:27:44 +00:00
func (receiver *Service) GetHistoryByID(ctx context.Context, historyID string) errors.Error {
2023-11-25 18:28:26 +00:00
if historyID == "" {
receiver.logger.Error("history id is missing in <GetHistoryById> of <HistoryService>")
return errors.New(
2023-11-25 18:28:26 +00:00
fmt.Errorf("history id is missing: %w", errors.ErrInvalidArgs),
errors.ErrInvalidArgs,
)
}
tariffs, err := receiver.repository.GetHistoryById(ctx, historyID)
if err != nil {
receiver.logger.Error(
"failed to get history by id in <GetHistoryById> of <HistoryService>",
2023-11-25 18:28:26 +00:00
zap.String("historyID", historyID),
zap.Error(err),
)
return err
2023-11-25 18:28:26 +00:00
}
if tariffs.Key != models.CustomerHistoryKeyPayCart {
receiver.logger.Error(
"invalid history record key",
zap.String("historyID", historyID),
zap.Error(err),
)
return err
}
2023-12-01 09:24:38 +00:00
historyMap, err := receiver.repository.GetDocNumber(ctx, tariffs.UserID)
2023-11-29 19:01:14 +00:00
if err != nil {
receiver.logger.Error(
"failed to get history of sorting by date created in <GetDocNumber> of <HistoryService>",
zap.String("historyID", historyID),
zap.Error(err),
)
return err
}
verifuser, err := receiver.VerificationClient.GetUser(ctx, tariffs.UserID)
if err != nil {
receiver.logger.Error("failed to get user verification on <GetHistoryById> of <HistoryService>",
zap.Error(err),
zap.String("userID", tariffs.UserID),
)
return err
}
if !verifuser.Accepted {
receiver.logger.Error(
"verification not accepted",
zap.String("userID", tariffs.UserID),
zap.Error(err),
)
return err
}
authuser, err := receiver.AuthClient.GetUser(ctx, tariffs.UserID)
if err != nil {
receiver.logger.Error("failed to get user on <GetHistoryById> of <HistoryService>",
zap.Error(err),
zap.String("userID", tariffs.UserID),
)
return err
}
2023-11-29 19:01:14 +00:00
fileContents, readerr := os.ReadFile("./report.docx")
if readerr != nil {
return errors.New(
fmt.Errorf("failed to read file: %w", errors.ErrInternalError),
errors.ErrInternalError,
)
2023-11-29 19:01:14 +00:00
}
2023-11-29 19:01:14 +00:00
for _, tariff := range tariffs.RawDetails.Tariffs {
totalAmount := uint64(0)
for _, privilege := range tariff.Privileges {
totalAmount += privilege.Amount
}
data := models.RespGeneratorService{
2023-12-01 09:24:38 +00:00
DocNumber: historyMap[historyID] + 1,
2023-11-29 19:01:14 +00:00
Date: time.Now().Format("2006-01-02"),
OrgTaxNum: verifuser.TaxNumber,
OrgName: models.Name{Orgname: "Orgname"},
Name: tariff.Name,
Amount: totalAmount,
Price: tariffs.RawDetails.Price,
Sum: tariffs.RawDetails.Price,
}
err = receiver.TemlategenClient.SendData(ctx, data, fileContents, authuser.Email)
if err != nil {
receiver.logger.Error("failed to send report to user on <GetHistoryById> of <HistoryService>",
zap.Error(err),
zap.String("userID", tariffs.UserID),
)
return err
}
}
return nil
2023-11-25 18:28:26 +00:00
}