Merge branch 'amorule' into 'staging'

Amorule

See merge request backend/quiz/amocrm!19
This commit is contained in:
Mikhail 2024-06-28 18:37:03 +00:00 committed by skeris
commit c6fa5d5170
7 changed files with 107 additions and 146 deletions

2
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-20240628183520-89234a64c7fe
)
require (

6
go.sum

@ -142,5 +142,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-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=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240628183520-89234a64c7fe h1:KRz7Blk/yniyY1iC5omxS8yZPb/uBEm0HhM6HGhs6Rw=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240628183520-89234a64c7fe/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 {
if fieldValue != "" {
if fieldValue != "" && fieldMap[string(fieldType)] != 0 {
values := make([]models.ValueInterface, 0)
values = append(values, models.Values{Value: fieldValue})

@ -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
}

@ -6,6 +6,7 @@ import (
"amocrm/internal/tools"
"amocrm/pkg/amoClient"
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
@ -220,23 +221,28 @@ 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 entityType, rule := range entityRules {
for _, data := range allAnswers {
if fieldID, ok := rule.Questionid[int(data.QuestionID)]; ok {
if fieldID, ok := rule[int(data.QuestionID)]; ok {
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 {
if fieldData.Type == model.TypeAmoText || fieldData.Type == model.TypeAmoTextarea {
values := entityFieldsMap[entityType][fieldID]
content := strings.ReplaceAll(data.Content, " ", "")
if content == "" {
@ -260,7 +266,6 @@ func (wc *PostDeals) constructField(ctx context.Context, allAnswers []model.Resu
}
}
}
}
var leadFields []models.FieldsValues
var contactFields []models.FieldsValues
@ -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,

@ -568,11 +568,11 @@ 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
leadIDs, companyIDs, customerIDs, contactIDs []int32
leadQuestions, companyQuestions, customerQuestions, contactQuestions []model.Question
questionsTypeMap = make(map[model.EntityType][]model.Question)
newFields []model.Field
lead, company, customer []model.FieldRule
lead, company, customer, contact model.FieldRule
currentFieldsRule = msg.Rule.Fieldsrule
err error
)
@ -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,15 +668,18 @@ 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 {
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 := make(map[int]int)
ruleMap[questionID] = dataQues.FieldID
*fieldRule = append(*fieldRule, model.FieldRule{Questionid: ruleMap})
break
}
}
@ -687,26 +693,25 @@ func (m *Methods) CheckFieldRule(ctx context.Context, token string, msg models.K
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})
}
}
}
}
}
}
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 {

@ -447,20 +447,12 @@ components:
description: правила заполнения полей сущностей в амо
properties:
lead:
type: array
items:
$ref: '#/components/schemas/FieldRule'
contact:
type: array
items:
$ref: '#/components/schemas/ContactRules'
company:
type: array
items:
$ref: '#/components/schemas/FieldRule'
customer:
type: array
items:
$ref: '#/components/schemas/FieldRule'
TagsToAdd:
type: object
@ -842,25 +834,13 @@ components:
type: object
properties:
Lead:
type: array
items:
$ref: "#/components/schemas/FieldRule"
description: "правила заполнения полей сущностей в амо"
$ref: '#/components/schemas/FieldRule'
Contact:
type: array
items:
$ref: "#/components/schemas/ContactRules"
description: "правила заполнения полей контакта в амо, передавать не стоит, будут парситься из конфига опроса переданного в правиле"
$ref: '#/components/schemas/ContactRules'
Company:
type: array
items:
$ref: "#/components/schemas/FieldRule"
description: "правила заполнения полей сущностей в амо"
$ref: '#/components/schemas/FieldRule'
Customer:
type: array
items:
$ref: "#/components/schemas/FieldRule"
description: "правила заполнения полей сущностей в амо"
$ref: '#/components/schemas/FieldRule'
TagsToAdd:
type: object
properties:
@ -902,6 +882,11 @@ components:
type: string
additionalProperties:
type: integer
QuestionID:
type: integer
additionalProperties:
type: integer
description: сопоставление айдишника вопроса полю, которое будет заполняться ответом. соответственно QuestionID это айдишник вопроса. это я так мэпу пытался записать. Мапа, где ключи - QuestionID, значения - ID кастомного поля
responses:
'200':