notifier/internal/workers/notifyer.go

161 lines
4.4 KiB
Go
Raw Normal View History

2024-03-31 20:04:15 +00:00
package workers
import (
"context"
_ "embed"
"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"
)
//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 {
repo *repository.Repository
mailClient *clients.MailClient
customerClient *clients.Customer
2024-04-04 07:10:09 +00:00
quizClient *clients.QuizClient
logger *zap.Logger
2024-03-31 20:04:15 +00:00
}
type NotifyerDeps struct {
Repo *repository.Repository
MailClient *clients.MailClient
CustomerClient *clients.Customer
2024-04-04 07:10:09 +00:00
QuizClient *clients.QuizClient
Logger *zap.Logger
2024-03-31 20:04:15 +00:00
}
func NewNotifyer(deps NotifyerDeps) *Notifyer {
return &Notifyer{
repo: deps.Repo,
mailClient: deps.MailClient,
customerClient: deps.CustomerClient,
2024-04-04 07:10:09 +00:00
quizClient: deps.QuizClient,
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))
}
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 {
record.SendNoneCreated = true
err = n.repo.Update(ctx, record)
if err != nil {
n.logger.Error("error updating record for checkCreatedQuiz:", zap.Error(err))
continue
}
// тут запрос к кору на получение всех квизов, если длина больше 0, то отправляем письмо
2024-04-04 07:10:09 +00:00
resp, err := n.quizClient.GetQuizzes(ctx, &notifyer.GetQuizzesRequest{
AccountId: record.AccountID,
})
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{
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-04 07:10:09 +00:00
}
2024-05-06 15:58:59 +00:00
if record.SendRegistration && !record.SendUnpublished && now.Sub(record.SendAt) >= 14*24*time.Hour {
record.SendUnpublished = true
err = n.repo.Update(ctx, record)
if err != nil {
n.logger.Error("error updating record for checkPublishedQuiz:", zap.Error(err))
continue
}
// тут запрос к кору на получение всех квизов со start = true, если нет ниодного со start true, то отправляем письмо
2024-04-04 07:10:09 +00:00
resp, err := n.quizClient.GetStartedQuizzes(ctx, &notifyer.GetStartedQuizzesRequest{
AccountId: record.AccountID,
})
2024-04-04 07:10:09 +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{
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-05-06 15:58:59 +00:00
if record.SendRegistration && !record.SendPaid && now.Sub(record.SendAt) > 14*24*time.Hour {
record.SendPaid = true
err = n.repo.Update(ctx, record)
if err != nil {
n.logger.Error("error updating record for checkPaidHistory:", zap.Error(err))
continue
}
account, err := n.customerClient.GetAccount(record.AccountID)
if err != nil {
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{
Subject: "PAID",
Email: record.Email,
Tmpl: noPaid,
})
if err != nil {
n.logger.Error("error sending message to mailbox checkPaidHistory:", zap.Error(err))
}
}
}
2024-04-01 15:22:13 +00:00
}
}