amocrm/internal/tools/for_rules.go

122 lines
3.6 KiB
Go
Raw Normal View History

package tools
import (
"amocrm/internal/models"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
)
2024-05-17 19:13:22 +00:00
//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
//}
func ToCreatedUpdateQuestionRules(questionsTypeMap map[model.EntityType][]model.Question, currentFields []model.Field) (map[model.EntityType][]models.AddLeadsFields, map[int]int) {
toUpdate := make(map[int]int) // на обновление ключ id вопроса значение id кастомного поля для тех у кого имя совпадает
toCreated := make(map[model.EntityType][]models.AddLeadsFields)
for entity, questions := range questionsTypeMap {
for _, question := range questions {
matched := false
for _, field := range currentFields {
2024-05-05 12:22:54 +00:00
if question.Title == field.Name && entity == field.Entity {
toUpdate[int(question.Id)] = int(field.Amoid)
matched = true
break
}
}
if !matched {
2024-05-04 18:38:36 +00:00
//Type: model.TypeMapping[question.Type]
2024-05-10 08:51:56 +00:00
fieldType := model.TypeAmoText
if question.Type == model.TypeFile {
fieldType = model.TypeAmoFile
}
toCreated[entity] = append(toCreated[entity], models.AddLeadsFields{Type: fieldType, Name: question.Title})
}
}
}
return toCreated, toUpdate
}
func ToQuestionIDs(rules []model.FieldRule) []int32 {
var ids []int32
for _, rule := range rules {
for queID := range rule.Questionid {
ids = append(ids, int32(queID))
}
}
return ids
}
2024-05-07 15:56:02 +00:00
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
}