public static async Task GetADUser() { TimeSheetDb timesheetDb = new TimeSheetDb(); string NTI_Staff_GroupID = ConfigurationManager.AppSettings["ida:NTI_Staff_GroupID"]; var userList = new List <User>(); try { ActiveDirectoryClient client = UserProfileController.GetActiveDirectoryClient(); IGroup group = await client.Groups.GetByObjectId(NTI_Staff_GroupID).ExecuteAsync(); IGroupFetcher groupFetcher = group as IGroupFetcher; IPagedCollection <IDirectoryObject> pagedCollection = await groupFetcher.Members.ExecuteAsync(); if (pagedCollection != null) { do { List <IDirectoryObject> directoryObjects = pagedCollection.CurrentPage.ToList(); foreach (IDirectoryObject directoryObject in directoryObjects) { if (directoryObject is User) { var user = (User)directoryObject; userList.Add(user); } } pagedCollection = await pagedCollection.GetNextPageAsync(); } while (pagedCollection != null); } } catch (Exception e) { throw e; } if (userList != null) { List <ADUser> SystemUserList = timesheetDb.ADUsers.ToList(); if (SystemUserList.Count != userList.Count) { foreach (var item in userList) { if (timesheetDb.ADUsers.Find(item.Mail) == null) { timesheetDb.ADUsers.Add(new ADUser { UserName = item.DisplayName, Email = item.Mail, JobCode = item.JobTitle, Department = item.Department } ); } } timesheetDb.SaveChanges(); } } }
static public void UpdateLeaveBalance() { // Only updates when it is the end day of a pay period if ((DateTime.Now - FirstPayPeriod).Days % 14 == 0) { using (TimeSheetDb context = new TimeSheetDb()) { const double auunalAccuralRate = 0.076923; const double sickAccuralRate = 0.038462; _leaveType leaveType; double rate; List <ADUser> users = context.ADUsers.Where(u => u.ContractHours != 0).ToList(); foreach (var user in users) { for (int i = 0; i < 2; i++) { if (i == 0) { leaveType = _leaveType.annual; rate = auunalAccuralRate; } else { leaveType = _leaveType.sick; rate = sickAccuralRate; } // Update leaves balances // Formula: Current leaves - leaves taken in the pay period + (accural Rate * contract hours) LeaveBalance balance = context.LeaveBalances.Find(user.Email, leaveType); if (balance == null) { balance = new LeaveBalance { UserID = user.Email, UserName = user.UserName, LeaveType = leaveType, AvailableLeaveHours = 0.0 }; context.LeaveBalances.Add(balance); } else { balance.AvailableLeaveHours += user.ContractHours * rate; context.Entry(balance).State = EntityState.Modified; } } } context.SaveChanges(); } } }