private async Task <IReadOnlyList <string> > NotifyUsersOverChatAsync( IHangoutsChatConnector connector, TimesheetStates state, Timesheet[] timesheets) { var notifiedUserList = new List <string>(); var addressesForUpdate = new List <GoogleAddress>(); var storeAddresses = await _storageService.GetAddressesAsync(); var emails = timesheets.Select(it => it.UserEmail).ToArray(); var filteredAddresses = storeAddresses.Where(it => emails.Contains(it.UserEmail)).ToArray(); IReadOnlyList <GoogleAddress> privateAddresses = new GoogleAddress[0]; if (filteredAddresses.Length < timesheets.Length) { var storeAddressesNames = storeAddresses.Select(it => it.SpaceName).Distinct().ToArray(); privateAddresses = connector .GetPrivateAddress(storeAddressesNames) .Select(it => new GoogleAddress { SpaceName = it.Space.Name, UserName = it.Sender.Name, UserDisplayName = it.Sender.DisplayName }) .ToArray(); // Store inactive spaces, so not to be requested the next time addressesForUpdate.AddRange(privateAddresses.Where(it => it.UserName == null)); } var textMessage = OpenAirText.GetText(state, OpenAirTextTypes.Notify); foreach (var timesheet in timesheets) { var message = timesheet.UserName + textMessage; var addr = filteredAddresses.FirstOrDefault(it => it.UserEmail == timesheet.UserEmail); if (addr == null) { addr = privateAddresses.FirstOrDefault(it => it.UserDisplayName == timesheet.UserName); if (addr == null) { continue; } addr.UserEmail = timesheet.UserEmail; addressesForUpdate.Add(addr); } notifiedUserList.Add(timesheet.UserName); await connector.SendMessageAsync( message, new GoogleChatAddress(addr.SpaceName, string.Empty, "DM", addr.UserName, addr.UserDisplayName)); } if (addressesForUpdate.Count > 0) { await _storageService.AddAddressesAsync(addressesForUpdate); } return(notifiedUserList); }
/// <summary>Processes the specified timesheets.</summary> private async Task ProcessNotify( IReadOnlyList <Timesheet> timesheets, string department, bool notify, GoogleChatAddress address, IHangoutsChatConnector connector) { string text; var filteredTimesheet = department == null ? timesheets : timesheets .Where(it => it.DepartmentName.Equals(department, StringComparison.InvariantCultureIgnoreCase)) .ToArray(); var notifiedUserList = new List <string>(); if (filteredTimesheet.Count == 0) { text = "<b>All user have submitted timesheets.</b>"; } else if (notify && filteredTimesheet.Count > 0) { var emails = filteredTimesheet.Select(it => it.UserEmail).ToArray(); var storeAddresses = _storageService.GetAddresses(); var filteredAddresses = storeAddresses.Where(it => emails.Contains(it.UserEmail)).ToArray(); var addressesForUpdate = new List <GoogleAddress>(); IReadOnlyList <GoogleAddress> privateAddresses = new GoogleAddress[0]; if (filteredAddresses.Length < timesheets.Count) { var storeAddressesNames = storeAddresses.Select(it => it.SpaceName).ToArray(); privateAddresses = connector .GetPrivateAddress(storeAddressesNames) .Select(it => new GoogleAddress { SpaceName = it.Space.Name, UserName = it.Sender.Name, UserDisplayName = it.Sender.DisplayName }) .ToArray(); // Store inactive spaces, so not to be requested the next time addressesForUpdate.AddRange(privateAddresses.Where(it => it.UserName == null)); } foreach (var timesheet in filteredTimesheet) { var message = $"{timesheet.UserName}, Please submit your timesheet. Your current hours are {timesheet.Total}."; var addr = filteredAddresses.FirstOrDefault(it => it.UserEmail == timesheet.UserEmail); if (addr == null) { addr = privateAddresses.FirstOrDefault(it => it.UserDisplayName == timesheet.UserName); if (addr == null) { continue; } addr.UserEmail = timesheet.UserEmail; addressesForUpdate.Add(addr); } notifiedUserList.Add(timesheet.UserName); await connector.SendMessageAsync( message, new GoogleChatAddress(addr.SpaceName, string.Empty, "DM", addr.UserName, addr.UserDisplayName)); } if (addressesForUpdate.Count > 0) { await _storageService.AddAddressesAsync(addressesForUpdate); } text = notifiedUserList.Count == filteredTimesheet.Count ? "All users width unsubmitted timesheets are notified! Total of " + notifiedUserList.Count : "The following people where not notified: <br>" + GetCardText(filteredTimesheet, notifiedUserList); } else { text = "The following people have to submit timesheet: <br>" + GetCardText(filteredTimesheet, notifiedUserList); } var paragraph = new TextParagraph { Text = text }; var card = ChatEventFactory.CreateCard(paragraph); await connector.SendMessageAsync(null, address, card); }