update logic creating contact in deal

This commit is contained in:
Pavel 2024-05-07 18:56:02 +03:00
parent 029b9624d4
commit 72854b0ad0
6 changed files with 118 additions and 23 deletions

2
go.mod

@ -13,7 +13,7 @@ require (
github.com/twmb/franz-go v1.16.1
go.uber.org/zap v1.27.0
google.golang.org/protobuf v1.33.0
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240506200710-edeece4bfbf3
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240507152904-b2e6000c1f40
penahub.gitlab.yandexcloud.net/backend/quiz/core.git v0.0.0-20240219174804-d78fd38511af
)

4
go.sum

@ -169,7 +169,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
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/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240506200710-edeece4bfbf3 h1:s2hbSHeeFlnGlKILgSGAoFnP+ZRIgXkhIgMonDtigQw=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240506200710-edeece4bfbf3/go.mod h1:oRyhT55ctjqp/7ZxIzkR7OsQ7T/NLibsfrbb7Ytns64=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240507152904-b2e6000c1f40 h1:2/7GQGyMcwISIXVj0bapGEy2FDHrI9FskMSqc6peGjM=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240507152904-b2e6000c1f40/go.mod h1:oRyhT55ctjqp/7ZxIzkR7OsQ7T/NLibsfrbb7Ytns64=
penahub.gitlab.yandexcloud.net/backend/quiz/core.git v0.0.0-20240219174804-d78fd38511af h1:jQ7HaXSutDX5iepU7VRImxhikK7lV/lBKkiloOZ4Emo=
penahub.gitlab.yandexcloud.net/backend/quiz/core.git v0.0.0-20240219174804-d78fd38511af/go.mod h1:5S5YwjSXWmnEKjBjG6MtyGtFmljjukDRS8CwHk/CF/I=

