/// <summary>Sends the scheduled timesheet notifications asynchronous.</summary> public async Task SendScheduledTimesheetNotificationsAsync(DateTime scheduleDate) { var result = await _cognitiveService.GetCognitiveTextAnalysisResultAsync( new TextDeconstructionInformation(null, TimesheetsProperties.ProcessorSubjectName), null); var processor = result.CommandProcessor as ITimesheetProcessor; var notificationsGroups = result.PropertiesAccessor.GetPluginPropertyGroup(TimesheetsProperties.AutoNotificationsGroup); var customerToExcludes = result.PropertiesAccessor.GetAllPluginPropertyValues <string>(TimesheetsProperties.FilterByCustomer); var timesheets = new Dictionary <string, IReadOnlyList <Timesheet> >(); var addresses = new Dictionary <string, GoogleChatAddress>(); foreach (var group in notificationsGroups) { var cron = group.GetValue <string>(TimesheetsProperties.AutoNotificationsCron); if (CronCheck(cron, scheduleDate)) { var space = group.GetValue <string>(TimesheetsProperties.AutoNotificationsSpaces); var email = group.GetValue <string>(TimesheetsProperties.AutoNotificationsManagerEmail); var stateName = group.GetValue <string>(TimesheetsProperties.AutoNotificationsReportName); var notify = group.GetValue <bool>(TimesheetsProperties.AutoNotificationsNotify); var state = Enum.Parse <TimesheetStates>(stateName, true); if (string.IsNullOrEmpty(email) || state == TimesheetStates.None) { continue; } var key = string.Concat(stateName, "_", email); if (string.IsNullOrEmpty(space)) { if (notify) { await SendNotificationsAsync( timesheets, key, email, notify : true, state, scheduleDate, address : null, customerToExcludes, processor); } continue; } var spaces = space.Split(',').Select(sp => sp.StartsWith("spaces/", StringComparison.InvariantCultureIgnoreCase) ? sp : ("spaces/" + sp)); foreach (var spaceName in spaces) { if (!addresses.TryGetValue(spaceName, out var address)) { address = _hangoutsChatConnector.GetAddressByName(spaceName); addresses.Add(spaceName, address); } if (address == null) { continue; } await SendNotificationsAsync( timesheets, key, email, notify, state, scheduleDate, address, customerToExcludes, processor); } } } await SaveGlobalStatisticsAsync(scheduleDate, customerToExcludes, result.PropertiesAccessor, processor); }
public async Task SendScheduledTimesheetShouldSendToSpace() { var date = new DateTime(2020, 7, 1, 20, 0, 0, DateTimeKind.Local); var timesheetProcessor = Substitute.For <ITimesheetProcessor>(); var propertiesAccessor = Substitute.For <IPluginPropertiesAccessor>(); var deconstructionInformation = new TextDeconstructionInformation(null, null); var analysisResult = new CognitiveTextAnalysisResult(deconstructionInformation, timesheetProcessor, propertiesAccessor); var address = new GoogleChatAddress("S1", "Space 1", "RM", "U1", "User 1"); var timesheet = new Timesheet { UserEmail = "[email protected]", UserName = "******", DepartmentName = "Account", ManagerName = "*****@*****.**", Total = 5, UtilizationInHours = 10 }; var excludeCustomers = new[] { "C1" }; _cognitiveService.GetCognitiveTextAnalysisResultAsync(null, null).ReturnsForAnyArgs(analysisResult); propertiesAccessor.GetAllPluginPropertyValues <string>("OpenAir.Filters.Customer").Returns(excludeCustomers); propertiesAccessor .GetPluginPropertyGroup("OpenAir.AutoNotifications") .Returns( new[] { new [] { new PluginPropertyValue { Key = "OpenAir.AutoNotifications.Spaces", Value = "S1" }, new PluginPropertyValue { Key = "OpenAir.AutoNotifications.Email", Value = "[email protected]" }, new PluginPropertyValue { Key = "OpenAir.AutoNotifications.ReportName", Value = "unsubmitted" }, new PluginPropertyValue { Key = "OpenAir.AutoNotifications.Cron", Value = "20 Wed" }, new PluginPropertyValue { Key = "OpenAir.AutoNotifications.Notify", Value = false }, } }); _hangoutsChatConnector.GetAddressByName("spaces/S1").Returns(address); timesheetProcessor .GetTimesheetsAsync(Arg.Any <DateTime>(), TimesheetStates.Unsubmitted, "[email protected]", true, excludeCustomers) .Returns(new[] { timesheet }); await _timesheetService.SendScheduledTimesheetNotificationsAsync(date); timesheetProcessor .Received() .SendTimesheetNotificationsToUsersAsync( Arg.Is <IReadOnlyList <Timesheet> >(it => it[0].UserName == "Jhon Doe"), "[email protected]", null, false, false, TimesheetStates.Unsubmitted, address, _hangoutsChatConnector); }