107 lines
3.4 KiB
Go
107 lines
3.4 KiB
Go
package tools
|
||
|
||
import (
|
||
"fmt"
|
||
"penahub.gitlab.yandexcloud.net/backend/quiz/bitrix/internal/models"
|
||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
||
"strings"
|
||
)
|
||
|
||
func ToQuestionIDs(rule map[int]string) []int32 {
|
||
var ids []int32
|
||
for queID, fieldID := range rule {
|
||
if fieldID != "" {
|
||
continue
|
||
}
|
||
ids = append(ids, int32(queID))
|
||
}
|
||
return ids
|
||
}
|
||
|
||
func ForContactRules(quizConfig model.QuizContact, currentFields []model.BitrixField) ([]models.AddFields, map[string]string) {
|
||
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]string)
|
||
var toCreated []models.AddFields
|
||
for _, contactField := range contactFieldsArr {
|
||
matched := false
|
||
for _, field := range currentFields {
|
||
if field.EditFromLabel == string(contactField) && field.EntityID == model.FieldTypeContact {
|
||
matched = true
|
||
forAdding[string(contactField)] = field.BitrixID
|
||
break
|
||
}
|
||
}
|
||
|
||
if !matched {
|
||
toCreated = append(toCreated, models.AddFields{
|
||
UserTypeID: model.StringCustomFieldsType,
|
||
EditFormLabel: string(contactField),
|
||
ListColumnLabel: string(contactField),
|
||
})
|
||
forAdding[string(contactField)] = ""
|
||
}
|
||
}
|
||
|
||
return toCreated, forAdding
|
||
}
|
||
|
||
type ToUpdate struct {
|
||
FieldID string
|
||
Entity model.FieldsType
|
||
}
|
||
|
||
func ToCreatedUpdateQuestionRules(questionsTypeMap map[model.FieldsType][]model.Question, currentFields []model.BitrixField) (map[model.FieldsType][]models.AddFields, map[int]ToUpdate) {
|
||
toUpdate := make(map[int]ToUpdate) // на обновление ключ id вопроса значение id кастомного поля для тех у кого имя совпадает
|
||
toCreated := make(map[model.FieldsType][]models.AddFields)
|
||
for entity, questions := range questionsTypeMap {
|
||
for _, question := range questions {
|
||
// если заголоввок пустой у вопроса делаем ему заголовок чтоб в амо легли филды нормально
|
||
title := strings.ToLower(strings.ReplaceAll(question.Title, " ", ""))
|
||
if title == "" {
|
||
question.Title = fmt.Sprintf("Вопрос №%d", question.Page)
|
||
}
|
||
title = strings.ToLower(strings.ReplaceAll(question.Title, " ", ""))
|
||
matched := false
|
||
for _, field := range currentFields {
|
||
fieldName := strings.ToLower(strings.ReplaceAll(field.EditFromLabel, " ", ""))
|
||
if title == fieldName && entity == field.EntityID {
|
||
toUpdate[int(question.Id)] = ToUpdate{
|
||
FieldID: field.BitrixID,
|
||
Entity: entity,
|
||
}
|
||
matched = true
|
||
break
|
||
}
|
||
}
|
||
|
||
if !matched {
|
||
fieldType := model.StringCustomFieldsType
|
||
if question.Type == model.TypeFile {
|
||
fieldType = model.FileCustomFieldsType
|
||
}
|
||
toCreated[entity] = append(toCreated[entity], models.AddFields{
|
||
UserTypeID: fieldType,
|
||
EditFormLabel: question.Title,
|
||
ListColumnLabel: question.Title,
|
||
})
|
||
}
|
||
}
|
||
}
|
||
|
||
return toCreated, toUpdate
|
||
}
|