generated from PenaSide/GolangTemplate
add loging events
This commit is contained in:
parent
b43aad5209
commit
472fdd84dc
@ -98,11 +98,18 @@ func (receiver *AccountController) Get(ctx *fiber.Ctx) error {
|
||||
return receiver.middleWare.NoAuth(ctx)
|
||||
}
|
||||
|
||||
hlogger := receiver.middleWare.ExtractLogger(ctx)
|
||||
|
||||
account, err := receiver.accountRepo.FindByUserID(ctx.Context(), userID)
|
||||
if err != nil {
|
||||
return receiver.middleWare.ErrorOld(ctx, err)
|
||||
}
|
||||
|
||||
hlogger.Emit(models.InfoGetAccount{
|
||||
CtxUserID: userID,
|
||||
CtxAccountID: account.ID,
|
||||
})
|
||||
|
||||
return ctx.Status(fiber.StatusOK).JSON(account)
|
||||
}
|
||||
|
||||
@ -112,6 +119,8 @@ func (receiver *AccountController) Create(ctx *fiber.Ctx) error {
|
||||
return receiver.middleWare.NoAuth(ctx)
|
||||
}
|
||||
|
||||
hlogger := receiver.middleWare.ExtractLogger(ctx)
|
||||
|
||||
var er error
|
||||
|
||||
quizFrom := ctx.Cookies("quizFrom")
|
||||
@ -143,6 +152,23 @@ func (receiver *AccountController) Create(ctx *fiber.Ctx) error {
|
||||
return receiver.middleWare.ErrorOld(ctx, err)
|
||||
}
|
||||
|
||||
quiz := ""
|
||||
if quizFrom != "" {
|
||||
quiz = "quiz"
|
||||
}
|
||||
|
||||
hlogger.Emit(models.InfoCreateAccount{
|
||||
CtxUserID: userID,
|
||||
CtxAccountID: account.ID,
|
||||
KeyFromSource: quiz,
|
||||
KeyFromID: quizFrom,
|
||||
KeyFromPartner: quizUser,
|
||||
CtxLogin: user.Login,
|
||||
CtxEmail: user.Email,
|
||||
CtxPhone: user.PhoneNumber,
|
||||
KeyCurrency: account.Wallet.Currency,
|
||||
})
|
||||
|
||||
return ctx.Status(fiber.StatusOK).JSON(account)
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils"
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils/transfer"
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/validate"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@ -83,6 +84,8 @@ func (receiver *CartController) Add2cart(ctx *fiber.Ctx) error {
|
||||
return receiver.middleWare.NoAuth(ctx)
|
||||
}
|
||||
|
||||
hlogger := receiver.middleWare.ExtractLogger(ctx)
|
||||
|
||||
tariffID := ctx.Query("id")
|
||||
if tariffID == "" {
|
||||
return receiver.middleWare.Error(ctx, fiber.StatusBadRequest, "empty item id")
|
||||
@ -102,6 +105,12 @@ func (receiver *CartController) Add2cart(ctx *fiber.Ctx) error {
|
||||
return receiver.middleWare.ErrorOld(ctx, err)
|
||||
}
|
||||
|
||||
hlogger.Emit(models.InfoAddToCart{
|
||||
CtxUserID: userID,
|
||||
CtxAccountID: cartItems.ID,
|
||||
CtxTariffID: tariffID,
|
||||
})
|
||||
|
||||
return ctx.Status(fiber.StatusOK).JSON(cartItems)
|
||||
}
|
||||
|
||||
@ -116,6 +125,8 @@ func (receiver *CartController) Pay(ctx *fiber.Ctx) error {
|
||||
return receiver.middleWare.NoAuth(ctx)
|
||||
}
|
||||
|
||||
hlogger := receiver.middleWare.ExtractLogger(ctx)
|
||||
|
||||
account, err := receiver.accountRepo.FindByUserID(ctx.Context(), userID)
|
||||
if err != nil {
|
||||
return receiver.middleWare.ErrorOld(ctx, err)
|
||||
@ -158,6 +169,16 @@ func (receiver *CartController) Pay(ctx *fiber.Ctx) error {
|
||||
}))
|
||||
|
||||
if account.Wallet.Money < int64(discountResponse.Price) {
|
||||
hlogger.Emit(models.InfoPayCart{
|
||||
CtxUserID: userID,
|
||||
CtxAccountID: account.ID,
|
||||
KeySuccess: false,
|
||||
CtxPrice: discountResponse.Price - uint64(account.Wallet.Money),
|
||||
CtxTariff: strings.Join(account.Cart, ","),
|
||||
CtxDiscount: strings.Join(utils.GetAppliedDiscountsIDs(discountResponse.AppliedDiscounts), ","),
|
||||
CtxRowPrice: tariffsAmount,
|
||||
CtxRowData: utils.MarshalRawDetails(models.RawDetails{Tariffs: tariffs, Price: int64(discountResponse.Price)}),
|
||||
})
|
||||
return receiver.middleWare.Error(ctx, fiber.StatusPaymentRequired, "insufficient funds: %d", int64(discountResponse.Price)-account.Wallet.Money)
|
||||
}
|
||||
|
||||
@ -269,5 +290,16 @@ func (receiver *CartController) Pay(ctx *fiber.Ctx) error {
|
||||
|
||||
updatedAccount.Cart = []string{}
|
||||
|
||||
hlogger.Emit(models.InfoPayCart{
|
||||
CtxUserID: userID,
|
||||
CtxAccountID: updatedAccount.ID,
|
||||
KeySuccess: true,
|
||||
CtxPrice: discountResponse.Price,
|
||||
CtxTariff: strings.Join(account.Cart, ","),
|
||||
CtxDiscount: strings.Join(utils.GetAppliedDiscountsIDs(discountResponse.AppliedDiscounts), ","),
|
||||
CtxRowPrice: tariffsAmount,
|
||||
CtxRowData: utils.MarshalRawDetails(models.RawDetails{Tariffs: tariffs, Price: int64(discountResponse.Price)}),
|
||||
})
|
||||
|
||||
return ctx.Status(fiber.StatusOK).JSON(updatedAccount)
|
||||
}
|
||||
|
@ -12,7 +12,9 @@ import (
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
|
||||
codeword_rpc "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/proto/codeword"
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/history"
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -164,6 +166,8 @@ func (receiver *HistoryController) GetRecentTariffs(ctx *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func (receiver *HistoryController) SendReport(ctx *fiber.Ctx) error {
|
||||
hlogger := receiver.middleWare.ExtractLogger(ctx)
|
||||
|
||||
var req struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
@ -279,20 +283,17 @@ func (receiver *HistoryController) SendReport(ctx *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
hlogger.Emit(models.InfoReportRequest{
|
||||
CtxUserID: tariffs.UserID,
|
||||
CtxAccountID: account.ID,
|
||||
CtxID: req.Id,
|
||||
CtxTariff: strings.Join(utils.GetTariffsIDs(tariffs.RawDetails.Tariffs), ","),
|
||||
CtxOrgName: account.Name.Orgname,
|
||||
})
|
||||
|
||||
return ctx.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
||||
type QuizLogoStat2 struct {
|
||||
Count int64 `json:"count,omitempty"`
|
||||
Items map[string]Item `json:"items,omitempty"`
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
Money int64 `json:"money,omitempty"`
|
||||
Quizes map[string][2]int `json:"quizes,omitempty"`
|
||||
Regs int `json:"regs,omitempty"`
|
||||
}
|
||||
|
||||
func (receiver *HistoryController) QuizLogoStat(ctx *fiber.Ctx) error {
|
||||
var req struct {
|
||||
From *int `json:"from,omitempty"`
|
||||
|
@ -55,7 +55,7 @@ func (mw *MiddleWare) GetHealth(ctx *fiber.Ctx) error {
|
||||
return ctx.Status(fiber.StatusOK).SendString("OK")
|
||||
}
|
||||
|
||||
func (mw *MiddleWare) ExtractLogger(ctx *fiber.Ctx) (hlog.Logger, bool) {
|
||||
logger, ok := ctx.Context().UserValue(models.LoggerKey).(hlog.Logger)
|
||||
return logger, ok
|
||||
func (mw *MiddleWare) ExtractLogger(ctx *fiber.Ctx) hlog.Logger {
|
||||
logger := ctx.Context().UserValue(models.LoggerKey).(hlog.Logger)
|
||||
return logger
|
||||
}
|
||||
|
@ -59,6 +59,8 @@ func (receiver *WalletController) RequestMoney(ctx *fiber.Ctx) error {
|
||||
return receiver.middleWare.NoAuth(ctx)
|
||||
}
|
||||
|
||||
hlogger := receiver.middleWare.ExtractLogger(ctx)
|
||||
|
||||
var request models.GetPaymentLinkBody
|
||||
if err := ctx.BodyParser(&request); err != nil {
|
||||
return receiver.middleWare.Error(ctx, fiber.StatusBadRequest, "failed to bind payment link")
|
||||
@ -77,6 +79,16 @@ func (receiver *WalletController) RequestMoney(ctx *fiber.Ctx) error {
|
||||
return receiver.middleWare.ErrorOld(ctx, err)
|
||||
}
|
||||
|
||||
hlogger.Emit(models.InfoRequestMoney{
|
||||
CtxUserID: userID,
|
||||
// todo
|
||||
CtxAccountID: "",
|
||||
KeyPaymentType: string(request.Type),
|
||||
KeyCurrency: request.Currency,
|
||||
CtxPrice: request.Amount,
|
||||
CtxReturnURL: link,
|
||||
})
|
||||
|
||||
return ctx.Status(fiber.StatusOK).JSON(&models.GetPaymentLinkResponse{Link: link})
|
||||
}
|
||||
|
||||
@ -270,6 +282,8 @@ func (receiver *WalletController) PostWalletRspay(ctx *fiber.Ctx) error {
|
||||
return receiver.middleWare.NoAuth(ctx)
|
||||
}
|
||||
|
||||
hlogger := receiver.middleWare.ExtractLogger(ctx)
|
||||
|
||||
var req struct {
|
||||
Money *float32 `json:"money,omitempty"`
|
||||
}
|
||||
@ -310,5 +324,12 @@ func (receiver *WalletController) PostWalletRspay(ctx *fiber.Ctx) error {
|
||||
return receiver.middleWare.ErrorOld(ctx, err)
|
||||
}
|
||||
|
||||
hlogger.Emit(models.InfoRSPay{
|
||||
CtxUserID: userID,
|
||||
CtxAccountID: user.ID,
|
||||
CtxPrice: int64(*req.Money),
|
||||
CtxLogin: authData.Login,
|
||||
})
|
||||
|
||||
return ctx.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
@ -47,29 +47,29 @@ type InfoCreateAccount struct {
|
||||
|
||||
type InfoGetAccount struct {
|
||||
CtxUserID string //айдишник юзера из токена
|
||||
CtxAccountID string // айдишник свежесозданного аккаунта
|
||||
CtxAccountID string // айдишник аккаунта
|
||||
}
|
||||
|
||||
type InfoAddToCart struct {
|
||||
CtxUserID string //айдишник юзера из токена
|
||||
CtxAccountID string // айдишник свежесозданного аккаунта
|
||||
CtxAccountID string // айдишник аккаунта
|
||||
CtxTariffID string //айдишник тарифа, добавленного в корзину
|
||||
}
|
||||
|
||||
type InfoPayCart struct {
|
||||
CtxUserID string //айдишник юзера из токена
|
||||
CtxAccountID string // айдишник свежесозданного аккаунта
|
||||
CtxAccountID string // айдишник аккаунта
|
||||
KeySuccess bool // получилось оплатить или не хватило денег
|
||||
CtxPrice int64 // сумма в копейках. если не удалось оплатить - записать сколько денег не хватило
|
||||
CtxPrice uint64 // сумма в копейках. если не удалось оплатить - записать сколько денег не хватило
|
||||
CtxTariff string // через запятую список покупаемых тарифов
|
||||
CtxDiscount string // через запятую список применённых скидок
|
||||
CtxRowPrice int64 // стоимость без скидок
|
||||
CtxRowPrice uint64 // стоимость без скидок
|
||||
CtxRowData string // замаршаленные данные, которые обычно складываются в RawDetails
|
||||
}
|
||||
|
||||
type InfoRequestMoney struct {
|
||||
CtxUserID string //айдишник юзера из токена
|
||||
CtxAccountID string // айдишник свежесозданного аккаунта
|
||||
CtxAccountID string // айдишник аккаунта
|
||||
KeyPaymentType string //направление оплаты, через которое оплачиваем
|
||||
KeyCurrency string //значение валюты кошелька. сейчас там фиксированное, но потом пригодится
|
||||
CtxPrice int64 // сумма в копейках
|
||||
@ -86,7 +86,7 @@ type InfoMoneyIncome struct {
|
||||
|
||||
type InfoReportRequest struct {
|
||||
CtxUserID string //айдишник юзера из токена
|
||||
CtxAccountID string // айдишник свежесозданного аккаунта
|
||||
CtxAccountID string // айдишник аккаунта
|
||||
CtxID string //айдишник истории, по которой создаётся акт
|
||||
CtxTariff string // через запятую список покупаемых тарифов
|
||||
CtxOrgName string // orgname
|
||||
@ -94,7 +94,7 @@ type InfoReportRequest struct {
|
||||
|
||||
type InfoRSPay struct {
|
||||
CtxUserID string //айдишник юзера из токена
|
||||
CtxAccountID string // айдишник свежесозданного аккаунта
|
||||
CtxAccountID string // айдишник аккаунта
|
||||
CtxPrice int64 // сумма в копейках
|
||||
CtxLogin string // значение логина. мы там получаем его из сервиса авторизации
|
||||
}
|
||||
|
31
internal/utils/hlog_helpers.go
Normal file
31
internal/utils/hlog_helpers.go
Normal file
@ -0,0 +1,31 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/proto/discount"
|
||||
)
|
||||
|
||||
func GetAppliedDiscountsIDs(appliedDiscounts []*discount.Discount) []string {
|
||||
discounts := make([]string, len(appliedDiscounts))
|
||||
for i, discount := range appliedDiscounts {
|
||||
discounts[i] = discount.ID
|
||||
}
|
||||
return discounts
|
||||
}
|
||||
|
||||
func MarshalRawDetails(details models.RawDetails) string {
|
||||
data, err := json.Marshal(details)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func GetTariffsIDs(tariffs []models.Tariff) []string {
|
||||
result := make([]string, len(tariffs))
|
||||
for i, tariff := range tariffs {
|
||||
result[i] = tariff.ID
|
||||
}
|
||||
return result
|
||||
}
|
Loading…
Reference in New Issue
Block a user