some change logic in rules

This commit is contained in:
Pavel 2024-06-28 13:32:04 +03:00
parent 13de58e8c8
commit 93e3dad1e8
6 changed files with 92 additions and 118 deletions

2
go.mod

@ -12,7 +12,7 @@ require (
github.com/twmb/franz-go v1.16.1 github.com/twmb/franz-go v1.16.1
go.uber.org/zap v1.27.0 go.uber.org/zap v1.27.0
google.golang.org/protobuf v1.33.0 google.golang.org/protobuf v1.33.0
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240621165314-e374d50e42e6 penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240628071842-da12f589207e
) )
require ( require (

4
go.sum

@ -142,5 +142,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 h1:oV+/HNX+JPoQ3/GUx08hio7d45WpY0AMGrFs7j70QlA= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 h1:oV+/HNX+JPoQ3/GUx08hio7d45WpY0AMGrFs7j70QlA=
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240621165314-e374d50e42e6 h1:Ts1PGn2drIA3WJoSdlGvtMqsWBSQ02Pp/q4NxqIq/0c= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240628071842-da12f589207e h1:9wh9ch9UaJcC/b/SCgDWdj7UX1mPK7ko1PBNp5PpH5U=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240621165314-e374d50e42e6/go.mod h1:nfZkoj8MCYaoP+xiPeUn5D0lIzinUr1qDkNfX0ng9rk= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240628071842-da12f589207e/go.mod h1:nfZkoj8MCYaoP+xiPeUn5D0lIzinUr1qDkNfX0ng9rk=

@ -113,7 +113,7 @@ func EmojiUnicode(text string) string {
} }
func AddContactFields(contactFields []models.FieldsValues, fieldValue string, fieldType model.ContactQuizConfig, fieldMap map[string]int) []models.FieldsValues { func AddContactFields(contactFields []models.FieldsValues, fieldValue string, fieldType model.ContactQuizConfig, fieldMap map[string]int) []models.FieldsValues {
if fieldValue != "" { if fieldValue != "" && fieldMap[string(fieldType)] != 0 {
values := make([]models.ValueInterface, 0) values := make([]models.ValueInterface, 0)
values = append(values, models.Values{Value: fieldValue}) values = append(values, models.Values{Value: fieldValue})

@ -7,45 +7,6 @@ import (
"strings" "strings"
) )
//func ToCreatedUpdate(utms []model.UTM, fields []model.Field) ([]models.AddLeadsFields, []int32) {
// var toCreated []models.AddLeadsFields
// var toUpdate []int32
// for _, utm := range utms {
// matched := false
// for _, field := range fields {
// if utm.Name == field.Name {
// toUpdate = append(toUpdate, int32(utm.ID))
// matched = true
// break
// }
// }
// if !matched {
// toCreated = append(toCreated, models.AddLeadsFields{Type: model.TypeAmoText, Name: utm.Name})
// }
// }
// return toCreated, toUpdate
//}
//
//func MatchingUTMs(utms []model.UTM, fields []models.CustomField) []model.UTM {
// var forUpdate []model.UTM
// for _, utm := range utms {
// for _, field := range fields {
// if utm.Name == field.Name {
// updUtm := model.UTM{
// ID: utm.ID,
// Amofieldid: int32(field.ID),
// Quizid: utm.Quizid,
// Accountid: utm.Accountid,
// Name: field.Name,
// }
// forUpdate = append(forUpdate, updUtm)
// }
// }
// }
//
// return forUpdate
//}
type ToUpdate struct { type ToUpdate struct {
FieldID int FieldID int
Entity model.EntityType Entity model.EntityType
@ -89,12 +50,13 @@ func ToCreatedUpdateQuestionRules(questionsTypeMap map[model.EntityType][]model.
return toCreated, toUpdate return toCreated, toUpdate
} }
func ToQuestionIDs(rules []model.FieldRule) []int32 { func ToQuestionIDs(rule map[int]int) []int32 {
var ids []int32 var ids []int32
for _, rule := range rules { for queID, fieldID := range rule {
for queID := range rule.Questionid { if fieldID != 0 {
ids = append(ids, int32(queID)) continue
} }
ids = append(ids, int32(queID))
} }
return ids return ids
} }

@ -6,6 +6,7 @@ import (
"amocrm/internal/tools" "amocrm/internal/tools"
"amocrm/pkg/amoClient" "amocrm/pkg/amoClient"
"context" "context"
"database/sql"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -220,43 +221,47 @@ func (wc *PostDeals) constructField(ctx context.Context, allAnswers []model.Resu
entityFieldsMap[model.LeadsType] = make(map[int][]models.ValueInterface) entityFieldsMap[model.LeadsType] = make(map[int][]models.ValueInterface)
entityFieldsMap[model.CompaniesType] = make(map[int][]models.ValueInterface) entityFieldsMap[model.CompaniesType] = make(map[int][]models.ValueInterface)
entityFieldsMap[model.CustomersType] = make(map[int][]models.ValueInterface) entityFieldsMap[model.CustomersType] = make(map[int][]models.ValueInterface)
entityFieldsMap[model.ContactsType] = make(map[int][]models.ValueInterface)
entityRules := make(map[model.EntityType][]model.FieldRule) entityRules := make(map[model.EntityType]map[int]int)
entityRules[model.LeadsType] = result.FieldsRule.Lead entityRules[model.LeadsType] = result.FieldsRule.Lead.Questionid
entityRules[model.CompaniesType] = result.FieldsRule.Company entityRules[model.CompaniesType] = result.FieldsRule.Company.Questionid
entityRules[model.CustomersType] = result.FieldsRule.Customer entityRules[model.CustomersType] = result.FieldsRule.Customer.Questionid
entityRules[model.ContactsType] = result.FieldsRule.Contact.Questionid
for entityType, ruleList := range entityRules { for entityType, rule := range entityRules {
for _, rule := range ruleList { for _, data := range allAnswers {
for _, data := range allAnswers { if fieldID, ok := rule[int(data.QuestionID)]; ok {
if fieldID, ok := rule.Questionid[int(data.QuestionID)]; ok {
fieldData, err := wc.amoRepo.AmoRepo.GetFieldByID(ctx, int32(fieldID)) fieldData, err := wc.amoRepo.AmoRepo.GetFieldByID(ctx, int32(fieldID))
if err != nil {
if err == sql.ErrNoRows {
wc.logger.Info("This field id does not exist in db", zap.Any("fieldID", fieldID))
continue
}
return nil, nil, nil, nil, err
}
if fieldData.Type == model.TypeAmoText || fieldData.Type == model.TypeAmoTextarea {
values := entityFieldsMap[entityType][fieldID]
content := strings.ReplaceAll(data.Content, " ", "")
if content == "" {
data.Content = "Пустая строка"
}
values = append(values, models.Values{Value: tools.EmojiUnicode(data.Content)})
entityFieldsMap[entityType][fieldID] = values
continue
}
if fieldData.Type == model.TypeFile && data.Content != "" && result.DriveURL != "" {
value, err := wc.amoClient.UploadFileToAmo(data.Content, result.AccessToken, result.DriveURL)
if err != nil { if err != nil {
return nil, nil, nil, nil, err return nil, nil, nil, nil, err
} }
values := entityFieldsMap[entityType][fieldID]
if fieldData.Type == model.TypeAmoText { values = append(values, value)
values := entityFieldsMap[entityType][fieldID] entityFieldsMap[entityType][fieldID] = values
content := strings.ReplaceAll(data.Content, " ", "") continue
if content == "" {
data.Content = "Пустая строка"
}
values = append(values, models.Values{Value: tools.EmojiUnicode(data.Content)})
entityFieldsMap[entityType][fieldID] = values
continue
}
if fieldData.Type == model.TypeFile && data.Content != "" && result.DriveURL != "" {
value, err := wc.amoClient.UploadFileToAmo(data.Content, result.AccessToken, result.DriveURL)
if err != nil {
return nil, nil, nil, nil, err
}
values := entityFieldsMap[entityType][fieldID]
values = append(values, value)
entityFieldsMap[entityType][fieldID] = values
continue
}
} }
} }
} }
@ -280,6 +285,8 @@ func (wc *PostDeals) constructField(ctx context.Context, allAnswers []model.Resu
companyFields = append(companyFields, field) companyFields = append(companyFields, field)
case model.CustomersType: case model.CustomersType:
customerFields = append(customerFields, field) customerFields = append(customerFields, field)
case model.ContactsType:
contactFields = append(contactFields, field)
} }
} }
} }
@ -319,7 +326,7 @@ func (wc *PostDeals) constructField(ctx context.Context, allAnswers []model.Resu
return nil, nil, nil, nil, err return nil, nil, nil, nil, err
} }
if errors.Is(err, pj_errors.ErrNotFound) || len(existContactData) == 0 { if errors.Is(err, pj_errors.ErrNotFound) || len(existContactData) == 0 {
fmt.Println("NO CONTACT") fmt.Println("NO CONTACT", contactFields)
contactResp, err := wc.amoClient.CreateContact([]models.CreateContactReq{ contactResp, err := wc.amoClient.CreateContact([]models.CreateContactReq{
{ {
Name: resultInfo.Name, Name: resultInfo.Name,

@ -568,13 +568,13 @@ func (m *Methods) CreateUserFromWebHook(ctx context.Context, msg models.KafkaMes
func (m *Methods) CheckFieldRule(ctx context.Context, token string, msg models.KafkaMessage) error { func (m *Methods) CheckFieldRule(ctx context.Context, token string, msg models.KafkaMessage) error {
var ( var (
leadIDs, companyIDs, customerIDs []int32 leadIDs, companyIDs, customerIDs, contactIDs []int32
leadQuestions, companyQuestions, customerQuestions []model.Question leadQuestions, companyQuestions, customerQuestions, contactQuestions []model.Question
questionsTypeMap = make(map[model.EntityType][]model.Question) questionsTypeMap = make(map[model.EntityType][]model.Question)
newFields []model.Field newFields []model.Field
lead, company, customer []model.FieldRule lead, company, customer, contact model.FieldRule
currentFieldsRule = msg.Rule.Fieldsrule currentFieldsRule = msg.Rule.Fieldsrule
err error err error
) )
user, err := m.repo.AmoRepo.GetCurrentAccount(ctx, msg.AccountID) user, err := m.repo.AmoRepo.GetCurrentAccount(ctx, msg.AccountID)
@ -602,9 +602,10 @@ func (m *Methods) CheckFieldRule(ctx context.Context, token string, msg models.K
return err return err
} }
leadIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Lead) leadIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Lead.Questionid)
customerIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Customer) customerIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Customer.Questionid)
companyIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Company) companyIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Company.Questionid)
contactIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Contact.Questionid)
getQuestions := func(questionIDs []int32, questions *[]model.Question) { getQuestions := func(questionIDs []int32, questions *[]model.Question) {
if len(questionIDs) > 0 { if len(questionIDs) > 0 {
@ -619,10 +620,12 @@ func (m *Methods) CheckFieldRule(ctx context.Context, token string, msg models.K
getQuestions(leadIDs, &leadQuestions) getQuestions(leadIDs, &leadQuestions)
getQuestions(customerIDs, &customerQuestions) getQuestions(customerIDs, &customerQuestions)
getQuestions(companyIDs, &companyQuestions) getQuestions(companyIDs, &companyQuestions)
getQuestions(contactIDs, &contactQuestions)
questionsTypeMap[model.LeadsType] = append(questionsTypeMap[model.LeadsType], leadQuestions...) questionsTypeMap[model.LeadsType] = append(questionsTypeMap[model.LeadsType], leadQuestions...)
questionsTypeMap[model.CustomersType] = append(questionsTypeMap[model.CustomersType], customerQuestions...) questionsTypeMap[model.CustomersType] = append(questionsTypeMap[model.CustomersType], customerQuestions...)
questionsTypeMap[model.CompaniesType] = append(questionsTypeMap[model.CompaniesType], companyQuestions...) questionsTypeMap[model.CompaniesType] = append(questionsTypeMap[model.CompaniesType], companyQuestions...)
questionsTypeMap[model.ContactsType] = append(questionsTypeMap[model.ContactsType], contactQuestions...)
toCreated, toUpdate := tools.ToCreatedUpdateQuestionRules(questionsTypeMap, currentFields) toCreated, toUpdate := tools.ToCreatedUpdateQuestionRules(questionsTypeMap, currentFields)
contactFieldsToCreate, forAdding := tools.ForContactRules(quizConfig, currentFields) contactFieldsToCreate, forAdding := tools.ForContactRules(quizConfig, currentFields)
@ -665,48 +668,50 @@ func (m *Methods) CheckFieldRule(ctx context.Context, token string, msg models.K
} }
} }
constructFieldRules := func(fieldRuleArrCurrent []model.FieldRule, questions []model.Question, fieldRule *[]model.FieldRule, currentEntity model.EntityType) { constructFieldRules := func(fieldRuleArrCurrent map[int]int, questions []model.Question, fieldRule *model.FieldRule, currentEntity model.EntityType) {
for _, rules := range fieldRuleArrCurrent { ruleMap := make(map[int]int)
for questionID := range rules.Questionid { for questionID, fieldID := range fieldRuleArrCurrent {
for _, question := range questions { if fieldID != 0 {
if dataQues, ok := toUpdate[questionID]; ok { // если fieldID уже заполнен добавляем его как есть
if dataQues.Entity == currentEntity { ruleMap[questionID] = fieldID
ruleMap := make(map[int]int) continue
ruleMap[questionID] = dataQues.FieldID }
*fieldRule = append(*fieldRule, model.FieldRule{Questionid: ruleMap}) for _, question := range questions {
break if dataQues, ok := toUpdate[questionID]; ok {
} if dataQues.Entity == currentEntity {
ruleMap[questionID] = dataQues.FieldID
break
} }
if questionID == int(question.Id) { }
// тут также делаем чтобы сверить филд с вопросом if questionID == int(question.Id) {
title := strings.ToLower(strings.ReplaceAll(question.Title, " ", "")) // тут также делаем чтобы сверить филд с вопросом
if title == "" { title := strings.ToLower(strings.ReplaceAll(question.Title, " ", ""))
question.Title = fmt.Sprintf("Вопрос №%d", question.Page) if title == "" {
} question.Title = fmt.Sprintf("Вопрос №%d", question.Page)
title = strings.ToLower(strings.ReplaceAll(question.Title, " ", "")) }
for _, field := range newFields { title = strings.ToLower(strings.ReplaceAll(question.Title, " ", ""))
fieldName := strings.ToLower(strings.ReplaceAll(field.Name, " ", "")) for _, field := range newFields {
if title == fieldName && field.Entity == currentEntity { fieldName := strings.ToLower(strings.ReplaceAll(field.Name, " ", ""))
ruleMap := make(map[int]int) if title == fieldName && field.Entity == currentEntity {
ruleMap[questionID] = int(field.Amoid) ruleMap[questionID] = int(field.Amoid)
*fieldRule = append(*fieldRule, model.FieldRule{Questionid: ruleMap})
}
} }
} }
} }
} }
} }
fieldRule.Questionid = ruleMap
} }
constructFieldRules(currentFieldsRule.Lead, leadQuestions, &lead, model.LeadsType) constructFieldRules(currentFieldsRule.Lead.Questionid, leadQuestions, &lead, model.LeadsType)
constructFieldRules(currentFieldsRule.Customer, customerQuestions, &customer, model.CustomersType) constructFieldRules(currentFieldsRule.Customer.Questionid, customerQuestions, &customer, model.CustomersType)
constructFieldRules(currentFieldsRule.Company, companyQuestions, &company, model.CompaniesType) constructFieldRules(currentFieldsRule.Company.Questionid, companyQuestions, &company, model.CompaniesType)
constructFieldRules(currentFieldsRule.Contact.Questionid, contactQuestions, &contact, model.ContactsType)
err = m.repo.AmoRepo.UpdateFieldRules(ctx, model.Fieldsrule{ err = m.repo.AmoRepo.UpdateFieldRules(ctx, model.Fieldsrule{
Lead: lead, Lead: lead,
Customer: customer, Customer: customer,
Company: company, Company: company,
Contact: model.ContactRules{ContactRuleMap: forAdding}, Contact: model.ContactRules{ContactRuleMap: forAdding, Questionid: contact.Questionid},
}, msg.AccountID, msg.Rule.QuizID) }, msg.AccountID, msg.Rule.QuizID)
if err != nil { if err != nil {