Beispiel #1
0
 public void TestInitialize()
 {
     _storageService        = Substitute.For <IStorageService>();
     _cognitiveService      = Substitute.For <ICognitiveService>();
     _hangoutsChatConnector = Substitute.For <IHangoutsChatConnector>();
     _timesheetService      = new TimesheetService(_storageService, _cognitiveService, _hangoutsChatConnector);
 }
Beispiel #2
0
 /// <summary>Get timesheets and notifies by message or email the users asynchronous.</summary>
 public async Task NotifyAsync(
     DateTime date,
     TimesheetStates state,
     string email,
     IReadOnlyList <string> customersToExclude,
     string department,
     bool notify,
     bool notifyByEmail,
     bool filterOutSender,
     GoogleChatAddress address,
     IHangoutsChatConnector connector) =>
 await SendTimesheetNotificationsToUsersAsync(
     await _openAirConnector.GetUnsubmittedTimesheetsAsync(
         date,
         Contract.LocalDateTime.Date,
         state,
         email,
         filterOutSender,
         TimesheetsProperties.UserMaxHours,
         customersToExclude),
     email,
     department,
     notify,
     notifyByEmail,
     state,
     address,
     connector);
 /// <summary>Initializes a new instance of the <see cref="TimesheetService"/> class.</summary>
 public TimesheetService(
     IStorageService storageService,
     ICognitiveService cognitiveService,
     IHangoutsChatConnector hangoutsChatConnector)
 {
     _storageService        = storageService;
     _cognitiveService      = cognitiveService;
     _hangoutsChatConnector = hangoutsChatConnector;
 }
Beispiel #4
0
 /// <summary>Get timesheets and notifies by message or email the users asynchronous.</summary>
 public Task NotifyAsync(
     DateTime date,
     TimesheetStates state,
     string email,
     string[] customersToExclude,
     string department,
     bool notify,
     bool notifyByEmail,
     GoogleChatAddress address,
     IHangoutsChatConnector connector) =>
 _openAirConnector.GetUnsubmittedTimesheetsAsync(date, state, email, customersToExclude)
 .ContinueWith(task => ProcessNotifyAsync(
                   task.Result,
                   email,
                   department,
                   notify,
                   notifyByEmail,
                   state,
                   address,
                   connector));
Beispiel #5
0
        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);
        }
Beispiel #6
0
        /// <summary>Processes the specified timesheets.</summary>
        public async Task SendTimesheetNotificationsToUsersAsync(
            IReadOnlyList <Timesheet> timesheets,
            string email,
            string department,
            bool notify,
            bool notifyByEmail,
            TimesheetStates state,
            GoogleChatAddress address,
            IHangoutsChatConnector connector)
        {
            string text;
            var    filteredTimesheet = timesheets
                                       .Where(it =>
                                              department == null ||
                                              department.Equals(it.DepartmentName, StringComparison.InvariantCultureIgnoreCase))
                                       .OrderBy(it => it.ManagerName)
                                       .ThenBy(it => it.UserName)
                                       .ToArray();

            var notifiedUserList = new List <string>();

            if (filteredTimesheet.Length == 0)
            {
                var responses = OpenAirText.GetText(state, OpenAirTextTypes.AllAreDone).Split('|', StringSplitOptions.RemoveEmptyEntries);
                var rand      = new Random();
                text = responses[rand.Next(0, responses.Length - 1)];
            }
            else if (notify && filteredTimesheet.Length > 0)
            {
                notifiedUserList.AddRange(
                    await NotifyUsersOverChatAsync(connector, state, filteredTimesheet));

                if (notifyByEmail)
                {
                    var textMessage = OpenAirText.GetText(state, OpenAirTextTypes.Notify);
                    var emails      = filteredTimesheet
                                      .Where(it => !notifiedUserList.Contains(it.UserName))
                                      .Apply(it => notifiedUserList.Add(it.UserName))
                                      .Select(it => it.UserEmail)
                                      .Distinct()
                                      .ToArray();

                    await _mailService.SendMailAsync("Timesheet is pending", textMessage, emails);
                }

                text = notifiedUserList.Count == filteredTimesheet.Length ?
                       string.Format(
                    CultureInfo.InvariantCulture,
                    OpenAirText.GetText(state, OpenAirTextTypes.AllAreNotified),
                    notifiedUserList.Count) :
                       OpenAirText.GetText(state, OpenAirTextTypes.SomeAreNotified) + GetCardText(filteredTimesheet, notifiedUserList);
            }
            else
            {
                text = OpenAirText.GetText(state, OpenAirTextTypes.SomeAreDone) + GetCardText(filteredTimesheet, notifiedUserList);
            }

            var paragraph = new TextParagraph {
                Text = text
            };
            var card = ChatEventFactory.CreateCard(paragraph);

            if (address != null)
            {
                await connector.SendMessageAsync(null, address, card);
            }
            else if (email != null)
            {
                var emailedUsers = string.Join("</b><br/><b>", notifiedUserList);
                var emailText    =
                    $"{text}<br/><br/><b>The following people where notified by a direct massage or email:" +
                    $"<br/><b>{emailedUsers}</b>";
                await _mailService.SendMailAsync("Users not notified", emailText, email);
            }
        }
Beispiel #7
0
        /// <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);
        }