/// <inheritdoc /> /// <summary> /// Send Notification /// </summary> /// <param name="usersIds"></param> /// <param name="notification"></param> /// <returns></returns> public virtual async Task SendNotificationAsync(IEnumerable <Guid> usersIds, Notification notification) { if (notification == null) { throw new NullReferenceException(); } try { var users = usersIds.ToList(); if (!users.Any()) { return; } if (!notification.SendLocal && !notification.SendEmail) { return; } var emails = new HashSet <string>(); foreach (var userId in users) { var user = await _userManager.UserManager.FindByIdAsync(userId.ToString()); if (user == null) { continue; } //send email only if email was confirmed if (notification.SendEmail && user.EmailConfirmed) { emails.Add(user.Email); } if (!notification.SendLocal) { continue; } notification.Id = Guid.NewGuid(); notification.UserId = userId; var tenant = await _userManager.IdentityContext.Tenants.FirstOrDefaultAsync(x => x.Id.Equals(user.TenantId)); var response = await _dataService.Add <SystemNotifications>(_dataService.GetDictionary(notification), tenant.MachineName); if (!response.IsSuccess) { _logger.LogError("Fail to add new notification in database"); } } if (notification.SendLocal) { _hub.SendNotification(users, NotificationMapper.Map(notification)); } if (notification.SendEmail) { await _emailSender.SendEmailAsync(emails, notification.Subject, notification.Content); } } catch (Exception e) { _logger.LogCritical(e.Message); } }