diff --git a/internal/interface/swagger/api.2.go b/internal/interface/swagger/api.2.go index 77c3c46..75c0666 100644 --- a/internal/interface/swagger/api.2.go +++ b/internal/interface/swagger/api.2.go @@ -4,6 +4,7 @@ import ( "fmt" "math" "net/http" + "os" "sync" "time" @@ -43,6 +44,8 @@ type clients struct { currency *client.CurrencyClient discount *client.DiscountClient payment *client.PaymentClient + verify *client.VerificationClient + template *client.TemplateClient } var _ ServerInterface = (*API2)(nil) @@ -594,17 +597,150 @@ func (api *API2) ChangeCurrency(ctx echo.Context) error { return ctx.JSON(http.StatusOK, updatedAccount) } +// todo need tests func (api *API2) CalculateLTV(ctx echo.Context) error { - //TODO implement me - panic("implement me") + var req CalculateLTVJSONBody + + if err := ctx.Bind(&req); err != nil { + api.logger.Error("failed to bind request", zap.Error(err)) + return api.error(ctx, http.StatusBadRequest, "failed to bind request") + } + + if req.From > req.To && req.To != 0 { + api.logger.Error("From timestamp must be less than To timestamp unless To is 0") + return api.error(ctx, http.StatusBadRequest, "From timestamp must be less than To timestamp unless To is 0") + } + + ltv, err := api.history.CalculateCustomerLTV(ctx.Request().Context(), req.From, req.To) + if err != nil { + api.logger.Error("failed to calculate LTV", zap.Error(err)) + return api.errorOld(ctx, err) + } + + response := struct { + LTV int64 `json:"LTV"` + }{ + LTV: ltv, + } + + return ctx.JSON(http.StatusOK, response) } func (api *API2) GetRecentTariffs(ctx echo.Context) error { - //TODO implement me - panic("implement me") + userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) + if !ok { + api.logger.Error("failed to convert jwt payload to string on of ") + return api.error(ctx, http.StatusBadRequest, "failed to convert jwt payload to string") + } + + if userID == "" { + api.logger.Error("user id is missing in of ") + return api.error(ctx, http.StatusBadRequest, "user id is missing") + } + + tariffs, err := api.history.GetRecentTariffs(ctx.Request().Context(), userID) + if err != nil { + api.logger.Error("failed to get recent tariffs on of ", + zap.String("userId", userID), + zap.Error(err), + ) + return api.errorOld(ctx, err) + } + + return ctx.JSON(http.StatusOK, tariffs) } func (api *API2) SendReport(ctx echo.Context) error { - //TODO implement me - panic("implement me") + historyID := ctx.Param("id") + if historyID == "" { + api.logger.Error("history id is missing in of ") + return api.error(ctx, http.StatusBadRequest, "history id is missing") + } + + tariffs, err := api.history.GetHistoryByID(ctx.Request().Context(), historyID) + if err != nil { + api.logger.Error( + "failed to get history by id in of ", + zap.String("historyID", historyID), + zap.Error(err), + ) + return api.errorOld(ctx, err) + } + + if tariffs.Key != models.CustomerHistoryKeyPayCart { + api.logger.Error( + "invalid history record key", + zap.String("historyID", historyID), + zap.Error(err), + ) + return api.error(ctx, http.StatusBadRequest, "invalid history record key") + } + + historyMap, err := api.history.GetDocNumber(ctx.Request().Context(), tariffs.UserID) + if err != nil { + api.logger.Error( + "failed to get history of sorting by date created in of ", + zap.String("historyID", historyID), + zap.Error(err), + ) + return api.errorOld(ctx, err) + } + + verifuser, err := api.clients.verify.GetVerification(ctx.Request().Context(), tariffs.UserID) + if err != nil { + api.logger.Error("failed to get user verification on of ", + zap.Error(err), + zap.String("userID", tariffs.UserID), + ) + return api.errorOld(ctx, err) + } + if !verifuser.Accepted { + api.logger.Error( + "verification not accepted", + zap.String("userID", tariffs.UserID), + zap.Error(err), + ) + return api.error(ctx, http.StatusBadRequest, "verification not accepted") + } + + authuser, err := api.clients.auth.GetUser(ctx.Request().Context(), tariffs.UserID) + if err != nil { + api.logger.Error("failed to get user on of ", + zap.Error(err), + zap.String("userID", tariffs.UserID), + ) + return api.errorOld(ctx, err) + } + + fileContents, readerr := os.ReadFile("./report.docx") + if readerr != nil { + return api.error(ctx, http.StatusInternalServerError, "failed to read file") + } + + for _, tariff := range tariffs.RawDetails.Tariffs { + totalAmount := uint64(0) + for _, privilege := range tariff.Privileges { + totalAmount += privilege.Amount + } + data := models.RespGeneratorService{ + DocNumber: historyMap[historyID] + 1, + 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 = api.clients.template.SendData(ctx.Request().Context(), data, fileContents, authuser.Email) + if err != nil { + api.logger.Error("failed to send report to user on of ", + zap.Error(err), + zap.String("userID", tariffs.UserID), + ) + return api.errorOld(ctx, err) + } + } + + return ctx.NoContent(http.StatusOK) } diff --git a/internal/interface/swagger/api.gen.go b/internal/interface/swagger/api.gen.go index 7d5fb62..796ccd6 100644 --- a/internal/interface/swagger/api.gen.go +++ b/internal/interface/swagger/api.gen.go @@ -411,10 +411,10 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL router.PATCH(baseURL+"/account/:userId", wrapper.SetAccountVerificationStatus) router.GET(baseURL+"/accounts", wrapper.PaginationAccounts) router.DELETE(baseURL+"/cart", wrapper.RemoveFromCart)//- - router.PATCH(baseURL+"/cart", wrapper.Add2cart) - router.POST(baseURL+"/cart/pay", wrapper.PayCart) + router.PATCH(baseURL+"/cart", wrapper.Add2cart)//+ + router.POST(baseURL+"/cart/pay", wrapper.PayCart)//+ router.GET(baseURL+"/currencies", wrapper.GetCurrencies) - router.PUT(baseURL+"/currencies", wrapper.UpdateCurrencies) + router.PUT(baseURL+"/currencies", wrapper.UpdateCurrencies)//- router.GET(baseURL+"/history", wrapper.GetHistory) router.POST(baseURL+"/history/ltv", wrapper.CalculateLTV) router.GET(baseURL+"/recent", wrapper.GetRecentTariffs) diff --git a/tests/e2e/сurrencies_test.go b/tests/e2e/сurrencies_test.go new file mode 100644 index 0000000..a2adec6 --- /dev/null +++ b/tests/e2e/сurrencies_test.go @@ -0,0 +1,34 @@ +package e2e_test + +import ( + "context" + "fmt" + "github.com/stretchr/testify/assert" + "net/http" + "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" + "penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/client" + "testing" +) + +func TestCurrencies(t *testing.T) { + t.Run("Получение текущих доступных курсов", func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + assert.NotPanics(t, func() { + responseGetCurrencies, errCurrencies := client.Get[[]models.CurrencyList, models.ResponseErrorHTTP](ctx, &client.RequestSettings{ + URL: "http://localhost:8000/currencies", + }) + if isNoError := assert.NoError(t, errCurrencies); !isNoError { + return + } + if isNoRequestError := assert.Nil(t, responseGetCurrencies.Error); !isNoRequestError { + return + } + + fmt.Println(responseGetCurrencies.Body) + assert.Equal(t, http.StatusOK, responseGetCurrencies.StatusCode) + }) + }) + +}