@ -2,6 +2,7 @@ package tools
import (
"amocrm/internal/models"
"encoding/json"
"fmt"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
"strings"
@ -69,16 +70,14 @@ func ToField(amoField []models.CustomField, entity model.EntityType) []model.Fie
}
// todo Для добавляемых сущностей (сделка, контакт, компания), можно передать не более 40 значений дополнительных полей.
func ConstructField(allAnswers []model.ResultAnswer, result model.AmoUsersTrueResults) ([]models.FieldsValues, []models.Contact, []models.Company) {
func ConstructField(allAnswers []model.ResultAnswer, result model.AmoUsersTrueResults) ([]models.FieldsValues, []models.Contact, []models.Company, error) {
entityFieldsMap := make(map[model.EntityType]map[int][]models.Values)
entityFieldsMap[model.LeadsType] = make(map[int][]models.Values)
entityFieldsMap[model.ContactsType] = make(map[int][]models.Values)
entityFieldsMap[model.CompaniesType] = make(map[int][]models.Values)
entityFieldsMap[model.CustomersType] = make(map[int][]models.Values)
entityRules := make(map[model.EntityType][]model.FieldRule)
entityRules[model.LeadsType] = result.FieldsRule.Lead
entityRules[model.ContactsType] = result.FieldsRule.Contact
entityRules[model.CompaniesType] = result.FieldsRule.Company
entityRules[model.CustomersType] = result.FieldsRule.Customer
@ -108,8 +107,6 @@ func ConstructField(allAnswers []model.ResultAnswer, result model.AmoUsersTrueRe
switch entityType {
case model.LeadsType:
leadFields = append(leadFields, field)
case model.ContactsType:
contactFields = append(contactFields, field)
case model.CompaniesType:
companyFields = append(companyFields, field)
case model.CustomersType:
@ -118,9 +115,23 @@ func ConstructField(allAnswers []model.ResultAnswer, result model.AmoUsersTrueRe
}
}
var resultInfo model.ResultContent
err := json.Unmarshal([]byte(result.Content), &resultInfo)
if err != nil {
return nil, nil, nil, err
}
contactRuleMap := result.FieldsRule.Contact.ContactRuleMap
contactFields = addContactField(contactFields, resultInfo.Name, model.TypeContactName, contactRuleMap)
contactFields = addContactField(contactFields, resultInfo.Phone, model.TypeContactPhone, contactRuleMap)
contactFields = addContactField(contactFields, resultInfo.Text, model.TypeContactText, contactRuleMap)
contactFields = addContactField(contactFields, resultInfo.Email, model.TypeContactEmail, contactRuleMap)
contactFields = addContactField(contactFields, resultInfo.Address, model.TypeContactAddress, contactRuleMap)
return leadFields, []models.Contact{
{
Name: result.PerformerName,
Name: resultInfo.Name,
ResponsibleUserID: result.PerformerID,
CreatedBy: 0,
UpdatedBy: 0,
@ -136,7 +147,7 @@ func ConstructField(allAnswers []model.ResultAnswer, result model.AmoUsersTrueRe
CreatedAt: time.Now().Unix(),
CustomFieldsValues: companyFields,
},
}
}, nil
}
func isEmoji(r rune) bool {
@ -183,3 +194,17 @@ func emojiUnicode(text string) string {
}
return result.String()
}
func addContactField(contactFields []models.FieldsValues, fieldValue string, fieldType model.ContactQuizConfig, fieldMap map[string]int) []models.FieldsValues {
if fieldValue != "" {
contactFields = append(contactFields, models.FieldsValues{
FieldID: fieldMap[string(fieldType)],
Values: []models.Values{
{
Value: fieldValue,
},
},
})
}
return contactFields
}

@ -77,3 +77,41 @@ func ToQuestionIDs(rules []model.FieldRule) []int32 {
}
return ids
}
func ForContactRules(quizConfig model.QuizContact, currentFields []model.Field) ([]models.AddLeadsFields, map[string]int) {
var contactFieldsArr []model.ContactQuizConfig
contactFieldTypes := map[model.ContactQuizConfig]bool{
model.TypeContactName: quizConfig.FormContact.Fields.Name.Used,
model.TypeContactEmail: quizConfig.FormContact.Fields.Email.Used,
model.TypeContactPhone: quizConfig.FormContact.Fields.Phone.Used,
model.TypeContactText: quizConfig.FormContact.Fields.Text.Used,
model.TypeContactAddress: quizConfig.FormContact.Fields.Address.Used,
}
for fieldType, used := range contactFieldTypes {
if used {
contactFieldsArr = append(contactFieldsArr, fieldType)
}
}
forAdding := make(map[string]int)
var toCreated []models.AddLeadsFields
for _, contactField := range contactFieldsArr {
matched := false
for _, field := range currentFields {
if field.Name == string(contactField) {
matched = true
forAdding[string(contactField)] = int(field.Amoid)
break
}
}
if !matched {
//Type: model.TypeMapping[question.Type]
toCreated = append(toCreated, models.AddLeadsFields{Type: model.TypeAmoText, Name: string(contactField)})
forAdding[string(contactField)] = 0
}
}
return toCreated, forAdding
}

@ -87,7 +87,11 @@ func (wc *PostDeals) startFetching(ctx context.Context) {
RequestID: strconv.Itoa(int(result.AnswerID)),
}
leadFields, contactData, companyData := tools.ConstructField(allAnswers, result)
leadFields, contactData, companyData, err := tools.ConstructField(allAnswers, result)
if err != nil {
wc.logger.Error("error serialization resultContent to model ResultContent")
return
}
err = wc.redisRepo.CachingLeadFieldsToRedis(ctx, result.AnswerID, leadFields)
if err != nil {

@ -5,6 +5,7 @@ import (
"amocrm/internal/tools"
"amocrm/pkg/amoClient"
"context"
"encoding/json"
"go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
@ -425,17 +426,29 @@ func (m *Methods) CheckUTMs(ctx context.Context, token, accountID string, ids []
func (m *Methods) CheckFieldRule(ctx context.Context, token string, accountID string, req models.KafkaRule, currentFields []model.Field) error {
var (
leadIDs, contactIDs, companyIDs, customerIDs []int32
leadQuestions, contactQuestions, companyQuestions, customerQuestions []model.Question
questionsTypeMap = make(map[model.EntityType][]model.Question)
newFields []model.Field
lead, contact, company, customer []model.FieldRule
currentFieldsRule = req.Fieldsrule
err error
leadIDs, companyIDs, customerIDs []int32
leadQuestions, companyQuestions, customerQuestions []model.Question
questionsTypeMap = make(map[model.EntityType][]model.Question)
newFields []model.Field
lead, company, customer []model.FieldRule
currentFieldsRule = req.Fieldsrule
err error
)
quiz, err := m.repo.QuizRepo.GetQuizById(ctx, accountID, uint64(req.QuizID))
if err != nil {
m.logger.Error("error getting quiz by quizID and accountID", zap.Error(err))
return err
}
var quizConfig model.QuizContact
err = json.Unmarshal([]byte(quiz.Config), &quizConfig)
if err != nil {
m.logger.Error("error serialization quizConfig to model QuizContact", zap.Error(err))
return err
}
leadIDs = tools.ToQuestionIDs(req.Fieldsrule.Lead)
contactIDs = tools.ToQuestionIDs(req.Fieldsrule.Contact)
customerIDs = tools.ToQuestionIDs(req.Fieldsrule.Customer)
companyIDs = tools.ToQuestionIDs(req.Fieldsrule.Company)
@ -450,16 +463,15 @@ func (m *Methods) CheckFieldRule(ctx context.Context, token string, accountID st
}
getQuestions(leadIDs, &leadQuestions)
getQuestions(contactIDs, &contactQuestions)
getQuestions(customerIDs, &customerQuestions)
getQuestions(companyIDs, &companyQuestions)
questionsTypeMap[model.LeadsType] = append(questionsTypeMap[model.LeadsType], leadQuestions...)
questionsTypeMap[model.ContactsType] = append(questionsTypeMap[model.ContactsType], contactQuestions...)
questionsTypeMap[model.CustomersType] = append(questionsTypeMap[model.CustomersType], customerQuestions...)
questionsTypeMap[model.CompaniesType] = append(questionsTypeMap[model.CompaniesType], companyQuestions...)
toCreated, toUpdate := tools.ToCreatedUpdateQuestionRules(questionsTypeMap, currentFields)
contactFieldsToCreate, forAdding := tools.ForContactRules(quizConfig, currentFields)
for entity, fields := range toCreated {
if len(fields) == 0 {
@ -474,6 +486,23 @@ func (m *Methods) CheckFieldRule(ctx context.Context, token string, accountID st
newFields = append(newFields, tools.ToField(createdFields.Embedded.CustomFields, entity)...)
}
if len(contactFieldsToCreate) > 0 {
createdFields, err := m.amoClient.AddFields(contactFieldsToCreate, model.ContactsType, token)
if err != nil {
m.logger.Error("error adding fields to amo", zap.Any("type", model.ContactsType), zap.Error(err))
}
contructedFields := tools.ToField(createdFields.Embedded.CustomFields, model.ContactsType)
newFields = append(newFields, contructedFields...)
for _, field := range contructedFields {
if _, ok := forAdding[field.Name]; ok {
forAdding[field.Name] = int(field.Amoid)
}
}
}
if len(newFields) > 0 {
err = m.repo.AmoRepo.CheckFields(ctx, newFields, accountID)
if err != nil {
@ -507,7 +536,6 @@ func (m *Methods) CheckFieldRule(ctx context.Context, token string, accountID st
}
constructFieldRules(currentFieldsRule.Lead, leadQuestions, &lead)
constructFieldRules(currentFieldsRule.Contact, contactQuestions, &contact)
constructFieldRules(currentFieldsRule.Customer, customerQuestions, &customer)
constructFieldRules(currentFieldsRule.Company, companyQuestions, &company)
@ -515,7 +543,7 @@ func (m *Methods) CheckFieldRule(ctx context.Context, token string, accountID st
Lead: lead,
Customer: customer,
Company: company,
Contact: contact,
Contact: model.ContactRules{ContactRuleMap: forAdding},
}, accountID, req.QuizID)
if err != nil {