public static async Task <IEnumerable <Plugin> > GetPluginsAsync( [HttpTrigger(AuthorizationLevel.Function, nameof(HttpMethod.Get), Route = null)] HttpRequestData req, FunctionContext context) { Contract.Ensures(req != null, "Request is not instanciated"); await context.Get <IAccessTokenService>().EnsureRole(req, UserRoles.Administrator); var storage = context.Get <IStorageService>() ?? throw new NullReferenceException(); var plugins = await storage.GetAllPluginsAsync(); var systemPlugins = SystemPlugins.GetSystemPlugins(); if (!plugins?.Any() ?? false) { await storage.AddOrUpdatePluginsAsync(systemPlugins); return(systemPlugins); } var hasMissingGroups = false; var pluginsIds = plugins.Select(it => it.Id).ToArray(); var missingPlugins = systemPlugins.Where(it => !pluginsIds.Contains(it.Id)).ToArray(); var combinedPlugins = plugins .Join( systemPlugins, it => it.Id, it => it.Id, (p, ps) => { if (p.Groups == null) { hasMissingGroups = true; p.Groups = ps.Groups; return(p); } var pluginGroupsCount = p.Groups.Length; var systemPluginsGroupCount = ps.Groups?.Length ?? 0; if (pluginGroupsCount < systemPluginsGroupCount) { hasMissingGroups = true; var names = p.Groups.Select(it => it.UniqueName).ToArray(); p.Groups = p.Groups.Concat(ps.Groups.Where(it => !names.Contains(it.UniqueName))).ToArray(); } return(p); }) .Concat(missingPlugins) .ToArray(); if (missingPlugins.Any() || hasMissingGroups) { await storage.AddOrUpdatePluginsAsync(combinedPlugins); } return(combinedPlugins); }
public static async Task SavePluginsAsync( [HttpTrigger(AuthorizationLevel.Function, nameof(HttpMethod.Post), Route = null)] HttpRequestData req, FunctionContext context) { await context.Get <IAccessTokenService>().EnsureRole(req, UserRoles.Administrator); var storageService = context.Get <IStorageService>(); context.Get <IMemoryCache>().Remove(Constants.PluginsCacheKey); var plugins = await req.ReadAsAsync <IReadOnlyList <Plugin> >(); await storageService.AddOrUpdatePluginsAsync(plugins); }
public static async Task SaveUserPropertiesAsync( [HttpTrigger(AuthorizationLevel.Function, nameof(HttpMethod.Post), Route = null)] HttpRequestData req, FunctionContext context) { await context.Get <IAccessTokenService>().EnsureRole(req, UserRoles.Administrator); var storageService = context.Get <IStorageService>(); var userInfo = await req.ReadAsAsync <UserInfo>(); var user = await storageService.GetUserByIdAsync(userInfo.Id); user.Properties = userInfo.Properties; await storageService.AddOrUpdateUserAsync(user); }
public static async Task SyncUsersAsync( [TimerTrigger("0 0 9 * * Fri")] TimerInfo myTimer, FunctionContext context) { Contract.Ensures(myTimer != null, "Timer is not instanciated"); var openAirConnector = context.Get <IOpenAirConnector>(); await openAirConnector.SyncUsersAsync(); }
public static async Task <IEnumerable <MessagesStatistic> > GetMessagesStatisticsAsync( [HttpTrigger(AuthorizationLevel.Function, nameof(HttpMethod.Get), Route = null)] HttpRequestData req, FunctionContext context) { Contract.Ensures(req != null, "Request is not instanciated"); await context.Get <IAccessTokenService>().EnsureRole(req, UserRoles.User | UserRoles.Administrator); var storage = context.Get <IStorageService>() ?? throw new NullReferenceException(); var messages = (await storage.GetMessagesAsync()) .GroupBy(it => it.ProbabilityPercentage / 10) .Select(group => new MessagesStatistic { ProbabilityPercentage = (byte)(group.Key * 10), Count = group.Count() }); return(messages); }
public static async Task <IEnumerable <TimesheetChartStatistic> > GetTimesheetStatisticsAsync( [HttpTrigger(AuthorizationLevel.Function, nameof(HttpMethod.Get), Route = null)] HttpRequestData req, FunctionContext context) { Contract.Ensures(req != null, "Request is not instanciated"); await context.Get <IAccessTokenService>().EnsureRole(req, UserRoles.User | UserRoles.Administrator); var storageService = context.Get <IStorageService>() ?? throw new NullReferenceException(); const int hour = 20; const DayOfWeek reportDayOfWeek = DayOfWeek.Friday; var dataCount = 10; var startDate = GetLastDateTime(Contract.LocalDateTime, reportDayOfWeek, hour); var data = new List <TimesheetChartStatistic>(); while (dataCount-- > 0) { var dateValue = startDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); var timeValue = startDate.ToString("HH:00", CultureInfo.InvariantCulture); var stats = await storageService.GetStatisticsAsync <TimesheetStatistics[]>(dateValue, timeValue); var chartStats = stats .SelectMany(it => it.Data) .Where(it => it.State == TimesheetStates.Unsubmitted) .GroupBy(it => it.DepartmentName) .Select(group => new TimesheetChartStatistic { Date = dateValue, Department = group.Key, Count = group.Count(), }); data.AddRange(chartStats); startDate = startDate.AddDays(-7); } data.Reverse(); return(data); }
public static async Task <AccessTokenUserInfo> GetUserInfoAsync( [HttpTrigger(AuthorizationLevel.Function, nameof(HttpMethod.Get), Route = null)] HttpRequestData req, FunctionContext context) { Contract.Ensures(req != null, "Request is not instanciated"); var accessTokenService = context.Get <IAccessTokenService>() ?? throw new NullReferenceException(); var userInfo = await accessTokenService.ValidateTokenAsync(req); return(userInfo); }
public static async Task ExecuteTimesheetsReminderAsync( [TimerTrigger("0 */15 * * * 1-5")] TimerInfo myTimer, FunctionContext context) { Contract.Ensures(myTimer != null, "Timer is not instanciated"); var timesheetService = context.Get <ITimesheetService>(); var now = GetLocalDateTime(context); var dateTimeMinutes = now.Minute - (now.Minute % 5); var dateTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, dateTimeMinutes, 0, 0, now.Kind); await timesheetService.SendScheduledTimesheetNotificationsAsync(dateTime); }
public static async Task <IEnumerable <UserInfo> > GetUsersAsync( [HttpTrigger(AuthorizationLevel.Function, nameof(HttpMethod.Get), Route = null)] HttpRequestData req, FunctionContext context) { Contract.Ensures(req != null, "Request is not instanciated"); await context.Get <IAccessTokenService>().EnsureRole(req, UserRoles.Administrator); var storageService = context.Get <IStorageService>() ?? throw new NullReferenceException(); var users = await storageService.GetAllActiveUsersAsync(); return(users.Select(it => new UserInfo { Id = it.Id, Name = it.Name, Email = it.Email, Manager = it.Manager?.Email, Department = it.Department?.Name, Role = Enum.GetName(typeof(UserRoles), it.Role), Customers = string.Join(", ", it.Customers?.Select(customer => customer.Name) ?? Enumerable.Empty <string>()), Properties = it.Properties, })); }
public static async Task <HttpResponseData> RunAsync( [HttpTrigger(AuthorizationLevel.Function, nameof(HttpMethod.Post), Route = null)] HttpRequestData req, FunctionContext context) { Debug.Write(req); var storageService = context.Get <IStorageService>(); var log = context.GetLogger(nameof(HangoutChatEvent)); var hangoutsChatService = context.Get <IHangoutsChatService>(); var options = context.Get <GoogleCloudOptions>(); var hangoutChatEvent = await req.ReadAsAsync <ChatEvent>(); log.LogDebug( $"Message from '{hangoutChatEvent.Message.Sender.DisplayName}' is '{hangoutChatEvent.Message.Text}'."); if (!hangoutChatEvent.Token.Equals(options.HangoutChatRequestToken, StringComparison.InvariantCulture)) { log.LogError("The tokens do not match. Unauthorized access. " + hangoutChatEvent.Token); return(req.CreateResponse(HttpStatusCode.Unauthorized)); } var result = await hangoutsChatService .BasicAsync(hangoutChatEvent) .ConfigureAwait(false); if (storageService != null) { await storageService .SaveMessageAsync(result) .ConfigureAwait(false); } log.LogInformation(result.Output?.Text); return(await req.CreateContentResponseAsync(result.Output)); }
private static DateTime GetLocalDateTime(FunctionContext context) => TimeZoneInfo.ConvertTime( context.Get <Func <DateTime> >()(), context.Get <Func <TimeZoneInfo> >()());