private PortalActionResult UpdateZohoIfNeeded(ZohoContact contact, Models.ZohoPartnerPortal partnerPortal, string oldPortalAdminContactId)
        {
            PortalActionResult result = new PortalActionResult();

            StringBuilder message = new StringBuilder();

            result.IsSuccess = false;

            var needUpdateContact       = true;
            var needUpdatePartnerPortal = true;
            var needUpdateOldContact    = false;
            //var needUpdateAccount = false;


            ZohoContact zohoOldAdminContact = null;

            if (!string.IsNullOrEmpty(oldPortalAdminContactId))
            {
                zohoOldAdminContact =
                    ZohoRepository.Contacts.FirstOrDefault(x => x.ContactID == oldPortalAdminContactId);
                if (zohoOldAdminContact.PortalAdmin)
                {
                    needUpdateOldContact = true;
                }
            }

            if (contact.PortalAdmin)
            {
                needUpdateContact = false;
                message.AppendLine("Portal Admin is true in Database, ignore update");
            }

            if (partnerPortal.ACLCreated.Equals("true", StringComparison.CurrentCultureIgnoreCase))
            {
                needUpdatePartnerPortal = false;
                message.AppendLine("Portal Portal ACL Created is true in Database, ignore update");
            }


            var account = ZohoRepository.Accounts.FirstOrDefault(x => x.AccountId == partnerPortal.PartnerAccount);

            if (account != null && account.PortalEnabled == false)
            {
                using (ZohoCRMProxy.ZohoCRMAccount accountRequest = new ZohoCRMProxy.ZohoCRMAccount(_zohoToken))
                {
                    try
                    {
                        var zAccount = new ZohoCRMProxy.ZohoAccount
                        {
                            Id            = partnerPortal.PartnerAccount,
                            PortalEnabled = true
                        };
                        accountRequest.Update(zAccount);
                        result.IsSuccess = true;
                        message.AppendLine("Zoho Account Updated.");
                    }
                    catch (Exception e)
                    {
                        result.IsSuccess = false;
                        message.AppendLine(e.Message);
                    }
                }
            }


            if (needUpdateContact || needUpdateOldContact)
            {
                var zohoContact = new ZohoCRMProxy.ZohoContact
                {
                    Id                   = contact.ContactID,
                    PortalAdmin          = true,
                    PartnerPortalEnabled = false,
                    SAPUnqualified       = contact.SAPUnqualified.Equals("true", StringComparison.CurrentCultureIgnoreCase),
                    EmailOptOut          = contact.EmailOptOut.Equals("true", StringComparison.CurrentCultureIgnoreCase),
                };

                using (ZohoCRMProxy.ZohoCRMContact request = new ZohoCRMContact(_zohoToken))
                {
                    try
                    {
                        if (needUpdateContact)
                        {
                            var contactResult = request.Update(zohoContact);

                            result.IsSuccess = true;
                            message.AppendLine("Zoho Contact Portal Admin Updated.");
                        }

                        if (needUpdateOldContact)
                        {
                            var oldAdminContactResult = request.Update(new ZohoCRMProxy.ZohoContact
                            {
                                Id                   = zohoOldAdminContact.ContactID,
                                PortalAdmin          = false,
                                PartnerPortalEnabled = true,
                                SAPUnqualified       =
                                    zohoOldAdminContact.SAPUnqualified.Equals("true",
                                                                              StringComparison.CurrentCultureIgnoreCase),
                                EmailOptOut =
                                    zohoOldAdminContact.EmailOptOut.Equals("true",
                                                                           StringComparison.CurrentCultureIgnoreCase),
                            });

                            result.IsSuccess = true;
                            message.AppendLine("Zoho Old Portal Admin Contact Updated.");
                        }
                    }
                    catch (Exception e)
                    {
                        result.IsSuccess = false;
                        message.AppendLine(e.Message);
                    }
                }
            }

            if (needUpdatePartnerPortal && result.IsSuccess)
            {
                var portal = new ZohoCRMProxy.ZohoPartnerPortal
                {
                    Id         = partnerPortal.PartnerPortalID,
                    ACLCreated = true
                };
                using (ZohoCRMProxy.ZohoCRMPartnerPortal portalRequest = new ZohoCRMProxy.ZohoCRMPartnerPortal(_zohoToken))
                {
                    try
                    {
                        portalRequest.Update(portal);
                        result.IsSuccess = true;
                        message.AppendLine("Zoho Partner Portal ACL Created Updated.");
                    }
                    catch (Exception e)
                    {
                        result.IsSuccess = false;
                        message.AppendLine(e.Message);
                    }
                }
            }



            result.Message = message.ToString();

            return(result);
        }
        protected override async Task <PortalActionResult> ProcessImport(string id = "")
        {
            string validateResult = await ValidatePartnerPortalAsync(id);

            if (!string.IsNullOrEmpty(validateResult))
            {
                return(new PortalActionResult
                {
                    IsSuccess = false,
                    Message = validateResult
                });
            }

            var partnerPortal =
                ZohoRepository.PartnerPortals.FirstOrDefault(x => x.PartnerPortalID.Equals(id));

            var accountId = partnerPortal.PartnerAccount;

            var contactId = partnerPortal.PortalAdmin;

            //PortalActionResult myobResult = await MyobContactCustomerDataSyncAysnc(accountId, id);
            //if (!myobResult.IsSuccess)
            //{
            //    return myobResult;
            //}


            //var subject = myobResult.IsSuccess
            //    ? $"[Account:{accountId}] added to Myob"
            //    : $"[Account:{accountId}] could not add to Myob";

            //var tos = _myobApiService.SalesEmail.Split(new char[] { ';' }).ToList();
            //var body = string.IsNullOrEmpty(myobResult.Message) ? "Success" : myobResult.Message;

            ////do not need wait for email result
            //EmailSender.SendEmailAsync(subject, body, tos);


            //check contact created in company
            var company = await _userManager.GetCompanyByZohoAccountIdAsync(accountId);

            int companyId;

            if (company == null)
            {
                companyId = await _userManager.CreateCompanyAsync(new Company
                {
                    CompanyZohoAccountId = accountId,
                    CreatedBy            = "importer",
                    CreatedTime          = DateTime.Now
                });
            }
            else
            {
                companyId = company.Id;
            }

            string oldPortalAdminZohoContactId = await UpdateOldPortalAdminIfNeededAsync(companyId, contactId);

            //check user information
            var contact = ZohoRepository.Contacts.FirstOrDefault(x => x.ContactID == contactId);

            var user = await _userManager.FindByEmailAsync(contact.Email);

            var message   = string.Empty;
            var isSuccess = false;

            if (user == null)
            {
                PortalActionResult updateResult
                    = await CreateUserAndAddUserAsPortalAdminAsync(contact, companyId);

                isSuccess = updateResult.IsSuccess;
                message   = updateResult.Message;

                //create user
            }
            else
            {
                var updateResult
                    = await UpdateExistingUserAsPortalAdminAsync(user, companyId);

                isSuccess = updateResult.IsSuccess;
                message   = updateResult.Message;
            }

            if (isSuccess)
            {
                var updateResult = UpdateZohoIfNeeded(contact, partnerPortal, oldPortalAdminZohoContactId);
                return(new PortalActionResult
                {
                    IsSuccess = updateResult.IsSuccess,
                    Message = string.Format("{0}\r\n{1}", message, updateResult.Message)
                });
            }
            else
            {
                return(new PortalActionResult
                {
                    IsSuccess = false,
                    Message = message
                });
            }
        }
        private PortalActionResult ValidateAccount(Models.ZohoAccount account)
        {
            var message = new StringBuilder();

            var result = new PortalActionResult
            {
                IsSuccess = true
            };

            if (string.IsNullOrEmpty(account.AccountName))
            {
                result.IsSuccess = false;
                message.AppendLine("Account Name is empty");
            }

            if (string.IsNullOrEmpty(account.Phone))
            {
                result.IsSuccess = false;
                message.AppendLine("Phone is empty");
            }

            //if (string.IsNullOrEmpty(account.Fax))
            //{
            //    result.IsSuccess = false;
            //    message.AppendLine("Fax is empty");
            //}

            //if (string.IsNullOrEmpty(account.Website))
            //{
            //    result.IsSuccess = false;
            //    message.AppendLine("Website is empty");
            //}

            //
            if (string.IsNullOrEmpty(account.BillingStreet))
            {
                result.IsSuccess = false;
                message.AppendLine("Billing Street is empty");
            }

            if (string.IsNullOrEmpty(account.BillingCity))
            {
                result.IsSuccess = false;
                message.AppendLine("Billing City is empty");
            }

            if (string.IsNullOrEmpty(account.BillingState))
            {
                result.IsSuccess = false;
                message.AppendLine("Billing State is empty");
            }

            if (string.IsNullOrEmpty(account.BillingCode))
            {
                result.IsSuccess = false;
                message.AppendLine("Billing Postcode is empty");
            }

            if (string.IsNullOrEmpty(account.BillingCountry))
            {
                result.IsSuccess = false;
                message.AppendLine("Billing Country is empty");
            }

            //if (string.IsNullOrEmpty(account.AbnCompanyNum))
            //{
            //    result.IsSuccess = false;
            //    message.AppendLine("Company ABN is empty");
            //}

            //if (string.IsNullOrEmpty(account.MyobDataFile) ||
            //    !_myobApiService.MyobOptions.MyobCompanyFileOptions.ContainsKey(account.MyobDataFile))
            //{
            //    result.IsSuccess = false;
            //    message.AppendLine($"Account {account.AccountId} myob Data file {account.MyobDataFile} mapping not exist");
            //}

            //if (string.IsNullOrEmpty(account.MyobDataFile) ||
            //    !_myobApiService.ContactCustomerImportOptions.ContainsKey(account.MyobDataFile))
            //{
            //    result.IsSuccess = false;
            //    message.AppendLine($"Account {account.AccountId} myob Contact Customer options {account.MyobDataFile} not exist");
            //}


            result.Message = message.ToString();
            return(result);
        }