Esempio n. 1
0
        public List <Amo_id> Run()
        {
            if (_lead1C.amo_ids is null)
            {
                _lead1C.amo_ids = new();
            }

            try
            {
                int amo_acc = 28395871;
                if (_lead1C.is_corporate)
                {
                    amo_acc = 19453687;
                }
                if (_lead1C.organization == "ООО «Первый Профессиональный Институт Эстетики»")
                {
                    amo_acc = 29490250;
                }

                var leadRepo = _amo.GetAccountById(amo_acc).GetRepo <Lead>();

                #region Checking if lead already linked to entity an updating if possible
                if (_lead1C.amo_ids.Any(x => x.account_id == amo_acc))
                {
                    try
                    {
                        UpdateLeadInAmo(_lead1C, leadRepo, _lead1C.amo_ids.First().entity_id, amo_acc, _filter);

                        _log.Add($"Updated lead {_lead1C.amo_ids.First().entity_id} in amo {amo_acc}.");

                        return(_lead1C.amo_ids);
                    }
                    catch (Exception e)
                    {
                        _log.Add($"Unable to update existing lead {_lead1C.amo_ids.First().entity_id} in amo. Creating new.");
                    }
                }
                #endregion

                #region Getting connected entitites ids
                Client1C  client1C  = new ClientRepository(_cred1C).GetClient((Guid)_lead1C.client_id_1C);
                Course1C  course1C  = new CourseRepository(_cred1C).GetCourse((Guid)_lead1C.product_id_1C);
                Company1C company1C = null;
                if (_lead1C.is_corporate)
                {
                    company1C = new CompanyRepository(_cred1C).GetCompany((Guid)_lead1C.company_id_1C);
                }

                var contact_id = 0;
                var course_id  = 0;
                var company_id = 0;

                if (client1C.amo_ids is not null &&
                    client1C.amo_ids.Any(x => x.account_id == amo_acc))
                {
                    contact_id = client1C.amo_ids.First(x => x.account_id == amo_acc).entity_id;
                }

                if (course1C.amo_ids is not null &&
                    course1C.amo_ids.Any(x => x.account_id == amo_acc))
                {
                    course_id = course1C.amo_ids.First(x => x.account_id == amo_acc).entity_id;
                }

                if (company1C is not null &&
                    company1C.amo_ids is not null &&
                    company1C.amo_ids.Any(x => x.account_id == amo_acc))
                {
                    company_id = company1C.amo_ids.First(x => x.account_id == amo_acc).entity_id;
                }
                #endregion

                #region Creating new lead
                var lead_id = CreateLeadInAmo(_lead1C, leadRepo, amo_acc, contact_id, course_id, company_id, _filter);
                _lead1C.amo_ids.Add(new()
                {
                    account_id = amo_acc,
                    entity_id  = lead_id
                });

                _log.Add($"Created lead {lead_id} in amo {amo_acc}.");
                #endregion
            }
            catch (Exception e)
            {
                _log.Add($"Unable to create or update lead {_lead1C.lead_id_1C} in amo: {e.Message}");
            }

            return(_lead1C.amo_ids);
        }
Esempio n. 2
0
 public PopulateCourses(Amo amo, Log log, Cred1C cred1C)
 {
     _amo    = amo;
     _log    = log;
     _repo1C = new(cred1C);
 }