feat: history details

This commit is contained in:
Kirill 2023-09-10 14:54:41 +00:00 committed by Mikhail
parent 19f3fcbe69
commit f31b22c97d
5 changed files with 58 additions and 34 deletions

@ -7,7 +7,7 @@ type History struct {
UserID string `json:"userId" bson:"userId"` UserID string `json:"userId" bson:"userId"`
Comment string `json:"comment" bson:"comment"` Comment string `json:"comment" bson:"comment"`
Key string `json:"key" bson:"key"` Key string `json:"key" bson:"key"`
RawDetails string `json:"rawDetails" bson:"rawDetails"` RawDetails any `json:"rawDetails" bson:"rawDetails"`
Deleted bool `json:"isDeleted" bson:"isDeleted"` Deleted bool `json:"isDeleted" bson:"isDeleted"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`

@ -2,6 +2,7 @@ package callback
import ( import (
"context" "context"
"fmt"
"log" "log"
"go.uber.org/zap" "go.uber.org/zap"
@ -82,22 +83,26 @@ func (receiver *PaymentCallbackService) SuccessEvent(ctx context.Context, event
return err return err
} }
if _, err := receiver.historyService.CreateHistory(ctx, &models.History{ go func() {
UserID: account.UserID, if _, err := receiver.historyService.CreateHistory(ctx, &models.History{
Comment: event.Message, UserID: account.UserID,
Key: event.Key, Comment: event.Message,
}); err != nil { Key: event.Key,
receiver.logger.Error("failed to create history on <SuccessEvent> of <PaymentCallbackService>", zap.Error(err)) RawDetails: fmt.Sprintf("%d%s", event.Amount, event.Currency),
} }); err != nil {
receiver.logger.Error("failed to create history on <SuccessEvent> of <PaymentCallbackService>", zap.Error(err))
}
}()
return nil return nil
} }
func (receiver *PaymentCallbackService) FailureEvent(ctx context.Context, event *models.PaymentEvent) errors.Error { func (receiver *PaymentCallbackService) FailureEvent(ctx context.Context, event *models.PaymentEvent) errors.Error {
if _, err := receiver.historyService.CreateHistory(ctx, &models.History{ if _, err := receiver.historyService.CreateHistory(ctx, &models.History{
UserID: event.UserID, UserID: event.UserID,
Comment: event.Message, Comment: event.Message,
Key: event.Key, Key: event.Key,
RawDetails: fmt.Sprintf("%d%s", event.Amount, event.Currency),
}); err != nil { }); err != nil {
receiver.logger.Error("failed to create history on <FailureEvent> of <PaymentCallbackService>", zap.Error(err)) receiver.logger.Error("failed to create history on <FailureEvent> of <PaymentCallbackService>", zap.Error(err))
return err return err

@ -186,13 +186,16 @@ func (receiver *Service) Pay(ctx context.Context, accessToken string, userID str
return nil, err return nil, err
} }
if _, historyErr := receiver.historyService.CreateHistory(ctx, &models.History{ go func(tariffs []models.Tariff) {
Key: models.CustomerHistoryKeyPayCart, if _, historyErr := receiver.historyService.CreateHistory(ctx, &models.History{
UserID: account.UserID, Key: models.CustomerHistoryKeyPayCart,
Comment: "Успешная оплата корзины", UserID: account.UserID,
}); historyErr != nil { Comment: "Успешная оплата корзины",
receiver.logger.Error("failed to insert history on <Pay> of <CartService>", zap.Error(historyErr)) RawDetails: utils.TranslateTariffsArrayToMapPair(tariffs),
} }); historyErr != nil {
receiver.logger.Error("failed to insert history on <Pay> of <CartService>", zap.Error(historyErr))
}
}(tariffs)
// TODO: обработать ошибки при отправке сообщений // TODO: обработать ошибки при отправке сообщений

@ -2,6 +2,7 @@ package wallet
import ( import (
"context" "context"
"fmt"
"log" "log"
"go.uber.org/zap" "go.uber.org/zap"
@ -97,19 +98,21 @@ func (receiver *Service) ReplenishAccountWallet(ctx context.Context, request *mo
zap.String("Currency", request.Account.Wallet.Currency), zap.String("Currency", request.Account.Wallet.Currency),
zap.Int64("Money", request.Account.Wallet.Money+request.Cash), zap.Int64("Money", request.Account.Wallet.Money+request.Cash),
zap.Int64("Cash", request.Account.Wallet.Cash+cash), zap.Int64("Cash", request.Account.Wallet.Cash+cash),
zap.Bool("Is currensy equal internal", request.Currency == models.InternalCurrencyKey),
) )
return nil, changeErr return nil, changeErr
} }
if _, historyErr := receiver.historyService.CreateHistory(ctx, &models.History{ go func() {
Key: models.CustomerHistoryKeyReplenish, if _, historyErr := receiver.historyService.CreateHistory(ctx, &models.History{
UserID: request.Account.UserID, Key: models.CustomerHistoryKeyReplenish,
Comment: "Успешное пополнение средств (Без конвертации валюты)", UserID: request.Account.UserID,
}); historyErr != nil { Comment: "Успешное пополнение средств (Без конвертации валюты)",
receiver.logger.Error("failed to insert history on <ReplenishAccountWallet> of <WalletService>", zap.Error(historyErr)) RawDetails: fmt.Sprintf("%d%s", cash, models.InternalCurrencyKey),
} }); historyErr != nil {
receiver.logger.Error("failed to insert history on <ReplenishAccountWallet> of <WalletService>", zap.Error(historyErr))
}
}()
return updatedAccount, nil return updatedAccount, nil
} }
@ -136,7 +139,7 @@ func (receiver *Service) ReplenishAccountWallet(ctx context.Context, request *mo
receiver.logger.Error("failed to replenish wallet on <ReplenishAccountWallet> of <WalletService>", receiver.logger.Error("failed to replenish wallet on <ReplenishAccountWallet> of <WalletService>",
zap.Error(err), zap.Error(err),
zap.String("Currency", request.Account.Wallet.Currency), zap.String("Currency", request.Account.Wallet.Currency),
zap.Int64("Money", request.Account.Wallet.Money+request.Cash), zap.Int64("Money", request.Account.Wallet.Money+money),
zap.Int64("Cash", request.Account.Wallet.Cash+cash), zap.Int64("Cash", request.Account.Wallet.Cash+cash),
zap.Bool("Is currensy equal internal", request.Currency == models.InternalCurrencyKey), zap.Bool("Is currensy equal internal", request.Currency == models.InternalCurrencyKey),
) )
@ -144,13 +147,16 @@ func (receiver *Service) ReplenishAccountWallet(ctx context.Context, request *mo
return nil, err return nil, err
} }
if _, historyErr := receiver.historyService.CreateHistory(ctx, &models.History{ go func() {
Key: models.CustomerHistoryKeyReplenish, if _, historyErr := receiver.historyService.CreateHistory(ctx, &models.History{
UserID: request.Account.UserID, Key: models.CustomerHistoryKeyReplenish,
Comment: "Успешное пополнение средств (C конвертацией валюты)", UserID: request.Account.UserID,
}); historyErr != nil { Comment: "Успешное пополнение средств (C конвертацией валюты)",
receiver.logger.Error("failed to insert history on <ReplenishAccountWallet> of <WalletService>", zap.Error(historyErr)) RawDetails: fmt.Sprintf("%d%s", cash, request.Currency),
} }); historyErr != nil {
receiver.logger.Error("failed to insert history on <ReplenishAccountWallet> of <WalletService>", zap.Error(historyErr))
}
}()
return updatedAccount, nil return updatedAccount, nil
} }

@ -23,3 +23,13 @@ func CalculateCartPurchasesAmount(tariffs []models.Tariff) uint64 {
return sum return sum
} }
func TranslateTariffsArrayToMapPair(tariffs []models.Tariff) map[string]string {
pair := make(map[string]string, len(tariffs))
for _, tariff := range tariffs {
pair[tariff.Name] = tariff.ID
}
return pair
}