2024-02-19 18:20:09 +00:00
|
|
|
|
package answerwc
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
_ "embed"
|
|
|
|
|
"encoding/json"
|
2024-06-24 13:53:25 +00:00
|
|
|
|
"errors"
|
2024-02-19 18:20:09 +00:00
|
|
|
|
"fmt"
|
2024-06-11 17:09:52 +00:00
|
|
|
|
"github.com/go-redis/redis/v8"
|
2024-03-13 16:36:23 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
|
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
2024-06-24 13:53:25 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors"
|
2024-06-11 16:56:32 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/worker.git/senders"
|
2024-02-19 18:20:09 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/worker.git/wctools"
|
2024-06-03 11:23:28 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/customer_clients"
|
2024-03-13 16:36:23 +00:00
|
|
|
|
|
2024-02-19 18:20:09 +00:00
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DepsSendToClient struct {
|
|
|
|
|
Redis *redis.Client
|
|
|
|
|
Dal *dal.DAL
|
2024-06-11 16:56:32 +00:00
|
|
|
|
LeadSenders []senders.LeadSender
|
2024-06-03 11:23:28 +00:00
|
|
|
|
CustomerService *customer_clients.CustomersClient
|
2024-02-19 18:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SendToClient struct {
|
2024-06-03 11:23:28 +00:00
|
|
|
|
redis *redis.Client
|
|
|
|
|
dal *dal.DAL
|
2024-06-11 17:09:52 +00:00
|
|
|
|
leadSenders []senders.LeadSender
|
2024-06-03 11:23:28 +00:00
|
|
|
|
customerService *customer_clients.CustomersClient
|
|
|
|
|
errChan chan<- error
|
2024-02-19 18:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PendingTasks struct {
|
|
|
|
|
Count int64
|
|
|
|
|
QuizConfig model.QuizConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//go:embed mail/to_client.tmpl
|
|
|
|
|
var toClientTemplate string
|
|
|
|
|
|
|
|
|
|
//go:embed mail/reminder.tmpl
|
|
|
|
|
var reminderTemplate string
|
|
|
|
|
|
2024-06-03 11:23:28 +00:00
|
|
|
|
func NewSendToClient(deps DepsSendToClient, errChan chan<- error) *SendToClient {
|
2024-02-19 18:20:09 +00:00
|
|
|
|
return &SendToClient{
|
2024-06-03 11:23:28 +00:00
|
|
|
|
redis: deps.Redis,
|
|
|
|
|
dal: deps.Dal,
|
|
|
|
|
customerService: deps.CustomerService,
|
|
|
|
|
errChan: errChan,
|
2024-02-19 18:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *SendToClient) Start(ctx context.Context) {
|
|
|
|
|
answerTicker := time.NewTicker(30 * time.Second)
|
|
|
|
|
defer answerTicker.Stop()
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-answerTicker.C:
|
|
|
|
|
w.processPendingAnswer(ctx)
|
|
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *SendToClient) processPendingAnswer(ctx context.Context) {
|
2024-06-03 11:23:28 +00:00
|
|
|
|
pendingAnswers, err := w.redis.Keys(ctx, "answer:*").Result()
|
2024-02-19 18:20:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error getting keys from redis")
|
|
|
|
|
w.errChan <- err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("ANS")
|
|
|
|
|
|
|
|
|
|
for _, key := range pendingAnswers {
|
|
|
|
|
func() {
|
|
|
|
|
fmt.Println("ANS1", key)
|
2024-06-03 11:23:28 +00:00
|
|
|
|
answerJSON, err := w.redis.GetDel(ctx, key).Result()
|
2024-02-19 18:20:09 +00:00
|
|
|
|
if err == redis.Nil {
|
|
|
|
|
return
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
w.reportError(err, "Error getting and deleting data from redis")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
w.reportError(nil, fmt.Sprintf("recovering from panic or error setting redis value %v", r))
|
|
|
|
|
fmt.Println("ANS1ERRR", r)
|
2024-06-03 11:23:28 +00:00
|
|
|
|
_ = w.redis.Set(ctx, key, answerJSON, 0).Err()
|
2024-02-19 18:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
var answer model.Answer
|
|
|
|
|
err = json.Unmarshal([]byte(answerJSON), &answer)
|
|
|
|
|
fmt.Println("ANS2", err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, "Error unmarshal answer")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
answerContent, err := wctools.ProcessAnswer(answer.Content)
|
|
|
|
|
fmt.Println("ANS3", err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, "Error unmarshal answer content")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 11:23:28 +00:00
|
|
|
|
allAnswers, err := w.dal.AnswerRepo.GetAllAnswersByQuizID(ctx, answer.Session)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
fmt.Println("ANS4", err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, "Error getting all answers by quizID")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 11:23:28 +00:00
|
|
|
|
questionsMap, sortedallAnswers, err := w.dal.QuestionRepo.GetMapQuestions(ctx, allAnswers)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
fmt.Println("ANS5", err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, "Error getting questionsMap")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if answer.QuizId == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 11:23:28 +00:00
|
|
|
|
quizConfig, accountId, err := w.dal.QuizRepo.GetQuizConfig(ctx, answer.QuizId)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
fmt.Println("ANS6", err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, "Error getting quiz config")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 11:23:28 +00:00
|
|
|
|
quiz, err := w.dal.QuizRepo.GetQuizById(ctx, accountId, answer.QuizId)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
fmt.Println("ANS60", err, accountId, answer.QuizId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, "Error getting quiz")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quizConfig.Mailing.Reply = quiz.Name
|
|
|
|
|
|
|
|
|
|
if quizConfig.Mailing.Theme == "" {
|
|
|
|
|
quizConfig.Mailing.Theme = quiz.Name
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 11:23:28 +00:00
|
|
|
|
account, privileges, err := w.dal.AccountRepo.GetAccAndPrivilegeByEmail(ctx, accountId)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
fmt.Println("ANS7", err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, "Error getting account and privileges by email")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 18:04:16 +00:00
|
|
|
|
result, err := w.processAnswerWithPrivileges(ctx, ProcessAnsWithPriv{
|
|
|
|
|
quiz: quiz,
|
|
|
|
|
quizConfig: quizConfig,
|
|
|
|
|
questionsMap: questionsMap,
|
|
|
|
|
privileges: privileges,
|
|
|
|
|
account: account,
|
|
|
|
|
allAnswers: sortedallAnswers,
|
|
|
|
|
answerContent: answerContent,
|
|
|
|
|
answerTime: answer.CreatedAt,
|
|
|
|
|
})
|
2024-02-19 18:20:09 +00:00
|
|
|
|
fmt.Println("ANS8", err, result, privileges)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, "Error process answer with privileges")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !result {
|
2024-06-03 11:23:28 +00:00
|
|
|
|
err = w.redis.Set(ctx, fmt.Sprintf("%s:%s", account.ID, key), answerJSON, 0).Err()
|
2024-02-19 18:20:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, "Error setting redis value")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 18:04:16 +00:00
|
|
|
|
type ProcessAnsWithPriv struct {
|
|
|
|
|
quiz *model.Quiz
|
|
|
|
|
quizConfig model.QuizConfig
|
|
|
|
|
questionsMap map[uint64]string
|
|
|
|
|
privileges []model.ShortPrivilege
|
|
|
|
|
account model.Account
|
|
|
|
|
allAnswers []model.ResultAnswer
|
|
|
|
|
answerContent model.ResultContent
|
|
|
|
|
answerTime time.Time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *SendToClient) processAnswerWithPrivileges(ctx context.Context, data ProcessAnsWithPriv) (bool, error) {
|
2024-02-19 18:20:09 +00:00
|
|
|
|
|
2024-06-12 18:04:16 +00:00
|
|
|
|
err := w.notificationCustomer(ctx, data.account, data.privileges)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
fmt.Println("ANS81", err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 18:04:16 +00:00
|
|
|
|
if wctools.HasUnlimitedPrivilege(data.privileges) {
|
|
|
|
|
err := w.ProcessMessageToClient(ctx, DepsProcessMsgToClient{
|
|
|
|
|
quizConfig: data.quizConfig,
|
|
|
|
|
questionsMap: data.questionsMap,
|
|
|
|
|
account: data.account,
|
|
|
|
|
allAnswers: data.allAnswers,
|
|
|
|
|
answerContent: data.answerContent,
|
|
|
|
|
answerTime: data.answerTime,
|
|
|
|
|
quiz: data.quiz,
|
|
|
|
|
})
|
2024-02-19 18:20:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
2024-06-12 18:04:16 +00:00
|
|
|
|
privilege := wctools.HasQuizCntPrivilege(data.privileges)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
if privilege != nil {
|
2024-06-12 18:04:16 +00:00
|
|
|
|
err := w.ProcessMessageToClient(ctx, DepsProcessMsgToClient{
|
|
|
|
|
quizConfig: data.quizConfig,
|
|
|
|
|
questionsMap: data.questionsMap,
|
|
|
|
|
account: data.account,
|
|
|
|
|
allAnswers: data.allAnswers,
|
|
|
|
|
answerContent: data.answerContent,
|
|
|
|
|
answerTime: data.answerTime,
|
|
|
|
|
quiz: data.quiz,
|
|
|
|
|
})
|
2024-02-19 18:20:09 +00:00
|
|
|
|
fmt.Println("PMC", err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return true, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
privilege.Amount--
|
2024-06-03 11:23:28 +00:00
|
|
|
|
err = w.dal.AccountRepo.UpdatePrivilegeAmount(ctx, privilege.ID, privilege.Amount)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
} else {
|
2024-06-24 13:53:25 +00:00
|
|
|
|
w.checkAndSendTaskReminders(ctx, sendTaskRemindersDeps{
|
|
|
|
|
quiz: data.quiz,
|
2024-06-12 18:04:16 +00:00
|
|
|
|
account: data.account,
|
|
|
|
|
theme: data.quiz.Name,
|
2024-02-19 18:20:09 +00:00
|
|
|
|
config: model.QuizConfig{
|
|
|
|
|
Mailing: model.ResultInfo{
|
|
|
|
|
When: "email",
|
2024-06-12 18:04:16 +00:00
|
|
|
|
Theme: fmt.Sprintf("не удалось отправить заявку по опросу\"%s\"", data.quiz.Name),
|
2024-02-19 18:20:09 +00:00
|
|
|
|
Reply: "noreply@pena.digital",
|
|
|
|
|
ReplName: "Reminder",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *SendToClient) recordPendingTasks(ctx context.Context, Email string, quizConfig model.QuizConfig) error {
|
|
|
|
|
key := fmt.Sprintf("pending_tasks:%s", Email)
|
|
|
|
|
|
|
|
|
|
var pendingTasks PendingTasks
|
2024-06-03 11:23:28 +00:00
|
|
|
|
val, err := w.redis.HGet(ctx, key, "data").Result()
|
2024-02-19 18:20:09 +00:00
|
|
|
|
if err == nil {
|
|
|
|
|
err := json.Unmarshal([]byte(val), &pendingTasks)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
pendingTasks.Count++
|
|
|
|
|
} else {
|
|
|
|
|
pendingTasks = PendingTasks{
|
|
|
|
|
Count: 1,
|
|
|
|
|
QuizConfig: quizConfig,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pendingTasksJSON, err := json.Marshal(pendingTasks)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 11:23:28 +00:00
|
|
|
|
err = w.redis.HSet(ctx, key, "data", string(pendingTasksJSON)).Err()
|
2024-02-19 18:20:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type sendTaskRemindersDeps struct {
|
2024-06-12 18:04:16 +00:00
|
|
|
|
account model.Account
|
|
|
|
|
theme string
|
|
|
|
|
config model.QuizConfig
|
2024-06-24 13:53:25 +00:00
|
|
|
|
quiz *model.Quiz
|
2024-02-19 18:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-24 13:53:25 +00:00
|
|
|
|
func (w *SendToClient) checkAndSendTaskReminders(ctx context.Context, data sendTaskRemindersDeps) {
|
|
|
|
|
err := w.processReminderToClient(ctx, data.account, data.config, data.quiz)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
fmt.Println("PMC1", err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, "Error sending tasks reminder email")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 11:23:28 +00:00
|
|
|
|
func (w *SendToClient) notificationCustomer(ctx context.Context, account model.Account, privileges []model.ShortPrivilege) error {
|
2024-02-19 18:20:09 +00:00
|
|
|
|
for _, privilege := range privileges {
|
|
|
|
|
fmt.Println("NOTIFIC", privilege.PrivilegeID, privilege.Amount, !wctools.IsPrivilegeExpired(privilege))
|
|
|
|
|
if privilege.PrivilegeID == "quizUnlimTime" && !wctools.IsPrivilegeExpired(privilege) {
|
2024-06-03 11:23:28 +00:00
|
|
|
|
historyData := customer_clients.InsertHistoryDeps{
|
|
|
|
|
UserID: account.UserID,
|
|
|
|
|
Comment: fmt.Sprintf("%s privilege has expired, it was created at %d", privilege.PrivilegeID, privilege.CreatedAt.Unix()),
|
2024-06-03 12:37:29 +00:00
|
|
|
|
Key: "privilege_expired",
|
2024-02-19 18:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 11:23:28 +00:00
|
|
|
|
err := w.customerService.InsertHistory(ctx, historyData)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if privilege.PrivilegeID == "quizCnt" && privilege.Amount == 0 {
|
2024-06-03 11:23:28 +00:00
|
|
|
|
historyData := customer_clients.InsertHistoryDeps{
|
|
|
|
|
UserID: account.UserID,
|
|
|
|
|
Comment: fmt.Sprintf("%s privilege has expired, it was created at %d", privilege.PrivilegeID, privilege.CreatedAt.Unix()),
|
2024-06-03 12:37:29 +00:00
|
|
|
|
Key: "privilege_expired",
|
2024-02-19 18:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 11:23:28 +00:00
|
|
|
|
err := w.customerService.InsertHistory(ctx, historyData)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 18:04:16 +00:00
|
|
|
|
type DepsProcessMsgToClient struct {
|
|
|
|
|
quizConfig model.QuizConfig
|
|
|
|
|
questionsMap map[uint64]string
|
|
|
|
|
account model.Account
|
|
|
|
|
allAnswers []model.ResultAnswer
|
|
|
|
|
answerContent model.ResultContent
|
|
|
|
|
answerTime time.Time
|
|
|
|
|
quiz *model.Quiz
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 18:20:09 +00:00
|
|
|
|
// сделал экспортируемым для теста
|
2024-06-12 18:04:16 +00:00
|
|
|
|
func (w *SendToClient) ProcessMessageToClient(ctx context.Context, constructData DepsProcessMsgToClient) error {
|
|
|
|
|
leadTargetForAll, err := w.dal.AccountRepo.GetLeadTarget(ctx, constructData.quiz.AccountId, 0)
|
2024-06-24 13:53:25 +00:00
|
|
|
|
if err != nil && !errors.Is(err, pj_errors.ErrNotFound) {
|
2024-06-12 18:04:16 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
leadTargetForQuiz, err := w.dal.AccountRepo.GetLeadTarget(ctx, constructData.quiz.AccountId, int32(constructData.quiz.Id))
|
2024-06-24 13:53:25 +00:00
|
|
|
|
if err != nil && !errors.Is(err, pj_errors.ErrNotFound) {
|
2024-06-12 18:04:16 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if len(leadTargetForQuiz) > 0 {
|
|
|
|
|
leadTargetForAll = append(leadTargetForAll, leadTargetForQuiz...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
theme := constructData.quizConfig.Mailing.Theme
|
|
|
|
|
constructData.quizConfig.Mailing.Theme = constructData.quizConfig.Mailing.Reply
|
2024-02-19 18:20:09 +00:00
|
|
|
|
|
2024-06-11 16:56:32 +00:00
|
|
|
|
data := senders.TemplateData{
|
2024-06-12 18:04:16 +00:00
|
|
|
|
QuizConfig: constructData.quizConfig.Mailing,
|
|
|
|
|
AnswerContent: constructData.answerContent,
|
|
|
|
|
AllAnswers: constructData.allAnswers,
|
|
|
|
|
QuestionsMap: constructData.questionsMap,
|
2024-02-19 18:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 18:04:16 +00:00
|
|
|
|
dayOfWeek := wctools.DaysOfWeek[constructData.answerTime.Format("Monday")]
|
|
|
|
|
monthOfYear := wctools.MonthsOfYear[constructData.answerTime.Format("January")]
|
2024-02-19 18:20:09 +00:00
|
|
|
|
|
|
|
|
|
formattedTime := fmt.Sprintf("%s, %d %s %d г., %02d:%02d (UTC%s)",
|
|
|
|
|
dayOfWeek,
|
2024-06-12 18:04:16 +00:00
|
|
|
|
constructData.answerTime.Day(),
|
2024-02-19 18:20:09 +00:00
|
|
|
|
monthOfYear,
|
2024-06-12 18:04:16 +00:00
|
|
|
|
constructData.answerTime.Year(),
|
|
|
|
|
constructData.answerTime.Hour(),
|
|
|
|
|
constructData.answerTime.Minute(),
|
|
|
|
|
constructData.answerTime.Format("-07:00"),
|
2024-02-19 18:20:09 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
data.AnswerTime = formattedTime
|
|
|
|
|
|
2024-06-12 18:04:16 +00:00
|
|
|
|
mapLeadTarget := make(map[string][]senders.LeadData) // ключ имя сендера, модель отправки
|
|
|
|
|
for _, leadTarget := range leadTargetForAll {
|
|
|
|
|
mapLeadTarget[string(leadTarget.Type)] = append(mapLeadTarget[string(leadTarget.Type)], senders.LeadData{
|
|
|
|
|
To: leadTarget.Target,
|
|
|
|
|
Subject: theme,
|
|
|
|
|
TemplateData: data,
|
|
|
|
|
})
|
2024-06-10 14:01:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 17:09:52 +00:00
|
|
|
|
for _, sender := range w.leadSenders {
|
2024-06-12 18:04:16 +00:00
|
|
|
|
for _, sendData := range mapLeadTarget[sender.Name()] {
|
|
|
|
|
err := sender.SendLead(sendData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, fmt.Sprintf("Error sending lead through %s", sender.Name()))
|
|
|
|
|
}
|
2024-06-10 14:01:40 +00:00
|
|
|
|
}
|
2024-02-19 18:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-24 13:53:25 +00:00
|
|
|
|
func (w *SendToClient) processReminderToClient(ctx context.Context, account model.Account, quizConfig model.QuizConfig, quiz *model.Quiz) error {
|
|
|
|
|
leadTargetForAll, err := w.dal.AccountRepo.GetLeadTarget(ctx, account.ID, 0)
|
|
|
|
|
if err != nil && !errors.Is(err, pj_errors.ErrNotFound) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leadTargetForQuiz, err := w.dal.AccountRepo.GetLeadTarget(ctx, account.ID, int32(quiz.Id))
|
|
|
|
|
if err != nil && !errors.Is(err, pj_errors.ErrNotFound) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if len(leadTargetForQuiz) > 0 {
|
|
|
|
|
leadTargetForAll = append(leadTargetForAll, leadTargetForQuiz...)
|
2024-02-19 18:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-24 13:53:25 +00:00
|
|
|
|
mapLeadTarget := make(map[string][]senders.LeadData)
|
|
|
|
|
for _, leadTarget := range leadTargetForAll {
|
|
|
|
|
data := senders.TemplateData{
|
|
|
|
|
QuizConfig: model.ResultInfo{
|
|
|
|
|
When: quizConfig.Mailing.When,
|
|
|
|
|
Theme: quizConfig.Mailing.Theme,
|
|
|
|
|
Reply: leadTarget.Target,
|
|
|
|
|
ReplName: quizConfig.Mailing.ReplName,
|
|
|
|
|
},
|
|
|
|
|
AnswerContent: model.ResultContent{},
|
|
|
|
|
AllAnswers: []model.ResultAnswer{},
|
|
|
|
|
QuestionsMap: nil,
|
|
|
|
|
}
|
2024-02-19 18:20:09 +00:00
|
|
|
|
|
2024-06-24 13:53:25 +00:00
|
|
|
|
fmt.Println("PRTC", data, leadTarget.Target, quizConfig)
|
|
|
|
|
|
|
|
|
|
mapLeadTarget[string(leadTarget.Type)] = append(mapLeadTarget[string(leadTarget.Type)], senders.LeadData{
|
|
|
|
|
To: leadTarget.Target,
|
|
|
|
|
Subject: quizConfig.Mailing.Theme,
|
|
|
|
|
Template: reminderTemplate,
|
|
|
|
|
TemplateData: data,
|
|
|
|
|
})
|
2024-06-10 14:01:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 17:09:52 +00:00
|
|
|
|
for _, sender := range w.leadSenders {
|
2024-06-24 13:53:25 +00:00
|
|
|
|
for _, sendData := range mapLeadTarget[sender.Name()] {
|
|
|
|
|
err := sender.SendLead(sendData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.reportError(err, fmt.Sprintf("Error sending lead through %s", sender.Name()))
|
|
|
|
|
}
|
2024-06-10 14:01:40 +00:00
|
|
|
|
}
|
2024-02-19 18:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *SendToClient) reportError(err error, message string) {
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(message + ": " + err.Error())
|
|
|
|
|
w.errChan <- err
|
|
|
|
|
}
|
|
|
|
|
}
|