2024-04-29 12:23:40 +00:00
|
|
|
|
package tools
|
|
|
|
|
|
|
|
|
|
import (
|
2024-06-18 08:39:17 +00:00
|
|
|
|
"fmt"
|
2025-02-27 13:30:52 +00:00
|
|
|
|
"gitea.pena/SQuiz/amocrm/internal/models"
|
|
|
|
|
"gitea.pena/SQuiz/common/model"
|
2024-06-18 20:02:40 +00:00
|
|
|
|
"strings"
|
2024-04-29 12:23:40 +00:00
|
|
|
|
)
|
|
|
|
|
|
2024-06-15 10:33:35 +00:00
|
|
|
|
type ToUpdate struct {
|
|
|
|
|
FieldID int
|
|
|
|
|
Entity model.EntityType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ToCreatedUpdateQuestionRules(questionsTypeMap map[model.EntityType][]model.Question, currentFields []model.Field) (map[model.EntityType][]models.AddLeadsFields, map[int]ToUpdate) {
|
|
|
|
|
toUpdate := make(map[int]ToUpdate) // на обновление ключ id вопроса значение id кастомного поля для тех у кого имя совпадает
|
2024-05-02 15:30:12 +00:00
|
|
|
|
toCreated := make(map[model.EntityType][]models.AddLeadsFields)
|
|
|
|
|
for entity, questions := range questionsTypeMap {
|
|
|
|
|
for _, question := range questions {
|
2024-06-18 08:39:17 +00:00
|
|
|
|
// если заголоввок пустой у вопроса делаем ему заголовок чтоб в амо легли филды нормально
|
2024-06-23 15:17:28 +00:00
|
|
|
|
title := strings.ToLower(strings.ReplaceAll(question.Title, " ", ""))
|
2024-06-18 20:02:40 +00:00
|
|
|
|
if title == "" {
|
2024-06-23 15:17:28 +00:00
|
|
|
|
question.Title = fmt.Sprintf("Вопрос №%d", question.Page)
|
2024-06-18 08:39:17 +00:00
|
|
|
|
}
|
2024-06-23 15:17:28 +00:00
|
|
|
|
title = strings.ToLower(strings.ReplaceAll(question.Title, " ", ""))
|
2024-05-02 15:30:12 +00:00
|
|
|
|
matched := false
|
|
|
|
|
for _, field := range currentFields {
|
2024-06-23 15:17:28 +00:00
|
|
|
|
fieldName := strings.ToLower(strings.ReplaceAll(field.Name, " ", ""))
|
2024-06-23 14:52:17 +00:00
|
|
|
|
if title == fieldName && entity == field.Entity {
|
2024-06-15 10:33:35 +00:00
|
|
|
|
toUpdate[int(question.Id)] = ToUpdate{
|
|
|
|
|
FieldID: int(field.Amoid),
|
|
|
|
|
Entity: entity,
|
|
|
|
|
}
|
2024-05-02 15:30:12 +00:00
|
|
|
|
matched = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !matched {
|
2024-05-04 18:38:36 +00:00
|
|
|
|
//Type: model.TypeMapping[question.Type]
|
2024-06-19 15:41:23 +00:00
|
|
|
|
fieldType := model.TypeAmoTextarea
|
2024-05-10 08:51:56 +00:00
|
|
|
|
if question.Type == model.TypeFile {
|
|
|
|
|
fieldType = model.TypeAmoFile
|
|
|
|
|
}
|
|
|
|
|
toCreated[entity] = append(toCreated[entity], models.AddLeadsFields{Type: fieldType, Name: question.Title})
|
2024-05-02 15:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return toCreated, toUpdate
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-28 10:32:04 +00:00
|
|
|
|
func ToQuestionIDs(rule map[int]int) []int32 {
|
2024-05-02 15:30:12 +00:00
|
|
|
|
var ids []int32
|
2024-06-28 10:32:04 +00:00
|
|
|
|
for queID, fieldID := range rule {
|
|
|
|
|
if fieldID != 0 {
|
|
|
|
|
continue
|
2024-05-02 15:30:12 +00:00
|
|
|
|
}
|
2024-06-28 10:32:04 +00:00
|
|
|
|
ids = append(ids, int32(queID))
|
2024-05-02 15:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-06-18 20:34:02 +00:00
|
|
|
|
if field.Name == string(contactField) && field.Entity == model.ContactsType {
|
2024-05-07 15:56:02 +00:00
|
|
|
|
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
|
|
|
|
|
}
|