From 03d105a431a1cfc51826fef7836d8a430e5b1aa7 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 11 Feb 2024 21:22:37 +0300 Subject: [PATCH] update --- assets/new_verification.txt | 1 + assets/updated_verification.txt | 1 + deployments/test/docker-compose.yaml | 2 - go.mod | 5 +- go.sum | 17 +-- internal/app/app_verification.go | 19 ++- internal/client/assets/new_verification.txt | 14 ++ .../client/assets/updated_verification.txt | 14 ++ internal/client/customer.go | 1 - internal/client/telegram.go | 66 ++++++--- internal/client/telegram_test.go | 70 ++++++++++ internal/config/config.go | 1 + internal/controllers/verification.go | 10 +- internal/models/telegram.go | 15 ++ internal/models/verification.go | 1 - internal/repository/verification.go | 129 +++++++++--------- internal/server/http.go | 14 +- staging.env | 18 +-- 18 files changed, 264 insertions(+), 134 deletions(-) create mode 100644 internal/client/assets/new_verification.txt create mode 100644 internal/client/assets/updated_verification.txt create mode 100644 internal/client/telegram_test.go create mode 100644 internal/models/telegram.go diff --git a/assets/new_verification.txt b/assets/new_verification.txt index 74d421a..80174ab 100644 --- a/assets/new_verification.txt +++ b/assets/new_verification.txt @@ -1,6 +1,7 @@ New verification: ID: {{ .ID }} UserID: {{ .UserID }} +UserURL: {{ .UserURL }} Accepted: {{ .Accepted }} Status: {{ .Status }} UpdatedAt: {{ .UpdatedAt.Format "Mon, 02 Jan 2006 15:04:05 MST"}} diff --git a/assets/updated_verification.txt b/assets/updated_verification.txt index cb693b9..dfd4ec5 100644 --- a/assets/updated_verification.txt +++ b/assets/updated_verification.txt @@ -1,6 +1,7 @@ Updated verification: ID: {{ .ID }} UserID: {{ .UserID }} +UserURL: {{ .UserURL }} Accepted: {{ .Accepted }} Status: {{ .Status }} UpdatedAt: {{ .UpdatedAt.Format "Mon, 02 Jan 2006 15:04:05 MST"}} diff --git a/deployments/test/docker-compose.yaml b/deployments/test/docker-compose.yaml index 19266ae..116c139 100644 --- a/deployments/test/docker-compose.yaml +++ b/deployments/test/docker-compose.yaml @@ -11,7 +11,5 @@ services: volumes: - mongo_data:/data/db - volumes: mongo_data: - diff --git a/go.mod b/go.mod index b1fd83a..5178ae5 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ toolchain go1.21.4 require ( github.com/caarlos0/env/v8 v8.0.0 github.com/go-playground/validator/v10 v10.14.1 + github.com/gofiber/contrib/fiberzap v1.0.2 github.com/gofiber/fiber/v2 v2.51.0 github.com/joho/godotenv v1.5.1 github.com/minio/minio-go/v7 v7.0.56 @@ -16,8 +17,7 @@ require ( ) require ( - github.com/benbjohnson/clock v1.3.0 // indirect - go.uber.org/goleak v1.1.12 // indirect + github.com/kr/pretty v0.1.0 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) @@ -45,7 +45,6 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.3 // indirect github.com/rs/xid v1.5.0 // indirect diff --git a/go.sum b/go.sum index 881c1ff..2eb8929 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,8 @@ github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+j github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= 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/contrib/fiberzap v1.0.2 h1:EQwhggtszVfIdBeXxN9Xrmld71es34Ufs+ef8VMqZxc= +github.com/gofiber/contrib/fiberzap v1.0.2/go.mod h1:jGO8BHU4gRI9U0JtM6zj2CIhYfgVmW5JxziN8NTgVwE= github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ= github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= @@ -105,7 +107,6 @@ github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6 github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= -github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk= go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo= @@ -119,34 +120,24 @@ go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95a go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= -golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -167,14 +158,10 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/app/app_verification.go b/internal/app/app_verification.go index 2db8d43..9bac02a 100644 --- a/internal/app/app_verification.go +++ b/internal/app/app_verification.go @@ -2,6 +2,7 @@ package app import ( "context" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "go.uber.org/zap" @@ -44,7 +45,7 @@ func Run(cfg *config.Config) { logger.Fatal("MongoClient", zap.Error(err)) } - minioClient, err := minio.New(cfg.S3Endpoint, &minio.Options{ + minioClient, err := minio.New("localhost:9001", &minio.Options{ Creds: credentials.NewStaticV4(cfg.S3AccessKeyID, cfg.S3SecretKey, ""), Secure: true, }) @@ -56,12 +57,18 @@ func Run(cfg *config.Config) { if err != nil { logger.Fatal("Repositories", zap.Error(err)) } - //tgBot, err := tgbotapi.NewBotAPI(cfg.TelegramToken) - //if err != nil { - // logger.Fatal("TelegramBotApi", zap.Error(err)) - //} - telegram := client.NewTelegram(logger, nil, cfg.TelegramChannelID) + tgBot, err := tgbotapi.NewBotAPI(cfg.TelegramToken) + if err != nil { + logger.Fatal("TelegramBotApi", zap.Error(err)) + } + + telegram := client.NewTelegram(client.Deps{ + Logger: logger, + Bot: tgBot, + ChatID: cfg.TelegramChannelID, + StagingURL: cfg.StagingURL, + }) cons := initialize.NewControllers(reps, telegram, client.NewCustomer(logger, cfg.CustomerSvcAddress)) httpSrv := server.NewHTTP(cfg, logger).Register(cons.List()...) diff --git a/internal/client/assets/new_verification.txt b/internal/client/assets/new_verification.txt new file mode 100644 index 0000000..80174ab --- /dev/null +++ b/internal/client/assets/new_verification.txt @@ -0,0 +1,14 @@ +New verification: +ID: {{ .ID }} +UserID: {{ .UserID }} +UserURL: {{ .UserURL }} +Accepted: {{ .Accepted }} +Status: {{ .Status }} +UpdatedAt: {{ .UpdatedAt.Format "Mon, 02 Jan 2006 15:04:05 MST"}} +Comment: {{ .Comment }} +============================= + Files +============================= +{{ range .Files }} +Название: {{ .Name }} ({{ .Url }}) +{{ end }} \ No newline at end of file diff --git a/internal/client/assets/updated_verification.txt b/internal/client/assets/updated_verification.txt new file mode 100644 index 0000000..dfd4ec5 --- /dev/null +++ b/internal/client/assets/updated_verification.txt @@ -0,0 +1,14 @@ +Updated verification: +ID: {{ .ID }} +UserID: {{ .UserID }} +UserURL: {{ .UserURL }} +Accepted: {{ .Accepted }} +Status: {{ .Status }} +UpdatedAt: {{ .UpdatedAt.Format "Mon, 02 Jan 2006 15:04:05 MST"}} +Comment: {{ .Comment }} +============================= + Files +============================= +{{ range .Files }} +Название: {{ .Name }} ({{ .Url }}) +{{ end }} \ No newline at end of file diff --git a/internal/client/customer.go b/internal/client/customer.go index 2cd66e1..f8985bb 100644 --- a/internal/client/customer.go +++ b/internal/client/customer.go @@ -18,7 +18,6 @@ func NewCustomer(logger *zap.Logger, address string) *Customer { } func (c *Customer) UpdateAccountVerification(userId, status string) (*models.RespUpdateVerificationStatus, error) { - fmt.Println(c.address) agent := fiber.Patch(fmt.Sprintf("%s/account/%s", c.address, userId)) agent.JSON(&models.ReqCreateVerification{Status: status}) diff --git a/internal/client/telegram.go b/internal/client/telegram.go index fd7fd83..94a2ffb 100644 --- a/internal/client/telegram.go +++ b/internal/client/telegram.go @@ -2,48 +2,82 @@ package client import ( "bytes" + _ "embed" + "fmt" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "go.uber.org/zap" "penahub.gitlab.yandexcloud.net/backend/verification/internal/models" "text/template" ) +//go:embed assets/new_verification.txt +var NewVerification string + +//go:embed assets/updated_verification.txt +var UpdatedVerification string + +type Deps struct { + Logger *zap.Logger + Bot *tgbotapi.BotAPI + ChatID int64 + StagingURL string +} + type Telegram struct { - logger *zap.Logger - bot *tgbotapi.BotAPI - chatID int64 + logger *zap.Logger + bot *tgbotapi.BotAPI + chatID int64 + stagingURL string } -func NewTelegram(logger *zap.Logger, bot *tgbotapi.BotAPI, chatID int64) *Telegram { - return &Telegram{logger: logger, bot: bot, chatID: chatID} +func NewTelegram(deps Deps) *Telegram { + return &Telegram{logger: deps.Logger, bot: deps.Bot, chatID: deps.ChatID, stagingURL: deps.StagingURL} } -func (t *Telegram) SendVerification(data *models.Verification, isUpdate bool) error { - tplPath := "assets/new_verification.txt" - +func (t *Telegram) SendVerification(data *models.Verification, url string, isUpdate bool) error { + var tplPath string if isUpdate { - tplPath = "assets/updated_verification.txt" + tplPath = UpdatedVerification + } else { + tplPath = NewVerification } - tpl, err := template.ParseFiles(tplPath) + var userURL string + if url == t.stagingURL { + userURL = fmt.Sprintf("https://sadmin.pena/users/%s", data.UserID) + } else { + userURL = fmt.Sprintf("https://admin.pena/users/%s", data.UserID) + } + + tpl, err := template.New("verification_template").Parse(tplPath) if err != nil { - return err + return fmt.Errorf("error parsing template: %w", err) } var text bytes.Buffer - err = tpl.Execute(&text, data) + toTG := models.ToTelegram{ + ID: data.ID, + UserID: data.UserID, + UserURL: userURL, + Accepted: data.Accepted, + Status: data.Status, + UpdatedAt: data.UpdatedAt, + Comment: data.Comment, + Files: data.Files, + TaxNumber: data.TaxNumber, + } + + err = tpl.Execute(&text, toTG) if err != nil { - t.logger.Error("ClientTelegram", zap.Error(err)) - return err + return fmt.Errorf("error executing template: %w", err) } msg := tgbotapi.NewMessage(t.chatID, text.String()) _, err = t.bot.Send(msg) if err != nil { - t.logger.Error("ClientTelegram", zap.Error(err)) - return err + return fmt.Errorf("error sending message: %w", err) } return nil diff --git a/internal/client/telegram_test.go b/internal/client/telegram_test.go new file mode 100644 index 0000000..ecee9a3 --- /dev/null +++ b/internal/client/telegram_test.go @@ -0,0 +1,70 @@ +package client + +import ( + "bytes" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "github.com/stretchr/testify/assert" + "go.uber.org/zap" + "penahub.gitlab.yandexcloud.net/backend/verification/internal/models" + "testing" + "text/template" + "time" +) + +func TestTelegram_SendVerification(t *testing.T) { + logger, _ := zap.NewDevelopment() + bot, _ := tgbotapi.NewBotAPI("6712573453:AAFddRtlsF4ZH977xZDexWj6DhqeJD66hYw") + chatID := int64(542073142) + + client := NewTelegram(Deps{Logger: logger, Bot: bot, ChatID: chatID, StagingURL: "https://sadmin.pena"}) + + t.Run("SendVerification-success", func(t *testing.T) { + data := &models.Verification{ + ID: "641b2d73e0e07a7e90b59616", + UserID: "test-user", + Accepted: false, + Status: "nko", + UpdatedAt: time.Now(), + Comment: "test", + Files: []models.VerificationFile{ + {Name: "file1.txt", Url: "http://test.com/file1.txt"}, + {Name: "file2.txt", Url: "http://test.com/file2.txt"}, + }, + } + + isUpdate := false + + tpl := template.Must(template.New("verification").Parse("Verification: {{.UserID}}")) + var text bytes.Buffer + err := tpl.Execute(&text, data) + assert.NoError(t, err) + + err = client.SendVerification(data, "https://admin.pena", isUpdate) + assert.NoError(t, err) + }) + + t.Run("SendVerification-success", func(t *testing.T) { + data := &models.Verification{ + ID: "641b2d73e0e07a7e90b59616", + UserID: "test-user", + Accepted: false, + Status: "nko", + UpdatedAt: time.Now(), + Comment: "test", + Files: []models.VerificationFile{ + {Name: "file1.txt", Url: "http://test.com/file1.txt"}, + {Name: "file2.txt", Url: "http://test.com/file2.txt"}, + }, + } + + isUpdate := true + + tpl := template.Must(template.New("verification").Parse("Verification: {{.UserID}}")) + var text bytes.Buffer + err := tpl.Execute(&text, data) + assert.NoError(t, err) + + err = client.SendVerification(data, "https://sadmin.pena", isUpdate) + assert.NoError(t, err) + }) +} diff --git a/internal/config/config.go b/internal/config/config.go index d050933..dc732a7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -19,6 +19,7 @@ type Config struct { S3AccessKeyID string `env:"S3_ACCESS_KEY_ID,required"` S3SecretKey string `env:"S3_SECRET_KEY,required"` CustomerSvcAddress string `env:"CUSTOMER_SVC_ADDRESS,required"` + StagingURL string `env:"STAGING_URL,required"` } func NewConfig(file ...string) (*Config, error) { diff --git a/internal/controllers/verification.go b/internal/controllers/verification.go index 4742f17..169a283 100644 --- a/internal/controllers/verification.go +++ b/internal/controllers/verification.go @@ -2,7 +2,6 @@ package controllers import ( "errors" - "fmt" "mime/multipart" "github.com/gofiber/fiber/v2" @@ -57,6 +56,7 @@ func (r *VerificationController) CreateVerification(c *fiber.Ctx) error { var req models.ReqCreateVerification userID := c.Params("userID") + baseURL := c.BaseURL() if userID == "" { userID = c.Locals("userID").(string) @@ -117,7 +117,7 @@ func (r *VerificationController) CreateVerification(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } - err = r.telegram.SendVerification(result, false) + err = r.telegram.SendVerification(result, baseURL, false) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } @@ -132,7 +132,6 @@ func (r *VerificationController) SetVerificationStatus(c *fiber.Ctx) error { if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } - fmt.Println(req) errValidate := validateStruct(&req) if errValidate != nil { return c.Status(fiber.StatusBadRequest).JSON(errValidate) @@ -146,8 +145,6 @@ func (r *VerificationController) SetVerificationStatus(c *fiber.Ctx) error { TaxNumber: req.TaxNumber, }) - fmt.Println("repository.Update") - if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } @@ -164,6 +161,7 @@ func (r *VerificationController) SetVerificationStatus(c *fiber.Ctx) error { func (r *VerificationController) SetVerificationFile(c *fiber.Ctx) error { userID := c.Params("userID") + baseURL := c.BaseURL() if userID == "" { userID = c.Locals("userID").(string) @@ -194,7 +192,7 @@ func (r *VerificationController) SetVerificationFile(c *fiber.Ctx) error { } } - err = r.telegram.SendVerification(result, true) + err = r.telegram.SendVerification(result, baseURL, true) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } diff --git a/internal/models/telegram.go b/internal/models/telegram.go new file mode 100644 index 0000000..d662d60 --- /dev/null +++ b/internal/models/telegram.go @@ -0,0 +1,15 @@ +package models + +import "time" + +type ToTelegram struct { + ID string + UserID string + UserURL string + Accepted bool + Status string + UpdatedAt time.Time + Comment string + Files []VerificationFile + TaxNumber string +} diff --git a/internal/models/verification.go b/internal/models/verification.go index ffbdf78..84530d0 100644 --- a/internal/models/verification.go +++ b/internal/models/verification.go @@ -19,7 +19,6 @@ type VerificationFile struct { } type VerificationUpdate struct { - UserID string `json:"userID" bson:"user_id,omitempty"` Accepted bool `json:"accepted" bson:"accepted"` Status string `json:"status" bson:"status,omitempty"` UpdatedAt time.Time `json:"updated_at" bson:"updated_at"` diff --git a/internal/repository/verification.go b/internal/repository/verification.go index 9c7d698..588125c 100644 --- a/internal/repository/verification.go +++ b/internal/repository/verification.go @@ -2,8 +2,11 @@ package repository import ( "context" + "encoding/json" "errors" "fmt" + "github.com/minio/minio-go/v7/pkg/policy" + "github.com/minio/minio-go/v7/pkg/set" "mime/multipart" "strings" "time" @@ -38,67 +41,67 @@ func NewVerificationRepository(logger *zap.Logger, mongoDb *mongo.Database, s3 * } func (r *VerificationRepository) Init(ctx context.Context) error { - //ok, err := r.s3.BucketExists(ctx, VerificationBucket) - //if r.err(err) { - // return err - //} - // - //if !ok { - // err = r.s3.MakeBucket(ctx, VerificationBucket, minio.MakeBucketOptions{ObjectLocking: false}) - // if r.err(err) { - // return err - // } - // - // policyConsoleStatement := policy.Statement{ - // Actions: set.CreateStringSet("*"), - // Conditions: policy.ConditionMap{ - // "StringLike": policy.ConditionKeyMap{ - // "aws:referer": set.CreateStringSet(fmt.Sprintf("https://console.cloud.yandex.*/folders/*/storage/buckets/%s*", VerificationBucket)), - // }, - // }, - // Effect: "Allow", - // Principal: policy.User{AWS: set.CreateStringSet("*")}, - // Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket), - // fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)), - // Sid: "console-statement", - // } - // - // policyServiceAccount := policy.Statement{ - // Actions: set.CreateStringSet("*"), - // Conditions: nil, - // Effect: "Allow", - // Principal: policy.User{CanonicalUser: set.CreateStringSet("ajelmc4tjbct675tjdh9")}, - // Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket), - // fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)), - // Sid: "service-account-statement", - // } - // - // policySharingBucket := policy.Statement{ - // Actions: set.CreateStringSet("s3:GetObject"), - // Conditions: nil, - // Effect: "Allow", - // Principal: policy.User{AWS: set.CreateStringSet("*")}, - // Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket), - // fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)), - // Sid: "sharing-bucket", - // } - // - // p := policy.BucketAccessPolicy{Version: "2012-10-17", Statements: []policy.Statement{ - // policyConsoleStatement, - // policyServiceAccount, - // policySharingBucket, - // }} - // - // outPolicy, err := json.Marshal(&p) - // if r.err(err) { - // return err - // } - // - // err = r.s3.SetBucketPolicy(ctx, VerificationBucket, string(outPolicy)) - // if r.err(err) { - // return err - // } - //} + ok, err := r.s3.BucketExists(ctx, VerificationBucket) + if r.err(err) { + return err + } + + if !ok { + err = r.s3.MakeBucket(ctx, VerificationBucket, minio.MakeBucketOptions{ObjectLocking: false}) + if r.err(err) { + return err + } + + policyConsoleStatement := policy.Statement{ + Actions: set.CreateStringSet("*"), + Conditions: policy.ConditionMap{ + "StringLike": policy.ConditionKeyMap{ + "aws:referer": set.CreateStringSet(fmt.Sprintf("https://console.cloud.yandex.*/folders/*/storage/buckets/%s*", VerificationBucket)), + }, + }, + Effect: "Allow", + Principal: policy.User{AWS: set.CreateStringSet("*")}, + Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket), + fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)), + Sid: "console-statement", + } + + policyServiceAccount := policy.Statement{ + Actions: set.CreateStringSet("*"), + Conditions: nil, + Effect: "Allow", + Principal: policy.User{CanonicalUser: set.CreateStringSet("ajelmc4tjbct675tjdh9")}, + Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket), + fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)), + Sid: "service-account-statement", + } + + policySharingBucket := policy.Statement{ + Actions: set.CreateStringSet("s3:GetObject"), + Conditions: nil, + Effect: "Allow", + Principal: policy.User{AWS: set.CreateStringSet("*")}, + Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket), + fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)), + Sid: "sharing-bucket", + } + + p := policy.BucketAccessPolicy{Version: "2012-10-17", Statements: []policy.Statement{ + policyConsoleStatement, + policyServiceAccount, + policySharingBucket, + }} + + outPolicy, err := json.Marshal(&p) + if r.err(err) { + return err + } + + err = r.s3.SetBucketPolicy(ctx, VerificationBucket, string(outPolicy)) + if r.err(err) { + return err + } + } return nil } @@ -244,10 +247,9 @@ func (r *VerificationRepository) Update(ctx context.Context, record *models.Veri record.UpdatedAt = time.Now() test := models.VerificationUpdate{ - UserID: record.UserID, Accepted: record.Accepted, Status: record.Status, - UpdatedAt: record.UpdatedAt, + UpdatedAt: time.Now(), Comment: record.Comment, Files: record.Files, TaxNumber: record.TaxNumber, @@ -258,7 +260,6 @@ func (r *VerificationRepository) Update(ctx context.Context, record *models.Veri var result models.Verification err := r.mongo.FindOneAndUpdate(ctx, bson.M{"_id": objId}, bson.M{"$set": test}, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&result) if r.err(err) { - fmt.Println(err) return nil, err } return &result, nil diff --git a/internal/server/http.go b/internal/server/http.go index cc8ef26..176fbe1 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -1,7 +1,9 @@ package server import ( + "github.com/gofiber/contrib/fiberzap" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/recover" "go.uber.org/zap" "penahub.gitlab.yandexcloud.net/backend/verification/internal/config" "penahub.gitlab.yandexcloud.net/backend/verification/internal/initialize" @@ -20,12 +22,12 @@ func NewHTTP(cfg *config.Config, logger *zap.Logger) *HTTP { BodyLimit: 50 << 20, }) - //srv.Use( - // recover.New(recover.Config{EnableStackTrace: true}), - // fiberzap.New(fiberzap.Config{Logger: logger}), - // LocalJwt(), - // Jwt(), - //) + srv.Use( + recover.New(recover.Config{EnableStackTrace: true}), + fiberzap.New(fiberzap.Config{Logger: logger}), + LocalJwt(), + Jwt(), + ) return &HTTP{fiber: srv, cfg: cfg, logger: logger} } diff --git a/staging.env b/staging.env index 6d022d2..77881f8 100644 --- a/staging.env +++ b/staging.env @@ -1,14 +1,4 @@ -HTTP_ADDRESS=:8080 -CUSTOMER_SVC_ADDRESS=http://localhost:8003 -S3_SECRET_KEY=1 -S3_ACCESS_KEY_ID=1 -S3_ENDPOINT=1 -TELEGRAM_CHANNEL_ID=1 -TELEGRAM_TOKEN=1 - -MONGO_HOST=localhost -MONGO_PORT=27020 -MONGO_USER=test -MONGO_PASSWORD=test -MONGO_AUTH=admin -MONGO_DATABASE_NAME=admin \ No newline at end of file +MONGO_DATABASE_NAME=verification +HTTP_ADDRESS=:7035 +CUSTOMER_SVC_ADDRESS=https://admin.pena.digital/customer +STAGING_URL=https://sadmin.pena \ No newline at end of file