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

2
go.mod

@ -14,7 +14,7 @@ require (
github.com/twmb/franz-go v1.17.0
go.uber.org/zap v1.27.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
)

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=
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/quiz/common.git v0.0.0-20240611163623-9a8f348b2cc4 h1:cac/1YTF+7xZCINwrsvqPn0VAOja0X30xDK39NXjuXY=
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 h1:cTmv0YZE1B+ofsWfHYEiNxzToWKMy12rVW3cPOrtp30=
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/go.mod h1:hIMkN5Xe01vAVaX22QWsGD87Oi93IfX1hJGqxy0oJbE=