Ejemplo n.º 1
0
        public Guid?CreateContact(long contactId)
        {
            Guid?contactGuid = null;
            FreshdeskContactDto freshdeskContactDto = _freshdeskRepo.GetContact(contactId);

            if (freshdeskContactDto != null)
            {
                Entity contactEntity = _contactMapper.CreateContact(freshdeskContactDto);

                if (contactEntity != null)
                {
                    var result = UpsertContact(contactEntity);

                    contactGuid = result.EntityId;
                }
            }

            return(contactGuid);
        }
        static void ContactSync()
        {
            DateTime?lastSyncRecord = null;

            void ExecuteFreshdesk()
            {
                try
                {
                    IEnumerable <FreshdeskContactDto> contactDtos = freshdesk.GetContacts();

                    if (contactDtos.Any())
                    {
                        lastSyncRecord = contactDtos.OrderByDescending(t => t.UpdatedAt).First().UpdatedAt.DateTime;
                    }

                    foreach (FreshdeskContactDto contact in contactDtos)
                    {
                        try
                        {
                            Entity contactEntity = contactMapper.CreateContact(contact);

                            crm.UpsertContact(contactEntity);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            ExecuteFreshdesk();

            /*
             *
             * int page = 1;
             * var crmContacts = crm.GetContacts(500, page);
             *
             * void ExecuteCrm()
             * {
             *  foreach (var contact in crmContacts.Entities)
             *  {
             *      try
             *      {
             *          FreshdeskContactForCreation freshdeskContact = contactMapper.CreateContact(contact);
             *
             *
             *          if (contact.Contains("isw_freshdeskid"))
             *          {
             *              long freshdeskId = long.Parse(contact["isw_freshdeskid"].ToString());
             *              freshdesk.UpdateContact(freshdeskId, freshdeskContact);
             *          }
             *
             *          FreshdeskContactDto createdFreshdeskContact = freshdesk.CreateContact(freshdeskContact);
             *
             *          Entity contactEntity = contactMapper.CreateContact(createdFreshdeskContact);
             *
             *          crm.UpsertContact(contactEntity);
             *      }
             *      catch (Exception)
             *      {
             *      }
             *  }
             * }
             *
             * do
             * {
             *  ExecuteCrm();
             *  page++;
             *
             *  if (crmContacts.MoreRecords)
             *      crmContacts = crm.GetAccounts(500, page, crmContacts.PagingCookie);
             *  else
             *      break;
             * } while (crmContacts.MoreRecords);
             *
             */

            UpdateAppsettings("ContactLastSyncDate", 30, lastSyncRecord);
        }