public SyncService()
 {
     AdUserService = new AdUserService();
     DbUserService = new DbUserService();
     HelperService = new HelperService();
     FaxApiService = new FaxApiService();
     LogService    = new LogService();
 }
        private void SyncMissingDbUsers()
        {
            this.LogService.LogEvent("START: Sync missing users in was/is table");
            var adUsers      = AdUserService.GetCachedUsers();
            var dbUsers      = DbUserService.GetAllUsers();
            var dbUsersIds   = dbUsers.Select(x => x.AttorneyUserID.ToLower()).ToList();
            var missingUsers = adUsers.Where(x => !dbUsersIds.Contains(x.UserId.ToLower())).ToList();

            DbUserService.AddMissingUsers(missingUsers);
            this.LogService.LogEvent("FINISH: Sync missing users in was/is table");
        }
        public void Sync(int?logSessionId = null)
        {
            LogService.LogStartEvent("START - xMedius Script - Sync Users Script");
            LogService.LogEvent("Pulling users from Active Directory");
            var adUsers = AdUserService.GetCachedUsers();

            LogService.LogEvent("Pulling data from xMedius - Users,Groups,FaxNumbeers");
            var blackListedNumbers  = HelperService.GetBlackListedFaxNumbers();
            var faxNumbers          = FaxApiService.GetAllFaxNumbers().data;
            var faxUsers            = FaxApiService.GetAllUsers().data;
            var faxGroups           = FaxApiService.GetAllFaxGroups().data;
            var lstXMediusDomainObj = new List <XMediusUser>();

            LogService.LogEvent("Analyzing data...");
            foreach (var usr in adUsers)
            {
                var buildUser = new XMediusUser(usr as AdUser, faxGroups, faxUsers, faxNumbers, blackListedNumbers);
                lstXMediusDomainObj.Add(buildUser);
            }

            var usersWithActions       = lstXMediusDomainObj.Where(x => x.ActionList.Any()).ToList();
            var disabledUsers          = usersWithActions.Where(x => x.AdUser.Disabled).ToList();
            var updatedUsers           = usersWithActions.Where(x => x.ActionList.Where(y => y.ActionReason == ActionSyncReason.UpdateUserFaxGroup).Any()).ToList();
            var updatedUsersFax        = usersWithActions.Where(x => x.ActionList.Where(y => y.ActionReason == ActionSyncReason.UserIsUpdated).Any()).ToList();
            var userWithouFaxAndOFfice = usersWithActions.Where(x => x.ActionList.Where(y => y.ActionReason == ActionSyncReason.UserWihtoutFaxAndOffice).Any()).ToList();
            var restOfUsersNew         = usersWithActions.Where(x => x.ActionList.Where(y => y.ActionType == ActionSyncType.AddUser).Any()).ToList();
            var restOfUsersRemove      = usersWithActions.Where(x => x.ActionList.Where(y => y.ActionType == ActionSyncType.RemoveUser).Any()).ToList();
            var restOfUsersUpdate      = usersWithActions.Where(x => x.ActionList.Where(y => y.ActionType == ActionSyncType.UpdateUser).Any()).ToList();

            LogService.LogEvent($"Stats - AddUsers:{restOfUsersNew.Count()} RemoveUsers:{restOfUsersRemove.Count()} UpdateUsers:{restOfUsersUpdate.Count()}", false);

            restOfUsersNew.ForEach(x => x.ActionList.ForEach(y => LogService.LogEvent(y.ToString())));
            restOfUsersUpdate.ForEach(x => x.ActionList.ForEach(y => LogService.LogEvent(y.ToString())));
            restOfUsersRemove.ForEach(x => x.ActionList.ForEach(y => LogService.LogEvent(y.ToString())));

            LogService.LogEvent("Sync DONE!");

            File.WriteAllLines("XMedius_SyncUsers_Log_" + DateTime.Now.ToString("yyyyMMddHHmmssffff"), LogService.LogEventsList);
        }
        private Attorney BuildAttorney(IDbUser dbUser, List <string> blackListedFaxNumbers)
        {
            var attorney = AdUserService.GetUserById(dbUser.AttorneyUserID);

            if (attorney == null)
            {
                return(null);
            }

            var previousAssistant = AdUserService.GetUserById(dbUser.PreviousAssistantUserId);
            var currentAssistant  = AdUserService.GetUserById(dbUser.CurrentAssistantUserId);
            var newAssistant      = AdUserService.GetUserById(attorney.AssistantId);

            var attorneyObject = new Attorney(attorney.UserId, attorney.DisplayName, attorney.Disabled, attorney.Excluded);

            if (dbUser.PreviousAssistantUserId.IsNotEmpty())
            {
                attorneyObject.SetPreviousAssistant(previousAssistant?.UserId ?? dbUser.PreviousAssistantUserId, previousAssistant?.DisplayName ?? "");
            }

            if (dbUser.CurrentAssistantUserId.IsNotEmpty())
            {
                attorneyObject.SetCurrentAssistant(currentAssistant?.UserId ?? dbUser.CurrentAssistantUserId, currentAssistant?.DisplayName ?? "", currentAssistant?.Disabled ?? false);
            }

            if (newAssistant != null)
            {
                attorneyObject.SetNewAssistant(newAssistant.UserId, newAssistant.DisplayName, newAssistant.Disabled);
            }

            attorneyObject.SetFaxNumber(dbUser.PreviousFaxNumber, dbUser.CurrentFaxNumber, blackListedFaxNumbers);
            attorneyObject.SetNewFaxNumber(attorney.FaxNumber, blackListedFaxNumbers);
            attorneyObject.Process();

            return(attorneyObject);
        }