feat: patment notifications

This commit is contained in:
skeris 2024-05-09 21:14:53 +03:00
parent 0378b1f0f2
commit 8ce3723653
8 changed files with 75 additions and 3 deletions

@ -45,6 +45,9 @@ services:
- JWT_AUDIENCE=pena
- PUBLIC_KEY=$USER_PKEY
- PRIVATE_KEY=$USER_PRIVATEKEY
- ADMIN_FRONT_URL=https://admin.pena
- NOTIFICATION_CHANNEL=-1002017928045
- NOTIFICATION_BOT_TOKEN=6414077478:AAFk03HezovLT2kO_i9OYswH8Weirsgp9GU
ports:
- 10.8.0.8:8065:8065
- 10.8.0.8:9065:9065

2
go.mod

@ -49,6 +49,7 @@ require (
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/tealeg/xlsx v1.0.5 // indirect
@ -70,5 +71,6 @@ require (
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto v0.0.0-20240221002015-b0ce06bbee7c // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c // indirect
gopkg.in/tucnak/telebot.v2 v2.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

4
go.sum

@ -107,6 +107,7 @@ github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pioz/faker v1.7.3 h1:Tez8Emuq0UN+/d6mo3a9m/9ZZ/zdfJk0c5RtRatrceM=
github.com/pioz/faker v1.7.3/go.mod h1:xSpay5w/oz1a6+ww0M3vfpe40pSIykeUPeWEc3TvVlc=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@ -125,6 +126,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
@ -288,6 +290,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/tucnak/telebot.v2 v2.5.0 h1:i+NynLo443Vp+Zn3Gv9JBjh3Z/PaiKAQwcnhNI7y6Po=
gopkg.in/tucnak/telebot.v2 v2.5.0/go.mod h1:BgaIIx50PSRS9pG59JH+geT82cfvoJU/IaI5TJdN3v8=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

@ -19,6 +19,7 @@ import (
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/closer"
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/kafka"
qutils "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/utils"
tb "gopkg.in/tucnak/telebot.v2"
)
const (
@ -89,12 +90,28 @@ func Run(config *models.Config, logger *zap.Logger) (appErr error) {
MongoDB: mongoDB,
})
notificationBot, err := tb.NewBot(tb.Settings{
Token: config.Service.NotificationBotToken,
Verbose: false,
ParseMode: tb.ModeHTML,
Poller: &tb.LongPoller{
Timeout: time.Second,
},
})
if err != nil {
//logger.Emit(json.Token(err))
return err
}
services := initialize.NewServices(initialize.ServicesDeps{
Logger: logger,
Repositories: repositories,
Clients: clients,
ConfigurationGRPC: &config.GRPC,
Brokers: brokers,
Notifier: notificationBot,
NotificationChannel: config.Service.NotificationChannel,
AdminURL: config.Service.AdminURL,
})
controllers := initialize.NewControllers(initialize.ControllersDeps{
@ -109,7 +126,8 @@ func Run(config *models.Config, logger *zap.Logger) (appErr error) {
encrypt := qutils.NewEncrypt(config.Service.PubKey, config.Service.PrivKey)
api := swagger.NewAPI2(logger, mongoDB, config, brokers.TariffConsumer, brokers.TariffProducer, encrypt)
api := swagger.NewAPI2(logger, mongoDB, config, brokers.TariffConsumer, brokers.TariffProducer, encrypt, notificationBot, config.Service.NotificationChannel, config.Service.AdminURL)
serverHTTP, httpErr := server.NewHTTP(server.DepsHTTP{
Logger: logger,

@ -7,6 +7,7 @@ import (
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/callback"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/history"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/wallet"
tb "gopkg.in/tucnak/telebot.v2"
)
type ServicesDeps struct {
@ -15,6 +16,9 @@ type ServicesDeps struct {
Clients *Clients
Brokers *Brokers
ConfigurationGRPC *models.ConfigurationGRPC
Notifier *tb.Bot
NotificationChannel int64
AdminURL string
}
type Services struct {
@ -55,6 +59,9 @@ func NewServices(deps ServicesDeps) *Services {
AccountRepository: deps.Repositories.AccountRepository,
WalletService: walletService,
HistoryService: historyService,
Notifier: deps.Notifier,
NotifyChannel: deps.NotificationChannel,
AdminURL: deps.AdminURL,
}),
TariffBrokerService: tariffBrokerService,
}

@ -27,6 +27,7 @@ import (
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils/transfer"
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/echotools"
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/validate"
tb "gopkg.in/tucnak/telebot.v2"
)
const defaultCurrency = "RUB" // TODO move
@ -41,6 +42,9 @@ type API2 struct {
clients clients
grpc models.ConfigurationGRPC
encrypt *qutils.Encrypt
notifier *tb.Bot
notifyChannel int64
adminURL string
}
type clients struct {
@ -57,7 +61,7 @@ type clients struct {
var _ ServerInterface = (*API2)(nil)
func NewAPI2(logger *zap.Logger, db *mongo.Database, config *models.Config, consumer *tariff.Consumer, producer *tariff.Producer, encrypt *qutils.Encrypt) API2 {
func NewAPI2(logger *zap.Logger, db *mongo.Database, config *models.Config, consumer *tariff.Consumer, producer *tariff.Producer, encrypt *qutils.Encrypt, notfier *tb.Bot, channel int64, adminURL string) API2 {
return API2{
logger: logger,
history: repository.NewHistoryRepository2(logger, db.Collection("histories")),
@ -66,7 +70,10 @@ func NewAPI2(logger *zap.Logger, db *mongo.Database, config *models.Config, cons
consumer: consumer,
producer: producer,
encrypt: encrypt,
notifier: notfier,
grpc: config.GRPC,
notifyChannel: channel,
adminURL: adminURL,
clients: clients{
auth: client.NewAuthClient(client.AuthClientDeps{Logger: logger, URLs: &config.Service.AuthMicroservice.URL}),
hubadmin: client.NewHubadminClient(client.HubadminClientDeps{Logger: logger, URLs: &config.Service.HubadminMicroservice.URL}),
@ -492,6 +499,19 @@ func (api *API2) PayCart(ctx echo.Context) error {
if _, err := api.account.ClearCart(ctx.Request().Context(), account.UserID); err != nil {
return api.errorOld(ctx, err)
}
go func () {
var privilegesdesc string
for _, tariff := range tariffs {
for _, privilege := range tariff.Privileges {
privilegesdesc += privilege.Name + " " + fmt.Sprint(privilege.Amount) + "\n"
}
}
if _, err := api.notifier.Send(tb.ChatID(api.notifyChannel), fmt.Sprintf(`Куплена корзина за %.2f, пользователем %s
куплены:
%s`,float64(discountResponse.Price/100), api.adminURL + "/users/" + account.UserID,privilegesdesc)); err != nil {
fmt.Println("TBOER", err)
}
}()
updatedAccount.Cart = []string{}

@ -39,6 +39,9 @@ type ServiceConfiguration struct {
Mail MailConfiguration
PubKey string `env:"PUBLIC_KEY"`
PrivKey string `env:"PRIVATE_KEY"`
NotificationBotToken string `env:"NOTIFICATION_BOT_TOKEN"`
NotificationChannel int64 `env:"NOTIFICATION_CHANNEL"`
AdminURL string `env:"ADMIN_FRONT_URL"`
}
type KafkaConfiguration struct {

@ -3,10 +3,12 @@ package callback
import (
"context"
"log"
"fmt"
"go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
tb "gopkg.in/tucnak/telebot.v2"
)
type accountRepository interface {
@ -26,6 +28,9 @@ type PaymentCallbackServiceDeps struct {
AccountRepository accountRepository
WalletService walletService
HistoryService historyService
Notifier *tb.Bot
NotifyChannel int64
AdminURL string
}
type PaymentCallbackService struct {
@ -33,6 +38,9 @@ type PaymentCallbackService struct {
accountRepository accountRepository
walletService walletService
historyService historyService
notifier *tb.Bot
notifyChannel int64
adminURL string
}
func NewPaymentCallbackService(deps PaymentCallbackServiceDeps) *PaymentCallbackService {
@ -57,6 +65,9 @@ func NewPaymentCallbackService(deps PaymentCallbackServiceDeps) *PaymentCallback
accountRepository: deps.AccountRepository,
walletService: deps.WalletService,
historyService: deps.HistoryService,
notifier: deps.Notifier,
notifyChannel: deps.NotifyChannel,
adminURL: deps.AdminURL,
}
}
@ -96,7 +107,11 @@ func (receiver *PaymentCallbackService) SuccessEvent(ctx context.Context, event
return err
}
//}()
go func () {
if _, err := receiver.notifier.Send(tb.ChatID(receiver.notifyChannel), fmt.Sprintf(`Внесены деньги %.2f, пользователем %s`,float64(event.Amount/100), receiver.adminURL + "/users/" + account.UserID)); err != nil {
fmt.Println("TBOER", err)
}
}()
return nil
}