Skip to content

Commit

Permalink
#update: add telebot alert transaction status
Browse files Browse the repository at this point in the history
  • Loading branch information
tdatIT committed Mar 4, 2024
1 parent 9c4f5e8 commit 08ed663
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 4 deletions.
Binary file modified build/server_app_win.exe
Binary file not shown.
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct {
AdapterService AdapterService
RabbitMQ RabbitMQ
CronJob CronJob
Notify Notify
}

type Server struct {
Expand Down Expand Up @@ -58,6 +59,11 @@ type DB struct {
Mongodb Mongodb
}

type Notify struct {
Token string
GroupAlertID int64
}

type Mongodb struct {
DbName string
Username string
Expand Down
6 changes: 5 additions & 1 deletion config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ AdapterService:
InternalKey:

CronJob:
CheckingTxStatus: "@every 5m"
CheckingTxStatus: "@every 5m"

Notify:
Token:
GroupAlertID: -4122205761
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ require (
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible // indirect
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
github.com/gofiber/adaptor/v2 v2.2.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
Expand All @@ -38,9 +40,11 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/hashstructure v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
Expand All @@ -53,6 +57,8 @@ require (
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/tucnak/telebot v2.0.0+incompatible // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.50.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
Expand Down
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
Expand All @@ -27,6 +28,10 @@ github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ
github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-resty/resty/v2 v2.11.0 h1:i7jMfNOJYMp69lq7qozJP+bjgzfAzeOhuGlyDrqxT/8=
github.com/go-resty/resty/v2 v2.11.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/gofiber/adaptor/v2 v2.2.1 h1:givE7iViQWlsTR4Jh7tB4iXzrlKBgiraB/yTdHs9Lv4=
github.com/gofiber/adaptor/v2 v2.2.1/go.mod h1:AhR16dEqs25W2FY/l8gSj1b51Azg5dtPDmm+pruNOrc=
github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ=
Expand All @@ -50,6 +55,9 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
Expand All @@ -71,12 +79,19 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/mitchellh/hashstructure v1.1.0 h1:P6P1hdjqAAknpY/M1CGipelZgp+4y9ja9kmUZPXP+H0=
github.com/mitchellh/hashstructure v1.1.0/go.mod h1:xUDAozZz0Wmdiufv0uyhnHkUTN6/6d8ulp4AwfLKrmA=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8=
Expand Down Expand Up @@ -122,6 +137,10 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
github.com/tucnak/telebot v2.0.0+incompatible h1:Amnb+h23aEnfKSDqFKU/R1qGSGgnS78Hm56lLVVQL2A=
github.com/tucnak/telebot v2.0.0+incompatible/go.mod h1:TCLoYDyssqVcjhkdyYu+He6eldK40im537vXoex2LM0=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M=
Expand Down Expand Up @@ -217,6 +236,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 2 additions & 0 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"latipe-transaction-service/internal/domain/repos"
"latipe-transaction-service/internal/publisher"
"latipe-transaction-service/internal/service"
"latipe-transaction-service/internal/service/notifyserv"
"latipe-transaction-service/internal/subscriber"
"latipe-transaction-service/internal/subscriber/cancelPurchase"
"latipe-transaction-service/internal/subscriber/createPurchase"
Expand Down Expand Up @@ -68,6 +69,7 @@ func New() (*Server, error) {
mongodb.Set,
rabbitclient.Set,
repos.Set,
notifyserv.Set,
service.Set,
adapter.Set,
handler.Set,
Expand Down
5 changes: 5 additions & 0 deletions internal/service/notifyserv/inject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package notifyserv

import "github.com/google/wire"

var Set = wire.NewSet(NewTelegramBot)
7 changes: 7 additions & 0 deletions internal/service/notifyserv/notify_serv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notifyserv

import "latipe-transaction-service/internal/domain/entities"

type NotifyService interface {
SendMessageToTelegram(trans *entities.TransactionLog, reason string) error
}
51 changes: 51 additions & 0 deletions internal/service/notifyserv/telegram_bot_notify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package notifyserv

import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/gofiber/fiber/v2/log"
"latipe-transaction-service/config"
"latipe-transaction-service/internal/domain/entities"
"time"
)

type telegramBot struct {
cfg *config.Config
bot *tgbotapi.BotAPI
}

func NewTelegramBot(cfg *config.Config) NotifyService {
bot, err := tgbotapi.NewBotAPI(cfg.Notify.Token)
if err != nil {
log.Panic(err)
}

return &telegramBot{
cfg: cfg,
bot: bot,
}
}

func (t telegramBot) SendMessageToTelegram(trans *entities.TransactionLog, reason string) error {
log.Info("Sending message to telegram")

transactionHeader := "Transaction is failed"
if trans.TransactionStatus == entities.TX_SUCCESS {
transactionHeader = "Transaction is finished"
}

date := time.Now().Format("2006-01-02 15:04:05")
detailURL := fmt.Sprintf("http://localhost:5020/api/v1/transaction/detail/%s", trans.ID.Hex())
text := fmt.Sprintf("%s\n\nOrderID: %s\nStatus: %v\nReason: %s\nDate: %s\n\n %s",
transactionHeader, trans.OrderID, trans.TransactionStatus, reason, date, detailURL)

msg := tgbotapi.NewMessage(t.cfg.Notify.GroupAlertID, text)
msg.ParseMode = "markdown"

_, err := t.bot.Send(msg)
if err != nil {
return err
}

return nil
}
20 changes: 18 additions & 2 deletions internal/service/orderserv/orderserv_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"latipe-transaction-service/internal/domain/message"
"latipe-transaction-service/internal/domain/repos"
msgqueue "latipe-transaction-service/internal/publisher/createPurchase"
"latipe-transaction-service/internal/service/notifyserv"
redisCache "latipe-transaction-service/pkgs/cache/redis"
"strings"
"sync"
Expand All @@ -16,15 +17,18 @@ import (

type orderService struct {
transactionRepo repos.TransactionRepository
notifyServ notifyserv.NotifyService
transactionOrchestrator *msgqueue.OrderOrchestratorPub
cacheEngine *redisCache.CacheEngine
}

func NewOrderService(transRepos repos.TransactionRepository, orchestrator *msgqueue.OrderOrchestratorPub,
notifyService notifyserv.NotifyService,
cacheEngine *redisCache.CacheEngine) OrderService {
return &orderService{
transactionRepo: transRepos,
transactionOrchestrator: orchestrator,
notifyServ: notifyService,
cacheEngine: cacheEngine,
}
}
Expand Down Expand Up @@ -241,7 +245,6 @@ func (o orderService) HandleTransactionPurchaseReply(ctx context.Context, msg *m
// Update order transaction status and reply purchase message to order
// then delete the cache message
if count == len(trans.Commits)-1 {

errChan := make(chan error, 3)

var wg sync.WaitGroup
Expand Down Expand Up @@ -278,6 +281,12 @@ func (o orderService) HandleTransactionPurchaseReply(ctx context.Context, msg *m
log.Errorf("committing transaction is failed: %v", err)
}
log.Infof(" purchase transaction is committed [%v]", msg.OrderID)

err := o.notifyServ.SendMessageToTelegram(trans, "success")
if err != nil {
log.Error(err)
}

return nil
}

Expand All @@ -286,10 +295,10 @@ func (o orderService) HandleTransactionPurchaseReply(ctx context.Context, msg *m
ServiceName: MappingServiceName(serviceType),
TxStatus: entities.TX_FAIL,
}

if err := o.rollbackPurchaseToService(&commit, trans.OrderID); err != nil {
return err
}

}

}
Expand All @@ -306,6 +315,13 @@ func (o orderService) HandleTransactionPurchaseReply(ctx context.Context, msg *m
}

trans.TransactionStatus = entities.TX_FAIL

reason := fmt.Sprintf("Service failed:%s", MappingServiceName(serviceType))
err := o.notifyServ.SendMessageToTelegram(trans, reason)
if err != nil {
log.Error(err)
}

if err := o.transactionRepo.UpdateTransactionStatus(ctx, trans); err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion internal/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 08ed663

Please sign in to comment.