notifier/internal/workers/notifyer.go

161 lines
4.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package workers
import (
"context"
_ "embed"
"go.uber.org/zap"
"mailnotifier/internal/clients"
"mailnotifier/internal/proto/notifyer"
"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
type Notifyer struct {
repo *repository.Repository
mailClient *clients.MailClient
customerClient *clients.Customer
quizClient *clients.QuizClient
logger *zap.Logger
}
type NotifyerDeps struct {
Repo *repository.Repository
MailClient *clients.MailClient
CustomerClient *clients.Customer
QuizClient *clients.QuizClient
Logger *zap.Logger
}
func NewNotifyer(deps NotifyerDeps) *Notifyer {
return &Notifyer{
repo: deps.Repo,
mailClient: deps.MailClient,
customerClient: deps.CustomerClient,
quizClient: deps.QuizClient,
logger: deps.Logger,
}
}
func (n *Notifyer) Start(ctx context.Context) {
tiker := time.NewTicker(30 * time.Minute)
defer tiker.Stop()
for {
select {
case <-tiker.C:
n.notify(ctx)
case <-ctx.Done():
return
}
}
}
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()
for _, record := range records {
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, то отправляем письмо
resp, err := n.quizClient.GetQuizzes(ctx, &notifyer.GetQuizzesRequest{
AccountId: record.AccountID,
})
if err != nil {
n.logger.Error("error getting all created quizzes from core rpc:", zap.Error(err))
continue
}
// тут пока заменил но надо уточнить надо наверно с 0 сравнивать если событие не создания ниодного квиза
if len(resp.QuizIds) == 0 {
// отправка письма
err = n.mailClient.MailSender(clients.SenderDeps{
Subject: "CREATEQUIZ",
Email: record.Email,
Tmpl: noQuiz,
})
if err != nil {
n.logger.Error("error sending message to mailbox checkCreatedQuiz:", zap.Error(err))
}
}
}
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, то отправляем письмо
resp, err := n.quizClient.GetStartedQuizzes(ctx, &notifyer.GetStartedQuizzesRequest{
AccountId: record.AccountID,
})
if err != nil {
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,
})
if err != nil {
n.logger.Error("error sending message to mailbox checkPublishedQuiz:", zap.Error(err))
}
}
}
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))
}
}
}
}
}