2024-03-31 20:04:15 +00:00
|
|
|
|
package workers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-04-07 12:19:44 +00:00
|
|
|
|
_ "embed"
|
2024-04-01 14:24:35 +00:00
|
|
|
|
"go.uber.org/zap"
|
2024-03-31 20:04:15 +00:00
|
|
|
|
"mailnotifier/internal/clients"
|
2024-04-04 07:33:55 +00:00
|
|
|
|
"mailnotifier/internal/proto/notifyer"
|
2024-03-31 20:04:15 +00:00
|
|
|
|
"mailnotifier/internal/repository"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2024-04-07 12:19:44 +00:00
|
|
|
|
//go:embed mail/nopaid.tmpl
|
|
|
|
|
var noPaid string
|
|
|
|
|
|
|
|
|
|
//go:embed mail/noquiz.tmpl
|
|
|
|
|
var noQuiz string
|
|
|
|
|
|
|
|
|
|
//go:embed mail/noquizstart.tmpl
|
|
|
|
|
var noStartedQuiz string
|
2024-04-01 14:41:53 +00:00
|
|
|
|
|
2024-03-31 20:04:15 +00:00
|
|
|
|
type Notifyer struct {
|
2024-04-03 12:15:50 +00:00
|
|
|
|
repo *repository.Repository
|
|
|
|
|
mailClient *clients.MailClient
|
|
|
|
|
customerClient *clients.Customer
|
2024-04-04 07:10:09 +00:00
|
|
|
|
quizClient *clients.QuizClient
|
2024-04-03 12:15:50 +00:00
|
|
|
|
logger *zap.Logger
|
2024-03-31 20:04:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type NotifyerDeps struct {
|
2024-04-03 12:15:50 +00:00
|
|
|
|
Repo *repository.Repository
|
|
|
|
|
MailClient *clients.MailClient
|
|
|
|
|
CustomerClient *clients.Customer
|
2024-04-04 07:10:09 +00:00
|
|
|
|
QuizClient *clients.QuizClient
|
2024-04-03 12:15:50 +00:00
|
|
|
|
Logger *zap.Logger
|
2024-03-31 20:04:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewNotifyer(deps NotifyerDeps) *Notifyer {
|
|
|
|
|
return &Notifyer{
|
2024-04-03 12:15:50 +00:00
|
|
|
|
repo: deps.Repo,
|
|
|
|
|
mailClient: deps.MailClient,
|
|
|
|
|
customerClient: deps.CustomerClient,
|
2024-04-04 07:10:09 +00:00
|
|
|
|
quizClient: deps.QuizClient,
|
2024-04-03 12:15:50 +00:00
|
|
|
|
logger: deps.Logger,
|
2024-03-31 20:04:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *Notifyer) Start(ctx context.Context) {
|
2024-05-06 15:58:59 +00:00
|
|
|
|
tiker := time.NewTicker(30 * time.Minute)
|
2024-03-31 20:04:15 +00:00
|
|
|
|
defer tiker.Stop()
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-tiker.C:
|
2024-04-01 15:22:13 +00:00
|
|
|
|
n.notify(ctx)
|
2024-03-31 20:04:15 +00:00
|
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-01 15:22:13 +00:00
|
|
|
|
|
|
|
|
|
func (n *Notifyer) notify(ctx context.Context) {
|
|
|
|
|
records, err := n.repo.GetMany(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
n.logger.Error("error getting records from mongo", zap.Error(err))
|
|
|
|
|
}
|
2024-04-02 15:55:08 +00:00
|
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
2024-04-01 15:22:13 +00:00
|
|
|
|
for _, record := range records {
|
2024-05-06 15:58:59 +00:00
|
|
|
|
if record.SendRegistration && !record.SendNoneCreated && now.Sub(record.SendAt) >= 7*24*time.Hour {
|
2024-04-02 15:55:08 +00:00
|
|
|
|
record.SendNoneCreated = true
|
|
|
|
|
err = n.repo.Update(ctx, record)
|
|
|
|
|
if err != nil {
|
|
|
|
|
n.logger.Error("error updating record for checkCreatedQuiz:", zap.Error(err))
|
2024-04-03 12:15:50 +00:00
|
|
|
|
continue
|
2024-04-02 15:55:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// тут запрос к кору на получение всех квизов, если длина больше 0, то отправляем письмо
|
2024-04-04 07:10:09 +00:00
|
|
|
|
resp, err := n.quizClient.GetQuizzes(ctx, ¬ifyer.GetQuizzesRequest{
|
|
|
|
|
AccountId: record.AccountID,
|
2024-04-02 15:55:08 +00:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2024-04-04 07:10:09 +00:00
|
|
|
|
n.logger.Error("error getting all created quizzes from core rpc:", zap.Error(err))
|
|
|
|
|
continue
|
|
|
|
|
}
|
2024-04-05 14:56:45 +00:00
|
|
|
|
// тут пока заменил но надо уточнить надо наверно с 0 сравнивать если событие не создания ниодного квиза
|
|
|
|
|
if len(resp.QuizIds) == 0 {
|
2024-04-04 07:10:09 +00:00
|
|
|
|
// отправка письма
|
|
|
|
|
err = n.mailClient.MailSender(clients.SenderDeps{
|
2024-04-07 12:19:44 +00:00
|
|
|
|
Subject: "CREATEQUIZ",
|
|
|
|
|
Email: record.Email,
|
|
|
|
|
Tmpl: noQuiz,
|
2024-04-04 07:10:09 +00:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
n.logger.Error("error sending message to mailbox checkCreatedQuiz:", zap.Error(err))
|
|
|
|
|
}
|
2024-04-02 15:55:08 +00:00
|
|
|
|
}
|
2024-04-04 07:10:09 +00:00
|
|
|
|
|
2024-04-02 15:55:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 15:58:59 +00:00
|
|
|
|
if record.SendRegistration && !record.SendUnpublished && now.Sub(record.SendAt) >= 14*24*time.Hour {
|
2024-04-02 15:55:08 +00:00
|
|
|
|
record.SendUnpublished = true
|
|
|
|
|
err = n.repo.Update(ctx, record)
|
|
|
|
|
if err != nil {
|
|
|
|
|
n.logger.Error("error updating record for checkPublishedQuiz:", zap.Error(err))
|
2024-04-03 12:15:50 +00:00
|
|
|
|
continue
|
2024-04-02 15:55:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// тут запрос к кору на получение всех квизов со start = true, если нет ниодного со start true, то отправляем письмо
|
2024-04-04 07:10:09 +00:00
|
|
|
|
resp, err := n.quizClient.GetStartedQuizzes(ctx, ¬ifyer.GetStartedQuizzesRequest{
|
|
|
|
|
AccountId: record.AccountID,
|
2024-04-02 15:55:08 +00:00
|
|
|
|
})
|
2024-04-04 07:10:09 +00:00
|
|
|
|
|
2024-04-02 15:55:08 +00:00
|
|
|
|
if err != nil {
|
2024-04-04 07:10:09 +00:00
|
|
|
|
n.logger.Error("error getting all started quizzes from core rpc:", zap.Error(err))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(resp.QuizIds) == 0 {
|
|
|
|
|
// отправка письма
|
|
|
|
|
err = n.mailClient.MailSender(clients.SenderDeps{
|
2024-04-07 12:19:44 +00:00
|
|
|
|
Subject: "STARTQUIZ",
|
|
|
|
|
Email: record.Email,
|
|
|
|
|
Tmpl: noStartedQuiz,
|
2024-04-04 07:10:09 +00:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
n.logger.Error("error sending message to mailbox checkPublishedQuiz:", zap.Error(err))
|
|
|
|
|
}
|
2024-04-02 15:55:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 15:58:59 +00:00
|
|
|
|
if record.SendRegistration && !record.SendPaid && now.Sub(record.SendAt) > 14*24*time.Hour {
|
2024-04-02 15:55:08 +00:00
|
|
|
|
record.SendPaid = true
|
|
|
|
|
err = n.repo.Update(ctx, record)
|
|
|
|
|
if err != nil {
|
|
|
|
|
n.logger.Error("error updating record for checkPaidHistory:", zap.Error(err))
|
2024-04-03 12:15:50 +00:00
|
|
|
|
continue
|
2024-04-02 15:55:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 12:15:50 +00:00
|
|
|
|
account, err := n.customerClient.GetAccount(record.AccountID)
|
2024-04-02 15:55:08 +00:00
|
|
|
|
if err != nil {
|
2024-04-03 12:15:50 +00:00
|
|
|
|
n.logger.Error("error getting account from customer:", zap.Error(err))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if account.Wallet.Money == 0 && account.Wallet.Spent == 0 {
|
|
|
|
|
err = n.mailClient.MailSender(clients.SenderDeps{
|
2024-04-07 12:19:44 +00:00
|
|
|
|
Subject: "PAID",
|
|
|
|
|
Email: record.Email,
|
|
|
|
|
Tmpl: noPaid,
|
2024-04-03 12:15:50 +00:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
n.logger.Error("error sending message to mailbox checkPaidHistory:", zap.Error(err))
|
|
|
|
|
}
|
2024-04-02 15:55:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 15:22:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|