/// <summary> /// /// </summary> /// <param name="mail"></param> public void SendAll(MailJobModel mail) { var userDb = new ApplicationDbContext(); var Db = new TimeTableDbContext(); ICollection <ApplicationUser> userList; userList = userDb.Users.Where(u => u.MemberState == MemberState.Student || (u.MemberState == MemberState.Staff && u.LikeEMails)).ToList(); var sender = userDb.Users.SingleOrDefault(x => x.Id.Equals(mail.SenderId)); // jetzt reduzieren, um nicht FK Mitglieder! var deleteList = new List <ApplicationUser>(); var subService = new SemesterSubscriptionService(Db); foreach (var user in userList) { if (user.MemberState == MemberState.Student) { var isInSem = subService.IsSubscribed(user.Id, mail.SemesterId, mail.OrgId); if (!isInSem) { deleteList.Add(user); } } else { var isInFK = Db.Members.Any(x => x.Organiser.Id == mail.OrgId && string.IsNullOrEmpty(x.UserId) && x.UserId.Equals(user.Id)); if (!isInFK) { deleteList.Add(user); } } } foreach (var user in deleteList) { userList.Remove(user); } SendMail(userList, sender, mail); }
/// <summary> /// /// </summary> /// <param name="allSubscriptions"></param> /// <param name="group"></param> /// <returns></returns> public List <OccurrenceSubscription> GetSubscriptionsForGroup(ICollection <OccurrenceSubscription> allSubscriptions, OccurrenceGroup group) { var semSubService = new SemesterSubscriptionService(); var subList = new List <OccurrenceSubscription>(); foreach (var subscription in allSubscriptions) { var user = UserManager.FindById(subscription.UserId); if (user != null) { bool isInGroup = semSubService.IsSubscribed(user.Id, group.SemesterGroups, group.Occurrence.UseExactFit); if (isInGroup) { subList.Add(subscription); } } } return(subList); }
/// <summary> /// /// </summary> /// <param name="occId"></param> /// <param name="user"></param> /// <returns></returns> public SubscriptionResponse SubscribeOccurrence(Guid occId, ApplicationUser user) { var occurrence = Db.Occurrences.SingleOrDefault(ac => ac.Id == occId); if (occurrence == null || user == null) { return(GetErrorResponse("Missing Or Wrong Occurrence Or Missing User", false)); } // Nach einer bestehenden Eintragung suchen // sollte aus irgendeinem Grund bereits mehrere Eintragungen bestehen, dann ist das hier nicht von Interesse // es genügt die Information, dass bereits eine Eintragung existiert OccurrenceSubscription subscription = occurrence.Subscriptions.FirstOrDefault(s => s.UserId.Equals(user.Id)); if (subscription != null) { return(new SubscriptionResponse { HasError = false, Subscription = subscription }); } #region Zeitraum prüfen var timingState = GetSubscriptionState(occurrence); if (timingState == SubscriptionState.BeforeSubscriptionPhase) { return(new SubscriptionResponse { HasError = true, ShowUser = true, Message = string.Format("Noch zu früh! Eintragung erst ab {0} möglich", occurrence.FromDateTime.Value), }); } if (timingState == SubscriptionState.AfterSubscriptionPhase || timingState == SubscriptionState.DuringOccurrence || timingState == SubscriptionState.AfterOccurrence) { return(new SubscriptionResponse { HasError = true, ShowUser = true, Message = "Zu spät! Keine Eintragung mehr möglich!", }); } #endregion #region Alles auf Warteliste: Inaktiv gesetzt oder Platzverlosung var hasLottery = Db.Lotteries.Any(x => x.Occurrences.Any(y => y.Id == occurrence.Id)); if (occurrence.IsAvailable == false || hasLottery) { subscription = AddOnWaitingList(occurrence, user); return(new SubscriptionResponse { HasError = false, Subscription = subscription }); } #endregion #region Gruppen berücksichtigen if (occurrence.UseGroups) { var semSubService = new SemesterSubscriptionService(); // die beste Gruppe ermitteln => die erste, die passt var group = semSubService.GetBestFit(user.Id, occurrence.Groups); // Wenn der Benutzer nicht zur erforderlichen Gruppe gehört // dan automatisch auf der Warteliste positionieren if (group == null) { subscription = AddOnWaitingList(occurrence, user); return(new SubscriptionResponse { HasError = false, Subscription = subscription }); } // als nächstes die Anzahl der Teilnehmer in dieser Gruppe ermitteln var participants = GetSubscriptionsForGroup(occurrence.Subscriptions, group); // Es ist noch Platz in der Gruppe if (participants.Count < group.Capacity) { subscription = AddOnParticipantList(occurrence, user); return(new SubscriptionResponse { HasError = false, Subscription = subscription }); } // Alle Plätze in der Gruppe sind belegt // auf die Warteliste subscription = AddOnWaitingList(occurrence, user); return(new SubscriptionResponse { HasError = false, Subscription = subscription }); } else { // keine Gruppen berücksichtigen // und es interessiert hier nicht die Annahme des Platzes! var nParticipants = occurrence.Subscriptions.Count(s => s.OnWaitingList == false); if (occurrence.Capacity < 0 || nParticipants < occurrence.Capacity) { subscription = AddOnParticipantList(occurrence, user); return(new SubscriptionResponse { HasError = false, Subscription = subscription }); } // Auf Warteliste setzen // Spezialfälle // Bei einem Slot oder Datum (Sprechstunde) gibt es keine Wartelisten var summary = new ActivityService().GetSummary(occurrence.Id); if (summary is ActivitySlotSummary || summary is ActivityDateSummary) { return(new SubscriptionResponse { HasError = true, ShowUser = true, Message = "Leider bereits belegt", }); } else { subscription = AddOnWaitingList(occurrence, user); return(new SubscriptionResponse { HasError = false, Subscription = subscription }); } } #endregion }