notifier/internal/workers/notifyer.go

120 lines
3.4 KiB
Go
Raw Normal View History

2024-03-31 20:04:15 +00:00
package workers
import (
"context"
"go.uber.org/zap"
2024-03-31 20:04:15 +00:00
"mailnotifier/internal/clients"
"mailnotifier/internal/repository"
"time"
)
2024-04-01 14:41:53 +00:00
//todo тут будут заембенжены шаблоны
2024-03-31 20:04:15 +00:00
type Notifyer struct {
repo *repository.Repository
mailClient *clients.MailClient
logger *zap.Logger
2024-03-31 20:04:15 +00:00
}
type NotifyerDeps struct {
Repo *repository.Repository
MailClient *clients.MailClient
Logger *zap.Logger
2024-03-31 20:04:15 +00:00
}
func NewNotifyer(deps NotifyerDeps) *Notifyer {
return &Notifyer{
repo: deps.Repo,
mailClient: deps.MailClient,
logger: deps.Logger,
2024-03-31 20:04:15 +00:00
}
}
func (n *Notifyer) Start(ctx context.Context) {
2024-04-02 15:11:02 +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 {
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))
}
// тут запрос к кору на получение всех квизов, если длина больше 0, то отправляем письмо
// отправка письма
err = n.mailClient.MailSender(clients.SenderDeps{
Subject: "checkCreatedQuiz",
Email: record.Email,
TmplPath: "checkCreatedQuiz", // todo заменить на шаблон
})
if err != nil {
n.logger.Error("error sending message to mailbox checkCreatedQuiz:", zap.Error(err))
}
continue
}
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))
}
// тут запрос к кору на получение всех квизов со start = true, если нет ниодного со start true, то отправляем письмо
// отправка письма
err = n.mailClient.MailSender(clients.SenderDeps{
Subject: "checkPublishedQuiz",
Email: record.Email,
TmplPath: "checkPublishedQuiz", // todo заменить на шаблон
})
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))
}
// тут нужно получить историю пополнений пользователя если пустая то отправляем сообщение
// отправка письма
err = n.mailClient.MailSender(clients.SenderDeps{
Subject: "checkPaidHistory",
Email: record.Email,
TmplPath: "checkPaidHistory", // todo заменить на шаблон
})
if err != nil {
n.logger.Error("error sending message to mailbox checkPaidHistory:", zap.Error(err))
}
}
2024-04-01 15:22:13 +00:00
}
}