diff --git a/go.mod b/go.mod index 459a1ee..9f2052f 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,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-20240621165314-e374d50e42e6 + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240628071842-da12f589207e ) require ( diff --git a/go.sum b/go.sum index 6963eb6..92c2d2d 100644 --- a/go.sum +++ b/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= 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-20240621165314-e374d50e42e6 h1:Ts1PGn2drIA3WJoSdlGvtMqsWBSQ02Pp/q4NxqIq/0c= -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 h1:9wh9ch9UaJcC/b/SCgDWdj7UX1mPK7ko1PBNp5PpH5U= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240628071842-da12f589207e/go.mod h1:nfZkoj8MCYaoP+xiPeUn5D0lIzinUr1qDkNfX0ng9rk= diff --git a/internal/tools/construct.go b/internal/tools/construct.go index a46cb2e..4c7b026 100644 --- a/internal/tools/construct.go +++ b/internal/tools/construct.go @@ -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 { - if fieldValue != "" { + if fieldValue != "" && fieldMap[string(fieldType)] != 0 { values := make([]models.ValueInterface, 0) values = append(values, models.Values{Value: fieldValue}) diff --git a/internal/tools/for_rules.go b/internal/tools/for_rules.go index 90004b7..a6af7c8 100644 --- a/internal/tools/for_rules.go +++ b/internal/tools/for_rules.go @@ -7,45 +7,6 @@ import ( "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 { FieldID int Entity model.EntityType @@ -89,12 +50,13 @@ func ToCreatedUpdateQuestionRules(questionsTypeMap map[model.EntityType][]model. return toCreated, toUpdate } -func ToQuestionIDs(rules []model.FieldRule) []int32 { +func ToQuestionIDs(rule map[int]int) []int32 { var ids []int32 - for _, rule := range rules { - for queID := range rule.Questionid { - ids = append(ids, int32(queID)) + for queID, fieldID := range rule { + if fieldID != 0 { + continue } + ids = append(ids, int32(queID)) } return ids } diff --git a/internal/workers/post_deals_worker/deals_worker.go b/internal/workers/post_deals_worker/deals_worker.go index 7774bcd..ffcf9c1 100644 --- a/internal/workers/post_deals_worker/deals_worker.go +++ b/internal/workers/post_deals_worker/deals_worker.go @@ -6,6 +6,7 @@ import ( "amocrm/internal/tools" "amocrm/pkg/amoClient" "context" + "database/sql" "encoding/json" "errors" "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.CompaniesType] = 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[model.LeadsType] = result.FieldsRule.Lead - entityRules[model.CompaniesType] = result.FieldsRule.Company - entityRules[model.CustomersType] = result.FieldsRule.Customer + entityRules := make(map[model.EntityType]map[int]int) + entityRules[model.LeadsType] = result.FieldsRule.Lead.Questionid + entityRules[model.CompaniesType] = result.FieldsRule.Company.Questionid + entityRules[model.CustomersType] = result.FieldsRule.Customer.Questionid + entityRules[model.ContactsType] = result.FieldsRule.Contact.Questionid - for entityType, ruleList := range entityRules { - for _, rule := range ruleList { - for _, data := range allAnswers { - if fieldID, ok := rule.Questionid[int(data.QuestionID)]; ok { + for entityType, rule := range entityRules { + for _, data := range allAnswers { + if fieldID, ok := rule[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 { return nil, nil, nil, nil, err } - - if fieldData.Type == model.TypeAmoText { - 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 { - return nil, nil, nil, nil, err - } - values := entityFieldsMap[entityType][fieldID] - values = append(values, value) - entityFieldsMap[entityType][fieldID] = values - continue - } + 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) case model.CustomersType: 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 } 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{ { Name: resultInfo.Name, diff --git a/internal/workers_methods/methods.go b/internal/workers_methods/methods.go index 6309f04..c9ed034 100644 --- a/internal/workers_methods/methods.go +++ b/internal/workers_methods/methods.go @@ -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 { var ( - 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 = msg.Rule.Fieldsrule - err error + leadIDs, companyIDs, customerIDs, contactIDs []int32 + leadQuestions, companyQuestions, customerQuestions, contactQuestions []model.Question + questionsTypeMap = make(map[model.EntityType][]model.Question) + newFields []model.Field + lead, company, customer, contact model.FieldRule + currentFieldsRule = msg.Rule.Fieldsrule + err error ) 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 } - leadIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Lead) - customerIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Customer) - companyIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Company) + leadIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Lead.Questionid) + customerIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Customer.Questionid) + companyIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Company.Questionid) + contactIDs = tools.ToQuestionIDs(msg.Rule.Fieldsrule.Contact.Questionid) getQuestions := func(questionIDs []int32, questions *[]model.Question) { if len(questionIDs) > 0 { @@ -619,10 +620,12 @@ func (m *Methods) CheckFieldRule(ctx context.Context, token string, msg models.K getQuestions(leadIDs, &leadQuestions) getQuestions(customerIDs, &customerQuestions) getQuestions(companyIDs, &companyQuestions) + getQuestions(contactIDs, &contactQuestions) questionsTypeMap[model.LeadsType] = append(questionsTypeMap[model.LeadsType], leadQuestions...) questionsTypeMap[model.CustomersType] = append(questionsTypeMap[model.CustomersType], customerQuestions...) questionsTypeMap[model.CompaniesType] = append(questionsTypeMap[model.CompaniesType], companyQuestions...) + questionsTypeMap[model.ContactsType] = append(questionsTypeMap[model.ContactsType], contactQuestions...) toCreated, toUpdate := tools.ToCreatedUpdateQuestionRules(questionsTypeMap, 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) { - for _, rules := range fieldRuleArrCurrent { - for questionID := range rules.Questionid { - for _, question := range questions { - if dataQues, ok := toUpdate[questionID]; ok { - if dataQues.Entity == currentEntity { - ruleMap := make(map[int]int) - ruleMap[questionID] = dataQues.FieldID - *fieldRule = append(*fieldRule, model.FieldRule{Questionid: ruleMap}) - break - } + constructFieldRules := func(fieldRuleArrCurrent map[int]int, questions []model.Question, fieldRule *model.FieldRule, currentEntity model.EntityType) { + ruleMap := make(map[int]int) + for questionID, fieldID := range fieldRuleArrCurrent { + if fieldID != 0 { + // если fieldID уже заполнен добавляем его как есть + ruleMap[questionID] = fieldID + continue + } + for _, question := range questions { + if dataQues, ok := toUpdate[questionID]; ok { + if dataQues.Entity == currentEntity { + ruleMap[questionID] = dataQues.FieldID + break } - if questionID == int(question.Id) { - // тут также делаем чтобы сверить филд с вопросом - title := strings.ToLower(strings.ReplaceAll(question.Title, " ", "")) - if title == "" { - question.Title = fmt.Sprintf("Вопрос №%d", question.Page) - } - title = strings.ToLower(strings.ReplaceAll(question.Title, " ", "")) - for _, field := range newFields { - fieldName := strings.ToLower(strings.ReplaceAll(field.Name, " ", "")) - if title == fieldName && field.Entity == currentEntity { - ruleMap := make(map[int]int) - ruleMap[questionID] = int(field.Amoid) - *fieldRule = append(*fieldRule, model.FieldRule{Questionid: ruleMap}) - } + } + if questionID == int(question.Id) { + // тут также делаем чтобы сверить филд с вопросом + title := strings.ToLower(strings.ReplaceAll(question.Title, " ", "")) + if title == "" { + question.Title = fmt.Sprintf("Вопрос №%d", question.Page) + } + title = strings.ToLower(strings.ReplaceAll(question.Title, " ", "")) + for _, field := range newFields { + fieldName := strings.ToLower(strings.ReplaceAll(field.Name, " ", "")) + if title == fieldName && field.Entity == currentEntity { + ruleMap[questionID] = int(field.Amoid) } } } } } + fieldRule.Questionid = ruleMap } - constructFieldRules(currentFieldsRule.Lead, leadQuestions, &lead, model.LeadsType) - constructFieldRules(currentFieldsRule.Customer, customerQuestions, &customer, model.CustomersType) - constructFieldRules(currentFieldsRule.Company, companyQuestions, &company, model.CompaniesType) + constructFieldRules(currentFieldsRule.Lead.Questionid, leadQuestions, &lead, model.LeadsType) + constructFieldRules(currentFieldsRule.Customer.Questionid, customerQuestions, &customer, model.CustomersType) + constructFieldRules(currentFieldsRule.Company.Questionid, companyQuestions, &company, model.CompaniesType) + constructFieldRules(currentFieldsRule.Contact.Questionid, contactQuestions, &contact, model.ContactsType) err = m.repo.AmoRepo.UpdateFieldRules(ctx, model.Fieldsrule{ Lead: lead, Customer: customer, Company: company, - Contact: model.ContactRules{ContactRuleMap: forAdding}, + Contact: model.ContactRules{ContactRuleMap: forAdding, Questionid: contact.Questionid}, }, msg.AccountID, msg.Rule.QuizID) if err != nil {