public DepartmentGroup GetGroupForEvent(ProcessedNotification notification) { //NotificationItem dynamicData = (NotificationItem)JsonConvert.DeserializeObject(data); NotificationItem dynamicData = ObjectSerialization.Deserialize <NotificationItem>(notification.Data); if (notification.Type == EventTypes.UnitStatusChanged) { var unitEvent = _unitsService.GetUnitStateById(dynamicData.StateId); return(_departmentGroupsService.GetGroupById(unitEvent.Unit.StationGroupId.GetValueOrDefault())); } else if (notification.Type == EventTypes.PersonnelStatusChanged) { var userStaffing = _userStateService.GetUserStateById(dynamicData.StateId); var group = _departmentGroupsService.GetGroupForUser(userStaffing.UserId, notification.DepartmentId); return(group); } else if (notification.Type == EventTypes.PersonnelStatusChanged) { var actionLog = _actionLogsService.GetActionlogById(dynamicData.StateId); var group = _departmentGroupsService.GetGroupForUser(actionLog.UserId, notification.DepartmentId); return(group); } else if (notification.Type == EventTypes.UserAssignedToGroup) { return(_departmentGroupsService.GetGroupById(dynamicData.GroupId)); } return(null); }
public void PopulateQueue() { if (!_isLocked) { _isLocked = true; _departmentsService = Bootstrapper.GetKernel().Resolve <IDepartmentsService>(); _notificationService = Bootstrapper.GetKernel().Resolve <INotificationService>(); _userProfileService = Bootstrapper.GetKernel().Resolve <IUserProfileService>(); _departmentSettingsService = Bootstrapper.GetKernel().Resolve <IDepartmentSettingsService>(); var t1 = new Task(() => { try { var allNotifications = _notificationService.GetAll(); var items = new List <ProcessedNotification>(); BrokeredMessage message = null; while (message != null) { try { var item = new ProcessedNotification(); if (message.Properties["DepartmentId"] != null) { item.DepartmentId = int.Parse(message.Properties["DepartmentId"].ToString()); } if (message.Properties["Type"] != null) { item.Type = (EventTypes)message.Properties["Type"]; } if (message.Properties["Value"] != null) { item.Value = message.Properties["Value"].ToString(); } item.MessageId = message.MessageId; try { item.Data = message.GetBody <string>(); items.Add(item); // Remove message from subscription message.Complete(); } catch (InvalidOperationException) { message.Complete(); } } catch (Exception ex) { Logging.LogException(ex); // Indicate a problem, unlock message in subscription message.Abandon(); } } var groupedItems = from i in items group i by i.DepartmentId into itemGroup orderby itemGroup.Key select itemGroup; foreach (var group in groupedItems) { var queueItem = new NotificationQueueItem(); queueItem.Department = _departmentsService.GetDepartmentById(group.Key, false); queueItem.DepartmentTextNumber = _departmentSettingsService.GetTextToCallNumberForDepartment(group.Key); queueItem.NotificationSettings = allNotifications.Where(x => x.DepartmentId == group.Key).ToList(); queueItem.Notifications = group.ToList(); queueItem.Profiles = _userProfileService.GetAllProfilesForDepartment(group.Key); _queue.Enqueue(queueItem); } } catch (Exception ex) { Logging.LogException(ex); } finally { _isLocked = false; _cleared = false; _departmentsService = null; _notificationService = null; _userProfileService = null; _departmentSettingsService = null; } }); t1.Start(); } }
public static void ProcessNotificationItem(NotificationItem ni, string messageId, string body) { if (ni != null) { var _notificationService = Bootstrapper.GetKernel().Resolve <INotificationService>(); var _communicationService = Bootstrapper.GetKernel().Resolve <ICommunicationService>(); var _departmentsService = Bootstrapper.GetKernel().Resolve <IDepartmentsService>(); var _userProfileService = Bootstrapper.GetKernel().Resolve <IUserProfileService>(); var _departmentSettingsService = Bootstrapper.GetKernel().Resolve <IDepartmentSettingsService>(); var item = new ProcessedNotification(); if (ni.DepartmentId != 0) { item.DepartmentId = ni.DepartmentId; } else { item.DepartmentId = _notificationService.GetDepartmentIdForType(ni); } item.Type = (EventTypes)ni.Type; item.Value = ni.Value; item.MessageId = messageId; item.Data = body; item.ItemId = ni.ItemId; var queueItem = new NotificationQueueItem(); queueItem.Department = _departmentsService.GetDepartmentById(item.DepartmentId, false); queueItem.DepartmentTextNumber = _departmentSettingsService.GetTextToCallNumberForDepartment(item.DepartmentId); queueItem.NotificationSettings = _notificationService.GetNotificationsByDepartment(item.DepartmentId); queueItem.Profiles = _userProfileService.GetAllProfilesForDepartment(item.DepartmentId); queueItem.Notifications = new List <ProcessedNotification>(); queueItem.Notifications.Add(item); var notificaitons = _notificationService.ProcessNotifications(queueItem.Notifications, queueItem.NotificationSettings); if (notificaitons != null) { foreach (var notification in notificaitons) { var text = _notificationService.GetMessageForType(notification); if (!String.IsNullOrWhiteSpace(text)) { foreach (var user in notification.Users) { if (queueItem.Profiles.ContainsKey(user)) { _communicationService.SendNotification(user, notification.DepartmentId, text, queueItem.DepartmentTextNumber, "Notification", queueItem.Profiles[user]); } //else // _communicationService.SendNotification(user, notification.DepartmentId, text, queueItem.DepartmentTextNumber); } } } } _notificationService = null; _communicationService = null; _departmentsService = null; _userProfileService = null; _departmentSettingsService = null; } }
public static async Task <bool> ProcessNotificationItem(NotificationItem ni, string messageId, string body) { if (ni != null) { var _notificationService = Bootstrapper.GetKernel().Resolve <INotificationService>(); var _communicationService = Bootstrapper.GetKernel().Resolve <ICommunicationService>(); var _departmentsService = Bootstrapper.GetKernel().Resolve <IDepartmentsService>(); var _userProfileService = Bootstrapper.GetKernel().Resolve <IUserProfileService>(); var _departmentSettingsService = Bootstrapper.GetKernel().Resolve <IDepartmentSettingsService>(); var item = new ProcessedNotification(); if (ni.DepartmentId != 0) { item.DepartmentId = ni.DepartmentId; } else { item.DepartmentId = await _notificationService.GetDepartmentIdForTypeAsync(ni); } item.Type = (EventTypes)ni.Type; item.Value = ni.Value; item.MessageId = messageId; if (!String.IsNullOrWhiteSpace(body)) { item.Data = body; } else { item.Data = ObjectSerialization.Serialize(ni); } item.ItemId = ni.ItemId; var queueItem = new NotificationQueueItem(); queueItem.Department = await _departmentsService.GetDepartmentByIdAsync(item.DepartmentId, false); queueItem.DepartmentTextNumber = await _departmentSettingsService.GetTextToCallNumberForDepartmentAsync(item.DepartmentId); queueItem.NotificationSettings = await _notificationService.GetNotificationsByDepartmentAsync(item.DepartmentId); queueItem.Profiles = await _userProfileService.GetAllProfilesForDepartmentAsync(item.DepartmentId); queueItem.Notifications = new List <ProcessedNotification>(); queueItem.Notifications.Add(item); var notificaitons = await _notificationService.ProcessNotificationsAsync(queueItem.Notifications, queueItem.NotificationSettings); if (notificaitons != null) { foreach (var notification in notificaitons) { var text = await _notificationService.GetMessageForTypeAsync(notification); if (!String.IsNullOrWhiteSpace(text)) { foreach (var user in notification.Users) { if (queueItem.Profiles.ContainsKey(user)) { var profile = queueItem.Profiles[user]; if (!_notificationService.AllowToSendViaSms(notification.Type)) { profile.SendNotificationSms = false; } await _communicationService.SendNotificationAsync(user, notification.DepartmentId, text, queueItem.DepartmentTextNumber, "Notification", profile); } } } } } _notificationService = null; _communicationService = null; _departmentsService = null; _userProfileService = null; _departmentSettingsService = null; } return(true); }
public string GetMessageForType(ProcessedNotification notification) { try { NotificationItem data = ObjectSerialization.Deserialize <NotificationItem>(notification.Data); switch (notification.Type) { case EventTypes.UnitStatusChanged: var unitEvent = _unitsService.GetUnitStateById((int)data.StateId); var unitStatus = _customStateService.GetCustomUnitState(unitEvent); if (unitEvent != null && unitEvent.Unit != null) { return(String.Format("Unit {0} is now {1}", unitEvent.Unit.Name, unitStatus.ButtonText)); } else if (unitEvent != null) { return(String.Format("A Unit's status is now {0}", unitStatus.ButtonText)); } else { return("A unit's status changed"); } case EventTypes.PersonnelStaffingChanged: var userStaffing = _userStateService.GetUserStateById((int)data.StateId); var userProfile = _userProfileService.GetProfileByUserId(userStaffing.UserId); var userStaffingText = _customStateService.GetCustomPersonnelStaffing(data.DepartmentId, userStaffing); return(String.Format("{0} staffing is now {1}", userProfile.FullName.AsFirstNameLastName, userStaffingText.ButtonText)); case EventTypes.PersonnelStatusChanged: var actionLog = _actionLogsService.GetActionlogById(data.StateId); UserProfile profile = null; if (actionLog != null) { profile = _userProfileService.GetProfileByUserId(actionLog.UserId); } else if (data.UserId != String.Empty) { profile = _userProfileService.GetProfileByUserId(data.UserId); } var userStatusText = _customStateService.GetCustomPersonnelStatus(data.DepartmentId, actionLog); if (profile != null && userStatusText != null) { return(String.Format("{0} status is now {1}", profile.FullName.AsFirstNameLastName, userStatusText.ButtonText)); } else if (profile != null) { return(String.Format("{0} status has changed", profile.FullName.AsFirstNameLastName)); } return(String.Empty); case EventTypes.UserCreated: var newUserprofile = _userProfileService.GetProfileByUserId(data.UserId); if (newUserprofile != null) { return(String.Format("{0} has been added to your department", newUserprofile.FullName.AsFirstNameLastName)); } else { return("A new user has been added to your department"); } case EventTypes.UserAssignedToGroup: UserProfile groupUserprofile = null; try { if (data.UserId != String.Empty) { groupUserprofile = _userProfileService.GetProfileByUserId(data.UserId); } } catch { } DepartmentGroup newGroup = null; try { if (data.GroupId != 0) { newGroup = _departmentGroupsService.GetGroupById((int)data.GroupId, false); } } catch { } if (groupUserprofile != null && newGroup != null) { return(String.Format("{0} has been assigned to group {1}", groupUserprofile.FullName.AsFirstNameLastName, newGroup.Name)); } else if (groupUserprofile != null && newGroup == null) { return(String.Format("{0} has been assigned to group", groupUserprofile.FullName.AsFirstNameLastName)); } else if (newGroup != null && groupUserprofile == null) { return(String.Format("A user has been assigned to group {0}", newGroup.Name)); } else { return(String.Format("A has been assigned to a group")); } case EventTypes.CalendarEventUpcoming: var calandarItem = _calendarService.GetCalendarItemById((int)data.ItemId); return(String.Format("Event {0} is upcoming", calandarItem.Title)); case EventTypes.DocumentAdded: var document = _documentsService.GetDocumentById((int)data.ItemId); return(String.Format("Document {0} has been added", document.Name)); case EventTypes.NoteAdded: var note = _notesService.GetNoteById((int)data.ItemId); if (note != null) { return(String.Format("Message {0} has been added", note.Title)); } break; case EventTypes.UnitAdded: var unit = _unitsService.GetUnitById((int)data.UnitId); return(String.Format("Unit {0} has been added", unit.Name)); case EventTypes.LogAdded: var log = _workLogsService.GetWorkLogById((int)data.ItemId); if (log != null) { var logUserProfile = _userProfileService.GetProfileByUserId(log.LoggedByUserId); return(String.Format("{0} created log {1}", logUserProfile.FullName.AsFirstNameLastName, log.LogId)); } else { return(String.Format("A new log was created")); } case EventTypes.DepartmentSettingsChanged: return(String.Format("Settings have been updated for your department")); case EventTypes.RolesInGroupAvailabilityAlert: var userStateChanged = _userStateService.GetUserStateById(int.Parse(notification.Value)); var roleForGroup = _personnelRolesService.GetRoleById(notification.PersonnelRoleTargeted); var groupForRole = _departmentGroupsService.GetGroupForUser(userStateChanged.UserId, notification.DepartmentId); return(String.Format("Availability for role {0} in group {1} is at or below the lower limit", roleForGroup.Name, groupForRole.Name)); case EventTypes.RolesInDepartmentAvailabilityAlert: if (notification != null) { var roleForDep = _personnelRolesService.GetRoleById(notification.PersonnelRoleTargeted); if (roleForDep != null) { return(String.Format("Availability for role {0} for the department is at or below the lower limit", roleForDep.Name)); } } break; case EventTypes.UnitTypesInGroupAvailabilityAlert: if (data.UnitId != 0) { var unitForGroup = _unitsService.GetUnitById(data.UnitId); if (unitForGroup != null && unitForGroup.StationGroupId.HasValue) { var groupForUnit = _departmentGroupsService.GetGroupById(unitForGroup.StationGroupId.Value); return(String.Format("Availability for unit type {0} in group {1} is at or below the lower limit", unitForGroup.Type, groupForUnit.Name)); } } return(String.Empty); case EventTypes.UnitTypesInDepartmentAvailabilityAlert: return(String.Format("Availability for unit type {0} for the department is at or below the lower limit", notification.UnitTypeTargeted)); case EventTypes.CalendarEventAdded: var calEvent = _calendarService.GetCalendarItemById(notification.ItemId); var department = _departmentsService.GetDepartmentById(calEvent.DepartmentId); if (calEvent != null) { if (calEvent.ItemType == 0) { if (calEvent.IsAllDay) { return($"New Calendar Event {calEvent.Title} on {calEvent.Start.TimeConverter(department).ToShortDateString()}"); } else { return($"New Calendar Event {calEvent.Title} on {calEvent.Start.TimeConverter(department).ToShortDateString()} at {calEvent.Start.TimeConverter(department).ToShortTimeString()}"); } } else if (calEvent.IsAllDay) { return($"New Calendar RSVP Event {calEvent.Title} on {calEvent.Start.TimeConverter(department).ToShortDateString()}"); } else { return($"New Calendar RSVP Event {calEvent.Title} on {calEvent.Start.TimeConverter(department).ToShortDateString()} at {calEvent.Start.TimeConverter(department).ToShortTimeString()}"); } } else { return(String.Empty); } case EventTypes.CalendarEventUpdated: var calUpdatedEvent = _calendarService.GetCalendarItemById(notification.ItemId); var calUpdatedEventDepartment = _departmentsService.GetDepartmentById(calUpdatedEvent.DepartmentId); if (calUpdatedEvent != null) { return($"Calendar Event {calUpdatedEvent.Title} on {calUpdatedEvent.Start.TimeConverter(calUpdatedEventDepartment).ToShortDateString()} has changed"); } else { return(String.Empty); } default: throw new ArgumentOutOfRangeException("type"); } } catch (Exception ex) { Logging.LogException(ex, extraMessage: notification.Data); return(String.Empty); } return(String.Empty); }
public bool ValidateNotificationForProcessing(ProcessedNotification notification, DepartmentNotification setting) { //dynamic dynamicData = JsonConvert.DeserializeObject(notification.Data); NotificationItem dynamicData = ObjectSerialization.Deserialize <NotificationItem>(notification.Data); switch (notification.Type) { case EventTypes.UnitStatusChanged: if (!String.IsNullOrWhiteSpace(setting.BeforeData) && !String.IsNullOrWhiteSpace(setting.CurrentData)) { if (setting.BeforeData.Contains("-1") && setting.CurrentData.Contains("-1")) { return(true); } bool beforeAny = setting.BeforeData.Contains("-1"); bool currentAny = setting.CurrentData.Contains("-1"); UnitState beforeState = null; UnitState currentState = null; currentState = _unitsService.GetUnitStateById((int)dynamicData.StateId); if (!beforeAny) { beforeState = _unitsService.GetLastUnitStateBeforeId(currentState.UnitId, currentState.UnitStateId); } if ((currentAny || currentState.State == int.Parse(setting.CurrentData)) && (beforeAny || beforeState.State == int.Parse(setting.BeforeData))) { return(true); } } else { return(false); } break; case EventTypes.PersonnelStaffingChanged: if (!String.IsNullOrWhiteSpace(setting.BeforeData) && !String.IsNullOrWhiteSpace(setting.CurrentData)) { if (setting.BeforeData.Contains("-1") && setting.CurrentData.Contains("-1")) { return(true); } bool beforeAny = setting.BeforeData.Contains("-1"); bool currentAny = setting.CurrentData.Contains("-1"); UserState beforeState = null; UserState currentState = null; currentState = _userStateService.GetUserStateById((int)dynamicData.StateId); if (!beforeAny) { beforeState = _userStateService.GetPerviousUserState(currentState.UserId, currentState.UserStateId); } if ((currentAny || currentState.State == int.Parse(setting.CurrentData)) && (beforeAny || beforeState.State == int.Parse(setting.BeforeData))) { return(true); } } else { return(false); } break; case EventTypes.PersonnelStatusChanged: if (!String.IsNullOrWhiteSpace(setting.BeforeData) && !String.IsNullOrWhiteSpace(setting.CurrentData)) { if (setting.BeforeData.Contains("-1") && setting.CurrentData.Contains("-1")) { return(true); } bool beforeAny = setting.BeforeData.Contains("-1"); bool currentAny = setting.CurrentData.Contains("-1"); ActionLog beforeState = null; ActionLog currentState = null; currentState = _actionLogsService.GetActionlogById((int)dynamicData.StateId); if (!beforeAny) { beforeState = _actionLogsService.GetPreviousActionLog(currentState.UserId, currentState.ActionLogId); } if ((currentAny || currentState.ActionTypeId == int.Parse(setting.CurrentData)) && (beforeAny || beforeState.ActionTypeId == int.Parse(setting.BeforeData))) { return(true); } } else { return(false); } break; case EventTypes.RolesInGroupAvailabilityAlert: if (!String.IsNullOrWhiteSpace(setting.CurrentData) && !String.IsNullOrWhiteSpace(setting.Data)) { int count = 0; var userStateChanged = _userStateService.GetUserStateById((int)dynamicData.StateId); var usersInRole = _personnelRolesService.GetAllMembersOfRole(int.Parse(setting.Data)); var group = _departmentGroupsService.GetGroupForUser(userStateChanged.UserId, notification.DepartmentId); if (group == null || group.Members == null || !group.Members.Any()) { return(false); } var acceptableStaffingLevels = setting.CurrentData.Split(char.Parse(",")); if (usersInRole != null && !usersInRole.Any()) { return(false); } var staffingLevels = _userStateService.GetLatestStatesForDepartment(setting.DepartmentId); if (staffingLevels != null && staffingLevels.Any()) { foreach (var user in usersInRole) { var currentState = staffingLevels.FirstOrDefault(x => x.UserId == user.UserId); if (currentState != null && acceptableStaffingLevels.Any(x => x == currentState.State.ToString()) && group.Members.Any(x => x.UserId == user.UserId)) { count++; } } if (count <= setting.LowerLimit) { notification.PersonnelRoleTargeted = int.Parse(setting.Data); return(true); } } else { return(false); } } else { return(false); } break; case EventTypes.RolesInDepartmentAvailabilityAlert: if (!String.IsNullOrWhiteSpace(setting.CurrentData) && !String.IsNullOrWhiteSpace(setting.Data)) { int count = 0; var usersInRole = _personnelRolesService.GetAllMembersOfRole(int.Parse(setting.Data)); var acceptableStaffingLevels = setting.CurrentData.Split(char.Parse(",")); if (usersInRole != null && !usersInRole.Any()) { return(false); } var staffingLevels = _userStateService.GetLatestStatesForDepartment(setting.DepartmentId); if (staffingLevels != null && staffingLevels.Any()) { foreach (var user in usersInRole) { var currentState = staffingLevels.FirstOrDefault(x => x.UserId == user.UserId); if (currentState != null && acceptableStaffingLevels.Any(x => x == currentState.State.ToString())) { count++; } } if (count <= setting.LowerLimit) { notification.PersonnelRoleTargeted = int.Parse(setting.Data); return(true); } } else { return(false); } } else { return(false); } break; case EventTypes.UnitTypesInGroupAvailabilityAlert: if (!String.IsNullOrWhiteSpace(setting.CurrentData) && !String.IsNullOrWhiteSpace(setting.Data)) { int count = 0; var currentUnitState = _unitsService.GetUnitStateById((int)dynamicData.StateId); var unitsForType = _unitsService.GetAllUnitsForType(setting.DepartmentId, setting.Data); var unitForEvent = _unitsService.GetUnitById(currentUnitState.UnitId); if (unitForEvent?.StationGroupId == null) { return(false); } var acceptableUnitStates = setting.CurrentData.Split(char.Parse(",")); var unitsInGroup = _unitsService.GetAllUnitsForGroup(unitForEvent.StationGroupId.Value); if (unitsForType != null && !unitsForType.Any()) { return(false); } var staffingLevels = _unitsService.GetAllLatestStatusForUnitsByDepartmentId(setting.DepartmentId); foreach (var unit in unitsForType) { var currentState = staffingLevels.FirstOrDefault(x => x.UnitId == unit.UnitId); if (currentState != null && acceptableUnitStates.Any(x => x == currentState.State.ToString()) && unitsInGroup.Any(x => x.UnitId == unit.UnitId)) { count++; } } if (count <= setting.LowerLimit) { notification.UnitTypeTargeted = setting.Data; return(true); } } else { return(false); } break; case EventTypes.UnitTypesInDepartmentAvailabilityAlert: if (!String.IsNullOrWhiteSpace(setting.CurrentData) && !String.IsNullOrWhiteSpace(setting.Data)) { int count = 0; var unitsForType = _unitsService.GetAllUnitsForType(setting.DepartmentId, setting.Data); var acceptableUnitStates = setting.CurrentData.Split(char.Parse(",")); if (unitsForType != null && !unitsForType.Any()) { return(false); } var staffingLevels = _unitsService.GetAllLatestStatusForUnitsByDepartmentId(setting.DepartmentId); foreach (var unit in unitsForType) { var currentState = staffingLevels.FirstOrDefault(x => x.UnitId == unit.UnitId); if (currentState != null && acceptableUnitStates.Any(x => x == currentState.State.ToString())) { count++; } } if (count <= setting.LowerLimit) { notification.UnitTypeTargeted = setting.Data; return(true); } } else { return(false); } break; default: return(true); } return(false); }