using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Oqtane.Enums; using Oqtane.Extensions; using Oqtane.Infrastructure; using Oqtane.Models; using Oqtane.Repository; using Oqtane.Security; using Oqtane.Shared; using SZUAbsolventenverein.Module.EventRegistration.Models; using SZUAbsolventenverein.Module.EventRegistration.Repository; namespace SZUAbsolventenverein.Module.EventRegistration.Services { public class ServerEventRegistrationService : IEventRegistrationService { private readonly IEventRepository _EventRepository; private readonly IResponseRepository _ResponseRepository; private readonly INotificationRepository _NotificationRepository; private readonly IUserRepository _UserRepository; private readonly IUserPermissions _userPermissions; private readonly ILogManager _logger; private readonly IHttpContextAccessor _accessor; private readonly ISettingRepository _settingRepository; private readonly Alias _alias; public ServerEventRegistrationService(IEventRepository EventRepository, IResponseRepository ResponseRepository, INotificationRepository NotificationRepository, IUserRepository UserRepository, IUserPermissions userPermissions, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor, ISettingRepository settingRepository) { _EventRepository = EventRepository; _ResponseRepository = ResponseRepository; _NotificationRepository = NotificationRepository; _UserRepository = UserRepository; _userPermissions = userPermissions; _logger = logger; _accessor = accessor; _settingRepository = settingRepository; _alias = tenantManager.GetAlias(); } public Task AddEventAsync(Event NewEvent) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, NewEvent.ModuleId, PermissionNames.Edit)) { NewEvent = _EventRepository.AddEvent(NewEvent); _logger.Log(LogLevel.Information, this, LogFunction.Create, "EventRegistration Added {NewEvent}", NewEvent); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Add Attempt {NewEvent}", NewEvent); NewEvent = null; } return Task.FromResult(NewEvent); } public Task AddResponseAsync(Response Response) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, Response.ModuleId, PermissionNames.View)) { Response = _ResponseRepository.AddResponse(Response); Event currentEvent = _EventRepository.GetEvent(Response.EventRegistrationId); string subject = Response.ResponseType ? $"Du bist erfolgreich für '{currentEvent.Name}' Registriert worden." : $"Du hast erfolgreich für '{currentEvent.Name}' abgesagt."; string body = "Hier kann man die Infos des Events hineinpacken (HTML ist erlaubt)"; SendEventResponseNotification(subject, body); _logger.Log(LogLevel.Information, this, LogFunction.Create, "EventRegistration Added {NewEvent}", Response); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Add Attempt {NewEvent}", Response); Response = null; } return Task.FromResult(Response); } public Task UpdateResponseAsync(Response Response) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, Response.ModuleId, PermissionNames.View)) { Response = _ResponseRepository.UpdateResponse(Response); Event currentEvent = _EventRepository.GetEvent(Response.EventRegistrationId); string subject = Response.ResponseType ? $"Du bist erfolgreich für '{currentEvent.Name}' registriert." : $"Du hast erfolgreich für '{currentEvent.Name}' abgesagt."; string body = currentEvent.Description; SendEventResponseNotification(subject, body); _logger.Log(LogLevel.Information, this, LogFunction.Create, "EventRegistration Added {NewEvent}", Response); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized EventRegistration Add Attempt {NewEvent}", Response); Response = null; } return Task.FromResult(Response); } public Task DeleteEventAsync(int EventId, int ModuleId) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.Edit)) { _EventRepository.DeleteEvent(EventId); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "Event Deleted {EventId}", EventId); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Event Delete Attempt {EventId} {ModuleId}", EventId, ModuleId); } return Task.CompletedTask; } public Task GetEventAsync(int EventId, int ModuleId) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View)) { return Task.FromResult(_EventRepository.GetEvent(EventId, true)); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Event Get Attempt {ModuleId}", ModuleId); return null; } } public Task<(Event, Response)> GetEventDetails(int EventId, int ModuleId) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View)) { Event currentEvent = _EventRepository.GetEvent(EventId); Response rsvp = _ResponseRepository.GetResponse(EventId, _accessor.HttpContext.User.UserId()); return Task.FromResult((currentEvent, rsvp)); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Event Get Attempt {ModuleId}", ModuleId); return null; } } public Task> GetEventResponses(int EventId, int ModuleId) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.Edit)) { return Task.FromResult(_ResponseRepository.GetResponses(EventId, ModuleId).ToList()); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Event Response Get Attempt {ModuleId}", ModuleId); return null; } } public async Task> GetRecommendedResponses(int EventId, int ModuleId) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View)) { IEnumerable responses = _ResponseRepository.GetResponses(EventId, ModuleId).DistinctBy(r => r.OwnerId).Where(r => r.OwnerId != _accessor.HttpContext.User.UserId() && r.ResponseType); IEnumerable users = _UserRepository.GetUsers(); List userSettings = _settingRepository.GetSettings("User").ToList(); List requestorSettings = userSettings.FindAll(s => s.EntityId == _accessor.HttpContext.User.UserId()); string targetFachrichtung = requestorSettings.FirstOrDefault(s => s.SettingName == "Fachrichtung")?.SettingValue; int targetStartjahr = int.Parse(requestorSettings.FirstOrDefault(s => s.SettingName == "Jahrgang")?.SettingValue ?? "0"); IEnumerable gu = responses.Join(users, r => r.OwnerId, u => u.UserId, (response, user) => (response, user)).GroupJoin(userSettings, ru => ru.user.UserId, s => s.EntityId, (ru, s) => new GroupingUser(ru.user, ru.response, s, targetStartjahr, targetFachrichtung)).OrderBy(gu => gu.Score()); return gu.Select(gu => gu.User).Take(10).ToList(); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Event Response Get Attempt {ModuleId}", ModuleId); return null; } } public Task> GetEventsAsync(int ModuleId) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View)) { return Task.FromResult(_EventRepository.GetEvents(ModuleId).ToList()); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Events Get Attempt {ModuleId}", ModuleId); return null; } } public Task UpdateEventAsync(Event NewEvent) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, NewEvent.ModuleId, PermissionNames.Edit)) { NewEvent = _EventRepository.UpdateEvent(NewEvent); _logger.Log(LogLevel.Information, this, LogFunction.Update, "Event Updated {NewEvent}", NewEvent); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Event Update Attempt {NewEvent}", NewEvent); NewEvent = null; } return Task.FromResult(NewEvent); } private void SendEventResponseNotification(string subject, string body) { User user = _UserRepository.GetUser(_accessor.HttpContext.User.UserId()); Notification notification = new Notification(_alias.SiteId, user, subject, body); _NotificationRepository.AddNotification(notification); } } public class GroupingUser { private User _user; private Response _response; private string _fachrichtung; private int _startjahr; private int _targetyear; private string _targetfachrichtung; public User User { get { return _user; } } public IEnumerable Settings { set { if (value == null) { _fachrichtung = "-"; _startjahr = 0; return; } _fachrichtung = value.FirstOrDefault(v => v.SettingName == "Fachrichtung", new Setting(){SettingValue = "-"}).SettingValue; _startjahr = int.Parse(value.FirstOrDefault(v => v.SettingName == "Jahrgang", new Setting(){SettingValue = "0"}).SettingValue); } } public string TargetFachrichtung { set { _targetfachrichtung = value; } } public int TargetJahr { set { _targetyear = value; } } public GroupingUser(User user, Response response, IEnumerable settings, int targetyear, string targetfachrichtung) { _user = user; _response = response; Settings = settings; TargetJahr = targetyear; TargetFachrichtung = targetfachrichtung; } public int Score() { int total = 0; total += ScoreYear() * 5; total += ScoreFachrichtung() * 3; return total; } private int ScoreYear() { return Math.Abs(_targetyear - _startjahr); } private int ScoreFachrichtung() { if (_fachrichtung == _targetfachrichtung) { return 1; } else { return 0; } } } }