add more checks for construct field rules

This commit is contained in:
Pavel 2024-06-15 13:33:35 +03:00
parent f17e6adfd9
commit b4e095de9f
2 changed files with 23 additions and 13 deletions

@ -44,15 +44,23 @@ import (
// return forUpdate // return forUpdate
//} //}
func ToCreatedUpdateQuestionRules(questionsTypeMap map[model.EntityType][]model.Question, currentFields []model.Field) (map[model.EntityType][]models.AddLeadsFields, map[int]int) { type ToUpdate struct {
toUpdate := make(map[int]int) // на обновление ключ id вопроса значение id кастомного поля для тех у кого имя совпадает 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 кастомного поля для тех у кого имя совпадает
toCreated := make(map[model.EntityType][]models.AddLeadsFields) toCreated := make(map[model.EntityType][]models.AddLeadsFields)
for entity, questions := range questionsTypeMap { for entity, questions := range questionsTypeMap {
for _, question := range questions { for _, question := range questions {
matched := false matched := false
for _, field := range currentFields { for _, field := range currentFields {
if question.Title == field.Name && entity == field.Entity { if question.Title == field.Name && entity == field.Entity {
toUpdate[int(question.Id)] = int(field.Amoid) toUpdate[int(question.Id)] = ToUpdate{
FieldID: int(field.Amoid),
Entity: entity,
}
matched = true matched = true
break break
} }

@ -663,19 +663,21 @@ func (m *Methods) CheckFieldRule(ctx context.Context, token string, msg models.K
} }
} }
constructFieldRules := func(fieldRuleArrCurrent []model.FieldRule, questions []model.Question, fieldRule *[]model.FieldRule) { constructFieldRules := func(fieldRuleArrCurrent []model.FieldRule, questions []model.Question, fieldRule *[]model.FieldRule, currentEntity model.EntityType) {
for _, rules := range fieldRuleArrCurrent { for _, rules := range fieldRuleArrCurrent {
for questionID := range rules.Questionid { for questionID := range rules.Questionid {
for _, question := range questions { for _, question := range questions {
if fieldID, ok := toUpdate[questionID]; ok { if dataQues, ok := toUpdate[questionID]; ok {
ruleMap := make(map[int]int) if dataQues.Entity == currentEntity {
ruleMap[questionID] = fieldID ruleMap := make(map[int]int)
*fieldRule = append(*fieldRule, model.FieldRule{Questionid: ruleMap}) ruleMap[questionID] = dataQues.FieldID
break *fieldRule = append(*fieldRule, model.FieldRule{Questionid: ruleMap})
break
}
} }
if questionID == int(question.Id) { if questionID == int(question.Id) {
for _, field := range newFields { for _, field := range newFields {
if question.Title == field.Name { if question.Title == field.Name && field.Entity == currentEntity {
ruleMap := make(map[int]int) ruleMap := make(map[int]int)
ruleMap[questionID] = int(field.Amoid) ruleMap[questionID] = int(field.Amoid)
*fieldRule = append(*fieldRule, model.FieldRule{Questionid: ruleMap}) *fieldRule = append(*fieldRule, model.FieldRule{Questionid: ruleMap})
@ -687,9 +689,9 @@ func (m *Methods) CheckFieldRule(ctx context.Context, token string, msg models.K
} }
} }
constructFieldRules(currentFieldsRule.Lead, leadQuestions, &lead) constructFieldRules(currentFieldsRule.Lead, leadQuestions, &lead, model.LeadsType)
constructFieldRules(currentFieldsRule.Customer, customerQuestions, &customer) constructFieldRules(currentFieldsRule.Customer, customerQuestions, &customer, model.CustomersType)
constructFieldRules(currentFieldsRule.Company, companyQuestions, &company) constructFieldRules(currentFieldsRule.Company, companyQuestions, &company, model.CompaniesType)
err = m.repo.AmoRepo.UpdateFieldRules(ctx, model.Fieldsrule{ err = m.repo.AmoRepo.UpdateFieldRules(ctx, model.Fieldsrule{
Lead: lead, Lead: lead,