add base logic with leadTarget

This commit is contained in:
Pavel 2024-06-12 21:04:16 +03:00
parent 47c43abd7d
commit 4d2abfd8f5
3 changed files with 110 additions and 51 deletions

@ -2,6 +2,7 @@ package answerwc
import ( import (
"context" "context"
"database/sql"
_ "embed" _ "embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -155,7 +156,16 @@ func (w *SendToClient) processPendingAnswer(ctx context.Context) {
return return
} }
result, err := w.processAnswerWithPrivileges(ctx, quiz.Name, quizConfig, questionsMap, privileges, account, sortedallAnswers, answerContent, answer.CreatedAt) result, err := w.processAnswerWithPrivileges(ctx, ProcessAnsWithPriv{
quiz: quiz,
quizConfig: quizConfig,
questionsMap: questionsMap,
privileges: privileges,
account: account,
allAnswers: sortedallAnswers,
answerContent: answerContent,
answerTime: answer.CreatedAt,
})
fmt.Println("ANS8", err, result, privileges) fmt.Println("ANS8", err, result, privileges)
if err != nil { if err != nil {
w.reportError(err, "Error process answer with privileges") w.reportError(err, "Error process answer with privileges")
@ -172,26 +182,51 @@ func (w *SendToClient) processPendingAnswer(ctx context.Context) {
} }
} }
func (w *SendToClient) processAnswerWithPrivileges(ctx context.Context, quizName string, quizConfig model.QuizConfig, type ProcessAnsWithPriv struct {
questionsMap map[uint64]string, privileges []model.ShortPrivilege, account model.Account, allAnswers []model.ResultAnswer, quiz *model.Quiz
answerContent model.ResultContent, answerTime time.Time) (bool, error) { quizConfig model.QuizConfig
questionsMap map[uint64]string
privileges []model.ShortPrivilege
account model.Account
allAnswers []model.ResultAnswer
answerContent model.ResultContent
answerTime time.Time
}
err := w.notificationCustomer(ctx, account, privileges) func (w *SendToClient) processAnswerWithPrivileges(ctx context.Context, data ProcessAnsWithPriv) (bool, error) {
err := w.notificationCustomer(ctx, data.account, data.privileges)
fmt.Println("ANS81", err) fmt.Println("ANS81", err)
if err != nil { if err != nil {
return false, err return false, err
} }
if wctools.HasUnlimitedPrivilege(privileges) { if wctools.HasUnlimitedPrivilege(data.privileges) {
err := w.ProcessMessageToClient(quizConfig, questionsMap, account, allAnswers, answerContent, answerTime) 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,
})
if err != nil { if err != nil {
return false, err return false, err
} }
return true, nil return true, nil
} }
privilege := wctools.HasQuizCntPrivilege(privileges) privilege := wctools.HasQuizCntPrivilege(data.privileges)
if privilege != nil { if privilege != nil {
err := w.ProcessMessageToClient(quizConfig, questionsMap, account, allAnswers, answerContent, answerTime) 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,
})
fmt.Println("PMC", err) fmt.Println("PMC", err)
if err != nil { if err != nil {
return true, err return true, err
@ -205,13 +240,12 @@ func (w *SendToClient) processAnswerWithPrivileges(ctx context.Context, quizName
return true, nil return true, nil
} else { } else {
w.checkAndSendTaskReminders(sendTaskRemindersDeps{ w.checkAndSendTaskReminders(sendTaskRemindersDeps{
// todo account: data.account,
//email: account.Email, theme: data.quiz.Name,
theme: quizName,
config: model.QuizConfig{ config: model.QuizConfig{
Mailing: model.ResultInfo{ Mailing: model.ResultInfo{
When: "email", When: "email",
Theme: fmt.Sprintf("не удалось отправить заявку по опросу\"%s\"", quizName), Theme: fmt.Sprintf("не удалось отправить заявку по опросу\"%s\"", data.quiz.Name),
Reply: "noreply@pena.digital", Reply: "noreply@pena.digital",
ReplName: "Reminder", ReplName: "Reminder",
}, },
@ -253,12 +287,13 @@ func (w *SendToClient) recordPendingTasks(ctx context.Context, Email string, qui
} }
type sendTaskRemindersDeps struct { type sendTaskRemindersDeps struct {
email, theme string account model.Account
config model.QuizConfig theme string
config model.QuizConfig
} }
func (w *SendToClient) checkAndSendTaskReminders(deps sendTaskRemindersDeps) { func (w *SendToClient) checkAndSendTaskReminders(data sendTaskRemindersDeps) {
err := w.processReminderToClient(deps.email, deps.config) err := w.processReminderToClient(data.account, data.config)
fmt.Println("PMC1", err) fmt.Println("PMC1", err)
if err != nil { if err != nil {
w.reportError(err, "Error sending tasks reminder email") w.reportError(err, "Error sending tasks reminder email")
@ -299,59 +334,83 @@ func (w *SendToClient) notificationCustomer(ctx context.Context, account model.A
return nil return nil
} }
// сделал экспортируемым для теста type DepsProcessMsgToClient struct {
func (w *SendToClient) ProcessMessageToClient(quizConfig model.QuizConfig, questionsMap map[uint64]string, account model.Account, allAnswers []model.ResultAnswer, answerContent model.ResultContent, answerTime time.Time) error { quizConfig model.QuizConfig
theme := quizConfig.Mailing.Theme questionsMap map[uint64]string
quizConfig.Mailing.Theme = quizConfig.Mailing.Reply account model.Account
allAnswers []model.ResultAnswer
answerContent model.ResultContent
answerTime time.Time
quiz *model.Quiz
}
data := senders.TemplateData{ // сделал экспортируемым для теста
QuizConfig: quizConfig.Mailing, func (w *SendToClient) ProcessMessageToClient(ctx context.Context, constructData DepsProcessMsgToClient) error {
AnswerContent: answerContent, leadTargetForAll, err := w.dal.AccountRepo.GetLeadTarget(ctx, constructData.quiz.AccountId, 0)
AllAnswers: allAnswers, if err != nil {
QuestionsMap: questionsMap, return err
}
leadTargetForQuiz, err := w.dal.AccountRepo.GetLeadTarget(ctx, constructData.quiz.AccountId, int32(constructData.quiz.Id))
if err != nil && err != sql.ErrNoRows {
return err
}
if len(leadTargetForQuiz) > 0 {
leadTargetForAll = append(leadTargetForAll, leadTargetForQuiz...)
} }
dayOfWeek := wctools.DaysOfWeek[answerTime.Format("Monday")] theme := constructData.quizConfig.Mailing.Theme
monthOfYear := wctools.MonthsOfYear[answerTime.Format("January")] constructData.quizConfig.Mailing.Theme = constructData.quizConfig.Mailing.Reply
data := senders.TemplateData{
QuizConfig: constructData.quizConfig.Mailing,
AnswerContent: constructData.answerContent,
AllAnswers: constructData.allAnswers,
QuestionsMap: constructData.questionsMap,
}
dayOfWeek := wctools.DaysOfWeek[constructData.answerTime.Format("Monday")]
monthOfYear := wctools.MonthsOfYear[constructData.answerTime.Format("January")]
formattedTime := fmt.Sprintf("%s, %d %s %d г., %02d:%02d (UTC%s)", formattedTime := fmt.Sprintf("%s, %d %s %d г., %02d:%02d (UTC%s)",
dayOfWeek, dayOfWeek,
answerTime.Day(), constructData.answerTime.Day(),
monthOfYear, monthOfYear,
answerTime.Year(), constructData.answerTime.Year(),
answerTime.Hour(), constructData.answerTime.Hour(),
answerTime.Minute(), constructData.answerTime.Minute(),
answerTime.Format("-07:00"), constructData.answerTime.Format("-07:00"),
) )
data.AnswerTime = formattedTime data.AnswerTime = formattedTime
//fmt.Println("SUBJECT", theme, account.Email) mapLeadTarget := make(map[string][]senders.LeadData) // ключ имя сендера, модель отправки
for _, leadTarget := range leadTargetForAll {
leadData := senders.LeadData{ mapLeadTarget[string(leadTarget.Type)] = append(mapLeadTarget[string(leadTarget.Type)], senders.LeadData{
//todo To: leadTarget.Target,
//To: account.Email, Subject: theme,
Subject: theme, TemplateData: data,
TemplateData: data, })
} }
for _, sender := range w.leadSenders { for _, sender := range w.leadSenders {
err := sender.SendLead(leadData) for _, sendData := range mapLeadTarget[sender.Name()] {
if err != nil { err := sender.SendLead(sendData)
w.reportError(err, fmt.Sprintf("Error sending lead through %s", sender.Name())) if err != nil {
w.reportError(err, fmt.Sprintf("Error sending lead through %s", sender.Name()))
}
} }
} }
return nil return nil
} }
func (w *SendToClient) processReminderToClient(email string, quizConfig model.QuizConfig) error { // todo email
func (w *SendToClient) processReminderToClient(account model.Account, quizConfig model.QuizConfig) error {
data := senders.TemplateData{ data := senders.TemplateData{
QuizConfig: model.ResultInfo{ QuizConfig: model.ResultInfo{
When: quizConfig.Mailing.When, When: quizConfig.Mailing.When,
Theme: quizConfig.Mailing.Theme, Theme: quizConfig.Mailing.Theme,
Reply: email, Reply: "email",
ReplName: quizConfig.Mailing.ReplName, ReplName: quizConfig.Mailing.ReplName,
}, },
AnswerContent: model.ResultContent{}, AnswerContent: model.ResultContent{},
@ -359,10 +418,10 @@ func (w *SendToClient) processReminderToClient(email string, quizConfig model.Qu
QuestionsMap: nil, QuestionsMap: nil,
} }
fmt.Println("PRTC", data, email, quizConfig) //fmt.Println("PRTC", data, email, quizConfig)
leadData := senders.LeadData{ leadData := senders.LeadData{
To: email, To: "email",
Subject: quizConfig.Mailing.Theme, Subject: quizConfig.Mailing.Theme,
Template: reminderTemplate, Template: reminderTemplate,
TemplateData: data, TemplateData: data,

2
go.mod

@ -14,7 +14,7 @@ require (
github.com/twmb/franz-go v1.17.0 github.com/twmb/franz-go v1.17.0
go.uber.org/zap v1.27.0 go.uber.org/zap v1.27.0
gopkg.in/tucnak/telebot.v2 v2.5.0 gopkg.in/tucnak/telebot.v2 v2.5.0
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611163623-9a8f348b2cc4 penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240612083524-11882ffe22cf
penahub.gitlab.yandexcloud.net/pena-services/customer v1.0.1-0.20240608222239-5c78187bf014 penahub.gitlab.yandexcloud.net/pena-services/customer v1.0.1-0.20240608222239-5c78187bf014
) )

4
go.sum

@ -154,7 +154,7 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c h1:CWb4UcuNXhd1KTNOmy2U0TJO4+Qxgxrj5cwkyFqbgrk= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c h1:CWb4UcuNXhd1KTNOmy2U0TJO4+Qxgxrj5cwkyFqbgrk=
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611163623-9a8f348b2cc4 h1:cac/1YTF+7xZCINwrsvqPn0VAOja0X30xDK39NXjuXY= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240612083524-11882ffe22cf h1:cTmv0YZE1B+ofsWfHYEiNxzToWKMy12rVW3cPOrtp30=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240611163623-9a8f348b2cc4/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240612083524-11882ffe22cf/go.mod h1:n66zm88Dh12+idyfqh0vU5nd9BZYxM6Pv0XYnmy0398=
penahub.gitlab.yandexcloud.net/pena-services/customer v1.0.1-0.20240608222239-5c78187bf014 h1:ziG55nv824SGFZ02AfagKQC5D4ODirGXnpVPQTL6YFA= penahub.gitlab.yandexcloud.net/pena-services/customer v1.0.1-0.20240608222239-5c78187bf014 h1:ziG55nv824SGFZ02AfagKQC5D4ODirGXnpVPQTL6YFA=
penahub.gitlab.yandexcloud.net/pena-services/customer v1.0.1-0.20240608222239-5c78187bf014/go.mod h1:hIMkN5Xe01vAVaX22QWsGD87Oi93IfX1hJGqxy0oJbE= penahub.gitlab.yandexcloud.net/pena-services/customer v1.0.1-0.20240608222239-5c78187bf014/go.mod h1:hIMkN5Xe01vAVaX22QWsGD87Oi93IfX1hJGqxy0oJbE=