customer/internal/interface/controller/rest/history/history.go

136 lines
3.7 KiB
Go
Raw Normal View History

2023-06-22 09:36:43 +00:00
package history
import (
2023-11-20 22:07:13 +00:00
"fmt"
2023-06-22 09:36:43 +00:00
"log"
"net/http"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/swagger"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
2023-11-05 06:37:57 +00:00
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/history"
2023-06-22 09:36:43 +00:00
)
type Deps struct {
Logger *zap.Logger
2023-11-05 06:37:57 +00:00
HistoryService *history.Service
2023-06-22 09:36:43 +00:00
}
type Controller struct {
logger *zap.Logger
2023-11-05 06:37:57 +00:00
historyService *history.Service
2023-06-22 09:36:43 +00:00
}
func New(deps Deps) *Controller {
if deps.Logger == nil {
log.Panicln("logger is nil on <New (history controller)>")
}
if deps.HistoryService == nil {
log.Panicln("HistoryService is nil on <New (history controller)>")
}
return &Controller{
logger: deps.Logger,
historyService: deps.HistoryService,
}
}
func (receiver *Controller) GetHistoryList(ctx echo.Context, params swagger.GetHistoryParams) error {
2023-12-20 12:42:41 +00:00
var userID string
2023-11-20 22:07:13 +00:00
2023-12-20 12:42:41 +00:00
if params.AccountID != nil && *params.AccountID != "" {
userID = *params.AccountID
} else {
userID, _ = ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
2023-11-20 22:07:13 +00:00
}
2023-11-05 06:37:57 +00:00
histories, err := receiver.historyService.GetHistoryList(ctx.Request().Context(), &history.GetHistories{
2023-11-20 22:07:13 +00:00
Type: params.Type,
UserID: userID,
2023-09-14 10:07:28 +00:00
Pagination: &models.Pagination{
Page: int64(*params.Page),
Limit: int64(*params.Limit),
},
2023-06-22 09:36:43 +00:00
})
if err != nil {
receiver.logger.Error("failed to get histories on <GetHistoryList> of <HistoryController>", zap.Error(err))
return errors.HTTP(ctx, err)
}
return ctx.JSON(http.StatusOK, histories)
}
2023-11-22 17:31:17 +00:00
// TODO:tests.
2023-11-22 17:31:17 +00:00
func (receiver *Controller) GetRecentTariffs(ctx echo.Context) error {
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <GetRecentTariffs> of <HistoryController>")
return errors.HTTP(ctx, errors.New(
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
errors.ErrInvalidArgs,
))
}
tariffs, err := receiver.historyService.GetRecentTariffs(ctx.Request().Context(), userID)
if err != nil {
receiver.logger.Error("failed to get recent tariffs on <GetRecentTariffs> of <HistoryController>",
zap.String("userId", userID),
zap.Error(err),
)
return errors.HTTP(ctx, err)
}
return ctx.JSON(http.StatusOK, tariffs)
}
2023-11-25 18:28:26 +00:00
// TODO:tests.
func (receiver *Controller) SendReport(ctx echo.Context) error {
2023-11-25 18:28:26 +00:00
historyID := ctx.Param("id")
2023-12-01 11:27:44 +00:00
err := receiver.historyService.GetHistoryByID(ctx.Request().Context(), historyID)
2023-11-25 18:28:26 +00:00
if err != nil {
receiver.logger.Error("failed to send report on <SendReport> of <HistoryController>", zap.Error(err))
2023-11-25 18:28:26 +00:00
return errors.HTTP(ctx, err)
}
return ctx.NoContent(http.StatusOK)
2023-11-25 18:28:26 +00:00
}
2023-12-22 15:12:43 +00:00
// TODO:tests.
func (receiver *Controller) CalculateLTV(ctx echo.Context) error {
var req swagger.CalculateLTVJSONBody
if err := ctx.Bind(&req); err != nil {
receiver.logger.Error("failed to bind request", zap.Error(err))
return errors.HTTP(ctx, errors.New(
fmt.Errorf("failed to bind request: %s", err),
errors.ErrInvalidArgs,
))
}
if req.From > req.To && req.To != 0 {
receiver.logger.Error("From timestamp must be less than To timestamp unless To is 0")
return errors.HTTP(ctx, errors.New(
fmt.Errorf("From timestamp must be less than To timestamp unless To is 0"),
errors.ErrInvalidArgs,
))
}
ltv, err := receiver.historyService.CalculateCustomerLTV(ctx.Request().Context(), req.From, req.To)
if err != nil {
receiver.logger.Error("failed to calculate LTV", zap.Error(err))
return errors.HTTP(ctx, err)
}
response := struct {
LTV int64 `json:"LTV"`
}{
LTV: ltv,
}
return ctx.JSON(http.StatusOK, response)
}